Don't destroy selection/cursor anchors when encompassed by a change

Add the 'strong' option to anchors. If anchors are 'strong' instead of being destroyed by encompassing changes they move to the beginning of the change range.
This commit is contained in:
Nathan Sobo 2012-07-05 15:20:28 -06:00
parent 43edbcbe31
commit 569359b687
4 changed files with 20 additions and 4 deletions

View File

@ -1276,6 +1276,19 @@ describe "EditSession", ->
expect(cursor2.getScreenPosition()).toEqual [0, 8]
expect(cursor3.getScreenPosition()).toEqual [1, 0]
it "does not destroy cursor or selection anchors when a change encompasses them", ->
cursor = editSession.getLastCursor()
cursor.setBufferPosition [3, 3]
editSession.buffer.delete([[3, 1], [3, 5]])
expect(cursor.getBufferPosition()).toEqual [3, 1]
expect(editSession.getAnchors().indexOf(cursor.anchor)).not.toBe -1
selection = editSession.getLastSelection()
selection.setBufferRange [[3, 5], [3, 10]]
editSession.buffer.delete [[3, 3], [3, 8]]
expect(selection.getBufferRange()).toEqual [[3, 3], [3, 5]]
expect(editSession.getAnchors().indexOf(selection.anchor)).not.toBe -1
it "merges cursors when the change causes them to overlap", ->
editSession.setCursorScreenPosition([0, 0])
editSession.addCursorAtScreenPosition([0, 1])

View File

@ -9,14 +9,17 @@ class Anchor
screenPosition: null
constructor: (@editSession, options = {}) ->
{ @ignoreEqual } = options
{ @ignoreEqual, @strong } = options
handleBufferChange: (e) ->
{ oldRange, newRange } = e
position = @getBufferPosition()
if oldRange.containsPoint(position, exclusive: true)
@destroy()
if @strong
@setBufferPosition(oldRange.start)
else
@destroy()
return
if @ignoreEqual

View File

@ -12,7 +12,7 @@ class Cursor
wordRegex: /(\w+)|([^\w\s]+)/g
constructor: ({@editSession, screenPosition, bufferPosition}) ->
@anchor = @editSession.addAnchor()
@anchor = @editSession.addAnchor(strong: true)
@anchor.on 'change-screen-position', (args...) => @trigger 'change-screen-position', args...
@setScreenPosition(screenPosition) if screenPosition
@setBufferPosition(bufferPosition) if bufferPosition

View File

@ -226,7 +226,7 @@ class Selection
@trigger 'change-screen-range', newScreenRange unless oldScreenRange.isEqual(newScreenRange)
placeAnchor: ->
@anchor = @editSession.addAnchor()
@anchor = @editSession.addAnchor(strong: true)
@anchor.setScreenPosition(@cursor.getScreenPosition())
@anchor.on 'change-screen-position.selection', => @trigger 'change-screen-range'