If the user attempts to switch tab stops while the cursor is not *on* a tab stop, the snippet is terminated

This commit is contained in:
Nathan Sobo
2012-06-26 22:42:06 -06:00
parent 01993f1be2
commit 6db42114f9
3 changed files with 41 additions and 3 deletions

View File

@@ -93,6 +93,28 @@ describe "Snippets extension", ->
expect(buffer.lineForRow(0)).toBe 'go here first and then here second'
expect(editor.getSelectedBufferRange()).toEqual [[0, 8], [0, 13]]
describe "when the cursor is moved beyond the bounds of a tab stop", ->
fit "terminates the snippet on the next tab", ->
editor.setCursorScreenPosition([2, 0])
editor.insertText('t2')
editor.trigger keydownEvent('tab', target: editor[0])
editor.moveCursorRight()
editor.trigger keydownEvent('tab', target: editor[0])
expect(buffer.lineForRow(3)).toBe "go here first:() "
expect(editor.getCursorBufferPosition()).toEqual [3, 18]
# test we can terminate with shift-tab
editor.setCursorScreenPosition([4, 0])
editor.insertText('t2')
editor.trigger keydownEvent('tab', target: editor[0])
editor.trigger keydownEvent('tab', target: editor[0])
editor.moveCursorRight()
editor.trigger keydownEvent('tab', shiftKey: true, target: editor[0])
expect(editor.getCursorBufferPosition()).toEqual [4, 15]
describe "when a the start of the snippet is indented", ->
it "indents the subsequent lines of the snippet to be even with the start of the first line", ->
editor.setCursorScreenPosition([2, Infinity])

View File

@@ -16,6 +16,9 @@ class AnchorRange
getScreenRange: ->
new Range(@startAnchor.getScreenPosition(), @endAnchor.getScreenPosition())
containsBufferPosition: (bufferPosition) ->
@getBufferRange().containsPoint(bufferPosition)
destroy: ->
@startAnchor.destroy()
@endAnchor.destroy()

View File

@@ -39,6 +39,7 @@ module.exports =
class SnippetsSession
tabStopAnchorRanges: null
constructor: (@editSession, @snippetsByExtension) ->
@editSession.on 'move-cursor', => @terminateIfCursorIsOutsideTabStops()
expandSnippet: ->
return unless snippets = @snippetsByExtension[@editSession.buffer.getExtension()]
@@ -67,7 +68,7 @@ class SnippetsSession
@editSession.buffer.insert([row, 0], initialIndent)
goToNextTabStop: ->
return false unless @tabStopAnchorRanges
return false unless @ensureValidTabStops()
nextIndex = @tabStopIndex + 1
if nextIndex < @tabStopAnchorRanges.length
@setTabStopIndex(nextIndex)
@@ -77,12 +78,24 @@ class SnippetsSession
false
goToPreviousTabStop: ->
return false unless @tabStopAnchorRanges
return false unless @ensureValidTabStops()
@setTabStopIndex(@tabStopIndex - 1) if @tabStopIndex > 0
true
ensureValidTabStops: ->
@tabStopAnchorRanges? and @terminateIfCursorIsOutsideTabStops()
setTabStopIndex: (@tabStopIndex) ->
@editSession.setSelectedBufferRange(@tabStopAnchorRanges[@tabStopIndex].getBufferRange())
terminateIfCursorIsOutsideTabStops: ->
return unless @tabStopAnchorRanges
position = @editSession.getCursorBufferPosition()
for anchorRange in @tabStopAnchorRanges
return true if anchorRange.containsBufferPosition(position)
@terminateActiveSnippet()
false
terminateActiveSnippet: ->
anchor.destroy() for anchor in @tabStopAnchorRanges
anchorRange.destroy() for anchorRange in @tabStopAnchorRanges
@tabStopAnchorRanges = null