Saturday, November 3, 2012

Re: Variable amount of snippet arguments in ultisnips plugin

On Friday, November 2, 2012 5:44:35 PM UTC-4, Marco wrote:
> Hi,
>
>
>
> I discovered the plugin ultisnips and like it a lot. However, I have
>
> problems setting up the snippets (BTW: I don't know python). I need
>
> a snippet that has two mandatory arguments and several optional ones
>
> (no more than four). Here an example:
>
>
>
> snippet def "def"
>
> def ${1:${VISUAL:foo}}
>
> {${2}} {${3}} {${4}}
>
> $0
>
> endsnippet
>
>
>
> Typing def <Tab> foo <C-J>bar<C-J><C-J><C-J> yields
>
>
>
> def foo
>
> {bar} {} {}
>
>
>
> I don't need the last two braces. How can I omit the empty braces in
>
> this example without defining a snippet for each number of braces?
>
>
>
>
>
> Marco

I have two suggestions for this.

snippet def "def"
def ${1:${VISUAL:foo}}
{${2}} ${3:{${4}}} ${5:{${6}}}
$0
endsnippet

global !p
def ins1(t1, t2):
return t2 if t1 else ''
endglobal

snippet def2 "def"
def ${1:${VISUAL:foo}}
{${2}} `!p snip.rv=ins1(t[3], '{')`$3`!p snip.rv=ins1(t[3], '}')` `!p snip.rv=ins1(t[4], '{')`$4`!p snip.rv=ins1(t[4], '}')`
$0
endsnippet


the first one is similar to the one you have right now, but if you have to hit <c-j> to go inside the braces, if you hit backspace there the braces are just deleted and you move to the next one (you could even nest this one more level so if you hit backspace after the first parenthesis the optional ones would be skipped.


The second one just creates the parenthesis if you type something there. The disadvantage here is that you don't see the optional parenthesis until you type something there.

Try the two approaches and see if which one of them (if any) is more convenient to you.

Jorge

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: