Add EditSession.transact and friends

`EditSession.transact` adds more semantics on top of `UndoManager`'s
version pertaining to restoring selections on undo/redo of the
transaction.
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-01-04 12:42:09 -07:00
parent e65c2df093
commit ec13c38c7d
4 changed files with 51 additions and 23 deletions

View File

@@ -1678,6 +1678,28 @@ describe "EditSession", ->
expect(editSession.getSelectedBufferRange()).toEqual [[2, 2], [3, 3]]
expect(otherEditSession.getSelectedBufferRange()).toEqual [[3, 3], [3, 3]]
describe ".transact([fn])", ->
describe "when called without a function", ->
it "restores the selection when the transaction is undone/redone", ->
buffer.setText('1234')
editSession.setSelectedBufferRange([[0, 1], [0, 3]])
editSession.transact()
editSession.delete()
editSession.moveCursorToEndOfLine()
editSession.insertText('5')
expect(buffer.getText()).toBe '145'
editSession.commit()
editSession.undo()
expect(buffer.getText()).toBe '1234'
expect(editSession.getSelectedBufferRange()).toEqual [[0, 1], [0, 3]]
editSession.redo()
expect(buffer.getText()).toBe '145'
expect(editSession.getSelectedBufferRange()).toEqual [[0, 3], [0, 3]]
describe "when the buffer is changed (via its direct api, rather than via than edit session)", ->
it "moves the cursor so it is in the same relative position of the buffer", ->
expect(editSession.getCursorScreenPosition()).toEqual [0, 0]

View File

@@ -224,14 +224,11 @@ class Buffer
else
operation.do()
transact: (fn) ->
@undoManager.transact(fn)
undo: (editSession) ->
@undoManager.undo(editSession)
redo: (editSession) ->
@undoManager.redo(editSession)
transact: (fn) -> @undoManager.transact(fn)
undo: (editSession) -> @undoManager.undo(editSession)
redo: (editSession) -> @undoManager.redo(editSession)
commit: -> @undoManager.commit()
abort: -> @undoManager.abort()
save: ->
@saveAs(@getPath()) if @isModified()

View File

@@ -235,6 +235,27 @@ class EditSession
redo: ->
@buffer.redo(this)
transact: (fn) ->
isNewTransaction = @buffer.transact()
console.log isNewTransaction
oldSelectedRanges = @getSelectedBufferRanges()
@pushOperation
undo: (editSession) ->
editSession?.setSelectedBufferRanges(oldSelectedRanges)
if fn
fn()
@commit() if isNewTransaction
commit: ->
newSelectedRanges = @getSelectedBufferRanges()
@pushOperation
redo: (editSession) ->
editSession?.setSelectedBufferRanges(newSelectedRanges)
@buffer.commit()
abort: ->
@buffer.abort()
foldAll: ->
@displayBuffer.foldAll()
@@ -303,19 +324,6 @@ class EditSession
mutateSelectedText: (fn) ->
@transact => fn(selection) for selection in @getSelections()
transact: (fn) ->
@buffer.transact =>
oldSelectedRanges = @getSelectedBufferRanges()
@pushOperation
undo: (editSession) ->
editSession?.setSelectedBufferRanges(oldSelectedRanges)
fn()
newSelectedRanges = @getSelectedBufferRanges()
@pushOperation
redo: (editSession) ->
editSession?.setSelectedBufferRanges(newSelectedRanges)
pushOperation: (operation) ->
@buffer.pushOperation(operation, this)

View File

@@ -29,13 +29,14 @@ class UndoManager
@clear()
transact: (fn) ->
shouldCommit = not @currentTransaction?
isNewTransaction = not @currentTransaction?
@currentTransaction ?= []
if fn
try
fn()
finally
@commit() if shouldCommit
@commit() if isNewTransaction
isNewTransaction
commit: ->
@undoHistory.push(@currentTransaction) if @currentTransaction?.length