When snippet expansion is undone, the snippet is destroyed

This is still in progress. You can't *redo* snippet expansion and restore tab stops. Also, this commit performs all changes associated with snippet expansion in a transaction.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-06 12:09:45 -06:00
parent 6177b46cf9
commit 9f9c636883
7 changed files with 21 additions and 18 deletions

View File

@@ -142,9 +142,9 @@ class Buffer
@lines[startRow..endRow] = newLines
@modified = true
pushOperation: (operation) ->
pushOperation: (operation, editSession) ->
if @undoManager
@undoManager.pushOperation(operation)
@undoManager.pushOperation(operation, editSession)
else
operation.do()

View File

@@ -238,16 +238,19 @@ class EditSession
transact: (fn) ->
@buffer.transact =>
oldSelectedRanges = @getSelectedBufferRanges()
@buffer.pushOperation
@pushOperation
undo: (editSession) ->
editSession?.setSelectedBufferRanges(oldSelectedRanges)
fn()
newSelectedRanges = @getSelectedBufferRanges()
@buffer.pushOperation
@pushOperation
redo: (editSession) ->
editSession?.setSelectedBufferRanges(newSelectedRanges)
pushOperation: (operation) ->
@buffer.pushOperation(operation, this)
getAnchors: ->
new Array(@anchors...)

View File

@@ -12,13 +12,13 @@ class UndoManager
@undoHistory = []
@redoHistory = []
pushOperation: (operation) ->
pushOperation: (operation, editSession) ->
if @currentTransaction
@currentTransaction.push(operation)
else
@undoHistory.push([operation])
@redoHistory = []
operation.do?()
operation.do?(editSession)
transact: (fn) ->
if @currentTransaction

View File

@@ -5,11 +5,12 @@ class SnippetExpansion
constructor: (snippet, @editSession) ->
@editSession.selectToBeginningOfWord()
startPosition = @editSession.getCursorBufferPosition()
@editSession.insertText(snippet.body)
if snippet.tabStops.length
@placeTabStopAnchorRanges(startPosition, snippet.tabStops)
if snippet.lineCount > 1
@indentSubsequentLines(startPosition.row, snippet)
@editSession.transact =>
@editSession.insertText(snippet.body)
if snippet.tabStops.length
@placeTabStopAnchorRanges(startPosition, snippet.tabStops)
if snippet.lineCount > 1
@indentSubsequentLines(startPosition.row, snippet)
placeTabStopAnchorRanges: (startPosition, tabStopRanges) ->
@tabStopAnchorRanges = tabStopRanges.map ({start, end}) =>

View File

@@ -28,7 +28,10 @@ module.exports =
editSession = editor.activeEditSession
prefix = editSession.getLastCursor().getCurrentWordPrefix()
if snippet = @snippetsByExtension[editSession.getFileExtension()][prefix]
editSession.snippetExpansion = new SnippetExpansion(snippet, editSession)
editSession.transact ->
editSession.snippetExpansion = new SnippetExpansion(snippet, editSession)
editSession.pushOperation
undo: -> editSession.snippetExpansion.destroy()
else
e.abortKeyBinding()