visual-studio-codevscode-snippets

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


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

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?


Solution

  • 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}/}",