mirror of
https://github.com/atom/atom.git
synced 2026-01-23 05:48:10 -05:00
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:
@@ -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])
|
||||
|
||||
@@ -16,6 +16,9 @@ class AnchorRange
|
||||
getScreenRange: ->
|
||||
new Range(@startAnchor.getScreenPosition(), @endAnchor.getScreenPosition())
|
||||
|
||||
containsBufferPosition: (bufferPosition) ->
|
||||
@getBufferRange().containsPoint(bufferPosition)
|
||||
|
||||
destroy: ->
|
||||
@startAnchor.destroy()
|
||||
@endAnchor.destroy()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user