Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 3df058f

Browse files
Merge pull request #273 from savetheclocktower/moving-between-tab-stop-markers
Always terminate a snippet when the cursor leaves a tab stop…
2 parents 6dec589 + 0ea9f52 commit 3df058f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/snippet-expansion.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ class SnippetExpansion
3838

3939
cursorMoved: ({oldBufferPosition, newBufferPosition, textChanged}) ->
4040
return if @settingTabStop or textChanged
41-
@destroy() unless @tabStopMarkers[@tabStopIndex].some (item) ->
41+
itemWithCursor = @tabStopMarkers[@tabStopIndex].find (item) ->
4242
item.marker.getBufferRange().containsPoint(newBufferPosition)
4343

44+
@destroy() unless itemWithCursor and not itemWithCursor.insertion.isTransformation()
45+
4446
cursorDestroyed: -> @destroy() unless @settingTabStop
4547

4648
textChanged: (event) ->

spec/snippets-spec.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ describe "Snippets extension", ->
239239
"has a placeholder that mirrors another tab stop's content":
240240
prefix: 't17'
241241
body: "$4console.${3:log}('${2:uh $1}', $1);$0"
242+
"has a transformed tab stop such that it is possible to move the cursor between the ordinary tab stop and its transformed version without an intermediate step":
243+
prefix: 't18'
244+
body: '// $1\n// ${1/./=/}'
242245

243246
it "parses snippets once, reusing cached ones on subsequent queries", ->
244247
spyOn(Snippets, "getBodyParser").andCallThrough()
@@ -744,6 +747,24 @@ describe "Snippets extension", ->
744747
placeholder PLACEHOLDER FOO foo FOO
745748
"""
746749

750+
describe "when the snippet has a transformed tab stop such that it is possible to move the cursor between the ordinary tab stop and its transformed version without an intermediate step", ->
751+
it "terminates the snippet upon such a cursor move", ->
752+
editor.setText('t18')
753+
editor.setCursorScreenPosition([0, 3])
754+
simulateTabKeyEvent()
755+
expect(editor.getText()).toBe("// \n// ")
756+
expect(editor.getCursorBufferPosition()).toEqual [0, 3]
757+
editor.insertText('wat')
758+
expect(editor.getText()).toBe("// wat\n// ===")
759+
# Move the cursor down one line, then up one line. This puts the cursor
760+
# back in its previous position, but the snippet should no longer be
761+
# active, so when we type more text, it should not be mirrored.
762+
editor.setCursorScreenPosition([1, 6])
763+
editor.setCursorScreenPosition([0, 6])
764+
editor.insertText('wat')
765+
expect(editor.getText()).toBe("// watwat\n// ===")
766+
767+
747768
describe "when the snippet contains tab stops with an index >= 10", ->
748769
it "parses and orders the indices correctly", ->
749770
editor.setText('t10')

0 commit comments

Comments
 (0)