How can I make a VSCode theme recognize C# interfaces?

Viewed 3194

I am trying to get a theme for Visual Studio Code working to what I want. Currently, I'm trying to use Obsidian working with C# rules, but I'm not sure which key word to use to override color customizations. VSCode does not seem to recognize interfaces as they're language specific.

"editor.tokenColorCustomizations": {
        "functions" :{
            "foreground": "#F1F2F3"
        },
        "interface": { //not valid
            "foreground": "#B48C8C"
        } 
    }

How can I get VSCode color customizations to recognize c# specific syntaxes?

4 Answers

editor.tokenColorCustomizations can use a number of values: comments, functions, keywords, numbers, strings, types and variables. If none of those work for you textMateRules is available as well. So you can do something like:

"editor.tokenColorCustomizations": {
    "textMateRules": [{
        "scope": "yourScopeHere",
        "settings": {
            "fontStyle": "italic",
            "foreground": "#C69650"
        }
    }]
   },

So you just have to figure out what scope you need for "interface".

For that, try CTRL-Shift-P and type scope: choose

Developer: Inspect Editor Tokens and Scopes  

and for whichever keyword is selected, like interface you will get a listing of its textmate scope. That should be inserted as the scope value above. [In my experience, it is more accurate to open the "Inspect TM Scopes" panel and then click a couple of items and then the one, like interface, that you want - the scope panel will remain open.] You can copy from the scopes panel.

You may need only the main scope listed, but if need to narrow its scope you can include the others listed in a comma-separated list in the scopes: ..., ..., ...

Based on Davi's answer:

  1. Edit "C:\Program Files\Microsoft VS Code\resources\app\extensions\csharp\syntaxes\csharp.tmLanguage.json":
  2. Find:

    {"name":"storage.type.cs","match":"@?[[:alpha:]][[:alnum:]]*"}

  3. Replace with:

    {"name":"storage.type.interface.cs","match":"@?[I][[:upper:]][[:alpha:]][[:alnum:]]"},{"name":"storage.type.cs","match":"@?[_[:alpha:]][_[:alnum:]]"}

  4. Add to settings.json:

    "editor.tokenColorCustomizations": {
        "[Default Dark+]": { // remove scope to apply to all themes
            "textMateRules": [
                {
                    "scope": "entity.name.type.interface.cs",
                    "settings": {
                        "foreground": "#b8d7a3"
                    }
                },
                {
                    "scope": "storage.type.interface.cs",
                    "settings": {
                        "foreground": "#b8d7a3"
                    }
                }
            ]
        }
    },

VSCode 1.63.2:

Ctrl + Shift + P > Open Settings (JSON)

Paste this:

  "editor.tokenColorCustomizations": {
            "[Default Dark+]": { 
                "textMateRules": [
                    {
                        "scope": "entity.name.type.interface",
                        "settings": {
                            "foreground": "#a4ddaf"
                        }
                    }
                ]
            }
        },

I believe it can be partially done by editing "Program Files\Microsoft VS Code\resources\app\extensions\csharp\syntaxes" by adding support for "storage.type.interface.cs" with a regular expression that matches the convention.

Something like [I]([A-Z][a-z][A-Za-z]*)

You could also exclude possible mismatches like IISManager, IPhoneDevice

https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide https://www.apeth.com/nonblog/stories/textmatebundle.html

good luck with that and please let me know if you got it done

Related