4

我可以在 Powershell 的 VS 代码中保存带有选项卡间距的代码片段,但是$当我调用代码片段时,它通过不显示我的变量来忽略我的变量。它只会粘贴名称并省略$.

$当您选择代码片段时,如何让 VS 代码粘贴?

这是我用于“模板”片段的 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"
}

}

当我调用此代码段将模板粘贴到新的 .ps1 文件中时,它会省略所有$. 你怎么让那些留下来?

4

1 回答 1

4

在 Visual Studio 代码片段正文中使用\\$以嵌入文字 $.

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

注意:从片段解析器的角度来看,转义需要单个 \,但由于片段被定义为JavaScript 字符串,它们本身\用作转义字符,\\因此需要将单个\通过传递给片段解析器。

请参阅文档


作为旁白:

$$ 意外使用,有些工作,但它的目的不是逃避,它会导致不同的行为

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

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


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

于 2020-02-25T23:40:50.103 回答