Toggle line comments ignores last row of selection if it ends at col 0

Now that the cursor is hidden at the end of a selection, it's counter-intuitive for the commenting to extend to the next line since there's no visual indicator that the cursor extends to that location.
This commit is contained in:
Nathan Sobo
2012-10-29 16:04:09 -06:00
parent 88e246b622
commit db3d788664
5 changed files with 28 additions and 14 deletions

View File

@@ -1489,6 +1489,14 @@ describe "EditSession", ->
expect(buffer.lineForRow(6)).toBe " current < pivot ? left.push(current) : right.push(current);"
expect(buffer.lineForRow(7)).toBe " }"
it "does not comment the last line of a non-empty selection if it ends at column 0", ->
editSession.setSelectedBufferRange([[4, 5], [7, 0]])
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 "uncomments lines if the first line matches the comment regex", ->
editSession.setSelectedBufferRange([[4, 5], [4, 5]])
editSession.toggleLineCommentsInSelection()

View File

@@ -162,15 +162,15 @@ describe "LanguageMode", ->
editSession = fixturesProject.buildEditSessionForPath('sample.js', autoIndent: false)
{ buffer, languageMode } = editSession
describe ".toggleLineCommentsInRange(range)", ->
describe ".toggleLineCommentsForBufferRows(start, end)", ->
it "comments/uncomments lines in the given range", ->
languageMode.toggleLineCommentsInRange([[4, 5], [7, 8]])
languageMode.toggleLineCommentsForBufferRows(4, 7)
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 "// }"
languageMode.toggleLineCommentsInRange([[4, 5], [5, 8]])
languageMode.toggleLineCommentsForBufferRows(4, 5)
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);"
@@ -204,15 +204,15 @@ describe "LanguageMode", ->
editSession = fixturesProject.buildEditSessionForPath('coffee.coffee', autoIndent: false)
{ buffer, languageMode } = editSession
describe ".toggleLineCommentsInRange(range)", ->
describe ".toggleLineCommentsForBufferRows(start, end)", ->
it "comments/uncomments lines in the given range", ->
languageMode.toggleLineCommentsInRange([[4, 5], [7, 8]])
languageMode.toggleLineCommentsForBufferRows(4, 7)
expect(buffer.lineForRow(4)).toBe "# pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe "# left = []"
expect(buffer.lineForRow(6)).toBe "# right = []"
expect(buffer.lineForRow(7)).toBe "# "
languageMode.toggleLineCommentsInRange([[4, 5], [5, 8]])
languageMode.toggleLineCommentsForBufferRows(4, 5)
expect(buffer.lineForRow(4)).toBe " pivot = items.shift()"
expect(buffer.lineForRow(5)).toBe " left = []"
expect(buffer.lineForRow(6)).toBe "# right = []"

View File

@@ -286,8 +286,8 @@ class EditSession
autoDecreaseIndentForRow: (bufferRow) ->
@languageMode.autoDecreaseIndentForBufferRow(bufferRow)
toggleLineCommentsInRange: (range) ->
@languageMode.toggleLineCommentsInRange(range)
toggleLineCommentsForBufferRows: (start, end) ->
@languageMode.toggleLineCommentsForBufferRows(start, end)
mutateSelectedText: (fn) ->
@transact => fn(selection) for selection in @getSelections()

View File

@@ -64,18 +64,17 @@ class LanguageMode
@invertedPairedCharacters[close] = open
@invertedPairedCharacters
toggleLineCommentsInRange: (range) ->
range = Range.fromObject(range)
scopes = @getTokenizedBuffer().scopesForPosition(range.start)
toggleLineCommentsForBufferRows: (start, end) ->
scopes = @getTokenizedBuffer().scopesForPosition([start, 0])
return unless commentString = TextMateBundle.lineCommentStringForScope(scopes[0])
commentRegexString = _.escapeRegExp(commentString)
commentRegexString = commentRegexString.replace(/(\s+)$/, '($1)?')
commentRegex = new OnigRegExp("^\s*#{commentRegexString}")
shouldUncomment = commentRegex.test(@editSession.lineForBufferRow(range.start.row))
shouldUncomment = commentRegex.test(@editSession.lineForBufferRow(start))
for row in [range.start.row..range.end.row]
for row in [start..end]
line = @editSession.lineForBufferRow(row)
if shouldUncomment
if match = commentRegex.search(line)

View File

@@ -67,6 +67,13 @@ class Selection
else
new Range(@cursor.getBufferPosition(), @cursor.getBufferPosition())
getBufferRowRange: ->
range = @getBufferRange()
start = range.start.row
end = range.end.row
end = Math.max(start, end - 1) if range.end.column == 0
[start, end]
screenRangeChanged: ->
screenRange = @getScreenRange()
@trigger 'change-screen-range', screenRange
@@ -272,7 +279,7 @@ class Selection
toggleLineComments: ->
@modifySelection =>
@editSession.toggleLineCommentsInRange(@getBufferRange())
@editSession.toggleLineCommentsForBufferRows(@getBufferRowRange()...)
cutToEndOfLine: (maintainPasteboard) ->
@selectToEndOfLine() if @isEmpty()