Move anchor points to buffer; restore invalidated points on undo

This commit is contained in:
Nathan Sobo
2013-01-30 12:23:42 -07:00
parent 6c7b93872c
commit 30909a8e8f
6 changed files with 124 additions and 136 deletions

View File

@@ -658,6 +658,64 @@ describe 'Buffer', ->
expect(buffer.positionForCharacterIndex(61)).toEqual [2, 0]
expect(buffer.positionForCharacterIndex(408)).toEqual [12, 2]
fdescribe "anchor points", ->
[anchor1Id, anchor2Id, anchor3Id] = []
beforeEach ->
anchor1Id = buffer.addAnchorPoint([4, 23])
anchor2Id = buffer.addAnchorPoint([4, 23], ignoreSameLocationInserts: true)
anchor3Id = buffer.addAnchorPoint([4, 23], surviveSurroundingChanges: true)
describe "when the buffer changes", ->
describe "when the change precedes the anchor point", ->
it "moves the anchor", ->
buffer.insert([4, 5], '...')
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 26]
buffer.delete([[4, 5], [4, 8]])
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 23]
buffer.insert([0, 0], '\nhi\n')
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [6, 23]
# undo works
buffer.undo()
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 23]
buffer.undo()
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 26]
describe "when the change follows the anchor point", ->
it "does not move the anchor", ->
buffer.insert([6, 5], '...')
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 23]
buffer.delete([[6, 5], [6, 8]])
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 23]
buffer.insert([10, 0], '\nhi\n')
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 23]
describe "when the change is an insertion at the same location as the anchor point", ->
describe "if the anchor ignores same location inserts", ->
it "treats the insertion as being to the right of the anchor and does not move it", ->
buffer.insert([4, 23], '...')
expect(buffer.getAnchorPoint(anchor2Id)).toEqual [4, 23]
describe "if the anchor observes same location inserts", ->
it "treats the insertion as being to the left of the anchor and moves it accordingly", ->
buffer.insert([4, 23], '...')
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 26]
describe "when the change surrounds the anchor point", ->
describe "when the anchor survives surrounding changes", ->
it "preserves the anchor", ->
buffer.delete([[4, 20], [4, 26]])
expect(buffer.getAnchorPoint(anchor3Id)).toEqual [4, 20]
buffer.undo()
expect(buffer.getAnchorPoint(anchor3Id)).toEqual [4, 23]
describe "when the anchor does not survive surrounding changes", ->
it "invalidates the anchor but re-validates it on undo", ->
buffer.delete([[4, 20], [4, 26]])
expect(buffer.getAnchorPoint(anchor1Id)).toBeUndefined()
buffer.undo()
expect(buffer.getAnchorPoint(anchor1Id)).toEqual [4, 23]
describe "anchors", ->
[anchor, destroyHandler] = []

View File

@@ -1775,70 +1775,6 @@ describe "EditSession", ->
describe "anchors", ->
[anchor, destroyHandler] = []
fdescribe "anchor points", ->
[anchor1Id, anchor2Id, anchor3Id] = []
beforeEach ->
anchor1Id = editSession.addAnchorPointAtBufferPosition([4, 23])
anchor2Id = editSession.addAnchorPointAtBufferPosition([4, 23], ignoreSameLocationInserts: true)
anchor3Id = editSession.addAnchorPointAtBufferPosition([4, 23], surviveSurroundingChanges: true)
describe "when the buffer changes", ->
describe "when the change precedes the anchor point", ->
it "moves the anchor", ->
buffer.insert([4, 5], '...')
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 26]
buffer.delete([[4, 5], [4, 8]])
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 23]
buffer.insert([0, 0], '\nhi\n')
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [6, 23]
# undo works
editSession.undo()
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 23]
editSession.undo()
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 26]
describe "when the change follows the anchor point", ->
it "does not move the anchor", ->
buffer.insert([6, 5], '...')
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 23]
buffer.delete([[6, 5], [6, 8]])
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 23]
buffer.insert([10, 0], '\nhi\n')
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 23]
describe "when the change is an insertion at the same location as the anchor point", ->
describe "if the anchor ignores same location inserts", ->
it "treats the insertion as being to the right of the anchor and does not move it", ->
buffer.insert([4, 23], '...')
expect(editSession.getAnchorPointBufferPosition(anchor2Id)).toEqual [4, 23]
describe "if the anchor observes same location inserts", ->
it "treats the insertion as being to the left of the anchor and moves it accordingly", ->
buffer.insert([4, 23], '...')
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 26]
describe "when the change surrounds the anchor point", ->
describe "when the anchor survives surrounding changes", ->
it "preserves the anchor, moving it to the start of the change, but restores its location on undo", ->
buffer.delete([[4, 20], [4, 26]])
expect(editSession.getAnchorPointBufferPosition(anchor3Id)).toEqual [4, 20]
editSession.undo()
expect(editSession.getAnchorPointBufferPosition(anchor3Id)).toEqual [4, 23]
describe "when the anchor does not survive surrounding changes", ->
it "invalidates the anchor but re-validates it on undo", ->
buffer.delete([[4, 20], [4, 26]])
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toBeUndefined()
editSession.undo()
expect(editSession.getAnchorPointBufferPosition(anchor1Id)).toEqual [4, 23]
describe ".clipBufferPosition(bufferPosition)", ->
it "clips the given position to a valid position", ->
expect(editSession.clipBufferPosition([-1, -1])).toEqual [0,0]
expect(editSession.clipBufferPosition([Infinity, Infinity])).toEqual [12,2]
expect(editSession.clipBufferPosition([8, 57])).toEqual [8, 56]
describe ".deleteLine()", ->
it "deletes the first line when the cursor is there", ->
editSession.getCursor().moveToTop()