baouss
baouss

Reputation: 1890

VS code snippet substitution (transform) works with variables not placeholders

vs code supposedly is supports substation, i.e., transforms, in user-defined snippets. But its working for me only with (built-in) variables and not placeholders.

See the following snippet:

"substitution test" : {
    "prefix" : "abc",
    "body": [
        "${TM_FILENAME}",
        "${TM_FILENAME/^([^.]+)\\..+$/$1/}",
        "${TM_FILENAME/^([^.]+)\\..+$/${1:/capitalize}/}",
        "${TM_FILENAME/^([^.]+)\\..+$/${1:/upcase}/}",
        "${2:showMeInAllCapsWhenReferenced}",
        "${2/upcase}"
    ]
}

The output of lines 1-4 is as expected:

users.actions.ts
users
Users
USERS

In line 5 there is a placeholder and I reference it again in line 6. I want it to show both times, once as I type it, and again in all-caps. So e.g.:

fooFoo
FOOFOO

But the actual output is

showMeInAllCapsWhenReferenced
${2/upcase}

Is substitution/transformation of referenced placeholders (as I type) even possible?

Upvotes: 4

Views: 3338

Answers (1)

Mark
Mark

Reputation: 182131

Your last two lines should be:

"${2:showMeInAllCapsWhenReferenced}",
"${2/(.*)/${1:/upcase}/}"

After the final tab the transform is actually applied (so not technically "as you type" the placeholder replacement).

From placeholder transforms:

The inserted text is matched with the regular expression and the match or matches - depending on the options - are replaced with the specified replacement format text.

So you cannot just use :/upcase for example without the regex capture as you tried to do on line 5 - it can only transform a regex match.

Looking at the grammar section :

transform   ::= '/' regex '/' (format | text)+ '/' options 

format   ::= '$' int | '${' int '}'

            | '${' int ':' '/upcase' | '/downcase' | '/capitalize' '}'

            | '${' int ':+' if '}'

            | '${' int ':?' if ':' else '}'

            | '${' int ':-' else '}' | '${' int ':' else '}'

we see that the :/upcase must follow a regex. (The "format", of which upcase is one, must follow a "regex" in a "transform".)

Upvotes: 5

Related Questions