atom / snippets

Atom snippets package
MIT License
205 stars 100 forks source link

Support TextMate-style snippet transformations #257

Closed savetheclocktower closed 6 years ago

savetheclocktower commented 6 years ago

This is a gigantic feature request, but I’d argue it’s worth the effort to implement.

Since 1.x, TextMate has supported snippet “transformations” (see section 7.7) that allow certain tab stops to run a regex substitution on their contents before being put into the buffer. It's an awesome feature and one of the things I dearly miss, since there's no easy way to replicate the functionality.

Examples

Make HTML tag pair

Given a snippet:

<${1:p}>$0</${1/\s.*//}>

I can expand this snippet, type div class='row', and end up with

<div class='row'></div>

because the transformation in the latter tab stop replaces the first space and everything after it with an empty string.

Make Markdown banner

Given a snippet:

${1:banner}
${1/./=/}

I can expand this snippet, type Lorem Ipsum, and end up with

Lorem Ipsum
===========

because the transformation in the latter tab stop replaces each character I type with a = character.

Challenges

Based on how snippets are currently implemented, I think this would be quite hard. While a snippet is “active,” the snippets package appears to react only to the Tab key, and doesn’t exert control over how text is inserted. If a numbered tab stop appears in more than one place (“mirroring” in TextMate snippet parlance), the package supports this by creating multiple selections. It would be surprising for a user to see this…

screen shot 2017-12-05 at 2 15 19 pm

…and then, after some typing, find out that those two selections may not be identical.

So to implement this would probably involve hooking into TextEditor#onDidChange rather than treating transformed tab-stop content as a selection.

I’d be willing to take a stab at this, but I’m opening this issue to test the waters first. It’s possible I might be the only person on earth who cares about this feature.

savetheclocktower commented 6 years ago

Whoops! Duplicate of #40, which was a couple pages back in the issues list.