From f32e1fc6430b655592d520864e1252ff9be4776e Mon Sep 17 00:00:00 2001 From: Ben Ogle & Nathan Sobo Date: Wed, 17 Jul 2013 10:25:28 -0700 Subject: [PATCH] Replicate initial EditSession selection state --- spec/app/edit-session-replication-spec.coffee | 25 +++++++------ src/app/edit-session.coffee | 37 +++++++++++++------ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/spec/app/edit-session-replication-spec.coffee b/spec/app/edit-session-replication-spec.coffee index 6db80f82b..1fb22ad99 100644 --- a/spec/app/edit-session-replication-spec.coffee +++ b/spec/app/edit-session-replication-spec.coffee @@ -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 diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 9db5ca6da..5b6de878f 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -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)