Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode nested snippets (or include a snippet inside another snippet)

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.

like image 524
Jin Yan Avatar asked Oct 25 '19 22:10

Jin Yan


2 Answers

@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.

like image 117
Jin Yan Avatar answered Oct 22 '22 03:10

Jin Yan


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.

like image 44
Mark Avatar answered Oct 22 '22 03:10

Mark