Sublime Forum

Snippet in a Snippet

#1
hi there,
I'm playing around with snippets, all works well, but I have one little problem:

first snippet:
        <snippet>
    	<content><![CDATA[
     echo"  $1 ";
    ]]></content>
    	<tabTrigger>e</tabTrigger>
    </snippet>

second snippet:
    <snippet>
    	<content><![CDATA[
    <div class=\"${1:xxx}\" >$2</div>
    ]]></content>
    	<tabTrigger>div</tabTrigger>
    </snippet>

now I'm typing < e .. tab ..>
result is  

echo" “;
cursor is between the quotation-marks , like expected
when I now type directly <div … tab> , the expected div-class does not appear,
but only when I move the cursor manually out of the quotation-marks and in again and then type <div … tab>
the result is like expected
echo” <div class=“xxx” > ";
is there a posibility to move the cursor in the snippet by code out and in again, or must it be done each time manually ?
sense of the whole is to create a echo-line in PHP and insert different code-snippets .
thanks for ideas
Max

problem is partially defused by doing this

 echo"   ";$1

now the cursor must only be moved once to the left

0 Likes

#2

If you expand your first snippet with eTab, you’ll see that in the status bar it says Field 1 of 2, indicating that you’re in the first field of a 2 field snippet. If you press Tab while it says this, the cursor jumps to the end of the text and the text in the status bar clears.

Similarly, if you expand just the second snippet with divTab, the status bar says Field 1 of 3; pressing Tab jumps the cursor inside of the div and changes the status bar to Field 2 of 3, and pressing Tab one more time jumps the cursor to the end of the text and clears the status bar.

This is a bit confusing to think of because Sublime seems to be telling you that there are more fields in the snippet than you’ve actually defined, and it keeps wanting to jump the cursor to the end of the snippet text if you press Tab enough times.

Generally speaking, the rules for snippets that contain fields like this are:

  • Pressing Tab or Shift+Tab navigates you forward and back through the defined fields, letting you edit content
  • Snippet fields are visited in order, starting at $1, then $2 and so on
  • Once the highest numbered field is expanded, pressing Tab “finalizes” the snippet by jumping to the field $0
  • If a snippet does not define a $0 field, one is appended automatically at the very end of the snippet text.

So, even a snippet with only $1 has two fields in it, $1 and the (inferred) $0, which is two fields. Pressing Tab while in a snippet changes fields (and does not expand a new snippet for example).

This is what’s happening in your snippets here. In order to do what you want, you might try with these slightly modified snippets instead:

<snippet>
    <content><![CDATA[
 echo"  $0 ";
]]></content>
    <tabTrigger>e</tabTrigger>
</snippet>
<snippet>
    <content><![CDATA[
<div class=\"${1:xxx}\" >$0</div>
]]></content>
    <tabTrigger>div</tabTrigger>
</snippet>

Now when the first snippet expands, there are no fields in it to expand, so the text expands and the cursor jumps to the inside of the echo string and the snippet is finished. The status bar doesn’t think you’re inside of a snippet, so you can freely expand the second snippet with no issues.

The second snippet doesn’t need changing in your case for this to work, but here the fields have been altered so that it first jumps to asking you the class of the div, and then once you specify it the cursor jumps to the body and the snippet is completed.

Your workarounds work because if you move the cursor enough while Sublime is expanding snippet fields, it gives up and exits the snippet early; in your case you will see that when you take your actions as you outlined above, the status bar stops saying Field x of y because the snippet is done.

You can achieve that directly in cases where you want to by pressing Esc to exit the current snippet immediately, ignoring all remaining fields. In that case you don’t need to modify the snippets at all, though you do need to remember to press Esc every time.

0 Likes

#3

thanks very much
it works perfectly
because of this I’m your next follower on your youtube-channel :innocent:

Max

0 Likes