Home Download Buy Blog Forum Support

Some small changes to snippets

Some small changes to snippets

Postby mazzzzz on Fri Aug 03, 2012 5:48 pm

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! http://sublimetext.userecho.com/topic/6 ... s-removed/

I.
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:
Code: Select all
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.

II.
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:
Code: Select all
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.

EDIT: Solved
There actually is a command for this, it's 0:
Code: Select all
function ${1: name} (${2:"${3:Some Text}"})
{
    ${0:#Some Code}
}


III.
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:
Code: Select all
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.
Code: Select all
get_template_part('${1:slug}', '${2:name}'); ${/://slug-name.php}


EDIT
There is actually a trick using (I think) look backward regexs (info about it here: http://www.regular-expressions.info/lookaround.html, I believe the syntax they use is ?<=, but I'm not sure), an example:
Code: Select all
${1:something}${1/(^something$)|.*/?1: # some comment or something/}

This is also great for multichoice:
Code: Select all
${1} = ${1/(a$)|(b$)|(c$)|.*/?1:Apple:?2:Bannana:?3:Carrot/}

Thoughts, comments, constructive criticisms?
Last edited by mazzzzz on Fri Aug 03, 2012 11:32 pm, edited 2 times in total.
mazzzzz
 
Posts: 7
Joined: Fri Aug 03, 2012 5:37 pm

Re: Some small changes to snippets

Postby agibsonsw on Fri Aug 03, 2012 9:38 pm

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

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

Pressing Delete gives me

Code: Select all
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.

Code: Select all
{ "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.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Some small changes to snippets

Postby agibsonsw on Fri Aug 03, 2012 9:42 pm

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 ;)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Some small changes to snippets

Postby agibsonsw on Fri Aug 03, 2012 9:57 pm

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

Code: Select all
{ "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

Code: Select all
<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>
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Some small changes to snippets

Postby mazzzzz on Fri Aug 03, 2012 11:11 pm

@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
mazzzzz
 
Posts: 7
Joined: Fri Aug 03, 2012 5:37 pm


Return to Ideas and Feature Requests

Who is online

Users browsing this forum: Alexa [Bot], Exabot [Bot], Yahoo [Bot] and 11 guests