Selection anchors move on buffer changes

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-03-26 17:03:17 -07:00
parent 4d1f7b33e7
commit 37df49f77e
5 changed files with 55 additions and 7 deletions

View File

@ -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", ->

View File

@ -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()", ->

View File

@ -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)

View File

@ -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

View File

@ -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()