double-quoted string expansion

Asked by AbuAljori

Hello , And Thank you for making this cool plugin.

Trying to expand a snippet only if it's inside double quoted string.

this is a ruby snippet, which try to expand the '#' to '#{string}' .

 snippet "\"(.+)?#\"" "String interpolation" br
 #{${1:string}}
 endsnippet

How can I achieve this please ? . Thank you.

Question information

Language:
English Edit question
Status:
Solved
For:
UltiSnips Edit question
Assignee:
No assignee Edit question
Solved by:
SirVer
Solved:
Last query:
Last reply:
Revision history for this message
Best SirVer (sirver) said :
#1

You probably cannot - regular expressions are - well, regular. That means that they cannot count the number of " that come before the snippet expansion, i.e. you cannot know if you are inside a string or not.

This is an approximation of what you asked.

snippet # "Description" iw
`!p
if not snip.c:
 in_string = "rubyString" in vim.eval("synIDattr(synID(line('.'),col('.')-1,1),'name')")
 snip.rv = '#{' if in_string else "#"
`${0}`!p
if not snip.c:
 snip.rv = '' if not in_string else "}"`
endsnippet

it highjacks syntax highlighting to find out where you currently are in your file. It has some shortcomings:

1) if you are not in a string you would ideally not expand this snippet at all. But this does not work.
2) if you are in a string, you would love to have #{${1:blah}}$0, so that you can jump out of the "" with jump forward, this would need dynamic snippts though and these are not supported right now.

Revision history for this message
AbuAljori (bekacho-php) said :
#2

Thank you for the quick response and great support !

perfect implementation, exactly what I am looking for.

There is a lot to learn in regex in vim scripting too.

Thank you so much ! Have a good day !

Revision history for this message
AbuAljori (bekacho-php) said :
#3

Thanks SirVer, that solved my question.