From 9f9c636883f9285d439495a70e77e7d1112d6182 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Fri, 6 Jul 2012 12:09:45 -0600 Subject: [PATCH] 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. --- spec/app/edit-session-spec.coffee | 3 --- spec/extensions/snippets-spec.coffee | 5 ++--- src/app/buffer.coffee | 4 ++-- src/app/edit-session.coffee | 7 +++++-- src/app/undo-manager.coffee | 4 ++-- src/extensions/snippets/snippet-expansion.coffee | 11 ++++++----- src/extensions/snippets/snippets.coffee | 5 ++++- 7 files changed, 21 insertions(+), 18 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index d8ae398c3..2bdc18d68 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -8,9 +8,6 @@ describe "EditSession", -> beforeEach -> buffer = new Buffer() editSession = fixturesProject.open('sample.js', autoIndent: false) - - console.log editSession.tabText - buffer = editSession.buffer lineLengths = buffer.getLines().map (line) -> line.length diff --git a/spec/extensions/snippets-spec.coffee b/spec/extensions/snippets-spec.coffee index 39ad9a9b9..f10f1d785 100644 --- a/spec/extensions/snippets-spec.coffee +++ b/spec/extensions/snippets-spec.coffee @@ -43,7 +43,7 @@ describe "Snippets extension", -> endsnippet snippet t5 "Caused problems with undo" - first line $1 + first line$1 ${2:placeholder ending second line} endsnippet """ @@ -150,13 +150,12 @@ describe "Snippets extension", -> expect(editor.getCursorScreenPosition()).toEqual [0, 5] describe "when a previous snippet expansion has just been undone", -> - xit "expands the snippet based on the current prefix rather than jumping to the old snippet's tab stop", -> + it "expands the snippet based on the current prefix rather than jumping to the old snippet's tab stop", -> editor.insertText 't5\n' editor.setCursorBufferPosition [0, 2] editor.trigger keydownEvent('tab', target: editor[0]) expect(buffer.lineForRow(0)).toBe "first line" editor.undo() - editor.undo() expect(buffer.lineForRow(0)).toBe "t5" editor.trigger keydownEvent('tab', target: editor[0]) expect(buffer.lineForRow(0)).toBe "first line" diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index d84692e93..21aa60e57 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -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() diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 4300855b9..90af7d268 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -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...) diff --git a/src/app/undo-manager.coffee b/src/app/undo-manager.coffee index c3bcaa0df..21760ef62 100644 --- a/src/app/undo-manager.coffee +++ b/src/app/undo-manager.coffee @@ -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 diff --git a/src/extensions/snippets/snippet-expansion.coffee b/src/extensions/snippets/snippet-expansion.coffee index aaec2522a..963bfd95f 100644 --- a/src/extensions/snippets/snippet-expansion.coffee +++ b/src/extensions/snippets/snippet-expansion.coffee @@ -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}) => diff --git a/src/extensions/snippets/snippets.coffee b/src/extensions/snippets/snippets.coffee index 78d7a1b26..dbc0bff43 100644 --- a/src/extensions/snippets/snippets.coffee +++ b/src/extensions/snippets/snippets.coffee @@ -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()