Fix undo/redo of snippet expansions. Tab stops are restored correctly.

We're giving up on correctly restoring snippet expansions that
occurred in a different EditSession.
This commit is contained in:
Nathan Sobo
2013-01-08 12:54:49 -07:00
parent a03bb7bf2e
commit cab5b25e76
3 changed files with 25 additions and 33 deletions

View File

@@ -198,25 +198,10 @@ describe "Snippets extension", ->
expect(buffer.lineForRow(0)).toBe " first line"
editor.undo()
editor.redo()
expect(editor.getCursorBufferPosition()).toEqual [0, 14]
editor.trigger keydownEvent('tab', target: editor[0])
expect(editor.getSelectedBufferRange()).toEqual [[1, 6], [1, 36]]
it "restores tabs stops in active edit session even when the initial expansion was in a different edit session", ->
anotherEditor = editor.splitRight()
editor.insertText ' t6\n'
editor.setCursorBufferPosition [0, 6]
editor.trigger keydownEvent('tab', target: editor[0])
expect(buffer.lineForRow(0)).toBe " first line"
editor.undo()
anotherEditor.redo()
expect(anotherEditor.getCursorBufferPosition()).toEqual [0, 14]
anotherEditor.trigger keydownEvent('tab', target: anotherEditor[0])
expect(anotherEditor.getSelectedBufferRange()).toEqual [[1, 6], [1, 36]]
describe "snippet loading", ->
it "loads snippets from all atom packages with a snippets directory", ->
expect(syntax.getProperty(['.test'], 'snippets.test')?.constructor).toBe Snippet

View File

@@ -1,35 +1,44 @@
Subscriber = require 'subscriber'
_ = require 'underscore'
module.exports =
class SnippetExpansion
snippet: null
tabStopAnchorRanges: null
settingTabStop: false
constructor: (snippet, @editSession) ->
constructor: (@snippet, @editSession) ->
@editSession.selectToBeginningOfWord()
startPosition = @editSession.getCursorBufferPosition()
@editSession.transact =>
@editSession.insertText(snippet.body, autoIndent: false)
if snippet.tabStops.length
@placeTabStopAnchorRanges(startPosition, snippet.tabStops)
if snippet.lineCount > 1
@indentSubsequentLines(startPosition.row, snippet)
editSession.pushOperation
do: =>
@subscribe @editSession, 'cursor-moved.snippet-expansion', (e) => @cursorMoved(e)
@placeTabStopAnchorRanges(startPosition, snippet.tabStops)
@editSession.snippetExpansion = this
undo: => @destroy()
@indentSubsequentLines(startPosition.row, snippet) if snippet.lineCount > 1
@editSession.on 'cursor-moved.snippet-expansion', ({oldBufferPosition, newBufferPosition}) =>
return if @settingTabStop
cursorMoved: ({oldBufferPosition, newBufferPosition}) ->
return if @settingTabStop
oldTabStops = @tabStopsForBufferPosition(oldBufferPosition)
newTabStops = @tabStopsForBufferPosition(newBufferPosition)
oldTabStops = @tabStopsForBufferPosition(oldBufferPosition)
newTabStops = @tabStopsForBufferPosition(newBufferPosition)
@destroy() unless _.intersect(oldTabStops, newTabStops).length
@destroy() unless _.intersect(oldTabStops, newTabStops).length
placeTabStopAnchorRanges: (startPosition, tabStopRanges) ->
return unless @snippet.tabStops.length > 0
@tabStopAnchorRanges = tabStopRanges.map ({start, end}) =>
anchorRange = @editSession.addAnchorRange([startPosition.add(start), startPosition.add(end)])
anchorRange.on 'destroyed', => _.remove(@tabStopAnchorRanges, anchorRange)
@subscribe anchorRange, 'destroyed', =>
_.remove(@tabStopAnchorRanges, anchorRange)
anchorRange
@setTabStopIndex(0)
indentSubsequentLines: (startRow, snippet) ->
initialIndent = @editSession.lineForBufferRow(startRow).match(/^\s*/)[0]
for row in [startRow + 1...startRow + snippet.lineCount]
@@ -70,11 +79,13 @@ class SnippetExpansion
_.intersection(@tabStopAnchorRanges, @editSession.anchorRangesForBufferPosition(bufferPosition))
destroy: ->
anchorRange.destroy() for anchorRange in new Array(@tabStopAnchorRanges...)
@editSession.off '.snippet-expansion'
@unsubscribe()
anchorRange.destroy() for anchorRange in @tabStopAnchorRanges
@editSession.snippetExpansion = null
restore: (@editSession) ->
@editSession.snippetExpansion = this
@tabStopAnchorRanges = @tabStopAnchorRanges.map (anchorRange) =>
@editSession.addAnchorRange(anchorRange.getBufferRange())
_.extend(SnippetExpansion.prototype, Subscriber)

View File

@@ -42,11 +42,7 @@ module.exports =
prefix = editSession.getLastCursor().getCurrentWordPrefix()
if snippet = syntax.getProperty(editSession.getCursorScopes(), "snippets.#{prefix}")
editSession.transact ->
snippetExpansion = new SnippetExpansion(snippet, editSession)
editSession.snippetExpansion = snippetExpansion
editSession.pushOperation
undo: -> snippetExpansion.destroy()
redo: (editSession) -> snippetExpansion.restore(editSession)
new SnippetExpansion(snippet, editSession)
else
e.abortKeyBinding()