From 6db42114f9cb42e7092ca656b56c09f8b21543f8 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 26 Jun 2012 22:42:06 -0600 Subject: [PATCH] If the user attempts to switch tab stops while the cursor is not *on* a tab stop, the snippet is terminated --- spec/extensions/snippets-spec.coffee | 22 ++++++++++++++++++++++ src/app/anchor-range.coffee | 3 +++ src/extensions/snippets/snippets.coffee | 19 ++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/spec/extensions/snippets-spec.coffee b/spec/extensions/snippets-spec.coffee index 12a7bc420..2c63ed15c 100644 --- a/spec/extensions/snippets-spec.coffee +++ b/spec/extensions/snippets-spec.coffee @@ -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]) diff --git a/src/app/anchor-range.coffee b/src/app/anchor-range.coffee index a83c49984..6340945e7 100644 --- a/src/app/anchor-range.coffee +++ b/src/app/anchor-range.coffee @@ -16,6 +16,9 @@ class AnchorRange getScreenRange: -> new Range(@startAnchor.getScreenPosition(), @endAnchor.getScreenPosition()) + containsBufferPosition: (bufferPosition) -> + @getBufferRange().containsPoint(bufferPosition) + destroy: -> @startAnchor.destroy() @endAnchor.destroy() diff --git a/src/extensions/snippets/snippets.coffee b/src/extensions/snippets/snippets.coffee index c2720d554..92f1ff7c9 100644 --- a/src/extensions/snippets/snippets.coffee +++ b/src/extensions/snippets/snippets.coffee @@ -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