且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#语法突出显示颜色

更新时间:2022-10-15 23:38:23

要实现您想要的功能,荧光笔需要了解"图像.代码语义.在

语义标记也会为您提供修饰符(在此示例中为 static )

 "editor.semanticTokenColorCustomizations":{已启用":是的,规则":{"field.static":{前景":#FF0000","fontStyle":斜体",},},}, 

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

 "editor.tokenColorCustomizations":{"textMateRules":[{范围":"entity.name.variable.field.cs",设置":{前景":#FF0000","fontStyle":斜体",},},]}, 

Can I somehow change the color of field/local variables separately like in Visual Studio? Heres how it looks like in Visual Studio (field variable assignment, readwrite is green, and local variable assignment, readwrite is white):

but in VSCode, the scope for both local and field variable is "variable.other.object.cs" and "variable.other.readwrite.cs" resulting in:

(both player and anim are green)

I would also like to differentiate between a static and dynamic objects, which both have the "variable.other.object.cs" scope.

In Visual Studio it would look like this:

but it looks like this in VSCode:

Is there anything I can do about this? Thanks

To achieve what you want, the highlighter needs to "understand" code semantics. Semantic highlighting in VSCode was introduced early this year, and its support for languages are still limited. OmniSharp, the engine behind the C# highlighter, started supporting Roslyn-based semantics as an experimental feature.

You would first need to opt-in to enable the feature, then restart VSCode for it to take effect.

"csharp.semanticHighlighting.enabled": true,

I assume you know how to invoke the scope inspector, but for the sake of other readers, it's in View > Command Pallette > Developer: Inspect Editor Tokens and Scopes

You now have two options to specify custom highlighting; either using the newly available semantic token, or the old-school textMateRule scopes.

Semantic tokens will give you a modifier as well (static in this example)

"editor.semanticTokenColorCustomizations": {
    "enabled": true,
    "rules": {
        "field.static": {
            "foreground": "#FF0000",
            "fontStyle": "italic",
        },
    },
},

Or, if you prefer using textMateRules, I haven't figured out how to specify modifiers.

"editor.tokenColorCustomizations": {
    "textMateRules": [
        {
            "scope": "entity.name.variable.field.cs",
            "settings": {
                "foreground": "#FF0000",
                "fontStyle": "italic",
            },
        },
    ]
},