C# 语法高亮着色

Fad*_*sic 4 c# themes syntax-highlighting colors visual-studio-code

我可以像在 Visual Studio 中一样单独更改字段/局部变量的颜色吗?下面是 Visual Studio 中的样子(字段变量赋值,读写为绿色,局部变量赋值,读写为白色):

视觉工作室着色

但在 VSCode 中,本地变量和字段变量的范围都是“variable.other.object.cs”和“variable.other.readwrite.cs”,结果是:

在此输入图像描述

(玩家和动画都是绿色的)

我还想区分静态和动态对象,它们都具有“variable.other.object.cs”范围。

在 Visual Studio 中,它看起来像这样:

在此输入图像描述

但在 VSCode 中看起来像这样:

在此输入图像描述

我能做些什么吗?谢谢

NPr*_*ras 5

为了实现你想要的,荧光笔需要“理解”代码语义。VSCode 中的语义高亮是在今年年初推出的,它对语言的支持仍然有限。OmniSharp 是 C# 荧光笔背后的引擎,开始支持基于 Roslyn 的语义作为实验性功能

您首先需要选择启用该功能,然后重新启动 VSCode 才能使其生效。

"csharp.semanticHighlighting.enabled": true,
Run Code Online (Sandbox Code Playgroud)

我假设您知道如何调用范围检查器,但为了其他读者的缘故,它位于View>>Command PalletteDeveloper: Inspect Editor Tokens and Scopes

您现在有两个选项来指定自定义突出显示;要么使用新推出的semantic token,要么使用老式的textMateRule范围。

语义

语义标记也会给你一个修饰符(static在本例中)

"editor.semanticTokenColorCustomizations": {
    "enabled": true,
    "rules": {
        "field.static": {
            "foreground": "#FF0000",
            "fontStyle": "italic",
        },
    },
},
Run Code Online (Sandbox Code Playgroud)

或者,如果您更喜欢使用textMateRules,我还没有弄清楚如何指定修饰符。

"editor.tokenColorCustomizations": {
    "textMateRules": [
        {
            "scope": "entity.name.variable.field.cs",
            "settings": {
                "foreground": "#FF0000",
                "fontStyle": "italic",
            },
        },
    ]
},
Run Code Online (Sandbox Code Playgroud)