Home Download Buy Blog Forum Support

How do I setup "--" as a snippet tab trigger?

How do I setup "--" as a snippet tab trigger?

Postby carbon_steel on Sat Oct 12, 2013 8:33 am

I am currently working with fortran and I'm adding some snippets to make the workflow faster. Since fortran does not support the ++ or -- operators, I would like to make snippets that will change these operators to the explicit increment and decrement statements. For example
Code: Select all
a++

becomes
Code: Select all
a = a + 1

and
Code: Select all
a--

becomes
Code: Select all
a = a - 1

I have successfully implemented the ++ snippet as follows.
Code: Select all
<snippet>
    <content><![CDATA[ = $TM_CURRENT_WORD + 1]]></content>
    <tabTrigger>++</tabTrigger>
    <description>i = i + 1</description>
    <scope>source.fortran</scope>
</snippet>

However, my implementation of the -- snippet fails to trigger.
Code: Select all
<snippet>
    <content><![CDATA[ = $TM_CURRENT_WORD - 1]]></content>
    <tabTrigger>--</tabTrigger>
    <description>i = i - 1</description>
    <scope>source.fortran</scope>
</snippet>

Does anyone have any suggestions on how to get the -- snippet to trigger? Thanks.
carbon_steel
 
Posts: 2
Joined: Sat Oct 12, 2013 8:25 am

Re: How do I setup "--" as a snippet tab trigger?

Postby qgates on Wed Oct 16, 2013 4:11 am

Weird. Tab trigger of ++ or -- works fine for my snippets on ST3.
qgates
 
Posts: 223
Joined: Mon Nov 05, 2012 4:33 pm

Re: How do I setup "--" as a snippet tab trigger?

Postby carbon_steel on Fri Oct 18, 2013 9:01 pm

@qgates,

Does the -- snippet trigger for you when it is appended to other text? For example -- will trigger for me, but a-- will not trigger.
carbon_steel
 
Posts: 2
Joined: Sat Oct 12, 2013 8:25 am

Re: How do I setup "--" as a snippet tab trigger?

Postby qgates on Sat Oct 19, 2013 4:57 am

Ahh, and therein lies your problem. Snippets are designed to be triggered on whole words (so far as sublime sees it). The fact that a++ works for you is more by luck than by design, because for some reason sublime sees ++ as a separate "word". But it won't for almost anything else, except in a few other edge cases.

This is actually a good thing, because otherwise you'd get into a mess with snippets that have some characters of a word being triggered accidentally. For example, in your methodology, if you defined a tab trigger "bc", then typed "abc"<tab> the "bc" snippet would be triggered. The vast majority of the time we'd want to avoid that.

Given what you want to expand your snippet to, wouldn't it be better to trigger ++ and -- to something like:
Code: Select all
<snippet>
   <content><![CDATA[
${1:var} = $1 + 1
]]></content>
   <tabTrigger>++</tabTrigger>
   <scope>source.fortran</scope>
</snippet>

That way, you just type ++<tab> (or --, with the above appropriately modified), on its own, type your variable name once and it gets filled out twice, then hit <tab> to go to the end, or hit Ctrl+enter to go straight to a newline. That would seem the most logical to me. You could also make your constant a placeholder too, so you could change your increment amounts. Use this in your snippet:
Code: Select all
${1:var} = $1 + ${2:1}

By the way, with these approaches, Using <End> or <Ctrl+Enter> will leave you in field-editing mode, because snippets can be multiline. Press ESC to clear field editing.
qgates
 
Posts: 223
Joined: Mon Nov 05, 2012 4:33 pm


Return to General Discussion

Who is online

Users browsing this forum: Exabot [Bot] and 16 guests