conditional replacements

Asked by slanted

Having a heck of a time getting the conditonal snippet to work:

snippet mytest "mytest"
/**
 * ${1/(void)/(?1:----:=====)/}
 */
${1:void}
endsnippet

what I expect to happen is if the input is "void", I get -----, if its not void, I get ===== . But what happens is I never get ==== and only get the text that was inputed:

e.g.:

input of beans (expected =====)
 /**
 * beans
 */
beans

input of void
/**
 * ----
 */
void

What am I doing wrong?

Thanks!

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

Your regex does not match 'anything else'. It has to match for the transformation to be done. That means you have to include the "or else" case:

snippet mytest "mytest"
/**
 * ${1/(void)|(.*)/(?1:----:=====)/}
 */
${1:void}
endsnippet

Now the regex will always match but only capture void in the 1 group, otherwise everything else in the 2 group.

I strongly recommend using python interpolation for this kind of work though. It usually reads much clearer and is easier to understand than regexps. just my 2 c.

Revision history for this message
slanted (ryu-b) said :
#2

Excellent. Thanks

Revision history for this message
slanted (ryu-b) said :
#3

Thanks SirVer, that solved my question.