VSCode snippet: How to apply more than one transformation to variable

42 Views Asked by At

Similarly to some VSCode extensions, I want to create a snippet that would allow me to read the filename and transform it twice:

  • Capitalize its first letter
  • Convert it to camelcase

So if my filename is my-filename.tsx the snippet would return MyFilename

I can do one of them at a time, with this:

{
  "My Snippet": {
    "prefix": "snippet",
    "body": [
      "${TM_FILENAME_BASE/(.)/${1:/capitalize}/}",
    ]
  }
}

and

{
  "My Snippet": {
    "prefix": "snippet",
    "body": [
      "${TM_FILENAME_BASE/(.*)/${1:/camelcase}/}",
    ]
  }
}

But is it possible to chain both transformations and apply them at the same time?

1

There are 1 best solutions below

1
On BEST ANSWER

Try this snippet:

"My Snippet": {
    "prefix": "snippet",
    "body": [
        "${TM_FILENAME_BASE/(.)(.*)/${1:/capitalize}${2:/camelcase}/}",
    ]
}

It just captures the rest of your variable and camelCases it.

But doesn't this simpler version do what you want:

"${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/}",