Skip to content

[themes] Explore theming support for semantic colors #77133

Closed

Description

  • define a set of Token Classifications that can be used for semantic highlighting
  • evaluate actual color/style values based on the current text mate theme
  • allow themes to define such colors and styles

Activity

  1. added
    plan-item
    on Jul 10, 2019
  2. aeschli commented on Jul 10, 2019

    @aeschli
    ContributorAuthor

    related: #32813

  3. kanlukasz commented on Aug 1, 2019

    @kanlukasz
    * allow themes to define such colors and styles
    

    Do you mean that there will be option to set color variables and use them with themes making?
    I mean something like in Sublime Text

    Here is example what i talking about:
    https://stackoverflow.com/questions/56473901/is-it-possible-to-use-color-variables-in-the-visual-studio-code-when-creating-th

    Or maybe it's possible right now but i don't know how?

  4. aeschli commented on Sep 23, 2019

    @aeschli
    ContributorAuthor

    This is a proposal on how to represent the classification that a semantic highlighter computes (TokenTypes and TokenModifiers) and how themes and users can associate the style (TokenStyle). I'm still editing this proposal, but feedback and questions are welcome!

    Semantic highlight range provider

    A semantic highlight range provider takes a document (and potentially other arguments such as the current viewPorts) and returns a list of classified source ranges. The proposal is that each range can be classified with a TokenType and any number of TokenModifiers.

    interface SemanticHighlightRangeProvider {
       provideHighlightRanges(doc,...): [ Range, TokenType, TokenModifier[] ][];
    }

    Note: This proposal focuses just on the classification types. The full SemanticHighlightRangeProvider API with a proper return type and optimizations such as compact token representation or ranges grouped by line will be part of a different proposal.

    TokenTypes and TokenModifiers

    A TokenType describes the symbol (class, field, variable...) at the location. The TokenModifiers provide additional information on the symbols (static, declaration, write access...).
    TokenType and TokenModifiers are identified by a string id.

    The purpose of splitting the classification into type and modifier is to simplify the theme definition (described later) and to make it easier for extensions to contribute additional classifications.

    Predefined TokenTypes and TokenModifiers

    VSCode will define a set of predefined (standardized) TokenTypes and TokenTypeModifiers, but extensions can add more if necessary:

    Predefined TokeTypes:

    • comments, strings, numbers, keywords, regexp, operators

    • namespaces

    • types, structs(Type), classes(Type), interfaces(Type), enums(Type), parameterTypes(Type)

    • functions, macros(Function)

    • variables, constants(Variable), parameters(Variable), properties(Variable),

    • labels

    Predefined TokeTypes Modifiers:
    declaration : For all symbol declarations
    documentation: For all occurrences of symbols in documentation
    member: for member types, member variables (fields) and member functions (methods)
    static for static (member types), member variables and member functions
    modification for references where a variable is modified
    async for declarations or references for functions that are async
    deprecated: for annotating deprecated symbols
    abstract: for annotating abstract functions.

    TokenTypes and TokenModifiers Defaults

    Each TokenType and TokenModifier come with a default style which is used when the actual theme does not define a style for it.

    The default style can be specified as either

    • a set of textMateScopesToProbe that are ran against the text mate theme rules the current theme defines to find a matching style.
    • a predefined (static) style
    • a reference to a different token-type to inherit the style of the other token type
    • a function of one or more token-types
    interface TokenTypeRegistry {
        registerTokenType(id, default: { textMateScopesToProbe: string[], value: TokenStyleValue })
    }
    type TokenStyleValue: = TokenStyle | TokenType | FunctionOf(TokenStyleValue)

    Theme Definition

    The active color theme (or user setting) will define colors and styles (TokenStyle) for TokenTypes and TokenModifiers or combination thereof.

    interface TokenStyle {
       foreground?: Color | ColorFunction
       style?: bold | italic | underline
    }
    {
       "tokenStyles": {
           "(typenType|*)[.tokenTypeModifier]*": TokenStyle,
           //...
        }
    • The first segment of a rule is the tokenType or '*', followed by modifiers. The order of the modifiers is not relevant.
    • A rule matches if the token-type (e.g. const) matches or if the rule starts with * and when all modifiers of the rule are matched.
    • The rule specificity is defined by the number of segments matching. That means
      const < *.declaration < *.declaration.exported < const.declaration.exported.
      (still to be decided: order when multiple rules have the same number of segments matching)
    • The final style can be the result of multiple rules. For each style property (foreground, underline, bold, italics) the style of the most specific rule is applied. If a rule does not define a style, the rule is ignored for the evaluation of that style.
    • The foreground color can be a combination of colors if ColorFunctions ('darker()') are used.
    {
            "tokenStyles": {
                // all consts are blue and underlined
                "const": { "foreground": "#0000ff", "underline": true },
                // all declarations (types, functions, vars,....) are bold
                "*.declaration": { "bold": true },
                // all declaration that are also exported (public) are not bold, but underlined
                "*.declaration.exported": { "bold": false, "underline": true },
                // consts declarations that are exported are bold, underlined (inherited) and blue (inherited)
                "const.exported.declaration": { "bold": true },
    
                // all async decls and refs are rendered with a slightly darker color
                "*.asyncReturn": { "foreground": "darker()" }
            }
    
    	"colors": { ... /* same as before */ },
    	"tokenColors": [ 
                    // still there for syntax highlighting and for evaluating 
                    // default styles for TokenTypes and Modifier
    		{
    			"scope": [ "storage.type.annotation" ],
    			"settings": { "fontStyle": "bold", "foreground": "#AA3731" }
    		},
    		{
    			"scope": [ "entity.name.type", "entity.name.namespace" ],
    			"settings": { "fontStyle": "bold", "foreground": "#7A3E9D" }
    		}
    		// ...
    	]
    }

    TokenTypes and TokenModifiers Contribution

    {
       "contributes": {
           "tokenTypes": [ {
               "id": "annotationType",
               "default": {
                   "textMateScopes": ["storage.type.annotation"],
                   "value": "type" // inherits settings from 'type'
               }
           }],
           "tokenTypeModifier": [ {
               "id": "autoBoxed",
               "default": {
                   "value": { "bold": "true" }  // default to be used for '*.autoboxed'
               }
           }]
    }
  5. NTaylorMullen commented on Sep 25, 2019

    @NTaylorMullen

    Are there any thoughts on how this could work for semantically embedded languages? Aka, HTML with JS/CSS or Razor with HTML/JS/CSS.

    For instance in Razor if you do:

    <form asp-antiforgery="true">
    ...
    </form>

    The true can be C#.

    It'd be great if there was a way to say "this range is C#" and then adding additional semantic meaning on top of it would be up to Razor or HTML language servers or their extensions

  6. AmadeusW commented on Sep 25, 2019

    @AmadeusW
    Member

    This might be related to embedded laguages; will we support multiple tokens per location? How about multiple token modifiers per location?
    For example, static function or keyword in embedded XML (doc comment) are both used in C#.

  7. aeschli commented on Sep 26, 2019

    @aeschli
    ContributorAuthor

    N. Taylor Mullen (@NTaylorMullen) Amadeusz Wieczorek (@AmadeusW) That needs to be seen when we finalise the SemanticHighlightRangeProvider. Normally with providers we support more than one provider.

  8. NTaylorMullen commented on Sep 26, 2019

    @NTaylorMullen

    N. Taylor Mullen (@NTaylorMullen) Amadeusz Wieczorek (@AmadeusW) That needs to be seen when we finalise the SemanticHighlightRangeProvider. Normally with providers we support more than one provider.

    Definitely, but what about registering a "language" with a range semantically?

  9. aeschli commented on Sep 27, 2019

    @aeschli
    ContributorAuthor

    N. Taylor Mullen (@NTaylorMullen) This needs to be discussed in the issue where we design the SemanticHighlightRangeProvider. Maybe you can file an issue where you describe the idea?

  10. 22 remaining items

  11. mattacosta commented on Feb 7, 2020

    @mattacosta
    Contributor

    sharedprophet Oleg Utkin (@NonLogicalDev)

    This is from a quick and dirty example I was doing for another issue but, have a look at the parameters. Is there something I'm missing? It seems to be possible.

    semantictoken_params

  12. sharedprophet commented on Feb 7, 2020

    @sharedprophet

    Matt Acosta (@mattacosta) that looks good. How did you do it?

  13. mattacosta commented on Feb 17, 2020

    @mattacosta
    Contributor

    Basically I had my extension provide a set of default colors for certain modifiers and then had my server add one of those modifiers to each parameter.

  14. sharedprophet commented on Feb 18, 2020

    @sharedprophet

    Matt Acosta (@mattacosta) but can it work with other language servers? Can your language server act as an intermediary to other language servers so it is lanuguage agnostic?

  15. mattacosta commented on Feb 18, 2020

    @mattacosta
    Contributor

    Not sure if I follow exactly, but if you were able to ask those servers for a list of unique symbols in a file, then yes?

  16. sharedprophet commented on Feb 18, 2020

    @sharedprophet

    I'd like to be able to implement a solution that works for any language without changing the language servers for those languages.

  17. aeschli commented on Feb 18, 2020

    @aeschli
    ContributorAuthor

    I'd like to be able to implement a solution that works for any language without changing the language servers for those languages.

    There's no need to be part of the language server. Any extension can contribute a semantic token provider, for any language. Alexandru Dima (@alexdima) What we still have to work on is to decide what happens when there are multiple providers for a file (which one wins?).

  18. mattacosta commented on Feb 18, 2020

    @mattacosta
    Contributor

    sharedprophet Previous answer still applies. A highly theoretical yes.

    | vscode | -- semanticHighlights --> | semanticolor ext | -- documentSymbols --> | other LS |
                                                               <-- SymbolInfo[] --
                                                             -- documentHighlights -->
                  <-- highlights[] --    apply coloring logic   <-- Location[] --
    

    I also want to emphasize that just because you can doesn't mean you should... that first run would be hella expensive (if it works at all), and I have no idea what kind of optimizations would be possible/required.

  19. matklad commented on Feb 28, 2020

    @matklad

    Martin Aeschlimann (@aeschli) the wiki says

    Themes and user settings can define rules to assign classifications to styles (foreground, italic, underline, bold)

    Is this actually implemented for themes yet?

    I've tried adding

    {
    	"name": "Pale Fire",
    	"type": "dark",
    	"editor.tokenColorCustomizationsExperimental": {
    		"enumMember": "#ff0000"
    	},
    	"tokenColorCustomizationsExperimental": {
    		"enumMember": "#ff0000"
    	},
    }

    to my theme and it doesn't work. Adding this to user settings does work though.

    I know I can style things by mapping semantic topics to textmate scopes and then styling the latter as usual, but that's a very roundabout way to do this.

  20. aeschli commented on Feb 28, 2020

    @aeschli
    ContributorAuthor

    The property in the theme is called tokenStylingRules. It's not yet documented and might be changed.

  21. matklad commented on Feb 28, 2020

    @matklad

    Hm, doesn't seem to work:

    matklad/pale-fire@01bca94

    λ code-insiders --version
    1.43.0-insider
    b0be0672c210f1fa76109ce675365bb785657e84
    x64
    
  22. mattacosta commented on Feb 29, 2020

    @mattacosta
    Contributor

    If a theme has a tokenColors key, it looks like the _loadColorTheme function returns before ever reading tokenStylingRules.

    let tokenColors = contentValue.tokenColors;
    if (tokenColors) {
    if (Array.isArray(tokenColors)) {
    result.textMateRules.push(...tokenColors);
    return null;
    } else if (typeof tokenColors === 'string') {
    return _loadSyntaxTokens(extensionResourceLoaderService, resources.joinPath(resources.dirname(themeLocation), tokenColors), result);
    } else {
    return Promise.reject(new Error(nls.localize({ key: 'error.invalidformat.tokenColors', comment: ['{0} will be replaced by a path. Values in quotes should not be translated.'] }, "Problem parsing color theme file: {0}. Property 'tokenColors' should be either an array specifying colors or a path to a TextMate theme file", themeLocation.toString())));
    }
    }
    let tokenStylingRules = contentValue.tokenStylingRules;
    if (tokenStylingRules && typeof tokenStylingRules === 'object') {
    result.stylingRules = readCustomTokenStyleRules(tokenStylingRules, result.stylingRules);
    }

  23. aeschli commented on Mar 2, 2020

    @aeschli
    ContributorAuthor

    Ok, sorry, color style customization in themes in not ready yet to be used yet. I'll complete that in the March milestone.

  24. locked and limited conversation to collaborators on Mar 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requeston-testplanplan-itemthemes

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions