3

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.

Bird
  • 572
  • 5
  • 15
  • Just use : "${1/(.*)/${1:/upcase}/}", It isn't clear, do you want the tags as well? – Mark Feb 05 '19 at 02:17
  • Sorry for the misunderstanding! I want a placeholder at `$1` (before editing it). The `selection` tag is meant to simulate the **step-by-step** snippet mechanism. – Bird Feb 05 '19 at 03:05
  • Does this answer your question? [Transform and regex in Code Snippets in VSCode - Docs](https://stackoverflow.com/questions/51227382/transform-and-regex-in-code-snippets-in-vscode-docs) – Kia Kaha Nov 27 '20 at 20:37

1 Answers1

12

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.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • 1
    Thanks a bunch! `Just to save some frustration...` > Yes, I think I came across this situation once, but I felt like maybe I misunderstood something back then. Thanks for the link! – Bird Feb 05 '19 at 03:09
  • When I tried the hack with `${4:SECTION NAME}` or `${3:SECTION NAME}`, one of those default texts didn't transform to uppercase. Am I doing something wrong? – Qwerty Jul 03 '19 at 10:08
  • I am trying to achieve something similar here https://stackoverflow.com/questions/56869050/how-to-fill-line-to-certain-length-using-regex-replace – Qwerty Jul 03 '19 at 12:33
  • Thank you so very much. I was struggling with this a lot and your example for how it works with a default variable just saved my day. – Swashata Ghosh Mar 18 '21 at 09:28