diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 3d45d176b..ced211574 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2529,6 +2529,16 @@ describe "Editor", -> expect(editor.indentationForBufferRow(1)).toBe 1 expect(editor.indentationForBufferRow(2)).toBe 1 + describe "when the cursor is before whitespace", -> + it "indents that whitespace properly in the next line", -> + editor.setText(" var sort = function() {}") + editor.setCursorScreenPosition([0, 23]) + editor.insertNewline() + + expect(buffer.lineForRow(0)).toBe ' var sort = function()' + expect(buffer.lineForRow(1)).toBe ' {}' + expect(editor.getCursorScreenPosition()).toEqual [1, 2] + describe "when inserted text matches a decrease indent pattern", -> describe "when the preceding line matches an increase indent pattern", -> it "decreases the indentation to match that of the preceding line", -> diff --git a/src/editor.coffee b/src/editor.coffee index 1d7c6dde5..7a58c8c5e 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -373,10 +373,16 @@ class Editor extends Model # # bufferRow - A {Number} indicating the buffer row. # newLevel - A {Number} indicating the new indentation level. - setIndentationForBufferRow: (bufferRow, newLevel) -> - currentIndentLength = @lineForBufferRow(bufferRow).match(/^\s*/)[0].length + # options - An {Object} with the following keys: + # :ignoreLeadingWhitespace - true to not replace any of the leading + # whitespace already on the line (default: false) + setIndentationForBufferRow: (bufferRow, newLevel, {ignoreLeadingWhitespace}={}) -> + if ignoreLeadingWhitespace + endColumn = 0 + else + endColumn = @lineForBufferRow(bufferRow).match(/^\s*/)[0].length newIndentString = @buildIndentString(newLevel) - @buffer.change([[bufferRow, 0], [bufferRow, currentIndentLength]], newIndentString) + @buffer.change([[bufferRow, 0], [bufferRow, endColumn]], newIndentString) # Public: Get the indentation level of the given line of text. # diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 093995947..f13a6c6b8 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -265,10 +265,11 @@ class LanguageMode # Given a buffer row, this indents it. # - # bufferRow - The row {Number} - autoIndentBufferRow: (bufferRow) -> + # bufferRow - The row {Number}. + # options - An options {Object} to pass to {Editor::setIndentationForBufferRow}. + autoIndentBufferRow: (bufferRow, options) -> indentLevel = @suggestedIndentForBufferRow(bufferRow) - @editor.setIndentationForBufferRow(bufferRow, indentLevel) + @editor.setIndentationForBufferRow(bufferRow, indentLevel, options) # Given a buffer row, this decreases the indentation. # diff --git a/src/selection.coffee b/src/selection.coffee index 13a949c51..7c357cdb1 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -301,7 +301,7 @@ class Selection if options.autoIndent @editor.autoIndentBufferRow(row) for row in newBufferRange.getRows() else if options.autoIndentNewline and text == '\n' - @editor.autoIndentBufferRow(newBufferRange.end.row) + @editor.autoIndentBufferRow(newBufferRange.end.row, ignoreLeadingWhitespace: true) else if options.autoDecreaseIndent and /\S/.test text @editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row)