mirror of
https://github.com/atom/atom.git
synced 2026-02-11 07:05:11 -05:00
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:
6
src/app/keymaps/snippets.coffee
Normal file
6
src/app/keymaps/snippets.coffee
Normal 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'
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user