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

Commit e9ed1ca

Browse files
Merge pull request #280 from savetheclocktower/apd-278-end-stop-only
Fix behavior when pressing Tab after a snippet reaches an explicit end stop
2 parents df459af + 5fdecd4 commit e9ed1ca

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lib/snippet-expansion.coffee

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,15 @@ class SnippetExpansion
126126
else
127127
@goToNextTabStop()
128128
else
129-
succeeded = @goToEndOfLastTabStop()
130-
@destroy()
131-
succeeded
129+
# The user has tabbed past the last tab stop. If the last tab stop is a
130+
# $0, we shouldn't move the cursor any further.
131+
if @snippet.tabStopList.hasEndStop
132+
@destroy()
133+
false
134+
else
135+
succeeded = @goToEndOfLastTabStop()
136+
@destroy()
137+
succeeded
132138

133139
goToPreviousTabStop: ->
134140
@setTabStopIndex(@tabStopIndex - 1) if @tabStopIndex > 0
@@ -168,6 +174,7 @@ class SnippetExpansion
168174
# If this snippet has at least one transform, we need to observe changes
169175
# made to the editor so that we can update the transformed tab stops.
170176
@snippets.observeEditor(@editor) if @hasTransforms
177+
171178
markerSelected
172179

173180
goToEndOfLastTabStop: ->

lib/tab-stop-list.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class TabStopList {
1010
return Object.keys(this.list).length
1111
}
1212

13+
get hasEndStop () {
14+
return !!this.list[Infinity]
15+
}
16+
1317
findOrCreate ({ index, snippet }) {
1418
if (!this.list[index]) {
1519
this.list[index] = new TabStop({ index, snippet })

spec/snippets-spec.coffee

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ describe "Snippets extension", ->
111111
prefix: "t1"
112112
body: "this is a test"
113113

114+
"with only an end tab stop":
115+
prefix: "t1a"
116+
body: "something $0 strange"
117+
114118
"overlapping prefix":
115119
prefix: "tt1"
116120
body: "this is another test"
@@ -341,7 +345,7 @@ describe "Snippets extension", ->
341345
simulateTabKeyEvent()
342346
simulateTabKeyEvent()
343347
simulateTabKeyEvent()
344-
expect(editor.lineTextForBufferRow(2)).toBe "go here next:(abc) and finally go here:()"
348+
expect(editor.lineTextForBufferRow(2)).toBe "go here next:(abc) and finally go here:( )"
345349
expect(editor.getMarkerCount()).toBe markerCountBefore
346350

347351
describe "when tab stops are nested", ->
@@ -355,6 +359,19 @@ describe "Snippets extension", ->
355359
simulateTabKeyEvent()
356360
expect(editor.getSelectedBufferRange()).toEqual [[0, 5], [0, 10]]
357361

362+
describe "when the only tab stop is an end stop", ->
363+
it "terminates the snippet immediately after moving the cursor to the end stop", ->
364+
editor.setText('')
365+
editor.insertText 't1a'
366+
simulateTabKeyEvent()
367+
368+
expect(editor.lineTextForBufferRow(0)).toBe "something strange"
369+
expect(editor.getCursorBufferPosition()).toEqual [0, 10]
370+
371+
simulateTabKeyEvent()
372+
expect(editor.lineTextForBufferRow(0)).toBe "something strange"
373+
expect(editor.getCursorBufferPosition()).toEqual [0, 12]
374+
358375
describe "when tab stops are separated by blank lines", ->
359376
it "correctly places the tab stops (regression)", ->
360377
editor.setText('')

0 commit comments

Comments
 (0)