Search code examples
vscode-extensionscode-snippetstabstop

How would I add an "extended" tabstop in snippets?


I'm trying to make a snippet with multiple choices and I need only one of the choices to have a subset of choices after selecting it.

For Example:

"Basic Furniture": {
    "prefix": "!bfurniture",
    "description": "Basic Furniture Template",
    "scope": "json",
    "body": [
        "{",
        "  \"id\": $1,",
        "  \"props\": [",
        "    \"${2|nothing,chair,all|}\"",
        "  ],",
        "  \"description\": [",
        "    \"${0:...}\"",
        "  ]",
        "}"
    ]
}

Here I want it so that if I choose nothing then it will continue on to the next tabstop (e.g. $0}, but if I choose chair then it would open another choice like ${3|wooden,plastic|} and only then continue on to the next tabstop ($0).

Any ideas on how I can pull this off?

I tried using the nested tabstops like the documentation showed but their functionality is different. I also tried nesting another choice tabstop inside the $2 choice tabstop but only resulted in $2 turning to plain text while $3 would become the only choice tabstop.

(e.g. ${2|nothing,chair ${3|wooden,plastic|},all|})


Solution

  • If you don't find an answer you like you could do it a silly way like this:

    • Make the main snippet only do half of the snippet you have now, up to the choices.

    • Then create different snippets based on all the choices you have.

    For example create a separate 'chair' snippet with the prefix chairbf.

    Now when you choose 'chair' in the main snippet you can type 'bf' after to get the completion for the chair choice snippet. This separate snippet will have the chair submenu options plus the ending of the main snippet you have now.

    You'll have to create the other choice snippets, for 'nothing' and 'all', as well.

    Edit: For the string completion problem you can:

    • Move both quotes to the chair snippet so there is no string when doing the second snippet completion

    Edit2 Above way works. This way will leave an errant quote

    • If you don't want to do that you can also force completions in strings by using the keyboard shortcut control+space