From db3d788664079dc446561201f3af9755aeba482a Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 29 Oct 2012 16:04:09 -0600 Subject: [PATCH] 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. --- spec/app/edit-session-spec.coffee | 8 ++++++++ spec/app/language-mode-spec.coffee | 12 ++++++------ src/app/edit-session.coffee | 4 ++-- src/app/language-mode.coffee | 9 ++++----- src/app/selection.coffee | 9 ++++++++- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index abb3d854f..b6e741e66 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -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() diff --git a/spec/app/language-mode-spec.coffee b/spec/app/language-mode-spec.coffee index 035d1419b..9d9429bc8 100644 --- a/spec/app/language-mode-spec.coffee +++ b/spec/app/language-mode-spec.coffee @@ -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 = []" diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 7c94d928c..411afd421 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -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() diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index eae7a3f0f..4b98cc92a 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -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) diff --git a/src/app/selection.coffee b/src/app/selection.coffee index 3121fe678..6a19e5aa7 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -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()