atom / snippets

Atom snippets package
MIT License
204 stars 102 forks source link

Feature request: Allow tab triggers to invoke commands #221

Open JoshCheek opened 8 years ago

JoshCheek commented 8 years ago

https://github.com/JoshCheek/atom-seeing-is-believing/issues/25 was opened against my package, thought I'd play around and see if I liked anything. However, it doesn't look like there is any way to do it.

Here are some ideas for what it might look like:

Take action other than expand/tabstop-cycle

With this approach, pressing tab the second time evaluates the file instead of expanding (ie the action to take becomes configurable and defaults to the current behaviour of expansion/tabstop cycling).

'.source.ruby':
  'Add SiB annotation':
    'prefix': '#'
    'body': '# => '
  'Evaluate SiB annotation':
    'prefix': '# => '
    'action': 'seeing-is-believing:annotate-magic-comments'

A second argument for this approach is that it might handle the decision to expand / tabstop-cycle better and not require the two keymap registration files with the comment to explain them.

Hook into snippet events

This one is probably easiest, it's the same behaviour as presently exists, but it lets the user set a callback.

'.source.ruby':
  'Add SiB annotation':
    'prefix': '#'
    'body': '# => '
    'after-expand': 'seeing-is-believing:annotate-magic-comments'
JoshCheek commented 7 years ago

FWIW, I was able to hook into snippet expansion (a proof of concept) by observing the "snippets:expand" event and then checking if the scope matched and one of the cursors was just after the snippet I was interested in: https://github.com/JoshCheek/atom-seeing-is-believing/blob/fb62dcc243a34c9376d92e4d5b4c38eb8eb36b5a/lib/seeing-is-believing.coffee#L53-L64

This is just a proof of concept, but it illustrates that an evented approach could work well here (in terms of allowing extensibility and decoupling). Perhaps if the snippets emitted a 'did-expand-snippet' event, then other packages could register to be invoked after certain snippets got expanded? IDK, just musing :)

savetheclocktower commented 6 years ago

This feels like a variant of the problem I described here.

In TextMate, there are commands and there are snippets; and each can be invoked by a tab trigger or by a keystroke. In Atom, commands get invoked by keystroke and snippets get invoked by tab trigger. You can invoke a snippet by keystroke by inserting the snippet programmatically (or expanding it programmatically), but invoking a command by tab trigger is a harder problem.

The solution feels like it ought to be to make tab triggers be something that Atom core understands, rather than just a concept invented by the snippets package. But that's a lot of work. Just thinking out loud here.