I have a suspicion this can’t be done, but perhaps I’ve just missed something.
I’m trying to write a snippet to insert operator declarations in Swift. The basic syntax is
(postfix|infix|prefix) operator <name> { }
But, if we are declaring an infix operator, we may also add
precedence <level> associativity (left|right|none)
inside the curly brackets.
This is what I’ve got, based on the example at the end of section 6.8 in the manual.
${1|infix,prefix,postfix|} operator ${2:name} { ${1/(infix)|prefix|postfix/${1:+\n\tassociativity ${3|left,right, none|}\n\tprecedence${4:level}\n}/}}
As you can see if you can read that lot, I’m trying to use $3 and $4 as placeholders within the conditional insertion.
It doesn’t work of course. I assume this is because in that context $3 and $4 refer to captures from the regular expression. Is that so? Is there any way round this?
Thanks to anyone who can help
Nigel Chapman
On 2 Dec 2014, at 1:47, Nigel Chapman wrote:
I’m trying to write a snippet to insert operator declarations in Swift. […] I assume this is because in that context $3 and $4 refer to captures from the regular expression. Is that so? Is there any way round this?
Correct. A snippet can’t insert new placeholders via the format string.
An approximation would be something like this:
${1|infix,prefix,postfix|} operator ${2:name} {${1/(infix)|.*/${1:+\n\tassociativity }/}${3:${1/(infix)|.*/${1:+left}/}}${1/(infix)|.*/${1:+\n\tprecedence }/}${4:${1/(infix)|.*/${1:+level}/}}${1/(infix)|.*/${1:+\n}/}$0}
Or somewhat pretty-printed:
${1|infix,prefix,postfix|} operator ${2:name} { ${1/(infix)|.*/${1:+\n\tassociativity }/} ${3:${1/(infix)|.*/${1:+left}/}} ${1/(infix)|.*/${1:+\n\tprecedence }/} ${4:${1/(infix)|.*/${1:+level}/}} ${1/(infix)|.*/${1:+\n}/} $0}
Thanks Alan, that works well enough for me, the pop-up for associativity was just icing on the cake, which I can do without.
I will study the snippet and learn for the future.