diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 0f7e65c7f..65b08a413 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -827,6 +827,24 @@ describe "Editor", -> expect(cursor1.getBufferPosition()).toEqual [4,0] 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() + editor.setSelectionBufferRange([[0,4], [0,13]]) + editor.addSelectionForBufferRange([[0,22], [0,24]]) + + editor.insertText("x") + + [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(editor.lineForBufferRow(0)).toBe "var x = functx () {" + describe "backspace", -> describe "when cursors are on the same line", -> it "removes the characters preceding each cursor", -> diff --git a/spec/atom/selection-spec.coffee b/spec/atom/selection-spec.coffee index f57bb27db..254a1818e 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.anchor.getScreenPosition()).toEqual range.start + expect(selection.anchorPosition).toEqual range.start expect(selection.cursor.getScreenPosition()).toEqual range.end describe ".delete()", -> diff --git a/src/atom/composite-selection.coffee b/src/atom/composite-selection.coffee index 502e26329..368602266 100644 --- a/src/atom/composite-selection.coffee +++ b/src/atom/composite-selection.coffee @@ -27,6 +27,9 @@ class CompositeSeleciton selectionForCursor: (cursor) -> _.find @selections, (selection) -> selection.cursor == cursor + handleBufferChange: (e) -> + selection.handleBufferChange(e) for selection in @getSelections() + insertText: (text) -> @modifySelections (selection) -> selection.insertText(text) diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index c01c8dce2..6967b53ff 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -215,7 +215,8 @@ class Editor extends View @editSession.scrollLeft = @horizontalScroller.scrollLeft() handleBufferChange: (e) -> - @compositeCursor.handleBufferChange(e) if @isFocused + @compositeCursor.handleBufferChange(e) + @compositeSelection.handleBufferChange(e) handleRendererChange: (e) -> { oldRange, newRange } = e diff --git a/src/atom/selection.coffee b/src/atom/selection.coffee index 456a82a2f..e4f6573b5 100644 --- a/src/atom/selection.coffee +++ b/src/atom/selection.coffee @@ -21,8 +21,28 @@ class Selection extends View else @clearSelection() + + + handleBufferChange: (e) -> + return unless @anchorPosition + + { oldRange, newRange } = e + position = @getAnchorBufferPosition() + return if position.isLessThan(oldRange.end) + + newRow = newRange.end.row + newColumn = newRange.end.column + if position.row == oldRange.end.row + newColumn += position.column - oldRange.end.column + else + newColumn = position.column + newRow += position.row - oldRange.end.row + + @setAnchorBufferPosition([newRow, newColumn]) + + clearSelection: -> - @anchor = null + @anchorPosition = null @updateAppearance() updateAppearance: -> @@ -59,8 +79,8 @@ class Selection extends View @regions = [] getScreenRange: -> - if @anchor - new Range(@anchor.getScreenPosition(), @cursor.getScreenPosition()) + if @anchorPosition + new Range(@anchorPosition, @cursor.getScreenPosition()) else new Range(@cursor.getScreenPosition(), @cursor.getScreenPosition()) @@ -132,9 +152,15 @@ class Selection extends View @retainSelection = false placeAnchor: -> - return if @anchor + return if @anchorPosition cursorPosition = @cursor.getScreenPosition() - @anchor = { getScreenPosition: -> cursorPosition } + @anchorPosition = cursorPosition + + getAnchorBufferPosition: -> + @editor.bufferPositionForScreenPosition(@anchorPosition) + + setAnchorBufferPosition: (position) -> + @anchorPosition = @editor.screenPositionForBufferPosition(position) selectWord: -> row = @cursor.getScreenRow()