diff --git a/spec/extensions/snippets-spec.coffee b/spec/extensions/snippets-spec.coffee index 9db150add..499d88b95 100644 --- a/spec/extensions/snippets-spec.coffee +++ b/spec/extensions/snippets-spec.coffee @@ -13,6 +13,7 @@ describe "Snippets extension", -> editor = rootView.activeEditor() buffer = editor.buffer rootView.simulateDomAttachment() + rootView.enableKeymap() describe "when 'tab' is triggered on the editor", -> beforeEach -> @@ -25,13 +26,14 @@ describe "Snippets extension", -> first go here:$1 then here:$2 endsnippet """ + describe "when the letters preceding the cursor trigger a snippet", -> describe "when the snippet contains no tab stops", -> it "replaces the prefix with the snippet text and places the cursor at its end", -> editor.insertText("t1") expect(editor.getCursorScreenPosition()).toEqual [0, 2] - editor.trigger 'tab' + editor.trigger keydownEvent('tab', target: editor[0]) expect(buffer.lineForRow(0)).toBe "this is a testvar quicksort = function () {" expect(editor.getCursorScreenPosition()).toEqual [0, 14] @@ -59,7 +61,7 @@ describe "Snippets extension", -> expect(fs.read).toHaveBeenCalledWith('/tmp/foo/js.snippets') editor.insertText("t1") - editor.trigger 'tab' + editor.trigger 'snippets:expand' expect(buffer.lineForRow(0)).toBe "this is a test 1var quicksort = function () {" describe "Snippets parser", -> diff --git a/src/app/keymaps/snippets.coffee b/src/app/keymaps/snippets.coffee new file mode 100644 index 000000000..6227dd8c1 --- /dev/null +++ b/src/app/keymaps/snippets.coffee @@ -0,0 +1,6 @@ +window.keymap.bindKeys '.editor' + 'tab': 'snippets:expand' + +window.keymap.bindKeys '.editor' + 'tab': 'snippets:next-tab-stop' + 'shift-tab': 'snippets:previous-tab-stop' diff --git a/src/extensions/snippets/snippets.coffee b/src/extensions/snippets/snippets.coffee index 9e3517d00..d69a8c00d 100644 --- a/src/extensions/snippets/snippets.coffee +++ b/src/extensions/snippets/snippets.coffee @@ -9,12 +9,7 @@ module.exports = activate: (@rootView) -> @loadSnippets() - - for editor in @rootView.editors() - @enableSnippetsInEditor(editor) - - @rootView.on 'editor-open', (e, editor) => - @enableSnippetsInEditor(editor) + @rootView.on 'editor-open', (e, editor) => @enableSnippetsInEditor(editor) loadSnippets: -> snippetsDir = fs.join(atom.configDirPath, 'snippets') @@ -28,10 +23,13 @@ module.exports = @snippetsByExtension[extension] = @snippetsParser.parse(text) enableSnippetsInEditor: (editor) -> - editor.preempt 'tab', => + editor.on 'snippets:expand', (e) => editSession = editor.activeEditSession editSession.snippetsSession ?= new SnippetsSession(editSession, @snippetsByExtension) - editSession.snippetsSession.expandSnippet() + e.abortKeyBinding() unless editSession.snippetsSession.expandSnippet() + + # this is currently disabled. soon we will jump tab stops if a snippet is active + editor.on 'snippets:next-tab-stop', (e) -> e.abortKeyBinding() class SnippetsSession constructor: (@editSession, @snippetsByExtension) -> @@ -42,4 +40,6 @@ class SnippetsSession if body = snippets[prefix]?.body @editSession.selectToBeginningOfWord() @editSession.insertText(body) + true + else false