diff --git a/src/packages/snippets/spec/snippets-spec.coffee b/src/packages/snippets/spec/snippets-spec.coffee index 718ea0aca..29f96ad03 100644 --- a/src/packages/snippets/spec/snippets-spec.coffee +++ b/src/packages/snippets/spec/snippets-spec.coffee @@ -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 diff --git a/src/packages/snippets/src/snippet-expansion.coffee b/src/packages/snippets/src/snippet-expansion.coffee index dd7a5d11f..74dcd5d6f 100644 --- a/src/packages/snippets/src/snippet-expansion.coffee +++ b/src/packages/snippets/src/snippet-expansion.coffee @@ -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) diff --git a/src/packages/snippets/src/snippets.coffee b/src/packages/snippets/src/snippets.coffee index 3d7a35a48..5ff21f0b7 100644 --- a/src/packages/snippets/src/snippets.coffee +++ b/src/packages/snippets/src/snippets.coffee @@ -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()