Finish converting cursors to use markers.

The model layer works at least… haven't tested the view. Will test out
the view once I get the selection model working.
This commit is contained in:
Nathan Sobo
2013-01-31 15:52:28 -07:00
parent ec521b3fd3
commit 628701fd3a
8 changed files with 96 additions and 17 deletions

View File

@@ -727,6 +727,30 @@ describe 'Buffer', ->
buffer.setMarkerHeadPosition(marker, [6, 2])
expect(observeHandler).not.toHaveBeenCalled()
describe "marker destruction", ->
marker = null
beforeEach ->
marker = buffer.markRange([[4, 20], [4, 23]])
it "allows a marker to be destroyed", ->
buffer.destroyMarker(marker)
expect(buffer.getMarkerRange(marker)).toBeUndefined()
it "does not restore invalidated markers that have been destroyed", ->
buffer.delete([[4, 15], [4, 25]])
expect(buffer.getMarkerRange(marker)).toBeUndefined()
buffer.destroyMarker(marker)
buffer.undo()
expect(buffer.getMarkerRange(marker)).toBeUndefined()
# even "stayValid" markers get destroyed properly
marker2 = buffer.markRange([[4, 20], [4, 23]], stayValid: true)
buffer.delete([[4, 15], [4, 25]])
buffer.destroyMarker(marker2)
buffer.undo()
expect(buffer.getMarkerRange(marker2)).toBeUndefined()
describe "marker updates due to buffer changes", ->
[marker1, marker2] = []

View File

@@ -584,7 +584,7 @@ describe "DisplayBuffer", ->
beforeEach ->
displayBuffer.foldBufferRow(4)
describe "creation and manipulation", ->
describe "marker creation and manipulation", ->
it "allows markers to be created in terms of both screen and buffer coordinates", ->
marker1 = displayBuffer.markScreenRange([[5, 4], [5, 10]])
marker2 = displayBuffer.markBufferRange([[8, 4], [8, 10]])
@@ -603,7 +603,15 @@ describe "DisplayBuffer", ->
expect(displayBuffer.isMarkerReversed(marker)).toBeTruthy()
expect(displayBuffer.getMarkerBufferRange(marker)).toEqual [[5, 4], [8, 4]]
describe "observation", ->
it "clips screen positions before assigning them", ->
marker = displayBuffer.markScreenRange([[5, 4], [5, 10]])
displayBuffer.setMarkerHeadScreenPosition(marker, [-5, -4])
expect(displayBuffer.getMarkerBufferRange(marker)).toEqual [[0, 0], [8, 4]]
displayBuffer.setMarkerTailScreenPosition(marker, [-5, -4])
expect(displayBuffer.getMarkerBufferRange(marker)).toEqual [[0, 0], [0, 0]]
describe "marker observation", ->
observeHandler = null
beforeEach ->
@@ -662,3 +670,12 @@ describe "DisplayBuffer", ->
displayBuffer.unfoldBufferRow(4)
expect(observeHandler).not.toHaveBeenCalled()
describe "marker destruction", ->
it "allows markers to be destroyed", ->
marker = displayBuffer.markScreenRange([[5, 4], [5, 10]])
displayBuffer.destroyMarker(marker)
expect(displayBuffer.getMarkerBufferRange(marker)).toBeUndefined()

View File

@@ -76,12 +76,12 @@ describe "EditSession", ->
describe "when the cursor is on the first line", ->
it "moves the cursor to the beginning of the line, but retains the goal column", ->
editSession.setCursorScreenPosition(row: 0, column: 4)
editSession.setCursorScreenPosition([0, 4])
editSession.moveCursorUp()
expect(editSession.getCursorScreenPosition()).toEqual(row: 0, column: 0)
expect(editSession.getCursorScreenPosition()).toEqual([0, 0])
editSession.moveCursorDown()
expect(editSession.getCursorScreenPosition()).toEqual(row: 1, column: 4)
expect(editSession.getCursorScreenPosition()).toEqual([1, 4])
it "merges cursors when they overlap", ->
editSession.addCursorAtScreenPosition([1, 0])
@@ -185,9 +185,9 @@ describe "EditSession", ->
describe "when the cursor is on the last column of a line", ->
describe "when there is a subsequent line", ->
it "wraps to the beginning of the next line", ->
editSession.setCursorScreenPosition(row: 0, column: buffer.lineForRow(0).length)
editSession.setCursorScreenPosition([0, buffer.lineForRow(0).length])
editSession.moveCursorRight()
expect(editSession.getCursorScreenPosition()).toEqual(row: 1, column: 0)
expect(editSession.getCursorScreenPosition()).toEqual [1, 0]
describe "when the cursor is on the last line", ->
it "remains in the same position", ->

View File

@@ -95,8 +95,8 @@ class BufferChangeOperation
restoreMarkers: (markersToRestore) ->
for [id, previousRange] in markersToRestore
if existingMarker = @buffer.validMarkers[id]
existingMarker.setRange(previousRange)
else
@buffer.validMarkers[id] = @buffer.invalidMarkers[id]
if validMarker = @buffer.validMarkers[id]
validMarker.setRange(previousRange)
else if invalidMarker = @buffer.invalidMarkers[id]
@buffer.validMarkers[id] = invalidMarker

View File

@@ -283,6 +283,10 @@ class Buffer
markPosition: (position, options) ->
@markRange([position, position], _.defaults({noTail: true}, options))
destroyMarker: (id) ->
delete @validMarkers[id]
delete @invalidMarkers[id]
getMarkerPosition: (args...) ->
@getMarkerHeadPosition(args...)

View File

@@ -12,10 +12,10 @@ class Cursor
needsAutoscroll: null
constructor: ({@editSession, @marker}) ->
# @editSession.observeMarkerHeadScreenPosition @marker, (screenPosition) ->
# @needsAutoscroll ?= @isLastCursor()
# @trigger 'moved', e
# @editSession.trigger 'cursor-moved', e
@editSession.observeMarkerHeadScreenPosition @marker, (screenPosition) =>
@needsAutoscroll ?= @isLastCursor()
@trigger 'moved', screenPosition
@editSession.trigger 'cursor-moved', screenPosition
@needsAutoscroll = true
destroy: ->
@@ -36,7 +36,7 @@ class Cursor
@goalColumn = null
@clearSelection()
@needsAutoscroll = options.autoscroll ? @isLastCursor()
@editSession.setMarkerHeadBufferPosition(@marker, screenPosition, options)
@editSession.setMarkerHeadBufferPosition(@marker, bufferPosition, options)
getBufferPosition: ->
@editSession.getMarkerHeadBufferPosition(@marker)

View File

@@ -312,6 +312,11 @@ class DisplayBuffer
markBufferPosition: (bufferPosition) ->
@buffer.markPosition(bufferPosition)
destroyMarker: (id) ->
@buffer.destroyMarker(id)
delete @markerScreenPositionObservers[id]
delete @markerScreenPositions[id]
getMarkerScreenRange: (id) ->
@screenRangeForBufferRange(@getMarkerBufferRange(id))
@@ -328,6 +333,7 @@ class DisplayBuffer
@screenPositionForBufferPosition(@getMarkerHeadBufferPosition(id))
setMarkerHeadScreenPosition: (id, screenPosition, options) ->
screenPosition = @clipScreenPosition(screenPosition, options)
@setMarkerHeadBufferPosition(id, @bufferPositionForScreenPosition(screenPosition, options))
getMarkerHeadBufferPosition: (id) ->
@@ -340,6 +346,7 @@ class DisplayBuffer
@screenPositionForBufferPosition(@getMarkerTailBufferPosition(id))
setMarkerTailScreenPosition: (id, screenPosition, options) ->
screenPosition = @clipScreenPosition(screenPosition, options)
@setMarkerTailBufferPosition(id, @bufferPositionForScreenPosition(screenPosition, options))
getMarkerTailBufferPosition: (id) ->

View File

@@ -440,21 +440,48 @@ class EditSession
markBufferPosition: (args...) ->
@displayBuffer.markBufferPosition(args...)
destroyMarker: (args...) ->
@displayBuffer.destroyMarker(args...)
getMarkerBufferRange: (args...) ->
@displayBuffer.getMarkerBufferRange(args...)
getMarkerScreenRange: (args...) ->
@displayBuffer.getMarkerScreenRange(args...)
getMarkerScreenPosition: (args...) ->
@displayBuffer.getMarkerScreenPosition(args...)
getMarkerBufferPosition: (args...) ->
@displayBuffer.getMarkerBufferPosition(args...)
getMarkerHeadScreenPosition: (args...) ->
@displayBuffer.getMarkerHeadScreenPosition(args...)
setMarkerHeadScreenPosition: (args...) ->
@displayBuffer.setMarkerHeadScreenPosition(args...)
getMarkerHeadBufferPosition: (args...) ->
@displayBuffer.getMarkerHeadBufferPosition(args...)
setMarkerHeadBufferPosition: (args...) ->
@displayBuffer.setMarkerHeadBufferPosition(args...)
getMarkerTailScreenPosition: (args...) ->
@displayBuffer.getMarkerTailScreenPosition(args...)
setMarkerTailScreenPosition: (args...) ->
@displayBuffer.setMarkerTailScreenPosition(args...)
getMarkerTailBufferPosition: (args...) ->
@displayBuffer.getMarkerTailBufferPosition(args...)
setMarkerTailBufferPosition: (args...) ->
@displayBuffer.setMarkerTailBufferPosition(args...)
observeMarkerHeadScreenPosition: (args...) ->
@displayBuffer.observeMarkerHeadScreenPosition(args...)
addAnchor: (options={}) ->
anchor = @buffer.addAnchor(_.extend({editSession: this}, options))
@anchors.push(anchor)
@@ -495,7 +522,7 @@ class EditSession
@addSelection(marker).cursor
addCursorAtBufferPosition: (bufferPosition) ->
marker = @markBufferPosition(screenPosition, stayValid: true)
marker = @markBufferPosition(bufferPosition, stayValid: true)
@addSelection(marker).cursor
addCursor: (marker) ->