diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 3d45d176b..de8d55bfb 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2506,7 +2506,7 @@ describe "Editor", -> describe "when the line preceding the newline does't add a level of indentation", -> it "indents the new line to the same level a as the preceding line", -> - editor.setCursorBufferPosition([5, 13]) + editor.setCursorBufferPosition([5, 14]) editor.insertText('\n') expect(editor.indentationForBufferRow(6)).toBe editor.indentationForBufferRow(5) @@ -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 "retains the whitespace following the cursor on the new 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..800eb3ba0 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: + # :preserveLeadingWhitespace - true to preserve any whitespace already at + # the beginning of the line (default: false). + setIndentationForBufferRow: (bufferRow, newLevel, {preserveLeadingWhitespace}={}) -> + if preserveLeadingWhitespace + 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..bc075a662 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 through 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..795e93004 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, preserveLeadingWhitespace: true) else if options.autoDecreaseIndent and /\S/.test text @editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row)