5

In C/C++, in Eclipse editor you can set colors at a more language level: For eg function argument, local variable can have different colors. Static and global variable colors can be changed. Macros and Macro references can be configured.

How to do this in Visual Studio Code? I am not sure if the text mate tmLanguage can achieve this. I could not find any documentation for this.

sadashiv30
  • 601
  • 2
  • 7
  • 15
  • I doubt vs code is evolved this much :) – Dreamweaver Jun 22 '18 at 19:18
  • 2
    TextMate grammars are for syntax coloring only. For semantic coloring, a parser is required that can differ between items with the same name but in different scopes/namespaces. – howlger Jun 22 '18 at 19:40
  • This question is a bit vague, and the answers appear to have taken the interpretation of semantic coloring = coloring a variable differently based on whether it is a function, class, regular variable, etc. I've asked a question about semantic coloring in VS Code where different variables are colored differently: https://stackoverflow.com/questions/61473510/color-different-variables-differently-in-vs-code – mic Nov 18 '20 at 23:58
  • I am genuinely stunned how complicated something as simple as customizing syntax coloring is in vs code. In Eclipse and most other IDEs/text editors, you have a simple GUI where you can simply select the color you want for any language token, done. In vs code I basically have to not only learn an entirely new language (textmate grammars), but also understand how tokenColorCustomizations and semanticTokenColorCustomizations work as well as their interaction with each other and any pre-installed theme. Been trying to get it to work for hours. How tf is vs code the most popular editor?! Crazy! – AdmiralAdama Jun 30 '22 at 18:49

3 Answers3

6

[see further below for v1.45 changes]


You are not limited to a theme's supplied semantic token highlighting. You can choose the color and fontstyle yourself for supported language-semantic tokens by doing this:

It's also possible to define semantic theming rules in the user settings:

"editor.tokenColorCustomizationsExperimental": {
    "property.readonly": {
        "foreground": "#35166d"
    },
    "*.defaultLibrary": {
        "fontStyle": "underline"
    }
}

from https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview#as-a-theme-author-do-i-need-to-change-my-theme-to-make-it-work-with-semantic-highlighting

And see especially https://github.com/microsoft/vscode/wiki/Semantic-Highlighting-Overview#how-can-i-find-out-what-semantic-tokens-are-available-and-how-they-are-themed for how to discover the semantic tokens. For example:

enter image description here

If there is no semantic token type listed, that language service does not yet support them. But undoubtedly, when they do you will want to modify some of their color and fontstyle choices.


vscode 1.45 will rename editor.tokenColorCustomizationsExperimental to editor.semanticTokenColorCustomizations

See https://github.com/microsoft/vscode/issues/96267

This syntax works for me in the Insiders' Build April, 2020:

"editor.semanticTokenColorCustomizations": {
    "enabled": true,
    "rules": {
        "enumMember": "#ff0000"
    },
    "[Default Dark+]": {
        "enabled": true,
        "rules": {
            "variable.readonly": "#ff00ff"
        },
    }
},

From https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_45.md#semantic-token-styling-in-the-user-settings:

Semantic coloring is available for TypeScript and JavaScript, with support for Java, C++ in the works. It is enabled by default for built-in themes and being adopted by theme extensions.

The editor.semanticTokenColorCustomizations allows users to overrule the theme settings and to customize the theming.

Example that adds semantic styling to all themes: ```

"editor.semanticTokenColorCustomizations": {
    "enabled": true,
    "rules": {
        "property.readonly": {
          "foreground": "#35166d"
        },
        "*.defaultLibrary": {
            "bold": true
        }
    }
}


The setting above colors all constants with #35166d and all occurrences of symbols from a default library (e.g. Math, setTimeout) bold.


Example that adds semantic styling to the Dark+ themes: ```

"editor.semanticTokenColorCustomizations": {
    "[Default Dark+]": {
        "enabled": true,
        "rules": {
            "parameter": { "underline": true },
            "*.declaration": { "bold": true }
        }
    }
}

The setting above underlines all parameters in the Dark + theme and
makes all symbol declarations bold.

Theming for semantic tokens is explained in more details in the
[Semantic Highlighting
Guide](/api/language-extensions/semantic-highlight-guide#theming).

Oddly, in my testing even with the enabled: true in the above "editor.semanticTokenColorCustomizations" you still need this setting enabled:

    "editor.semanticHighlighting.enabled": true

but that is the default.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • huh, I can't seem to get this work on either insiders or the default version. I'm looking at .ts typescript files, but there is no "semantic token type" listed after running the inspect tokens command. any tips? – chrismarx May 15 '20 at 02:28
  • Do you have that setting enabled and what version of typescript are you using? I believe it must be 3.8+. – Mark May 22 '20 at 04:10
  • Hmm, the workspace has 3.8.3, but my projects depend on a lower version, maybe that's it – chrismarx May 22 '20 at 13:25
3

You can read more about scopes and syntax highlighting colors here.

This is documented/requested in Microsoft/vscode issue 27894 last year, implemented by PR 29393.
See "User definable syntax highlighting colors" in VSCode 1.15, modifying a color theme.


Note that VSCode 1.42 does include Semantic highlighting for TypeScript & JavaScript

Semantic highlighting support for TypeScript and JavaScript is in development and not yet enabled by default.
But you can try it out by adding the following setting:

"editor.semanticHighlighting.enabled": true

When enabled, you will see that some identifiers get a new color and style and are now highlighted according to their resolved type.
The syntax (TextMate) highlighter classifies many tokens as variables. These now turn into namespaces, classes, parameters and so on.

You can see this best in the imports section where now each imported symbol is colored with the symbol's type:

TypeScript semantic highlighting -- https://github.com/microsoft/vscode-docs/raw/vnext/release-notes/images/1_42/ts-semantic-highlighting.png

You can use the Developer: Inspect Editor Tokens and Scopes command to inspect the semantic and syntax tokens that are computed for each location.

Couple that editor.semanticHighlighting.enabled with the revised 1.45 April 2020 editor.semanticTokenColorCustomizations (see Mark's answer), and you can define the syntax highlighting you want.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

At the time of writing, VSCode insider version supports a semantic highlighting API.

For a very simple example see semantic tokens sample

pgfearo
  • 2,087
  • 1
  • 22
  • 27