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

Commit

Permalink
Merge pull request #280 from savetheclocktower/apd-278-end-stop-only
Browse files Browse the repository at this point in the history
Fix behavior when pressing Tab after a snippet reaches an explicit end stop
  • Loading branch information
savetheclocktower committed Oct 17, 2018
2 parents df459af + 5fdecd4 commit e9ed1ca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/snippet-expansion.coffee
Expand Up @@ -126,9 +126,15 @@ class SnippetExpansion
else
@goToNextTabStop()
else
succeeded = @goToEndOfLastTabStop()
@destroy()
succeeded
# The user has tabbed past the last tab stop. If the last tab stop is a
# $0, we shouldn't move the cursor any further.
if @snippet.tabStopList.hasEndStop
@destroy()
false
else
succeeded = @goToEndOfLastTabStop()
@destroy()
succeeded

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

markerSelected

goToEndOfLastTabStop: ->
Expand Down
4 changes: 4 additions & 0 deletions lib/tab-stop-list.js
Expand Up @@ -10,6 +10,10 @@ class TabStopList {
return Object.keys(this.list).length
}

get hasEndStop () {
return !!this.list[Infinity]
}

findOrCreate ({ index, snippet }) {
if (!this.list[index]) {
this.list[index] = new TabStop({ index, snippet })
Expand Down
19 changes: 18 additions & 1 deletion spec/snippets-spec.coffee
Expand Up @@ -111,6 +111,10 @@ describe "Snippets extension", ->
prefix: "t1"
body: "this is a test"

"with only an end tab stop":
prefix: "t1a"
body: "something $0 strange"

"overlapping prefix":
prefix: "tt1"
body: "this is another test"
Expand Down Expand Up @@ -341,7 +345,7 @@ describe "Snippets extension", ->
simulateTabKeyEvent()
simulateTabKeyEvent()
simulateTabKeyEvent()
expect(editor.lineTextForBufferRow(2)).toBe "go here next:(abc) and finally go here:()"
expect(editor.lineTextForBufferRow(2)).toBe "go here next:(abc) and finally go here:( )"
expect(editor.getMarkerCount()).toBe markerCountBefore

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

describe "when the only tab stop is an end stop", ->
it "terminates the snippet immediately after moving the cursor to the end stop", ->
editor.setText('')
editor.insertText 't1a'
simulateTabKeyEvent()

expect(editor.lineTextForBufferRow(0)).toBe "something strange"
expect(editor.getCursorBufferPosition()).toEqual [0, 10]

simulateTabKeyEvent()
expect(editor.lineTextForBufferRow(0)).toBe "something strange"
expect(editor.getCursorBufferPosition()).toEqual [0, 12]

describe "when tab stops are separated by blank lines", ->
it "correctly places the tab stops (regression)", ->
editor.setText('')
Expand Down

0 comments on commit e9ed1ca

Please sign in to comment.