VSCode nested snippets (or include a snippet inside another snippet)
Asked Answered
C

2

11

I'm wondering if I can refer to another snippet within a snippet in VSCode user defined snippet.

Say I have

"Test1": {
        "prefix": "snippet_test1",
        "body": 
            "something"
}

and is there a way to insert snippet_test1 in another snippet like

"Test2": {
        "prefix": "snippet_test2",
        "body": 
            "${1:snippet_test1}"
}

Now snippet_test2 just outputs snippet_test1 instead of the content of snippet_test1.

Clipper answered 25/10, 2019 at 22:14 Comment(3)
I don't believe so, but you could chain snippets together with a macro easily.Abstractionism
do you know how to add a macro to do that?Clipper
I've opened github.com/microsoft/vscode/issues/101526 to VSC repo for exactly this since I have so many useful snippets I can't write because there is no way to embed snippets within other snippets. If this gets 20 thumbs ups, then they will implement it.Lo
C
5

@Mark provides a good answer to use macros and I got another possible answer for who are interested.

"Test1": {
        "prefix": "snippet_test1",
        "body": 
            "something"
}
"Test2": {
        "prefix": "snippet_test2",
        "body": 
            "${1:snippet_test1}"
}

For Test2, it does only show snippet_test1 other than Test1 content but if you hit ctrl+space at snippet_test1, it will show a list of possible snippet available and you can expand text in snippet_test2 to the full content in snippet_test1.

Clipper answered 28/10, 2019 at 17:30 Comment(1)
I like your idea however, it'll break snippet_test2 flow if user inserts other snippet and you cant move to next tab stop on test2. I'm not sure if the macro version works properly either.Rossen
A
4

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.

Abstractionism answered 27/10, 2019 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.