diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 08c1e0c59..2f1e06e6b 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -2241,6 +2241,14 @@ describe "EditSession", -> editSession.insertText('\n') expect(editSession.indentationForBufferRow(6)).toBe editSession.indentationForBufferRow(5) + describe "when the line preceding the newline is a comment", -> + it "maintains the indent of the commented line", -> + editSession.setCursorBufferPosition([0, 0]) + editSession.insertText(' //') + editSession.setCursorBufferPosition([0, Infinity]) + editSession.insertText('\n') + expect(editSession.indentationForBufferRow(1)).toBe 2 + it "does not indent the line preceding the newline", -> editSession.setCursorBufferPosition([2, 0]) editSession.insertText(' var this-line-should-be-indented-more\n') diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 37b762ef1..57c7f64b4 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -9,6 +9,7 @@ Subscriber = require 'subscriber' Range = require 'range' _ = require 'underscore' fsUtils = require 'fs-utils' +TextMateScopeSelector = require 'text-mate-scope-selector' # An `EditSession` manages the states between {Editor}s, {Buffer}s, and the project as a whole. module.exports = @@ -284,6 +285,14 @@ class EditSession # {Delegates to: Buffer.isRowBlank} isBufferRowBlank: (bufferRow) -> @buffer.isRowBlank(bufferRow) + # Test if an entire row is a comment + # + # Returns a {Boole}. + isBufferRowCommented: (bufferRow) -> + if match = @lineForBufferRow(bufferRow).match(/\S/) + scopes = @tokenForBufferPosition([bufferRow, match.index]).scopes + new TextMateScopeSelector('comment.*').matches(scopes) + # {Delegates to: Buffer.nextNonBlankRow} nextNonBlankBufferRow: (bufferRow) -> @buffer.nextNonBlankRow(bufferRow) diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 8b1531786..ad1965fe8 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -162,9 +162,8 @@ class LanguageMode return currentIndentLevel unless precedingRow? precedingLine = @buffer.lineForRow(precedingRow) - desiredIndentLevel = @editSession.indentationForBufferRow(precedingRow) - desiredIndentLevel += 1 if increaseIndentRegex.test(precedingLine) + desiredIndentLevel += 1 if increaseIndentRegex.test(precedingLine) and not @editSession.isBufferRowCommented(precedingRow) return desiredIndentLevel unless decreaseIndentRegex = @decreaseIndentRegexForScopes(scopes) desiredIndentLevel -= 1 if decreaseIndentRegex.test(currentLine)