From b00d0bacd910357372485074ccd7ad881f285abb Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 25 Sep 2012 13:19:59 -0600 Subject: [PATCH] When uncommenting multiple lines, don't raise an error if a line isn't commented Previously, we were assuming that when uncommenting, that every line would match the comment regex. But this might not be the case. If the first line in the selection is commented but some subsequent lines aren't, they won't match the comment regex. So this commit guards for that case. --- spec/app/edit-session-spec.coffee | 19 +++++++++++++++++++ src/app/language-mode.coffee | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index c77355740..9eb71c9d5 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -1323,6 +1323,25 @@ describe "EditSession", -> expect(buffer.lineForRow(6)).toBe " current < pivot ? left.push(current) : right.push(current);" expect(buffer.lineForRow(7)).toBe " }" + it "uncomments lines if the first line matches the comment regex", -> + editSession.setSelectedBufferRange([[4, 5], [4, 5]]) + editSession.toggleLineCommentsInSelection() + editSession.setSelectedBufferRange([[6, 5], [6, 5]]) + editSession.toggleLineCommentsInSelection() + + expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {" + expect(buffer.lineForRow(5)).toBe " current = items.shift();" + expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);" + expect(buffer.lineForRow(7)).toBe " }" + + editSession.setSelectedBufferRange([[4, 5], [7, 5]]) + editSession.toggleLineCommentsInSelection() + + expect(buffer.lineForRow(4)).toBe " while(items.length > 0) {" + expect(buffer.lineForRow(5)).toBe " current = items.shift();" + expect(buffer.lineForRow(6)).toBe " current < pivot ? left.push(current) : right.push(current);" + expect(buffer.lineForRow(7)).toBe " }" + it "preserves selection emptiness", -> editSession.setSelectedBufferRange([[4, 0], [4, 0]]) editSession.toggleLineCommentsInSelection() diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 9e52d49d4..2f519c8f7 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -55,8 +55,8 @@ class LanguageMode for row in [range.start.row..range.end.row] line = @editSession.lineForBufferRow(row) if shouldUncomment - match = commentRegex.search(line) - @editSession.buffer.change([[row, 0], [row, match[0].length]], "") + if match = commentRegex.search(line) + @editSession.buffer.change([[row, 0], [row, match[0].length]], "") else @editSession.buffer.insert([row, 0], commentString)