Closed gergap closed 1 year ago
Year, some python code would work here. I have not tested, but something along these lines:
global !p
def find_and_add_test(snip):
for idx, line in enumerate(snip.buffer):
if line.contains("register_tests"):
break
snip.buf[idx+1:idx+1] = ["A new line!"]
endglobal
post_expand "find_and_add_test(snip)"
snippet a "desc"
blub
endsnippet
Thx, I tried to make it working, but as usual when I try to use python I fail :-(
I simplified your code for testing according to documentation:
post_expand "snip += snip.mkline(line='bla')"
snippet tt "test_action" b
endsnippet
Still I get an exception callstack containing this at the bottom:
AttributeError: 'SnippetUtilForAction' object has no attribute 'mkline'
I interpret this as the snip object is not a UltiSnips.TextObjects.SnippetUtil instance but SnippetUtilForAction, which I can't find in the docs.
I also tried to invoke a VimScript function from python, which I would prefer anyway.
global !p
def find_and_add_test(snip):
vim.command("call RegisterTest(\"foo\")")
endglobal
post_expand "find_and_add_test(snip)"
snippet test "Test Function" b
void test_${1:name}(void)
{
$0
}
endsnippet
But then I get this error:
vim.error: Vim(normal):E565: Not allowed to change text or change window)
It looks like modifying the buffer from the action is not possible. From Vim manual:
Note: While completion [is](https://vimhelp.org/motion.txt.html#is) active [Insert](https://vimhelp.org/insert.txt.html#Insert) mode can't be used recursively and
buffer text cannot be changed. Mappings that somehow invoke "[:normal](https://vimhelp.org/various.txt.html#%3Anormal) i.."
will generate an [E565](https://vimhelp.org/insert.txt.html#E565) error.
Any ideas how to make this working? I could misuse vim timers, but this would be a very dirty hack.
I found a workaround for E565 using Vim autogroups:
global !p
def find_and_add_test(name):
vim.command(':au cursorhold <buffer> ++once call registertest('+name+')')
endglobal
post_expand "find_and_add_test(t[1])"
snippet test "test function" b
void test_${1:name}(void)
{
$0
}
endsnippet
This works when I pass a hardcoded function name, but t[1]
does not work, which should be the 1st placeholder.
File "/home/gergap/.vim/plugged/ultisnips/pythonx/UltiSnips/snippet/definition/base.py", line 195, in _eval_code
exec(compiled_code or code, glob)
File "<action-code>", line 1, in <module>
NameError: name 't' is not defined
I have the impression that actions don't have the same python arguments like the snippets when using python interpolation.
I finally figured out something that works. I need to use the post_jump action, because there exists the tabstops property. A little bit different than what I expected, but works.
Snippet:
global !p
def find_and_add_test(name):
vim.command(':au cursorhold <buffer> ++once call RegisterTest("'+name+'")')
endglobal
post_jump "if snip.tabstop == 0: find_and_add_test(snip.tabstops[1].current_text)"
snippet test "test function" b
void test_${1:name}(void)
{
$0
}
endsnippet
Vimscript:
" name: Function name to add
function! RegisterTest(name)
let save_pos = getpos(".")
call search("register_tests")
normal! j%
exe ":normal! OUREGISTER_TEST(test_".a:name.");"
call setpos(".", save_pos)
endfunction
Hi, this is just a question, not an issue.
I sometimes have the usecase that when expanding a snippet I want to modify also some other location in the file. Example: 1.) Creating a new unit test function using a snippet:
2.) Update test registration code
It would be rather easy to find and add this new UREGISTER call using a Vim macro or Vimscript, but how could I integrate this? I've found this Post-expand actions in the documentation, but I don't see how to call some Vimscript function and passing the Ultisnips variables as argument.
Example Snippet:
Post-expand functions:
Maybe this is possible using some python code, I don't know. Any suggestions how to achieve this?