Search code examples
visual-studio-codevscode-snippets

How do I apply a transform to a variable in a snippet in VSCode?


With the code below I want to transform the variable in $2 to lowercase but I can't quite get it to work.

    "Build Model": {
        "prefix": "mod",
        "body": [
            "import '${1:../backend.dart}';",
            "",
            "class ${2:Class} extends ManagedObject<_$2> implements _$2 {}",
            "${2/(*)/${2:/downcase}/}",
            "${2:/downcase}" //would be nice to be able to do this
            "@Table(name: \"${2/(*)/${2:/downcase}/}\")",
            "class _$2 {",
            "@Column(primaryKey: true, autoincrement: true, indexed: true)",
            "int id;",
            "$3",
            "}",
            ""
        ],
        "description": "Build a data model"
    },

Solution

  • I got it to work by changing it as following:

    "${2/([a-zA-Z]*)/${1:/downcase}/}",

    Here $1 refers to the variable within this expression (i.e. $2) and not the $1 variable higher up.

    EDIT: see answer below about using (.*) instead.