【问题标题】:Keep $ for Powershell variable in VS code snippet在 VS 代码片段中为 Powershell 变量保留 $
【发布时间】:2020-02-22 21:55:18
【问题描述】:

我可以在 Powershell 的 VS 代码中保存带有选项卡间距的代码 sn-ps,但是当我调用 sn-p 时,它不显示我的变量的 $,从而一直忽略我的变量。它只会粘贴名称并省略$

当你选择你的代码 sn-p 时,如何让 VS 代码粘贴到 $ 中?

这是我用于“模板”sn-p 的 VS Code JSON 文件

{
    // Place your snippets for powershell here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    // "Print to console": {
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // }

"Template": {
    "prefix": "template",
    "body": [
        "<#",
        ".SYNOPSIS",
        "\t<Overview of script>",
        "",
        ".SYNTAX",
        "\t<Cmdlet-Name> -Parameter <value>",
        "",
        ".DESCRIPTION",
        "\t<Brief description of script>",
        "",
        ".PARAMETER <Parameter_Name>",
        "\t<Brief description of parameter input required. Repeat this attribute if required>",
        "",
        ".INPUTS",
        "\t<Inputs if any, otherwise state None>",
        "",
        ".OUTPUTS",
        "\t<Outputs if any, otherwise state None - example: Log file stored in C:\file.log>",
        "", 
        ".EXAMPLE",
        "\t<Example goes here. Repeat this attribute for more than one example>",
        "",
        ".REMARKS",
        "\tVersion:        1.0",
        "#>",
        "",
        "#---------------------------------------------------------[Variables]------------------------------------------------------------",
        "",
        "$var1 = <stuff>",
        "$var2 = <stuff>",
        "",
        "#---------------------------------------------------------[Import Modules]--------------------------------------------------------",
        "",
        "Import-Module <name>",
        "",
        "#-----------------------------------------------------------[Functions]------------------------------------------------------------",
        "",
        "Function <FunctionName> {",
        "\t[CmdletBindinging()]",
        "\tparam(",
        "\t\t[Parameter()]",
        "\t\t[string]$MyOptionalParameter,",
        "",
        "\t\t[Parameter(Mandatory)]",
        "\t\t[int]$MyMandatoryParameter",
        "\t)",
        "",   
        "\tTry{",
        "\t\t<code goes here>",
        "\t}",
        "", 
        "\tCatch {",
        "\t\tWrite-Host $Error.Exception",
        "\t\t$Error.Exception | Out-File Out-File $env:TEMP\file.log",
        "\t\tBreak",
        "\t}",
        "",
        "\tFinally {",
        "\t\t$time = Get-Date",
        "\t\tcompleted at $time | Out-File $env:TEMP\file.log",
        "\t}",
        "}",
        "",
        "#-----------------------------------------------------------[Execution]------------------------------------------------------------",
        "",
        "<FunctionName>",
    ],
    "description": "test"
}

}

当我调用这个 sn-p 将模板粘贴到新的 .ps1 文件中时,它会省略所有 $。你如何让那些留下来?

【问题讨论】:

  • 虽然@vrdse 链接到的帖子是关于 Visual Studio 而不是 Visual Studio Code,但其中一个答案确实针对后者: stackoverflow.com/a/43427442/45375;简而言之:使用\\$
  • $$ 有效。你可以用另一个$ 转义$
  • @Mark,使用$$ 偶然,有点有效,但其目的不是逃避,它会导致不同的行为 - 请看我的回答。

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


【解决方案1】:

在 Visual Studio Code sn-p 正文中使用 \\$ 来嵌入 文字 $

例如,要嵌入 PowerShell 变量 $HOME,请使用 \\$HOME

注意:从 sn-p 解析器的角度来看,single \ 是转义所必需的,但由于 sn-ps 被定义为 JavaScript 字符串,它们本身使用\ 作为转义字符,需要\\ 才能将单个\ 传递给sn-p 解析器。

the docs


顺便说一句:

使用$$ 偶然,有点有效,但它的目的不是逃避,它会导致不同的行为

$$-prefixed 标识符的位置变为 制表符,因为 Visual Studio Code 将序列解释如下:

第一个$ 变成文字[1],但第二个$ 和后面的标识符被解释为Visual Studio Code 变量引用;如果该名称的内置变量不存在,则将其用作占位符文本。


[1] 任何 $not 后跟有效的 Visual Studio Code 占位符名称或变量引用均按字面意思处理。

【讨论】:

  • 我看你是完全正确的。 ` 第二个 $ 和后面的标识符被解释为 Visual Studio Code 变量引用;如果该名称的内置变量不存在,则将其用作占位符文本。`是在文档中的某个地方吗?
  • @Mark:关于将不存在的变量名转换为占位符文本的部分已记录在案(请参阅我添加到答案中的链接);据我所知,$ 后跟一个非名称字符(在这种情况下是另一个 $)实际上不是。
  • 新链接实际上没有链接到任何东西?
  • 我以为你指的是上标 - 抱歉。我在文档中看到When a variable is unknown (that is, its name isn't defined) the name of the variable is inserted and it is transformed into a placeholder. - 这解释了它成为一个占位符并因此成为一个制表符。谢谢你的澄清。
  • 如果有一个 sn-p 创建者可以为 Powershell 解释这个,那就太好了。根据语言,它可以将 \\ 放在每个 $ 前面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 2013-04-03
  • 2017-07-28
  • 1970-01-01
相关资源
最近更新 更多