首页 > 解决方案 > 如何在 VS Code 中为 Write-Debug 和 Write-Verbose 着色特定颜色?

问题描述

我在这里有一个简单的目标,但执行似乎相对复杂。我想要做的就是格式化Write-Debug以及Write-Verbose它们作为一行中的第一个文本出现的任何地方(有时缩进但前面没有字符)。

我想将它们格式化为与我的评论相同的颜色(或者只是手动将它们设置为相同的颜色就可以了)。可能我可能希望该行的其余部分使用相同的格式,但这是可选的。

我已经做了一个快速的谷歌,但答案的深度似乎让我对如此简单的事情有所了解,希望有人可以帮助简化它!

提前致谢。

标签: powershellvisual-studio-codevscode-settings

解决方案


您可以使用Highlight 扩展来做到这一点。

在您的设置中:

"highlight.regexes": {
  "((^\\s*Write-Debug)|(Write-Verbose))": { // A regex will be created from this string, don't forget to double escape it
    "regexFlags": "gm", // Flags used when building this regex
    "filterLanguageRegex": "powershell", // Apply only if current file's language matches this regex. Requires double escaping
    "decorations": [ // Decoration options to apply to the capturing groups
      { "color": "red" }, // Decoration options to apply to the first capturing group, in this case "//TODO"
    ]
  }
}

装饰选项中有更多样式选项。

突出显示 powershell 语法


要突出显示整行,请使用:

"((^\\s*Write-Debug.*)|(Write-Verbose.*))": {

文本必须在要修饰的捕获组中。如果你想以不同的方式装饰它,你可以把这条线的其余部分放到不同的捕获组中。


推荐阅读