SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.52k stars 690 forks source link

How to let a snippet modify other parts of the code as well? #1539

Closed gergap closed 1 year ago

gergap commented 1 year ago

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:

void test_foobar(void)
{
}

2.) Update test registration code

void register_tests(void)
{
    UREGISTER_NAME("module/component");
    UREGISTER_INIT(test_init);
    UREGISTER_CLEANUP(test_cleanup);
    UREGISTER_TEST(test_foobar);
}

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:

snippet test "Test Function" b
void test_${1:name}(void)
{
    $0
}
endsnippet

Post-expand functions:

" name: Function name to add
function! RegisterTest(name)
    call search("register_tests")
    normal! j%
    exe ":normal! OUREGISTER_TEST(".a:name.")"
endfunction

Maybe this is possible using some python code, I don't know. Any suggestions how to achieve this?

SirVer commented 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
gergap commented 1 year ago

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.

gergap commented 1 year ago

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.

gergap commented 1 year ago

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