Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VISUAL in post_jump - can we access? How #1524

Open
JoseConseco opened this issue Feb 27, 2023 · 4 comments
Open

VISUAL in post_jump - can we access? How #1524

JoseConseco opened this issue Feb 27, 2023 · 4 comments

Comments

@JoseConseco
Copy link

I see no discussion section on ultisnips page, so I post my question here:
how do I access VISUAL (or vim yank register) from post_jump event?

I want snippet for extracting variable e.g. - a+b:
something(a + b)
selecting content of bracket and running snipped would generate:

new_var = a+b
something(new_war)

I tried to access snip.v.text or snip.visual_text in post_jump :
post_jump "if snip.tabstop == 0: insert_method_call(snip.tabstops[1].current_text, snip.visual_text)"

But ultisnips complains about snip not having v or visual_text props.

My Code

global !p
def post(var_name, visual):
	snip.buffer[snip.snippet_start[0]:snip.snippet_start[0]] = [var_name + ' = ' + visual] 
endglobal
post_jump "if snip.tabstop == 0: post(snip.tabstops[1].current_text, snip.visual_text)"
snippet var "extract var" b
$1
endsnippet 

I also tried to use ${VISUAL} in expand_anon, but no luck.

Finally there is vim - getreg() :
vim.command(":call append(line('.')-1, '"+ var_name + " = '.getreg('+'))")
But then Ultisnip complains about modifying buffer using vim.command.

@SirVer
Copy link
Owner

SirVer commented Sep 5, 2023

This is currently not supported I think, a patch + test case would be very welcome

@paulfioravanti
Copy link

My workaround for this is to store the snip.visual_text in the context, to then be accessed later during post_jump. Here is an example:

global !p

def store_visual_text(snip):
    return {"visual": snip.visual_text}

def use_visual_text(snip):
    visual = snip.context["visual"].strip(" \t\n\r")
    snip.expand_anon("Visual was: ${1:" + visual + "}")

endglobal

context "store_visual_text(snip)"
post_jump "use_visual_text(snip)"
snippet visual "test post_jump visual output"
$0
endsnippet

In my own snippets, I've extracted this kind of storing the visual text into a global helper, then use it in a snippet that helps me build html tags with attributes, with this line being where the visual text gets extracted from the context.

@KirtiPriya07
Copy link

is it still open?

@SirVer
Copy link
Owner

SirVer commented Apr 22, 2024

Yes, still open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants