From f1678fdafe9b2b6ac51b2909175dd36adb0e8f02 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 20 Jun 2012 22:47:05 -0600 Subject: [PATCH] Use an overloaded 'tab' keybinding and the new `abortKeyBinding` method to implement conditional snippet expansion If the current word prefix doesn't correspond to a valid snippet, we abort the key binding and try the next one, which ends up being the standard tab binding so a typical tab gets inserted. This is a mechanism that could support overloading of arbitrary keys. --- spec/extensions/snippets-spec.coffee | 6 ++++-- src/app/keymaps/snippets.coffee | 6 ++++++ src/extensions/snippets/snippets.coffee | 16 ++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 src/app/keymaps/snippets.coffee 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