简体   繁体   中英

C# syntax highlight coloring

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):

Visual Studio 着色

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",
            },
        },
    ]
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM