Anchors are destroyed when encompassed by a buffer change

This commit is contained in:
Nathan Sobo
2012-07-05 14:53:37 -06:00
parent 0043a5045b
commit 60a4f23c50
3 changed files with 33 additions and 3 deletions

View File

@@ -1331,6 +1331,28 @@ describe "EditSession", ->
editSession.foldAll()
expect(editSession.getCursorBufferPosition()).toEqual([5,5])
describe "anchors", ->
[anchor, destroyHandler] = []
beforeEach ->
destroyHandler = jasmine.createSpy("destroyHandler")
anchor = editSession.addAnchorAtBufferPosition([4, 25])
anchor.on 'destroy', destroyHandler
describe "when a buffer change precedes an anchor", ->
it "moves the anchor in accordance with the change", ->
editSession.setSelectedBufferRange([[3, 0], [4, 10]])
editSession.delete()
expect(anchor.getBufferPosition()).toEqual [3, 15]
expect(destroyHandler).not.toHaveBeenCalled()
describe "when a buffer change surrounds an anchor", ->
it "destroys the anchor", ->
editSession.setSelectedBufferRange([[3, 0], [5, 0]])
editSession.delete()
expect(destroyHandler).toHaveBeenCalled()
expect(editSession.getAnchors().indexOf(anchor)).toBe -1
describe ".clipBufferPosition(bufferPosition)", ->
it "clips the given position to a valid position", ->
expect(editSession.clipBufferPosition([-1, -1])).toEqual [0,0]

View File

@@ -15,6 +15,10 @@ class Anchor
{ oldRange, newRange } = e
position = @getBufferPosition()
if oldRange.containsPoint(position, exclusive: true)
@destroy()
return
if @ignoreEqual
return if position.isLessThanOrEqual(oldRange.end)
else
@@ -62,7 +66,8 @@ class Anchor
@setScreenPosition(screenPosition, bufferChange: options.bufferChange, clip: false, assignBufferPosition: false)
destroy: ->
@off()
@editSession.removeAnchor(this)
@trigger 'destroy'
@off()
_.extend(Anchor.prototype, EventEmitter)

View File

@@ -49,9 +49,12 @@ class Range
else
otherRange.intersectsWith(this)
containsPoint: (point) ->
containsPoint: (point, { exclusive } = {}) ->
point = Point.fromObject(point)
point.isGreaterThanOrEqual(@start) and point.isLessThanOrEqual(@end)
if exclusive
point.isGreaterThan(@start) and point.isLessThan(@end)
else
point.isGreaterThanOrEqual(@start) and point.isLessThanOrEqual(@end)
containsRow: (row) ->
@start.row <= row <= @end.row