Restore selection on active edit session when undoing/redoing

The `do`, `undo`, and `redo` methods on operations take an optional editSession argument, which can be used to determine the context in which they are being run. We restore selections on that edit session instead of the session where the operations originally occurred.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-06 11:42:07 -06:00
parent 523f240fe3
commit 6177b46cf9
5 changed files with 49 additions and 31 deletions

View File

@@ -151,11 +151,11 @@ class Buffer
transact: (fn) ->
@undoManager.transact(fn)
undo: ->
@undoManager.undo()
undo: (editSession) ->
@undoManager.undo(editSession)
redo: ->
@undoManager.redo()
redo: (editSession) ->
@undoManager.redo(editSession)
save: ->
@saveAs(@getPath())

View File

@@ -95,6 +95,7 @@ class EditSession
new Point(row, column)
getFileExtension: -> @buffer.getExtension()
getPath: -> @buffer.getPath()
getEofBufferPosition: -> @buffer.getEofPosition()
bufferRangeForBufferRow: (row) -> @buffer.rangeForRow(row)
lineForBufferRow: (row) -> @buffer.lineForRow(row)
@@ -177,10 +178,10 @@ class EditSession
@insertText($native.readFromPasteboard())
undo: ->
@buffer.undo()
@buffer.undo(this)
redo: ->
@buffer.redo()
@buffer.redo(this)
foldSelection: ->
selection.fold() for selection in @getSelections()
@@ -237,10 +238,15 @@ class EditSession
transact: (fn) ->
@buffer.transact =>
oldSelectedRanges = @getSelectedBufferRanges()
@buffer.pushOperation(undo: => @setSelectedBufferRanges(oldSelectedRanges))
@buffer.pushOperation
undo: (editSession) ->
editSession?.setSelectedBufferRanges(oldSelectedRanges)
fn()
newSelectedRanges = @getSelectedBufferRanges()
@buffer.pushOperation(redo: => @setSelectedBufferRanges(newSelectedRanges))
@buffer.pushOperation
redo: (editSession) ->
editSession?.setSelectedBufferRanges(newSelectedRanges)
getAnchors: ->
new Array(@anchors...)

View File

@@ -75,26 +75,30 @@ class Project
getSoftWrap: -> @softWrap
setSoftWrap: (@softWrap) ->
open: (filePath) ->
open: (filePath, editSessionOptions={}) ->
if filePath?
filePath = @resolve(filePath)
buffer = @bufferWithPath(filePath) ? @buildBuffer(filePath)
else
buffer = @buildBuffer()
editSession = new EditSession
project: this
buffer: buffer
tabText: @getTabText()
autoIndent: @getAutoIndent()
softTabs: @getSoftTabs()
softWrap: @getSoftWrap()
@buildEditSession(buffer, editSessionOptions)
buildEditSession: (buffer, editSessionOptions) ->
options = _.extend(@defaultEditSessionOptions(), editSessionOptions)
options.project = this
options.buffer = buffer
editSession = new EditSession(options)
@editSessions.push editSession
@trigger 'new-edit-session', editSession
editSession
defaultEditSessionOptions: ->
tabText: @getTabText()
autoIndent: @getAutoIndent()
softTabs: @getSoftTabs()
softWrap: @getSoftWrap()
destroy: ->
for editSession in _.clone(@editSessions)
@removeEditSession(editSession)

View File

@@ -7,7 +7,7 @@ class UndoManager
redoHistory: null
currentTransaction: null
constructor: (@buffer) ->
constructor: ->
@startBatchCallCount = 0
@undoHistory = []
@redoHistory = []
@@ -29,18 +29,18 @@ class UndoManager
@undoHistory.push(@currentTransaction) if @currentTransaction.length
@currentTransaction = null
undo: ->
undo: (editSession) ->
if batch = @undoHistory.pop()
opsInReverse = new Array(batch...)
opsInReverse.reverse()
op.undo?() for op in opsInReverse
op.undo?(editSession) for op in opsInReverse
@redoHistory.push batch
batch.oldSelectionRanges
redo: ->
redo: (editSession) ->
if batch = @redoHistory.pop()
for op in batch
op.do?()
op.redo?()
op.do?(editSession)
op.redo?(editSession)
@undoHistory.push(batch)
batch.newSelectionRanges