【问题标题】:Visual studio code snippet choices and variablesVisual Studio 代码片段选择和变量
【发布时间】:2021-05-29 05:00:09
【问题描述】:

我正在编写自己的 sn-p 到 console.log

{  
  "Print to console": {
    "scope": "javascript,typescript",
    "prefix": "debug",
    "body": [
      "console.log('${1|[Debug],[Server],[$TM_FILENAME]|}','${2:~Line: $TM_LINE_NUMBER ~ File:$TM_FILENAME}',${3:$TM_SELECTED_TEXT}); //debug",
      "$4"
    ],
    "description": "Log output to console"
  }
}

一切正常,除了文件名静态文本 TM_FILENAME 显示在占位符“$1”的选项中 ${1|[Debug],[Server],[$TM_FILENAME]|}。 如何使文件名显示在此处? 感谢您的建议

【问题讨论】:

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


【解决方案1】:

虽然您不能在 sn-p 选项中直接使用变量,但您可以像这样解决它:

  "Print to console": {
    "scope": "javascript,typescript",
    "prefix": "debug",
    "body": [

      // moved brackets in the choice
      "console.log('[${1|Debug,Server],fileName|}]','${2:~Line: $TM_LINE_NUMBER ~ File:$TM_FILENAME}',${3:$TM_SELECTED_TEXT}); //debug",
      "$4"
    ],
    "description": "Log output to console"
  },

  "getfileName": {
    "scope": "javascript,typescript",
    "prefix": "fileName",   // <= same exact prefix as appears in the choice above
    "body": [
      "$TM_FILENAME"
    ],
    "description": "get the file name"
  }

fileName 选项实际上是另一个 sn-p,您可以在做出选择后触发它。选择 fileName 后,您必须按 Ctrl+Space 来调出第二个 sn-p。这是一个多一点的工作,但它确实允许您在 sn-p 选择中获得“类似变量”的行为。在第二个 sn-p 中,您可以随意添加或修改文件名。

此外,有时您必须稍微修改一下选择。我修改了你的,但输出是一样的。否则 [fileName] 将被打印出来,并且不会被视为与另一个 fileName 前缀的匹配项。尽管在您的情况下,您可以将其保留为:

"console.log('${1|[Debug],[Server],[fileName]|}','${2:~Line: $TM_LINE_NUMBER ~ File:$TM_FILENAME}',${3:$TM_SELECTED_TEXT}); //debug",

在第一个 sn-p 中,然后像这样制作第二个前缀:

"prefix": "fileName]",  // note the trailing bracket

同样的结果,只是要注意调整第二个前缀以匹配选择选择实际打印的内容,否则它将不起作用。

【讨论】:

    猜你喜欢
    • 2020-09-07
    • 2014-03-27
    • 1970-01-01
    • 2016-11-17
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多