From e464c4f047bf0af8dd5d2a7502a6adedb9d3e70b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 4 Apr 2014 10:02:24 -0700 Subject: [PATCH 1/2] Use current indent level for newline above Previously Editor::insertNewlineAbove would put the cursor at position 0 if there were empty lines above the current line regarding of the indent level of the cursor's current line. Now the indent level of the current line is always used for the newline above when the editor.autoIndent config is enabled. --- spec/editor-spec.coffee | 11 +++++++++++ src/editor.coffee | 5 ++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 80dc5a52f..3026fa053 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -1424,6 +1424,17 @@ 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('\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..d0ba2f9f8 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -622,10 +622,13 @@ 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() + @setIndentationForBufferRow(bufferRow, indentLevel) if @shouldAutoIndent() @moveCursorUp() if onFirstLine # Indent all lines intersecting selections. See {Selection::indent} for more From 6e422d54284ee646c8d811adaf51be15348d7674 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 4 Apr 2014 10:22:09 -0700 Subject: [PATCH 2/2] Move cursor to end of line when on first row This line may be indented now so move the cursor to the end of the indentation. --- spec/editor-spec.coffee | 9 +++++++++ src/editor.coffee | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 3026fa053..247f83e0f 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -1426,6 +1426,15 @@ describe "Editor", -> 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() diff --git a/src/editor.coffee b/src/editor.coffee index d0ba2f9f8..ba2d302b7 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -625,11 +625,15 @@ class Editor extends Model bufferRow = @getCursorBufferPosition().row indentLevel = @indentationForBufferRow(bufferRow) onFirstLine = bufferRow is 0 + @moveCursorToBeginningOfLine() @moveCursorLeft() @insertNewline() @setIndentationForBufferRow(bufferRow, indentLevel) if @shouldAutoIndent() - @moveCursorUp() if onFirstLine + + if onFirstLine + @moveCursorUp() + @moveCursorToEndOfLine() # Indent all lines intersecting selections. See {Selection::indent} for more # information.