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.
This commit is contained in:
Nathan Sobo
2012-06-20 22:47:05 -06:00
parent 68cb9992fc
commit f1678fdafe
3 changed files with 18 additions and 10 deletions

View File

@@ -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'

View File

@@ -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