Code Monkey home page Code Monkey logo

Comments (4)

SirVer avatar SirVer commented on June 18, 2024

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

from ultisnips.

gergap avatar gergap commented on June 18, 2024

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.

from ultisnips.

gergap avatar gergap commented on June 18, 2024

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.

from ultisnips.

gergap avatar gergap commented on June 18, 2024

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

from ultisnips.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.