Vscode: Feature Request: Allow snippet json prefix to be an array to support multiple prefixes for the same snippet

Created on 2 Feb 2018  路  7Comments  路  Source: microsoft/vscode

We have several of the same snippets in our snippets json:

"Function-Advanced": {
        "prefix": "function-advanced",
        "body": [
            "function ${1:Verb-Noun} {",
            "\t[CmdletBinding()]",
            "\tparam (",
            "\t\t$0",
            "\t)",
            "\t",
            "\tbegin {",
            "\t}",
            "\t",
            "\tprocess {",
            "\t}",
            "\t",
            "\tend {",
            "\t}",
            "}"
        ],
        "description": "Script advanced function definition snippet"
    },
"Cmdlet": {
        "prefix": "cmdlet",
        "body": [
            "function ${1:Verb-Noun} {",
            "\t[CmdletBinding()]",
            "\tparam (",
            "\t\t$0",
            "\t)",
            "\t",
            "\tbegin {",
            "\t}",
            "\t",
            "\tprocess {",
            "\t}",
            "\t",
            "\tend {",
            "\t}",
            "}"
        ],
        "description": "Script cmdlet definition snippet"
    },

This could be avoided by supporting an array of prefixes like so:

"Cmdlet": {
        "prefix": ["function-advanced", "cmdlet"], // <--- here
        "body": [
            "function ${1:Verb-Noun} {",
            "\t[CmdletBinding()]",
            "\tparam (",
            "\t\t$0",
            "\t)",
            "\t",
            "\tbegin {",
            "\t}",
            "\t",
            "\tprocess {",
            "\t}",
            "\t",
            "\tend {",
            "\t}",
            "}"
        ],
        "description": "Script cmdlet definition snippet"
    }

It'd help slim down snippet json files and support snippets that might go by different names.

feature-request good first issue help wanted release-notes snippets verification-needed verified

Most helpful comment

Nice work, @noamyogev84!! 馃帀

All 7 comments

Hi, can I try this one? I'm new to this repo. :)

Great. Some code pointers to get you started

  1. Update the JSON-schema so that prefix can be a string or an array here
  2. The serialised snippet (as defined in JSON) is declared here
  3. Creating snippets happens in the _parseSnippet-method. You are best off to create and push a new snippet per prefix when it is an array

Hi @jrieken thanks for this detailed howto.

@jrieken can you take a quick look at my implementation?

Done! All thanks to @noamyogev84 馃憦 馃弲

Nice work, @noamyogev84!! 馃帀

@jrieken @tylerl0706
Thanks guys!

Was this page helpful?
0 / 5 - 0 ratings