diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 80dc5a52f..247f83e0f 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -1424,6 +1424,26 @@ describe "Editor", -> editor.undo() expect(editor.getCursorBufferPosition()).toEqual [3,4] + it "indents the new line to the same indent level as the current line when editor.autoIndent is true", -> + atom.config.set('editor.autoIndent', true) + + editor.setText(' var test') + editor.setCursorBufferPosition([0,2]) + editor.insertNewlineAbove() + + expect(editor.getCursorBufferPosition()).toEqual [0,2] + expect(editor.lineForBufferRow(0)).toBe ' ' + expect(editor.lineForBufferRow(1)).toBe ' var test' + + editor.setText('\n var test') + editor.setCursorBufferPosition([1,2]) + editor.insertNewlineAbove() + + expect(editor.getCursorBufferPosition()).toEqual [1,2] + expect(editor.lineForBufferRow(0)).toBe '' + expect(editor.lineForBufferRow(1)).toBe ' ' + expect(editor.lineForBufferRow(2)).toBe ' var test' + describe ".backspace()", -> describe "when there is a single cursor", -> changeScreenRangeHandler = null diff --git a/src/editor.coffee b/src/editor.coffee index 800eb3ba0..ba2d302b7 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -622,11 +622,18 @@ class Editor extends Model # Public: For each cursor, insert a newline at the end of the preceding line. insertNewlineAbove: -> @transact => - onFirstLine = @getCursorBufferPosition().row is 0 + bufferRow = @getCursorBufferPosition().row + indentLevel = @indentationForBufferRow(bufferRow) + onFirstLine = bufferRow is 0 + @moveCursorToBeginningOfLine() @moveCursorLeft() @insertNewline() - @moveCursorUp() if onFirstLine + @setIndentationForBufferRow(bufferRow, indentLevel) if @shouldAutoIndent() + + if onFirstLine + @moveCursorUp() + @moveCursorToEndOfLine() # Indent all lines intersecting selections. See {Selection::indent} for more # information.