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.
This commit is contained in:
Nathan Sobo
2012-09-25 13:19:59 -06:00
parent d3df0a305b
commit b00d0bacd9
2 changed files with 21 additions and 2 deletions

View File

@@ -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()

View File

@@ -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)