use VimL function (from some addon) into snippet

Asked by ciberglo

Hello. I'm trying to create a TODO snippet that will be generic for all languages, as it'll use a vim-addon to make the comment behaviour.
In my case, I'll be using NerdCommenter vim-addon, because it provides a NerdComment(0, 'invert') function to comment the line or block that is visually selected.

So, I'm creating this snippet (based on your video(youtube) tutorials):

snippet todo "Todo reminder based on filetype" b
TODO: ${1:desc} (`date +"%F, %R"`, `echo $USER`)`
endsnippet

What I want to do is make this snippet behave like this step-by-step:
1) type "todo"<tab>
2) type description in tabstop 1, and press <tab>
3) now the snippet will visually select this line and call NerdComment to comment this TODO line <-- this is where I'm stuck

where <tab> key is my g:UltiSnipsJumpForwardTrigger

Example:
the step 3 will make this:

TODO: my description (2012-04-13, 00:10, ciberglo)

become this on a C file:

/*TODO: my description (2012-04-13, 00:10, ciberglo)*/

and this on a Python file:

#TODO: my description (2012-04-13, 00:10, ciberglo)

Could anybody help me?
Thanks in advance!

Question information

Language:
English Edit question
Status:
Answered
For:
UltiSnips Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
SirVer (sirver) said :
#1

Sorry, this is not supported currently. See https://bugs.launchpad.net/ultisnips/+bug/926256. The reason is mainly that UltiSnips would be unable to pick the changes up that the other script did which would lead to selecting the wrong tabstops or even overwriting parts of the current buffer. I have no idea how to get this right using the limited capabilities that Vim offers.

However, your special use case can be accomodated: see all.snippets shipped with ultisnips for the box snippet which works for all filetypes by parsing vim commentstring variable and inserting the correct comment symbols.

Revision history for this message
David Warren (davideugenewarren) said :
#2

Here's my solution for TODOs and FIXMEs:

def make_comment():
   b, e = vim.eval("&commentstring").split( '%s' )
   return b, e
endglobal

snippet todo "A universal TODO reminder" b
`!p
comment = make_comment()
snip.rv = comment[ 0 ]
` TODO: ${1:desc} (`!v $USERNAME`) <`!v strftime("%c")`> `!p
comment = make_comment()
snip.rv = comment[ 1 ]`
$0
endsnippet

snippet fix "A universal FIXME reminder" b
`!p
comment = make_comment()
snip.rv = comment[ 0 ]
` FIXME: ${1:desc} (`!v $USERNAME`) <`!v strftime("%c")`> `!p
comment = make_comment()
snip.rv = comment[ 1 ]`
$0
endsnippet

I'm on Windows, so $USERNAME is the relevant environment variable; $USER would work for *NIX. Any chance of merging this to all.snippets?

Revision history for this message
SirVer (sirver) said :
#3

Cool snippets! However, TODO markers are something quite personal and look different for every person; So i feel there is little benefit in including a general TODO and FIXME snippet with UltiSnips.

Can you help with this problem?

Provide an answer of your own, or ask ciberglo for more information if necessary.

To post a message you must log in.