diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 78d9417e0..6fe4a20a4 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -591,6 +591,14 @@ describe "Editor", -> expect(editor.buffer.lineForRow(2)).toEqual(" }") expect(editor.getCursorBufferPosition().column).toBe 3 + describe "when newlines are inserted for multiple cursors", -> + it "auto-indents the newline for each cursor", -> + editor.setCursorScreenPosition([1, 30]) + editor.addCursorAtScreenPosition([4, 29]) + editor.insertText("\n") + expect(editor.buffer.lineForRow(2)).toEqual(" ") + expect(editor.buffer.lineForRow(6)).toEqual(" ") + describe "when editing a line that spans multiple screen lines", -> beforeEach -> editor.setSoftWrap(true, 50) diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index cc8397a81..71dcec751 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -85,8 +85,11 @@ class Cursor extends View getBufferRow: -> @getBufferPosition().row + getCurrentBufferLine: -> + @editor.lineForBufferRow(@getBufferRow()) + isOnEOL: -> - @getScreenColumn() == @editor.lineForBufferRow(@getBufferRow()).length + @getScreenColumn() == @getCurrentBufferLine().length moveUp: -> { row, column } = @getScreenPosition() diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index c8abedd4f..309b9abbf 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -1,5 +1,4 @@ {View, $$} = require 'space-pen' -AceOutdentAdaptor = require 'ace-outdent-adaptor' Buffer = require 'buffer' CompositeCursor = require 'composite-cursor' CompositeSelection = require 'composite-selection' @@ -350,7 +349,6 @@ class Editor extends View moveCursorRight: -> @compositeCursor.moveRight() moveCursorLeft: -> @compositeCursor.moveLeft() - getCurrentScreenLine: -> @buffer.lineForRow(@getCursorScreenRow()) getCurrentBufferLine: -> @buffer.lineForRow(@getCursorBufferRow()) setCursorScreenPosition: (position) -> @compositeCursor.setScreenPosition(position) getCursorScreenPosition: -> @getCursor().getScreenPosition() @@ -381,27 +379,7 @@ class Editor extends View lineForBufferRow: (row) -> @buffer.lineForRow(row) insertText: (text) -> - { text, shouldOutdent } = @autoIndentText(text) @compositeSelection.insertText(text) - @autoOutdentText() if shouldOutdent - - autoIndentText: (text) -> - if @autoIndent - row = @getCursorScreenPosition().row - state = @renderer.lineForRow(row).state - if text[0] == "\n" - indent = @buffer.mode.getNextLineIndent(state, @getCurrentBufferLine(), atom.tabText) - text = text[0] + indent + text[1..] - else if @buffer.mode.checkOutdent(state, @getCurrentBufferLine(), text) - shouldOutdent = true - - {text, shouldOutdent} - - autoOutdentText: -> - screenRow = @getCursorScreenPosition().row - bufferRow = @getCursorBufferPosition().row - state = @renderer.lineForRow(screenRow).state - @buffer.mode.autoOutdent(state, new AceOutdentAdaptor(@buffer, this), bufferRow) cutSelection: -> @getSelection().cut() copySelection: -> @getSelection().copy() diff --git a/src/atom/selection.coffee b/src/atom/selection.coffee index b266d9230..a44b9746f 100644 --- a/src/atom/selection.coffee +++ b/src/atom/selection.coffee @@ -1,4 +1,5 @@ Cursor = require 'cursor' +AceOutdentAdaptor = require 'ace-outdent-adaptor' Range = require 'range' {View, $$} = require 'space-pen' @@ -78,7 +79,27 @@ class Selection extends View @editor.buffer.getTextInRange @getBufferRange() insertText: (text) -> + { text, shouldOutdent } = @autoIndentText(text) @editor.buffer.change(@getBufferRange(), text) + @autoOutdentText() if shouldOutdent + + autoIndentText: (text) -> + if @editor.autoIndent + row = @cursor.getScreenPosition().row + state = @editor.renderer.lineForRow(row).state + if text[0] == "\n" + indent = @editor.buffer.mode.getNextLineIndent(state, @cursor.getCurrentBufferLine(), atom.tabText) + text = text[0] + indent + text[1..] + else if @editor.buffer.mode.checkOutdent(state, @cursor.getCurrentBufferLine(), text) + shouldOutdent = true + + {text, shouldOutdent} + + autoOutdentText: -> + screenRow = @cursor.getScreenPosition().row + bufferRow = @cursor.getBufferPosition().row + state = @editor.renderer.lineForRow(screenRow).state + @editor.buffer.mode.autoOutdent(state, new AceOutdentAdaptor(@editor.buffer, @editor), bufferRow) backspace: -> @selectLeft() if @isEmpty()