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

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