C# syntax highlight coloring

Viewed 3277

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 Coloring

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

enter image description here

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

enter image description here

but it looks like this in VSCode:

enter image description here

Is there anything I can do about this? Thanks

1 Answers

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

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