I think the only way to include or nest snippets within each other is to use a macro or some other programmatic way. Here is a solution using the macro extension multi-command
.
Lets say you have these three snippets (in some snippets file):
"Master Snippet": {
"prefix": "master_snippet",
"body": [
"body of master",
"snippet2 = $2",
"$1",
"some other stuff",
"$1",
],
"description": "build the multi-snippet"
},
"snippet1": {
"prefix": "sn1",
"body": [
"body of snippet1",
],
"description": "insert1"
},
"snippet2": {
"prefix": "sn2",
"body": [
"I am snippet2",
],
"description": "insert2"
},
Then your macro would print Master Snippet
first and then wherever the cursor is - the cursor will be in both $1 tabstop positions initally - the macro will insert the snippet1.
Then with the "jumpToNextSnippetPlaceholder",
command in the macro you will jump to the next tabstop $2 which could be anywhere - I put it before $1 (where snippet1 got inserted) and snippet2 will be inserted at tabstop $2.
You can see that the Master Snippet
is where you build the structure for inserting the other snippets - according to tabstops.
The macro would look like this (in your settings.json):
"multiCommand.commands": [
{
"command": "multiCommand.insertMultipleSnippets",
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
"name": "Master Snippet",
}
},
{
"command": "editor.action.insertSnippet",
"args": {
"name": "snippet1",
}
},
"jumpToNextSnippetPlaceholder",
{
"command": "editor.action.insertSnippet",
"args": {
"name": "snippet2",
}
},
]
}
],
and then trigger the macro with some keybinding (keybindings.json):
{
"key": "alt+m", // or whichever keybinding you choose
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.insertMultipleSnippets" },
"when": "editorTextFocus"
},
You cannot use any snippet prefix to trigger the whole macro, but you can still use the individual snippet prefixes to trigger each snippet individually if you wish.
With the above Master Snippet
, snippet1
and snippet2
the result of running the macro would be:
body of master snippet
snippet2 = I am snippet2
body of snippet1
some other stuff
body of snippet1
You do lose some functionality, like the inserted snippet cannot be pre-selected like placeholder text would be - if used like ${1:howdy}
, the placeholder text howdy
is just overwritten by the first snippet inserted.