mirror of
https://github.com/atom/atom.git
synced 2026-01-24 06:18:03 -05:00
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:
@@ -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]
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user