Comments (4)
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.
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.
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.
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)
- Ungrab tab key in visual mode HOT 1
- Installing the newest version of `pynvim` makes Ultisnips laggy HOT 2
- multi-word triggers HOT 3
- Regex snippet starting with `\b(?i)` used to work but doesn't anymore. HOT 2
- RecursionError: maximum recursion depth exceeded in comparison HOT 4
- Some snippets use severe stutter HOT 1
- A simple snippet for mermaid within Markdown - NOT WORKING HOT 3
- Python interpolation not work HOT 2
- Python 3.12: Syntax warnings for escape sequences HOT 2
- Config file in a symlinked folder not properly recognized when calling UltiSnips Edit HOT 1
- just installed Ultisnips and got this error HOT 1
- Constant errors when python is missing (Can we enable discussions on github for this project?) HOT 1
- Error detected while processing TextChangedI Autocommands HOT 6
- [Feature Request] Subfolder in `filetype` snippets folder & Specific `extends` and `excludes` HOT 2
- Expansion of snippets no longer works HOT 1
- Is there any way to forcefully remove the current snippet status? HOT 1
- Reason why we need `snoremap <c-r> <c-g>"_c<c-r>`? HOT 2
- "Error detected while processing function UltiSnips#Edit[6]..provider#python3#Call:" HOT 3
- I can't make UltiSnips to set <tab> as jump forward trigger HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ultisnips.