Home Download Buy Blog Forum Support

Designate final cursor position in snippet

Designate final cursor position in snippet

Postby jamesgecko on Fri Mar 02, 2012 4:45 pm

I'm writing a few snippets to use in Ruby on Rails Erb views. For example:
Code: Select all
<snippet>
    <content><![CDATA[<% if $1 %>

<% else %>

<% end %>
    ]]></content>
    <tabTrigger>-if</tabTrigger>
    <scope>text.html.ruby</scope>
    <description>&lt;% if %&gt;&lt;% else %&gt;&lt;% end %&gt;</description>
</snippet>

Here I want to be able to fill out the if condition, then hit tab and have the cursor move inside the if block. Ideally, I'd like be able to trigger another snippet here, but the current snippet is still active so I can't. Failing that, I'd like to just leave the cursor inside the if block and end the snippet block.

What I want to do:
-if<tab>
(fill out the if statement)<tab>
(inside the if block. this snippet is over; now I can trigger other snippets)

What currently happens:
-if<tab>
(fill out the if statement)<tab>
(jump to after the end tag. move the cursor back up to the if block manually.)


I guess I can just make three snippets. This seemed more convenient.
jamesgecko
 
Posts: 1
Joined: Fri Mar 02, 2012 4:31 pm

Re: Designate final cursor position in snippet

Postby agibsonsw on Fri Mar 02, 2012 8:58 pm

Not sure that I fully understand, but.. you can just add ${2}, ${3} at the positions where you wish to tab to, and (generally) add $0 at the end of the line as a finishing point. You can use ${2:sometext} if you want a text place-holder/prompt.

To allow one snippet to be used within another, I set

Code: Select all
"auto_complete_commit_on_tab": true,
"auto_complete_with_fields": false,

in File Settings (Syntax Specific), and

Code: Select all
[
   { "keys": ["ctrl+alt+right"], "command": "insert_best_completion", "args": {"default": "", "exact": true} }, // deleted "\t"
   { "keys": ["ctrl+alt+right"], "command": "insert_best_completion", "args": {"default": "", "exact": false},  // deleted "\t"
      "context":
      [
         { "key": "setting.tab_completion", "operator": "equal", "operand": true }
      ]
   },
   { "keys": ["ctrl+alt+right"], "command": "replace_completion_with_next_completion", "context":
      [
         { "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
         { "key": "setting.tab_completion", "operator": "equal", "operand": true }
      ]
   },
   { "keys": ["ctrl+alt+left"], "command": "replace_completion_with_prev_completion", "context":
      [
         { "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
         { "key": "setting.tab_completion", "operator": "equal", "operand": true }
      ]
   }
]

in Key-Bindings (User). This allows me to tab between fields, but use Ctrl-Space (perhaps) and Ctrl-Alt_Left and Right for the other/nested snippet or completion.
"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: Designate final cursor position in snippet

Postby C0D312 on Fri Mar 02, 2012 9:02 pm

For more info on snippets, check out the unofficial docs: http://readthedocs.org/docs/sublime-tex ... ppets.html
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am


Return to General Discussion

Who is online

Users browsing this forum: ungoogleable and 27 guests