mirror of
https://github.com/atom/atom.git
synced 2026-02-16 17:45:24 -05:00
Make Buffer.transact restore marker ranges on undo/redo of transaction
We no longer need to restore selection ranges before and after transactions now because selections are based on markers so they go along for the ride for free. This allows us to delegate directly to Buffer.transact from EditSession.
This commit is contained in:
@@ -21,14 +21,15 @@ class BufferChangeOperation
|
||||
do: ->
|
||||
@buffer.pauseEvents()
|
||||
@pauseMarkerObservation()
|
||||
@oldText = @buffer.getTextInRange(@oldRange)
|
||||
@newRange = @calculateNewRange(@oldRange, @newText)
|
||||
@markersToRestoreOnUndo = @invalidateMarkers(@oldRange)
|
||||
newRange = @changeBuffer
|
||||
oldRange: @oldRange
|
||||
newRange: @newRange
|
||||
oldText: @oldText
|
||||
newText: @newText
|
||||
if @oldRange?
|
||||
@oldText = @buffer.getTextInRange(@oldRange)
|
||||
@newRange = @calculateNewRange(@oldRange, @newText)
|
||||
newRange = @changeBuffer
|
||||
oldRange: @oldRange
|
||||
newRange: @newRange
|
||||
oldText: @oldText
|
||||
newText: @newText
|
||||
@restoreMarkers(@markersToRestoreOnRedo) if @markersToRestoreOnRedo
|
||||
@buffer.resumeEvents()
|
||||
@resumeMarkerObservation()
|
||||
@@ -38,11 +39,12 @@ class BufferChangeOperation
|
||||
@buffer.pauseEvents()
|
||||
@pauseMarkerObservation()
|
||||
@markersToRestoreOnRedo = @invalidateMarkers(@newRange)
|
||||
@changeBuffer
|
||||
oldRange: @newRange
|
||||
newRange: @oldRange
|
||||
oldText: @newText
|
||||
newText: @oldText
|
||||
if @oldRange?
|
||||
@changeBuffer
|
||||
oldRange: @newRange
|
||||
newRange: @oldRange
|
||||
oldText: @newText
|
||||
newText: @oldText
|
||||
@restoreMarkers(@markersToRestoreOnUndo)
|
||||
@buffer.resumeEvents()
|
||||
@resumeMarkerObservation()
|
||||
@@ -107,7 +109,7 @@ class BufferChangeOperation
|
||||
|
||||
resumeMarkerObservation: ->
|
||||
marker.resumeEvents() for marker in @buffer.getMarkers(includeInvalid: true)
|
||||
@buffer.trigger 'markers-updated'
|
||||
@buffer.trigger 'markers-updated' if @oldRange?
|
||||
|
||||
updateMarkers: (bufferChange) ->
|
||||
marker.handleBufferChange(bufferChange) for marker in @buffer.getMarkers()
|
||||
|
||||
@@ -179,23 +179,24 @@ class BufferMarker
|
||||
###
|
||||
|
||||
tryToInvalidate: (changedRange) ->
|
||||
betweenStartAndEnd = @getRange().containsRange(changedRange, exclusive: false)
|
||||
containsStart = changedRange.containsPoint(@getStartPosition(), exclusive: true)
|
||||
containsEnd = changedRange.containsPoint(@getEndPosition(), exclusive: true)
|
||||
previousRange = @getRange()
|
||||
switch @invalidationStrategy
|
||||
when 'between'
|
||||
@invalidate() if betweenStartAndEnd or containsStart or containsEnd
|
||||
when 'contains'
|
||||
@invalidate() if containsStart or containsEnd
|
||||
when 'never'
|
||||
if containsStart or containsEnd
|
||||
if containsStart and containsEnd
|
||||
@setRange([changedRange.end, changedRange.end])
|
||||
else if containsStart
|
||||
@setRange([changedRange.end, @getEndPosition()])
|
||||
else
|
||||
@setRange([@getStartPosition(), changedRange.start])
|
||||
if changedRange
|
||||
betweenStartAndEnd = @getRange().containsRange(changedRange, exclusive: false)
|
||||
containsStart = changedRange.containsPoint(@getStartPosition(), exclusive: true)
|
||||
containsEnd = changedRange.containsPoint(@getEndPosition(), exclusive: true)
|
||||
switch @invalidationStrategy
|
||||
when 'between'
|
||||
@invalidate() if betweenStartAndEnd or containsStart or containsEnd
|
||||
when 'contains'
|
||||
@invalidate() if containsStart or containsEnd
|
||||
when 'never'
|
||||
if containsStart or containsEnd
|
||||
if containsStart and containsEnd
|
||||
@setRange([changedRange.end, changedRange.end])
|
||||
else if containsStart
|
||||
@setRange([changedRange.end, @getEndPosition()])
|
||||
else
|
||||
@setRange([@getStartPosition(), changedRange.start])
|
||||
[@id, previousRange]
|
||||
|
||||
handleBufferChange: (bufferChange) ->
|
||||
|
||||
@@ -618,26 +618,11 @@ class EditSession
|
||||
# Internal #
|
||||
###
|
||||
|
||||
transact: (fn) ->
|
||||
isNewTransaction = @buffer.transact()
|
||||
oldSelectedRanges = @getSelectedBufferRanges()
|
||||
@pushOperation
|
||||
undo: (editSession) ->
|
||||
editSession?.setSelectedBufferRanges(oldSelectedRanges, preserveFolds: true)
|
||||
if fn
|
||||
result = fn()
|
||||
@commit() if isNewTransaction
|
||||
result
|
||||
transact: (fn) -> @buffer.transact(fn)
|
||||
|
||||
commit: ->
|
||||
newSelectedRanges = @getSelectedBufferRanges()
|
||||
@pushOperation
|
||||
redo: (editSession) ->
|
||||
editSession?.setSelectedBufferRanges(newSelectedRanges, preserveFolds: true)
|
||||
@buffer.commit()
|
||||
commit: -> @buffer.commit()
|
||||
|
||||
abort: ->
|
||||
@buffer.abort()
|
||||
abort: -> @buffer.abort()
|
||||
|
||||
###
|
||||
# Public #
|
||||
|
||||
@@ -376,17 +376,30 @@ class Buffer
|
||||
operation.do()
|
||||
|
||||
# Internal:
|
||||
transact: (fn) -> @undoManager.transact(fn)
|
||||
transact: (fn) ->
|
||||
if isNewTransaction = @undoManager.transact()
|
||||
@pushOperation(new BufferChangeOperation(buffer: this)) # restores markers on undo
|
||||
if fn
|
||||
try
|
||||
fn()
|
||||
finally
|
||||
@commit() if isNewTransaction
|
||||
|
||||
commit: ->
|
||||
@pushOperation(new BufferChangeOperation(buffer: this)) # restores markers on redo
|
||||
@undoManager.commit()
|
||||
|
||||
abort: -> @undoManager.abort()
|
||||
|
||||
# Public: Undos the last operation.
|
||||
#
|
||||
# editSession - The {EditSession} associated with the buffer.
|
||||
undo: (editSession) -> @undoManager.undo(editSession)
|
||||
|
||||
# Public: Redos the last operation.
|
||||
#
|
||||
# editSession - The {EditSession} associated with the buffer.
|
||||
redo: (editSession) -> @undoManager.redo(editSession)
|
||||
commit: -> @undoManager.commit()
|
||||
abort: -> @undoManager.abort()
|
||||
|
||||
# Public: Saves the buffer.
|
||||
save: ->
|
||||
|
||||
@@ -31,11 +31,6 @@ class UndoManager
|
||||
transact: (fn) ->
|
||||
isNewTransaction = not @currentTransaction?
|
||||
@currentTransaction ?= []
|
||||
if fn
|
||||
try
|
||||
fn()
|
||||
finally
|
||||
@commit() if isNewTransaction
|
||||
isNewTransaction
|
||||
|
||||
commit: ->
|
||||
@@ -61,7 +56,6 @@ class UndoManager
|
||||
opsInReverse = new Array(batch...)
|
||||
opsInReverse.reverse()
|
||||
op.undo?(editSession) for op in opsInReverse
|
||||
|
||||
@redoHistory.push batch
|
||||
batch.oldSelectionRanges
|
||||
catch e
|
||||
|
||||
Reference in New Issue
Block a user