Search code examples
javavisual-studio-codecode-snippets

How to create a custom get method snippet in VsCode


I wanted to create a custom get method snippet on VsCode and I know about the users snippets so created one, but it did not came out as expected.

I tried creating a snippet like this one (for java):

"get": {
        "prefix": "get",
        "body": [
            "public ${1:Type} get${2:Property} () {",
            "\treturn ${2: property};",
            "}"
        ],
        "description": "Creates a get method"
    }

But when I type the first "parameter" of the snippet, it is correlated with the third (on the return line), what I wanted but in all in lower case, and if possible camel case, so the result would be something like this:

public Type getPropertyName(){
    return propertyName;
}

and not this:

public Type getPropertyName(){
    return PropertyName;
}

Solution

  • After some research about Regex I could bring a solution to it! The sinppet to the get method would be this one:

    "get": {
            "prefix": "get",
            "body": [
                "public ${1:Type} get${2:Property} () {",
                "\treturn ${2/([A-Z])/${1:/downcase}/};",
                "}"
            ],
            "description": "Creates a get method"
        }