Sublime Forum

Some small changes to snippets

#1

I’m sure a lot of you guys absolutely love the snippets, and they are really powerful, especially with some of the more complex things like nesting.

EDIT: 2 & 3 actually have solutions, so it’s really just #1, which is over on userecho, go vote for it now! sublimetext.userecho.com/topic/6 … s-removed/

[size=150]I.[/size]
The one issue I run into is that sometimes I’ll nest one placeholder in another in order to make a section optional. An example being:

function ${1: name} (${2:"${3:Some Text}"}) { ${4:#Some Code} }
(Note: Isn’t tested, just an example; and to clarify in this example #2 is the optional argument)

Now, this works great until I decide to backspace on the second placeholder (so as to get rid of the optional argument), when I tab, it still highlights the third placeholder, so then I need to tab again to get to the fourth.
It would be really nice if it realized the third placeholder didn’t exist anymore, and just skip it.

[size=150]II.[/size]
The second thing I’d like to see is the ability to specify the endpoint of a snippet. So when you tab for the last time instead of going to the outside of the snippet, the pointer would go there. In the above example the fourth placeholder is technically the end, so when I finish tabbing it should just go there. So I’d like to have a little special character, like #, that would be hit after the last placeholder was tabbed from.

So the above example would be:

function ${1: name} (${2:"${3:Some Text}"}) { ${#:#Some Code} }
It always bothers me to write a hundred lines of code while still filling in a snippet.

[size=150]EDIT: Solved[/size]
There actually is a command for this, it’s 0:

function ${1: name} (${2:"${3:Some Text}"}) { ${0:#Some Code} }

[size=150]III.[/size]
And the third thing I’d like to see is sections of the snippet that are only shown while the snippet is being created (help sections if you will).
An example with wordpress I ran into would be:

get_template_part('${1:slug}', '${2:name}'); //slug-name.php

The comment ‘//slug-name.php’ is only needed while I’m creating the snippet (it helps me remember what goes where), but I don’t want it cluttering up my code after. So I’d like something like / that denotes that section should be deleted after the snippet is done being created.

get_template_part('${1:slug}', '${2:name}'); ${/://slug-name.php}

[size=150]EDIT[/size]
There is actually a trick using (I think) look backward regexs (info about it here: regular-expressions.info/lookaround.html, I believe the syntax they use is ?<=, but I’m not sure), an example:

${1:something}${1/(^something$)|.*/?1: # some comment or something/}

This is also great for multichoice:

${1} = ${1/(a$)|(b$)|(c$)|.*/?1:Apple:?2:Bannana:?3:Carrot/}

Thoughts, comments, constructive criticisms?

0 Likes

#2

I agree with number I - it’s a pain having to tab through no-longer-existent fields. [Pressing Escape cancels any remaining fields.]

II. Use $0 to denote the end-point.

I use the following snippet for css-display and it indicates how help text could be included.

It initially displays as

display: Inline-BLock-TAble-List-RUn-in-CEll-CAption-COlumn-Footer-Header-ROw-group/None;

Pressing Delete gives me

display: |;I BL TA L RU CE CA CO F H RO N

indicating that if I press the letter ‘i’ it will insert ‘inline’. Having pressed ‘i’ the help text disappears. If I Backspace the help-text reappears. I don’t have to delete the help text.

{ "trigger": "display", "contents": "display: ${1/(bl|ta)$|(ce|ca|c|co|f|fo|h|he|r|ro)$|.*/?1:inline-:?2:table-/i}${1:Inline-BLock-TAble-List-RUn-in-CEll-CAption-COlumn-Footer-Header-ROw-group/None}${1/(b$)|(i$)|(ru$)|(t$)|(l$)|(bl$)|(ta$)|(ce$)|(ca$)|(c$)|(co$)|(f$)|(fo$)|(h$)|(he$)|(r$)|(ro$)|(n$)|(no$)|.*/?1:lock:?2:nline:?3:n-in:?4:able:?5:ist-item:?6:ock:?7:ble:?8:ll:?9:ption:?10:olumn:?11:lumn-group:?12:ooter-group:?13:oter-group:?14:eader-group:?15:ader-group:?16:ow:?17:w-group:?18:one:?19:ne/i};${1/($)|.*/?1:I BL TA L RU CE CA CO F H RO N/}" },

Variations on this can be created, but it is not perfect.

0 Likes

#3

I did consider previously displaying help text for function arguments in the status bar. It would require constantly monitoring the view, and extracting help from the relevant language files. But I decided against pursuing it :wink:

0 Likes

#4

Here’s a simple example displaying help text - it’s a completion rather than a snippet, but the principle is the same.

{ "trigger": "wibble", "contents": "wibble: $1 $2;${1/($)|.*/?1:Do you see me?/}${2/($)|.*/?1:What about me?/}" },

When at tab-position one it displays ‘Do you see me?’. This text disappears when you start typing.
The second help text ‘What about me?’ only appears when you tab to position 2, and disappears as soon as you start typing.
And, again, the help text doesn’t need to be deleted.

Added: snippet version

<snippet> <content><![CDATA[ Hello, $1 is a $2.${1/($)|.*/?1: # Do you see me?/}${2/($)|.*/?1: # What about me?/}$0 ]]></content> <tabTrigger>wobble</tabTrigger> <!-- Optional: Set a scope to limit where the snippet will trigger --> <!-- <scope>source.python</scope> --> </snippet>

0 Likes

#5

@agibsonsw
Thanks for the heads up on ${0:}, that’s exactly what I needed, and as for the hidden text, if they had the exact same thing that your solution does, but a bit cleaner, that would be nice; but for now your solution works pretty well:
get_template_part(’${1:slug}’, ‘${2:name}’);${2/(^name$)|.*/?1: //slug-name.php?/}

Thanks for the heads up on those, I’m loving snippets more and more XD

0 Likes