Skip to content

Add support for custom semantic token types and modifiers #871

Open

Description

@SCWells72

We've discussed this a few times, but some language servers return semantic token types that aren't present in SemanticTokenTypes and/or semantic token modifiers that aren't present in SemanticTokenModifiers. Sometimes those custom token types/modifiers are actually quite important, e.g., member from the TypeScript language server, and if not understood explicitly by LSP4IJ, important functionality can be missing ranging from semantic syntax highlighting to the way that a given token should be translated into an element the PSI hierarchy.

It should be possible for language server definitions to augment the known semantic token types and modifiers via both code and configuration. One way to do this would be to add the following methods to LSPSemanticTokensFeature:

/**
 * Returns custom semantic token types for the file.
 *
 * @param file the file
 * @return the file's custom semantic token types
 */
@NotNull
public Set<LSPSemanticTokenType> getCustomSemanticTokenTypes(@NotNull PsiFile file) {
    return Collections.emptySet();
}

/**
 * Returns custom semantic token modifiers for the file.
 *
 * @param file the file
 * @return the file's custom semantic token modifiers
 */
@NotNull
public Set<LSPSemanticTokenModifier> getCustomSemanticTokenModifiers(@NotNull PsiFile file) {
    return Collections.emptySet();
}

where LSPSemanticTokenType is defined as:

/**
 * A custom semantic token type.
 */
public class LSPSemanticTokenType {
    /** The semantic token type name */
    public String name;
    /** Whether or not the semantic token should be interpreted as being an identifier */
    public boolean identifier = false;
    /** Whether or not the semantic token should be interpreted as being a type */
    public boolean type = false;
    /** Whether or not the semantic token should be interpreted as being a keyword */
    public boolean keyword = false;
    /** The name of the existing semantic token type from which this one should inherit its behavior */
    public String inheritFrom = null;
    /** The text attributes key that should be used for syntax highlighting of this semantic token */
    public String textAttributesKey = null;
}

and LSPSemanticTokenModifier is defined as:

/**
 * A custom semantic token modifier.
 */
public class LSPSemanticTokenModifier {
    /** The semantic token modifier name */
    public String name = null;
    /** Whether or not semantic tokens with this modifier should be interpreted as being a declaration */
    public boolean declaration = false;
    /** The name of the existing semantic token modifier from which this one should inherit its behavior */
    public String inheritFrom = null;
    /** The text attributes key that should be used for syntax highlighting of semantic tokens with this modifier */
    public String textAttributesKey = null;
}

Custom language server definitions would just override these methods to return any known semantic token types/modifiers returned by the associated language server. User-defined language server definitions would provide the same information via client configuration, e.g.:

{
  "semanticTokens": {
    "customTokens": {
      "label": {
        "identifier": true,
        "textAttributesKey": "LSP_LABEL"
      }
      "member": {
        "identifier": true,
        "textAttributesKey": "LSP_METHOD"
      }
    },
    "customModifiers": {
      "virtual": {
        "inheritFrom": "abstract"
      }
    }
  }
}

DefaultSemanticTokensColorProvider would then be updated to use this information if available, as would LSPSemanticTokensFileViewProviderHelper that is being added in #853.

Activity

  1. angelozerr commented on Mar 7, 2025

    @angelozerr
    Contributor

    I love this idea. Have you started something?

  2. SCWells72 commented on Mar 12, 2025

    @SCWells72
    ContributorAuthor

    @SCWells72 do you think we could try to use the same syntax than https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide#custom-token-types-and-modifiers ?

    @angelozerr I think we can do something that's closer to the VS Code syntax, but it won't be exactly the same because of differences in the two tools. Here's a modified version of what I'd proposed taking into account the VS Code syntax so it's more similar:

    {
      "semanticTokens": {
        "contributes": {
          "semanticTokenTypes": [
            {
              "id": "label",
              "description": "A label",
              "identifier": true,
              "textAttributesKey": "LSP_LABEL"
            },
            {
              "id": "member",
              "description": "A type member",
              "identifier": true
              "superType": "method"
            },
            {
              "id": "identifier",
              "description": "An identifier declaration",
              "identifier": true,
              "declaration": true
            }
          ],
          "semanticTokenModifiers": [
            {
              "id": "virtual",
              "superModifier": "abstract"
            }
          ]
        }
      }
    }

    The three semanticTokenTypes are exactly what we have hard-coded now in LSPSemanticTokenTypes with the information required to make them work properly.

    The virtual modifier in semanticTokenModifiers is not one we have now but it is a modifier in the Salesforce Apex programming language that means effectively the opposite of final in Java, i.e., that the type or method can be subclassed/overridden. As a result, the example above says that it should be treated exactly the same as the known abstract semantic token modifier in terms of syntax highlighting.

    The example above also shows that instead of having hard-coded colors or theme names, you'd provide a textAttributesKey that should be used for styling of the semantic token type, and that will automatically be adapted for the user-selected theme based on the Language Server color scheme:

    Image

    We can obviously iterate on this more when I'm back, but hopefully the above shows that while we can get closer to how VS Code does this, there are going to be some differences simply based on the additional information that we need, e.g., whether it's an identifier/declaration/type/keyword, and the way that JetBrains' IDEs handle syntax highlighting in a theme-independent manner.

  3. d-biehl commented on Oct 22, 2026

    @d-biehl
    Contributor

    Another use case: RobotCode's language server uses custom token types such as keywordCall and keywordName for Robot Framework keywords. LSP4IJ doesn't treat them as identifiers, so Ctrl+hover doesn't underline keywords. As a workaround, the RobotCode plugin passes them to LSP4IJ as function. With getCustomSemanticTokenTypes as proposed here, I could drop that workaround.


    AI/tooling disclosure: prepared with assistance from Claude Code; I reviewed it manually.

  4. lagergren commented on Nov 28, 2026

    @lagergren
    Collaborator

    Some data from real language servers. It comes from a check of semantic tokens with seven of them for #2338, on 2026-11-28, with small sample files. These are the custom token types they sent that LSP4IJ didn't color:

    Language server Custom types sent, with no color What its VS Code extension declares (contributes.semanticTokenTypes)
    rust-analyzer 2026-11-22 builtinType (i32), const (const LIMIT), formatSpecifier ({} in println!), attributeBracket, builtinAttribute, generic (in #[deprecated(note = …)]) Super types for most of its custom types: builtinType → type, const → variable, typeAlias → type, procMacro → macro, boolean → keyword, among others. attribute and formatSpecifier have none; it colors them through semanticTokenScopes only.
    jdtls 1.64.0-SNAPSHOT record (a record and var of its type), recordComponent record → class, recordComponent → parameter, annotation → type, annotationMember → method
    zls 0.17.0 builtin (@import) vscode-zig declares no custom types

    clangd, gopls, typescript-language-server and lua-language-server sent only types that LSP4IJ colors.

    #2338 adds the super types that rust-analyzer's and vscode-java's extensions declare to LSP4IJ's built-in custom types, as VS Code's themes fall back to them. One exception: jdtls's annotation is colored as a decorator, as vscode-java's scopes and IntelliJ color annotations. That covers those two servers only, not this issue. A user-defined server, or the custom types of another server (such as RobotCode's keywordCall above), still can't be configured.

    For the VS Code syntax suggested above, a custom type with a superType is what these extensions already declare, e.g. {"id": "builtinType", "superType": "type"}, so a configuration could take the same shape.

  5. added this to the Backlog milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions