Repository navigation
Description
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
angelozerr commentedon Mar 7, 2025
I love this idea. Have you started something?
angelozerr commentedon Mar 7, 2025
@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 ?
SCWells72 commentedon Mar 12, 2025
@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
semanticTokenTypesare exactly what we have hard-coded now inLSPSemanticTokenTypeswith the information required to make them work properly.The
virtualmodifier insemanticTokenModifiersis not one we have now but it is a modifier in the Salesforce Apex programming language that means effectively the opposite offinalin 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 knownabstractsemantic 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
textAttributesKeythat 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: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.
d-biehl commentedon Oct 22, 2026
Another use case: RobotCode's language server uses custom token types such as
keywordCallandkeywordNamefor 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 asfunction. WithgetCustomSemanticTokenTypesas proposed here, I could drop that workaround.AI/tooling disclosure: prepared with assistance from Claude Code; I reviewed it manually.
lagergren commentedon Nov 28, 2026
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:
contributes.semanticTokenTypes)builtinType(i32),const(const LIMIT),formatSpecifier({}inprintln!),attributeBracket,builtinAttribute,generic(in#[deprecated(note = …)])builtinType→type,const→variable,typeAlias→type,procMacro→macro,boolean→keyword, among others.attributeandformatSpecifierhave none; it colors them throughsemanticTokenScopesonly.record(a record andvarof its type),recordComponentrecord→class,recordComponent→parameter,annotation→type,annotationMember→methodbuiltin(@import)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
annotationis 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'skeywordCallabove), still can't be configured.For the VS Code syntax suggested above, a custom type with a
superTypeis what these extensions already declare, e.g.{"id": "builtinType", "superType": "type"}, so a configuration could take the same shape.docs: semantic tokens are no longer experimental, with what is suppor…
docs: semantic tokens are no longer experimental, with what is suppor…