mirror of
https://github.com/atom/atom.git
synced 2026-02-19 02:44:29 -05:00
Distinguish remote and local selections in EditSession
Also: Add an Environment spec helper class that swaps out global variables to simulate running code in different environments for the replication specs.
This commit is contained in:
committed by
Corey Johnson & Matt Colyer
parent
5161a71bae
commit
636f81703c
@@ -141,6 +141,15 @@ class DisplayBufferMarker
|
||||
isDestroyed: ->
|
||||
@bufferMarker.isDestroyed()
|
||||
|
||||
getOriginSiteId: ->
|
||||
@bufferMarker.getOriginSiteId()
|
||||
|
||||
isLocal: ->
|
||||
@bufferMarker.isLocal()
|
||||
|
||||
isRemote: ->
|
||||
@bufferMarker.isRemote()
|
||||
|
||||
getAttributes: ->
|
||||
@bufferMarker.getAttributes()
|
||||
|
||||
|
||||
@@ -31,12 +31,16 @@ class EditSession
|
||||
languageMode: null
|
||||
displayBuffer: null
|
||||
cursors: null
|
||||
remoteCursors: null
|
||||
selections: null
|
||||
remoteSelections: null
|
||||
suppressSelectionMerging: false
|
||||
|
||||
constructor: (optionsOrState) ->
|
||||
@cursors = []
|
||||
@remoteCursors = []
|
||||
@selections = []
|
||||
@remoteSelections = []
|
||||
if optionsOrState instanceof telepath.Document
|
||||
@state = optionsOrState
|
||||
@id = @state.get('id')
|
||||
@@ -64,7 +68,10 @@ class EditSession
|
||||
scrollLeft: 0
|
||||
@setBuffer(buffer)
|
||||
@setDisplayBuffer(displayBuffer)
|
||||
@addCursorAtScreenPosition([0, 0]) unless suppressCursorCreation
|
||||
|
||||
if @getCursors().length is 0 and not suppressCursorCreation
|
||||
position = _.last(@getRemoteCursors())?.getBufferPosition() ? [0, 0]
|
||||
@addCursorAtBufferPosition(position)
|
||||
|
||||
@languageMode = new LanguageMode(this, @buffer.getExtension())
|
||||
@state.on 'changed', ({key, newValue}) =>
|
||||
@@ -831,6 +838,8 @@ class EditSession
|
||||
getCursor: ->
|
||||
_.last(@cursors)
|
||||
|
||||
getRemoteCursors: -> new Array(@remoteCursors...)
|
||||
|
||||
# Adds a cursor at the provided `screenPosition`.
|
||||
#
|
||||
# screenPosition - An {Array} of two numbers: the screen row, and the screen column.
|
||||
@@ -856,7 +865,10 @@ class EditSession
|
||||
# Returns the new {Cursor}.
|
||||
addCursor: (marker) ->
|
||||
cursor = new Cursor(editSession: this, marker: marker)
|
||||
@cursors.push(cursor)
|
||||
if marker.isLocal()
|
||||
@cursors.push(cursor)
|
||||
else
|
||||
@remoteCursors.push(cursor)
|
||||
@trigger 'cursor-added', cursor
|
||||
cursor
|
||||
|
||||
@@ -879,7 +891,12 @@ class EditSession
|
||||
@destroyFoldsIntersectingBufferRange(marker.getBufferRange())
|
||||
cursor = @addCursor(marker)
|
||||
selection = new Selection(_.extend({editSession: this, marker, cursor}, options))
|
||||
@selections.push(selection)
|
||||
|
||||
if marker.isLocal()
|
||||
@selections.push(selection)
|
||||
else
|
||||
@remoteSelections.push(selection)
|
||||
|
||||
selectionBufferRange = selection.getBufferRange()
|
||||
@mergeIntersectingSelections()
|
||||
if selection.destroyed
|
||||
@@ -929,7 +946,10 @@ class EditSession
|
||||
#
|
||||
# selection - The {Selection} to remove.
|
||||
removeSelection: (selection) ->
|
||||
_.remove(@selections, selection)
|
||||
if selection.isLocal()
|
||||
_.remove(@selections, selection)
|
||||
else
|
||||
_.remove(@remoteSelections, selection)
|
||||
|
||||
# Clears every selection. TODO
|
||||
clearSelections: ->
|
||||
@@ -964,14 +984,16 @@ class EditSession
|
||||
getLastSelection: ->
|
||||
_.last(@selections)
|
||||
|
||||
getRemoteSelections: -> new Array(@remoteSelections...)
|
||||
|
||||
# Gets all selections, ordered by their position in the buffer.
|
||||
#
|
||||
# Returns an {Array} of {Selection}s.
|
||||
getSelectionsOrderedByBufferPosition: ->
|
||||
@getSelections().sort (a, b) ->
|
||||
aRange = a.getBufferRange()
|
||||
bRange = b.getBufferRange()
|
||||
aRange.end.compare(bRange.end)
|
||||
@getSelections().sort (a, b) -> a.compare(b)
|
||||
|
||||
getRemoteSelectionsOrderedByBufferPosition: ->
|
||||
@getRemoteSelections().sort (a, b) -> a.compare(b)
|
||||
|
||||
# Gets the very last selection, as it's ordered in the buffer.
|
||||
#
|
||||
@@ -1042,6 +1064,9 @@ class EditSession
|
||||
getSelectedBufferRanges: ->
|
||||
selection.getBufferRange() for selection in @getSelectionsOrderedByBufferPosition()
|
||||
|
||||
getRemoteSelectedBufferRanges: ->
|
||||
selection.getBufferRange() for selection in @getRemoteSelectionsOrderedByBufferPosition()
|
||||
|
||||
# Gets the selected text of the most recently added {Selection}.
|
||||
#
|
||||
# Returns a {String}.
|
||||
|
||||
@@ -524,6 +524,15 @@ class Selection
|
||||
@setBufferRange(@getBufferRange().union(otherSelection.getBufferRange()), options)
|
||||
otherSelection.destroy()
|
||||
|
||||
compare: (other) ->
|
||||
@getBufferRange().compare(other.getBufferRange())
|
||||
|
||||
isLocal: ->
|
||||
@marker.isLocal()
|
||||
|
||||
isRemote: ->
|
||||
@marker.isRemote()
|
||||
|
||||
### Internal ###
|
||||
|
||||
screenRangeChanged: ->
|
||||
|
||||
Reference in New Issue
Block a user