【问题标题】:VSCode nested snippets (or include a snippet inside another snippet)VSCode 嵌套片段(或在另一个片段中包含一个片段)
【发布时间】:2019-10-25 22:14:17
【问题描述】:

我想知道我是否可以在 VSCode 用户定义的 sn-p 中引用另一个 sn-p。

说我有

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

有没有办法将 sn-p_test1 插入到另一个 sn-p 中

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

现在 sn-p_test2 只输出snippet_test1 而不是 sn-p_test1 的内容。

【问题讨论】:

  • 我不相信,但您可以轻松地将 sn-ps 与宏链接在一起。
  • 你知道如何添加一个宏来做到这一点吗?
  • 我已经为此打开了github.com/microsoft/vscode/issues/101526 到 VSC 存储库,因为我有很多有用的 sn-ps 我无法编写,因为无法将 sn-ps 嵌入到其他 sn-ps 中.如果这得到 20 个赞,那么他们将实施它。

标签: visual-studio-code code-snippets vscode-snippets


【解决方案1】:

@Mark 为使用宏提供了一个很好的答案,对于感兴趣的人,我得到了另一个可能的答案。

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

对于Test2,它只显示除Test1内容之外的sn-p_test1,但如果你在sn-p_test1点击ctrl+space,它将显示一个可用的sn-p列表,你可以将sn-p_test2中的文本扩展为sn-p_test1 中的完整内容。

【讨论】:

  • 我喜欢你的想法,但是如果用户插入其他 sn-p 并且你不能移动到 test2 上的下一个制表位,它将破坏 snippet_test2 流。我不确定宏版本是否也能正常工作。
【解决方案2】:

我认为在彼此之间包含或嵌套 sn-ps 的唯一方法是使用宏或其他一些编程方式。这是使用宏扩展 multi-command 的解决方案。


假设你有这三个 sn-ps(在一些 sn-ps 文件中):

"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"
},

然后您的宏将首先打印Master Snippet,然后在光标所在的任何位置打印 - 光标最初将位于两个 $1 制表位位置 - 宏将插入 sn-p1。

然后使用宏中的"jumpToNextSnippetPlaceholder", 命令,您将跳转到下一个tabstop $2,它可以在任何地方——我把它放在$1 之前(sn-p1 被插入的地方),sn-p2 将被插入到tabstop $2。

您可以看到Master Snippet 是您构建结构以插入其他 sn-ps - 根据制表位

宏看起来像这样(在您的 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",
        }
      },
    ]
  }
],

然后用一些键绑定(keybindings.json)触发宏:

{
  "key": "alt+m",    // or whichever keybinding you choose
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.insertMultipleSnippets" },
  "when": "editorTextFocus" 
},

您不能使用任何 sn-p 前缀来触发整个宏,但如果您愿意,您仍然可以使用单个 sn-p 前缀来单独触发每个 sn-p。

使用上述Master Snippetsnippet1snippet2,运行宏的结果将是:

body of master snippet
snippet2 = I am snippet2
body of snippet1
some other stuff
body of snippet1

你确实失去了一些功能,比如插入的 sn-p 不能像占位符文本那样被预先选择 - 如果像 ${1:howdy} 那样使用,占位符文本 howdy 只会被插入的第一个 sn-p 覆盖。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多