Tuesday, July 1, 2014

UltiSnips indentation question.

Good day all,

I have an UltiSnips question that I bet someone here can help me solve.

I have recently begun using UltiSnips pretty heavily and it is
certainly a time saver.

I defined my own (trivial) snippet that helps me write a quick C block
comment as follows:

snippet /* "Create opening/closing block comments markers"
/* $1 */
endsnippet

I use this pretty extensively. I want extend this snippet using the
'visual placeholder' feature of UltiSnips so that if I have a visual
selection the snippet simply wraps it with the C block comment
markers. I have come close to getting this to work but the indentation
is wrong. Here's what I have:

snippet /* "Create opening/closing block comments markers"
/*`!p snip.rv = '\n" if snip.v.text else " "`${VISUAL}$1`!p snip.rv =
"\n" if snip.v.text else " "`*/
endsnippet

This snippet works how I want it to if I use it with no visually
selected text; it works just like the original snippet. It ALMOST
works when I do have visually selected text. Here's what happens
though: (I hope that the text shows up indented properly.)

If I visually select the bit of C code below (note that it is indented)

for (int foo = 0; foo < 100; foo++)
if ((foo % 2) == 0)
cout << "foo is even...\n";

and I press <TAB>/*<TAB> to invoke the snippet I get:

/*
for (int foo = 0; foo < 100; foo++)
if ((foo % 2) == 0)
cout << "foo is even...\n";
*/

whereas I would like to get:

/*
for (int foo = 0; foo < 100; foo++)
if ((foo % 2) == 0)
cout << "foo is even...\n";
*/

or something similar. I would like the indentation of the visually
selected text to not change by invoking the snippet. I may find this
not as useful as I'd like but I wanted to get it working and see if I
end up liking it.

I have RTFM and I don't currently see a solution to this. I have tried
using the snip.mkline() feature but it is not giving me the
indentation I expect. Am I missing something obvious?

BTW, I do know about plugins like

https://github.com/tpope/vim-commentary
and
https://github.com/tomtom/tcomment_vim

but I've never gotten used to using them; this solution, if I can get
it working the way I'd like, would likely be an easier, more
intuitive way for me to quickly comment out blocks of code.

Thanks for your help!

Ethan Alan

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: