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

Expand output of interpolation #1488

Open
hektor opened this issue Sep 22, 2022 · 1 comment
Open

Expand output of interpolation #1488

hektor opened this issue Sep 22, 2022 · 1 comment

Comments

@hektor
Copy link

hektor commented Sep 22, 2022

I would like to create a snippet to make a choice from a list of options. As UltiSnips supports
choices via the ${1|<choice-1>,...,<choice-n>|} syntax I was wondering if there is any way to use shell code interpolation to generate the choice syntax.

When I use the following snippet

snippet fontnames_snippet "Snippet for fontnames"
${1|`cat /tmp/fontnames.txt | sort | uniq | awk 'NF' | paste -sd ','`|}
endsnippet

UltiSnips expands to the following:

${1|Adobe Courier Bold,...,STIXMathJax_Variants-Regular|}

where I would actually like to expand this again to get the desired menu of choices.
Is there a way to achieve this with UltiSnips?

@hektor hektor changed the title Expand output of interpolation (e.g. for generating choice syntax) Expand output of interpolation Sep 22, 2022
@BertrandSim
Copy link

BertrandSim commented Sep 28, 2022

This can be done through the expansion of an anonymous snippet:

global !p

def font_names_choicer(snip, filename):
	file = open(filename, "r")
	font_names = file.readlines()
	snip.expand_anon('${1|' + ','.join(font_names) + '|}')

endglobal


post_jump "font_names_choicer(snip, '/tmp/fontnames.txt')"
snippet fontnames_snippet "Snippet for fontnames"
endsnippet

In the above,
'${1|' + ','.join(font_names) + '|}' results in something of the form ${1|Adobe Courier Bold,...,STIXMathJax_Variants-Regular|}, which then gets expanded through expand_anon().

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

No branches or pull requests

2 participants