Comments can't increase the indent level of the next line

This commit is contained in:
probablycorey
2013-05-13 14:27:19 -07:00
parent eb25d2e6a8
commit 423b133e75
3 changed files with 18 additions and 2 deletions

View File

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

View File

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

View File

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