diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 65b08a413..f4c343e1f 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -828,22 +828,40 @@ describe "Editor", -> expect(cursor2.getBufferPosition()).toEqual [8,0] describe "when selections are on the same line", -> - it "replaces each selection range with the inserted characters", -> - editor.attachToDom() + beforeEach -> editor.setSelectionBufferRange([[0,4], [0,13]]) editor.addSelectionForBufferRange([[0,22], [0,24]]) - editor.insertText("x") + describe "when inserting characters other than newlines", -> + it "replaces each selection range with the inserted characters", -> + editor.insertText("x") - [cursor1, cursor2] = editor.compositeCursor.getCursors() - [selection1, selection2] = editor.compositeSelection.getSelections() + [cursor1, cursor2] = editor.compositeCursor.getCursors() + [selection1, selection2] = editor.compositeSelection.getSelections() - expect(cursor1.getScreenPosition()).toEqual [0, 5] - expect(cursor2.getScreenPosition()).toEqual [0, 14] - expect(selection1.isEmpty()).toBeTruthy() - expect(selection2.isEmpty()).toBeTruthy() + expect(cursor1.getScreenPosition()).toEqual [0, 5] + expect(cursor2.getScreenPosition()).toEqual [0, 15] + expect(selection1.isEmpty()).toBeTruthy() + expect(selection2.isEmpty()).toBeTruthy() + + expect(editor.lineForBufferRow(0)).toBe "var x = functix () {" + + describe "when inserting newlines", -> + it "replaces all selected ranges with newlines", -> + editor.insertText("\n") + + [cursor1, cursor2] = editor.compositeCursor.getCursors() + [selection1, selection2] = editor.compositeSelection.getSelections() + + expect(cursor1.getScreenPosition()).toEqual [1, 0] + expect(cursor2.getScreenPosition()).toEqual [2, 0] + expect(selection1.isEmpty()).toBeTruthy() + expect(selection2.isEmpty()).toBeTruthy() + + expect(editor.lineForBufferRow(0)).toBe "var " + expect(editor.lineForBufferRow(1)).toBe " = functi" + expect(editor.lineForBufferRow(2)).toBe " () {" - expect(editor.lineForBufferRow(0)).toBe "var x = functx () {" describe "backspace", -> describe "when cursors are on the same line", -> diff --git a/spec/atom/selection-spec.coffee b/spec/atom/selection-spec.coffee index 254a1818e..1376ac9b4 100644 --- a/spec/atom/selection-spec.coffee +++ b/spec/atom/selection-spec.coffee @@ -17,7 +17,7 @@ describe "Selection", -> it "places the anchor at the start of the range and the cursor at the end", -> range = new Range({row: 2, column: 7}, {row: 3, column: 18}) selection.setBufferRange(range) - expect(selection.anchorPosition).toEqual range.start + expect(selection.anchorScreenPosition).toEqual range.start expect(selection.cursor.getScreenPosition()).toEqual range.end describe ".delete()", -> diff --git a/src/atom/selection.coffee b/src/atom/selection.coffee index e4f6573b5..5adcc33b7 100644 --- a/src/atom/selection.coffee +++ b/src/atom/selection.coffee @@ -1,6 +1,6 @@ Cursor = require 'cursor' AceOutdentAdaptor = require 'ace-outdent-adaptor' - +Point = require 'point' Range = require 'range' {View, $$} = require 'space-pen' @@ -21,13 +21,11 @@ class Selection extends View else @clearSelection() - - handleBufferChange: (e) -> - return unless @anchorPosition + return unless @anchorScreenPosition { oldRange, newRange } = e - position = @getAnchorBufferPosition() + position = @anchorBufferPosition return if position.isLessThan(oldRange.end) newRow = newRange.end.row @@ -40,9 +38,8 @@ class Selection extends View @setAnchorBufferPosition([newRow, newColumn]) - clearSelection: -> - @anchorPosition = null + @anchorScreenPosition = null @updateAppearance() updateAppearance: -> @@ -79,8 +76,8 @@ class Selection extends View @regions = [] getScreenRange: -> - if @anchorPosition - new Range(@anchorPosition, @cursor.getScreenPosition()) + if @anchorScreenPosition + new Range(@anchorScreenPosition, @cursor.getScreenPosition()) else new Range(@cursor.getScreenPosition(), @cursor.getScreenPosition()) @@ -152,15 +149,18 @@ class Selection extends View @retainSelection = false placeAnchor: -> - return if @anchorPosition - cursorPosition = @cursor.getScreenPosition() - @anchorPosition = cursorPosition + return if @anchorScreenPosition + @setAnchorScreenPosition(@cursor.getScreenPosition()) - getAnchorBufferPosition: -> - @editor.bufferPositionForScreenPosition(@anchorPosition) + setAnchorScreenPosition: (screenPosition) -> + bufferPosition = Point.fromObject(screenPosition) + @anchorScreenPosition = screenPosition + @anchorBufferPosition = @editor.bufferPositionForScreenPosition(screenPosition) - setAnchorBufferPosition: (position) -> - @anchorPosition = @editor.screenPositionForBufferPosition(position) + setAnchorBufferPosition: (bufferPosition) -> + bufferPosition = Point.fromObject(bufferPosition) + @anchorBufferPosition = bufferPosition + @anchorScreenPosition = @editor.screenPositionForBufferPosition(bufferPosition) selectWord: -> row = @cursor.getScreenRow()