diff --git a/spec/extensions/snippets-spec.coffee b/spec/extensions/snippets-spec.coffee index f1c10caec..90b52f631 100644 --- a/spec/extensions/snippets-spec.coffee +++ b/spec/extensions/snippets-spec.coffee @@ -41,19 +41,26 @@ describe "Snippets extension", -> describe "when the snippet contains tab stops", -> it "places the cursor at the first tab-stop, and moves the cursor in response to 'next-tab-stop' events", -> + anchorCountBefore = editor.activeEditSession.getAnchors().length + editor.setCursorScreenPosition([2, 0]) editor.insertText('t2') editor.trigger keydownEvent('tab', target: editor[0]) - expect(buffer.lineForRow(0)).toBe "go here next:() and finally go here:()" - expect(buffer.lineForRow(1)).toBe "go here first:()" - expect(buffer.lineForRow(2)).toBe "var quicksort = function () {" - expect(editor.getCursorScreenPosition()).toEqual [1, 15] + expect(buffer.lineForRow(2)).toBe "go here next:() and finally go here:()" + expect(buffer.lineForRow(3)).toBe "go here first:()" + expect(buffer.lineForRow(4)).toBe " if (items.length <= 1) return items;" + expect(editor.getCursorScreenPosition()).toEqual [3, 15] editor.trigger keydownEvent('tab', target: editor[0]) - expect(editor.getCursorScreenPosition()).toEqual [0, 14] + expect(editor.getCursorScreenPosition()).toEqual [2, 14] editor.insertText 'abc' editor.trigger keydownEvent('tab', target: editor[0]) - expect(editor.getCursorScreenPosition()).toEqual [0, 40] + expect(editor.getCursorScreenPosition()).toEqual [2, 40] + + # terminate snippet + editor.trigger keydownEvent('tab', target: editor[0]) + expect(buffer.lineForRow(2)).toBe "go here next:(abc) and finally go here:( )" + expect(editor.activeEditSession.getAnchors().length).toBe anchorCountBefore describe "when the letters preceding the cursor don't match a snippet", -> it "inserts a tab as normal", -> diff --git a/src/app/anchor.coffee b/src/app/anchor.coffee index 676d3e97f..e8f1e50fd 100644 --- a/src/app/anchor.coffee +++ b/src/app/anchor.coffee @@ -56,4 +56,7 @@ class Anchor screenPosition = @editSession.screenPositionForBufferPosition(@bufferPosition, options) @setScreenPosition(screenPosition, bufferChange: options.bufferChange, clip: false, assignBufferPosition: false) + destroy: -> + @editSession.removeAnchor(this) + _.extend(Anchor.prototype, EventEmitter) diff --git a/src/extensions/snippets/snippets.coffee b/src/extensions/snippets/snippets.coffee index 32102afaa..a63d305d2 100644 --- a/src/extensions/snippets/snippets.coffee +++ b/src/extensions/snippets/snippets.coffee @@ -40,23 +40,34 @@ class SnippetsSession expandSnippet: -> return unless snippets = @snippetsByExtension[@editSession.buffer.getExtension()] prefix = @editSession.getLastCursor().getCurrentWordPrefix() - if @activeSnippet = snippets[prefix] + if snippet = snippets[prefix] @editSession.selectToBeginningOfWord() - @activeSnippetStartPosition = @editSession.getCursorBufferPosition() - @editSession.insertText(@activeSnippet.body) - @placeTabStopAnchors() - @setTabStopIndex(0) if @activeSnippet.tabStops.length + snippetStartPosition = @editSession.getCursorBufferPosition() + @editSession.insertText(snippet.body) + if snippet.tabStops.length + @placeTabStopAnchors(snippetStartPosition, snippet.tabStops) + @setTabStopIndex(0) true else false - placeTabStopAnchors: -> - @tabStopAnchors = @activeSnippet.tabStops.map (position) => - @editSession.addAnchorAtBufferPosition(position) + placeTabStopAnchors: (snippetStartPosition, tabStopPositions) -> + @tabStopAnchors = tabStopPositions.map (tabStopPosition) => + @editSession.addAnchorAtBufferPosition(snippetStartPosition.add(tabStopPosition)) goToNextTabStop: -> - return false unless @activeSnippet - @setTabStopIndex(@tabStopIndex + 1) + return false unless @tabStopAnchors + nextIndex = @tabStopIndex + 1 + if nextIndex < @tabStopAnchors.length + @setTabStopIndex(nextIndex) + true + else + @terminateActiveSnippet() + false setTabStopIndex: (@tabStopIndex) -> @editSession.setCursorBufferPosition(@tabStopAnchors[@tabStopIndex].getBufferPosition()) + + terminateActiveSnippet: -> + anchor.destroy() for anchor in @tabStopAnchors +