Replicate initial EditSession selection state

This commit is contained in:
Ben Ogle & Nathan Sobo
2013-07-17 10:25:28 -07:00
parent baeae2d8d5
commit f32e1fc643
2 changed files with 40 additions and 22 deletions

View File

@@ -5,22 +5,25 @@ describe "EditSession replication", ->
[editSession1, editSession2] = []
beforeEach ->
editSession1 = project.open('sample.js')
editSession1.setScrollTop(5)
editSession1.setScrollLeft(5)
editSession1.setCursorScreenPosition([0, 5])
editSession1.addSelectionForBufferRange([[1, 2], [3, 4]])
doc1 = editSession1.getState()
doc2 = doc1.clone(createSite(2))
doc1.connect(doc2)
editSession2 = deserialize(doc2)
it "replicates the selections", ->
expect(editSession2.getSelectedBufferRanges()).toEqual editSession1.getSelectedBufferRanges()
it "replicates the scroll position", ->
editor1 = new Editor(editSession1)
editor2 = new Editor(editSession2)
expect(editSession2.getScrollTop()).toBe editSession1.getScrollTop()
expect(editSession2.getScrollLeft()).toBe editSession1.getScrollLeft()
editor1.attachToDom().width(50).height(50)
editor2.attachToDom().width(50).height(50)
editSession1.setScrollTop(10)
expect(editSession2.getScrollTop()).toBe 10
editor1.scrollTop(10)
expect(editor1.scrollTop()).toBe 10
expect(editor2.scrollTop()).toBe 10
editor2.scrollLeft(20)
expect(editor2.scrollLeft()).toBe 20
expect(editor1.scrollLeft()).toBe 20
editSession2.setScrollLeft(20)
expect(editSession1.getScrollLeft()).toBe 20

View File

@@ -34,14 +34,27 @@ class EditSession
softWrap: false
constructor: (optionsOrState) ->
@cursors = []
@selections = []
if optionsOrState instanceof telepath.Document
project.editSessions.push(this)
@state = optionsOrState
{tabLength, softTabs, @softWrap} = @state.toObject()
@buffer = project.bufferForId(@state.get('bufferId'))
console.log @buffer.getMarkers().map (m) -> m.getAttributes()
@displayBuffer = new DisplayBuffer(@buffer, { tabLength })
console.log "looking for", @getSelectionMarkerAttributes()
for marker in @findMarkers(@getSelectionMarkerAttributes())
console.log marker
@addSelection(marker)
@setScrollTop(@state.get('scrollTop'))
@setScrollLeft(@state.get('scrollLeft'))
cursorScreenPosition = @state.getObject('cursorScreenPosition')
else
{@buffer, tabLength, softTabs, @softWrap} = optionsOrState
@state = telepath.Document.create
@@ -49,15 +62,11 @@ class EditSession
version: @constructor.version
scrollTop: 0
scrollLeft: 0
cursorScreenPosition = [0, 0]
@displayBuffer = new DisplayBuffer(@buffer, { tabLength })
@addCursorAtScreenPosition([0, 0])
@softTabs = @buffer.usesSoftTabs() ? softTabs ? true
@languageMode = new LanguageMode(this, @buffer.getExtension())
@displayBuffer = new DisplayBuffer(@buffer, { @languageMode, tabLength })
@cursors = []
@selections = []
@addCursorAtScreenPosition(cursorScreenPosition)
@softTabs = @buffer.usesSoftTabs() ? softTabs ? true
@buffer.retain()
@subscribe @buffer, "path-changed", =>
project.setPath(path.dirname(@getPath())) unless project.getPath()?
@@ -770,6 +779,9 @@ class EditSession
getMarker: (id) ->
@displayBuffer.getMarker(id)
findMarkers: (attributes) ->
@displayBuffer.findMarkers(attributes)
# {Delegates to: DisplayBuffer.markScreenRange}
markScreenRange: (args...) ->
@displayBuffer.markScreenRange(args...)
@@ -817,16 +829,19 @@ class EditSession
#
# Returns the new {Cursor}.
addCursorAtScreenPosition: (screenPosition) ->
marker = @markScreenPosition(screenPosition, invalidationStrategy: 'never')
marker = @markScreenPosition(screenPosition, @getSelectionMarkerAttributes())
@addSelection(marker).cursor
getSelectionMarkerAttributes: ->
type: 'selection', editSessionId: @id, invalidation: 'never'
# Adds a cursor at the provided `bufferPosition`.
#
# bufferPosition - An {Array} of two numbers: the buffer row, and the buffer column.
#
# Returns the new {Cursor}.
addCursorAtBufferPosition: (bufferPosition) ->
marker = @markBufferPosition(bufferPosition, invalidationStrategy: 'never')
marker = @markBufferPosition(bufferPosition, invalidation: 'never', type: 'selection')
@addSelection(marker).cursor
# Adds a cursor to the `EditSession`.
@@ -877,7 +892,7 @@ class EditSession
#
# Returns the new {Selection}.
addSelectionForBufferRange: (bufferRange, options={}) ->
options = _.defaults({invalidationStrategy: 'never'}, options)
options = _.defaults(@getSelectionMarkerAttributes(), options)
marker = @markBufferRange(bufferRange, options)
@addSelection(marker, options)