Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code Snippet | Placeholder + Transform

Goal

I can't find a way to create the following snippet :

[
    "${1:SECTION NAME/(.*)/${1:/upcase}/}",
    "====================================\n$0"
]

I want the following outcome :

<selection>SECTION NAME</selection>
====================================

I then enter: "i am hopeless". [TAB]

I AM HOPELESS
====================================
<selection />

Almost there!!

The closest I got is this :

[
    "${1/(.*)/${1:/upcase}/} ${1:SECTION NAME}",
    "====================================\n$0"
]

But I get a duplicate.

like image 495
Bird Avatar asked Sep 15 '26 04:09

Bird


1 Answers

Just to save some frustration, placeholder transforms do not work on default or choice syntaxes. As in:

"${1:foo/(.*)/$1:/upcase}/}"
"${2:|foo,bar|/(.*)/{1:/upcase}/}",

They do work when there is no default or choice value. So the following works:

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

Also, you can use a default variable if it is not transformed at the inital tabstop but is later transformed on a subsequent usage. So the following works:

  "${4:SECTION NAME}",      
  "${4/(.*)/${1:/upcase}/}",

or the reverse also works:

  "${3/(.*)/${1:/upcase}/}",
  "${3:SECTION NAME}",   

So you can provide a default but it cannot be transformed until another reference to that same tabstop.

See the discussion vscode issues: placeholder transforms. No word if work on supporting transforms of default/choice variables is being worked on.

like image 171
Mark Avatar answered Sep 21 '26 08:09

Mark