0

I'm building code snippets for VS Code. For a specific angular directive I need to lowercase the first letter of a previous placeholder:

<ng-container [bsInstanceof]="item">
  <ng-container *bsInstanceofCase="Ava; let ava">{{ Ava.a }}</ng-container>
  <ng-container *bsInstanceofCase="Bebe; let bebe">{{ Bebe.b }}</ng-container>
  <ng-container *bsInstanceofCase="Cece; let cece">{{ Cece.c }}</ng-container>
  <ng-container *bsInstanceofDefault>No match</ng-container>
</ng-container>

At the moment, I have the following code snippet (Marketplace):

"InstanceOf": {
    "prefix": "bs-instance-of",
    "description": "Template-driven `instanceof` switch-case",
    "body": [
        "<ng-container [bsInstanceof]=\"${1:item}\">",
        "\t<ng-container *bsInstanceofCase=\"${2:A}; let ${2/(.*)/${1:/uncapitalize}/}\">{{ ${2/(.*)/${1:/uncapitalize}/}.${3:a} }}</ng-container>",
        "\t<ng-container *bsInstanceofCase=\"${4:B}; let ${4/(.*)/${1:/uncapitalize}/}\">{{ ${4/(.*)/${1:/uncapitalize}/}.${5:b} }}</ng-container>",
        "\t<ng-container *bsInstanceofCase=\"${6:C}; let ${6/(.*)/${1:/uncapitalize}/}\">{{ ${6/(.*)/${1:/uncapitalize}/}.${7:c} }}</ng-container>",
        "\t<ng-container *bsInstanceofDefault>${8:No match}</ng-container>",
        "</ng-container>"
    ]
},

which, according to this answer and comments, should totally work. But the value of the placeholder is just being copied as-is:

VS Code snippet placeholder not transformed

Note that the comments in the linked question, state that the index before /uncapitalize is the regex match group index. Why is this not working as expected?

Pieterjan
  • 2,738
  • 4
  • 28
  • 55
  • where in the linked question does the word `uncapitalize` appear – rioV8 May 30 '23 at 09:49
  • Is your `${3:a}` somehow derived from `$2`? And are you worried about an input like `BeBe` - with another capital letter somewhere after the first letter? – Mark May 31 '23 at 20:47
  • No, placeholder 3 is the property of class `A`. The casing is a good point. I've tested it, and it works as I would expect: if I type `BeBe` and tab out of the placeholder, VS Code changes it to `beBe` – Pieterjan May 31 '23 at 22:05

1 Answers1

1

uncapitalize is not part of the snippet syntax.

You have to split the string in 2:

  • first character
  • the rest
"InstanceOf": {
  "prefix": "bs-instance-of",
  "description": "Template-driven `instanceof` switch-case",
  "body": [
      "<ng-container [bsInstanceof]=\"${1:item}\">",
      "\t<ng-container *bsInstanceofCase=\"${2:A}; let ${2/(.)(.*)/${1:/downcase}$2/}\">{{ ${2/(.)(.*)/${1:/downcase}$2/}.${3:a} }}</ng-container>",
      "\t<ng-container *bsInstanceofCase=\"${4:B}; let ${4/(.)(.*)/${1:/downcase}$2/}\">{{ ${4/(.)(.*)/${1:/downcase}$2/}.${5:b} }}</ng-container>",
      "\t<ng-container *bsInstanceofCase=\"${6:C}; let ${6/(.)(.*)/${1:/downcase}$2/}\">{{ ${6/(.)(.*)/${1:/downcase}$2/}.${7:c} }}</ng-container>",
      "\t<ng-container *bsInstanceofDefault>${8:No match}</ng-container>",
      "</ng-container>"
  ]
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • Thanks mate. This seems to work fine. I have to add that you have to `tab` beyond the placeholder before the uncap happens. It works like a charm now... – Pieterjan May 30 '23 at 10:35