Support hitting tab in snippets with no tab stops

Closes #149
This commit is contained in:
Kevin Sawicki
2013-01-22 16:35:24 -08:00
parent 12a9fa5bfa
commit 9d3751052a
2 changed files with 14 additions and 8 deletions

View File

@@ -77,6 +77,13 @@ describe "Snippets extension", ->
expect(buffer.lineForRow(0)).toBe "this is a testvar quicksort = function () {"
expect(editor.getCursorScreenPosition()).toEqual [0, 14]
it "inserts a real tab the next time a tab is pressed after the snippet is expanded", ->
editor.insertText("t1")
editor.trigger keydownEvent('tab', target: editor[0])
expect(buffer.lineForRow(0)).toBe "this is a testvar quicksort = function () {"
editor.trigger keydownEvent('tab', target: editor[0])
expect(buffer.lineForRow(0)).toBe "this is a test var quicksort = function () {"
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

View File

@@ -12,12 +12,13 @@ class SnippetExpansion
startPosition = @editSession.getCursorBufferPosition()
@editSession.transact =>
@editSession.insertText(snippet.body, autoIndent: false)
editSession.pushOperation
do: =>
@subscribe @editSession, 'cursor-moved.snippet-expansion', (e) => @cursorMoved(e)
@placeTabStopAnchorRanges(startPosition, snippet.tabStops)
@editSession.snippetExpansion = this
undo: => @destroy()
if snippet.tabStops.length > 0
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
cursorMoved: ({oldBufferPosition, newBufferPosition}) ->
@@ -29,8 +30,6 @@ class SnippetExpansion
@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)])
@subscribe anchorRange, 'destroyed', =>