Skip to content

Semantic tokens: what it takes to drop the "experimental" label #2334

Done

Description

@lagergren

What "experimental" refers to today

Two places in the docs say it:

Both lines come from the first implementation, 97980bd by azerr (#392, merged 2024-07-01, fixes #238, first released in 0.3.0). In #392 and in #238 the support was described as experimental, needing "some improvements", with an invitation to send PRs that improve it or change its API. No issue says what has to be true to drop the label, and the wording hasn't changed since, although many fixes followed (#394, #401, #477, #828, #1437, #1440, #1511, #2047, ...).

LSPSemanticTokensFeature is @ApiStatus.Experimental, but so is every feature class of LSPClientFeatures, so that annotation is not specific to semantic tokens.

This issue lists what LSP4IJ does today, what is missing or fragile, and a plan to get to a state where the label can go. Code references are to main at 0fd429a.

Where it stands, against the LSP 3.17 spec

Feature Status Code
textDocument/semanticTokens/full Supported LSPSemanticTokensSupport#L90-L92
full/delta, resultId Not supported. The client announces requests: { full: true, range: false }, no delta; resultId is ignored (#2239) ClientCapabilitiesFactory#L312
range, visible-range requests Not supported (#2240). A server with range but no full still gets full requests, because any semanticTokensProvider counts as support SemanticTokensCapabilityRegistry#L31-L32
workspace/semanticTokens/refresh Partly. Supported, but the re-highlighting waits at least 2 s, and any other restart request of the file within those 2 s postpones it again LanguageClientImpl#L358-L375, LSPFileSupport#L79, #L511-L536
multilineTokenSupport Supported: declared true, a token is start offset + length ClientCapabilitiesFactory#L310, SemanticTokensData#L125-L130
overlappingTokenSupport Not declared, which is right: the PSI path keeps one token per start offset LSPSemanticTokensHighlightVisitor#L289-L297
augmentsSyntaxTokens Not declared (#1101) ClientCapabilitiesFactory#L270-L315
serverCancelSupport Declared true, not honoured: a ServerCancelled answer becomes an empty result that is kept until the next edit, and the request is not sent again ClientCapabilitiesFactory#L311, CancellationSupport#L146-L160, CancellationUtil#L46
formats Supported (relative, the only format) ClientCapabilitiesFactory#L313
Token types in the client capabilities All 23 standard 3.17 types, plus label ClientCapabilitiesFactory#L272-L297
Token modifiers in the client capabilities Partly: 4 of 10 (declaration, definition, readonly, static). The rest is commented out, including defaultLibrary, which the default colors do use ClientCapabilitiesFactory#L298-L309
Server legend, static and dynamic registration Supported, per server. With dynamic registration, the legend of the first registration is used LSPSemanticTokensSupport#L105-L119, SemanticTokensCapabilityRegistry#L67-L73
Colors of the standard types Supported, through DefaultSemanticTokensColorsProvider. Of the modifiers, only declaration/definition, static, readonly and defaultLibrary (for function only) change the color; deprecated, abstract, async, modification and documentation don't DefaultSemanticTokensColorsProvider#L28-L205
Custom types from a server's legend Partly: three are hard-coded (label, TypeScript's member, SourceKit's identifier); every other custom type is not colored. A plugin can supply its own provider through the semanticTokensColorsProvider extension point; a user-defined server has no way to (#871) LSPSemanticTokenTypes#L29-L66
Several language servers on one file Partly: the request goes to every server, and the answer of one is used, the one that happened to finish starting first LSPSemanticTokensSupport#L73-L80, LanguageServiceAccessor#L703-L735
Partial results (partialResultToken) Not supported (#2244)
Cancellation on typing Supported: after an edit, the next highlighting pass cancels the earlier request ($/cancelRequest) and sends a new one. There is no debounce of its own; the daemon's auto-reparse delay acts as one AbstractLSPFeatureSupport#L60-L96, AbstractLSPDocumentFeatureSupport#L80-L83
TextMate and plain-text files Supported: the highlights are added on top of the lexer's LSPSemanticTokensHighlightVisitor#L114-L119
A language plugin's own PSI Supported (#1040): highlights per leaf element, on top of the plugin's highlighting. Not tested LSPSemanticTokensHighlightVisitor#L120-L158, LSPSemanticTokensFeature#L112-L151
Positions (UTF-16) Supported: only UTF-16 is offered, which matches IntelliJ's document offsets. Not tested with characters outside the BMP ClientCapabilitiesFactory#L41
Themes and color schemes Supported: each LSP_* key inherits a DefaultLanguageHighlighterColors key, so every scheme gets colors; the four scheme files only set LSP_TYPE_PARAMETER. One global "Language Server" color page SemanticTokensHighlightingColors, SemanticTokensDark.xml
Semantic Tokens Inspector Supported features/semanticTokens/inspector

Gaps and risks, in priority order

"Verified" means read in the code; "inferred" means it follows from the code and the platform's behaviour but hasn't been reproduced.

1. A failed or server-cancelled request wipes the semantic colors and is not retried

  • Verified: CancellationSupport turns a ResponseErrorException into a null result: RequestCancelled and ServerCancelled (L148, CancellationUtil#L46), ContentModified (L152), and any other error after reporting it (L157). LSPSemanticTokensSupport maps null to no data (L93-L97), and the visitor then adds no highlights. The future completed normally, so AbstractLSPFeatureSupport.isValidLSPFuture keeps it until the file is modified: no new request is sent until the next edit or refresh.
  • Inferred: a highlighting pass that adds nothing removes the semantic highlights of the earlier pass, so the file loses its semantic colors until the user types. Syntax Highlighting breaks since Jetbrains version 2024.2.1 on error #780 (a server that answers an error while the document doesn't parse) looks like this.
  • Against the spec: with serverCancelSupport: true, "If a server does the client needs to retrigger the request."
  • Fix: keep the last good result per server and document, and keep showing the current highlights on an error or cancellation instead of replacing them with nothing. Don't cache an empty answer that came from an error; for ServerCancelled and ContentModified, ask again after a short delay (bounded retries).
  • Test: a mock server that answers tokens, then an error after an edit: the earlier highlights stay. A mock server that answers ServerCancelled once, then tokens: the tokens show without any edit.

2. Several language servers on one file: one answer is used, and which one is not deterministic

  • Verified: the request is sent to every server with the capability (L73-L77), and only semanticTokensPerServerFutures.get(0) is used (L80, the merge is commented out). The list of servers is filled in the order in which they finish starting and connecting the file (LanguageServiceAccessor#L703, L730), so the server whose colors show can change from one IDE start to the next. The other servers' work is thrown away.
  • Fix: decode each server's tokens with its own legend and colors provider and add all of them (each as its own set of highlights), or let the definitions say which server wins; at the least, pick one deterministically (mapping order) and send the request to that one only. The view provider and the inspector should say which server a token came from.
  • Test: two mock servers with different legends and tokens on one file, with one of them slower to initialize: the result is the same whichever starts first.

3. The highlighting pass waits for the language server, and a cancelled wait empties the colors

  • Verified: analyze waits for the answer inside the highlighting read action (L191-L235). The future includes starting the server and initialize (getLanguageServers waits for getInitializedServer()), so on opening a file the pass can wait for as long as the server takes to start. This is the wait of the freezes in Semantic tokens, document links and LSP intentions hold highlighting read actions while they wait for the language server #2017. Since 2024.2 IntelliJ runs the visitors of a pass concurrently, so other visitors are not blocked, but the pass, and what waits for it, is.
  • Verified: when the future is cancelled while a pass waits, for example by workspace/semanticTokens/refresh (SemanticTokensEditorFeature#L40-L44), the visitor returns no tokens (L212-L216). Inferred: that pass then removes the semantic highlights, and they come back with the pass that the refresh restarts 2 s later.
  • Fix: the approach of Semantic tokens, document links and LSP intentions hold highlighting read actions while they wait for the language server #2017: don't wait in the pass. Use the answer if it is there for the current modification stamp; otherwise keep the current highlights and restart the highlighting of the file when the answer arrives (as refreshWhenLoaded and the document links' pendingFuture do). Whatever the approach, a pass must never end with no tokens only because the answer isn't there yet.
  • Test: a mock server that answers after a delay (no fixed sleeps in the test, wait for the request and the restart): a write action during the wait completes promptly, the earlier highlights stay until the answer, and the answer is applied without an edit. A refresh during a request doesn't make the colors disappear.

4. workspace/semanticTokens/refresh is slow, and can be postponed indefinitely

  • Verified: the refresh clears the cache and calls restartDaemonCodeAnalyzerWithDebounce (EditorFeatureManager#L140-L146), which waits 2000 ms (LSPFileSupport#L79) and cancels the pending restart each time another restart of the file is asked for (L519). Inferred: a server that sends refreshes (of any feature of the file) more often than every 2 s, as some do while indexing, keeps the semantic tokens from being updated.
  • Fix: a shorter delay for visible editors (the spec allows delaying only invisible ones), and a maximum wait so that repeated requests can't postpone the restart forever.
  • Test: a mock server changes its tokens and sends a refresh: the new tokens show within a bound well under 2 s; with refreshes sent every 500 ms, the tokens still update.

5. Range-only servers get no semantic tokens; no range and no delta

6. Colors: custom types, modifiers and a few default mappings

7. Performance on large files

  • Verified: every highlighting pass decodes the whole token array again, even when the cached answer hasn't changed (SemanticTokensData#L68-L169), looks up colors per token and, for the PSI path, allocates an array as long as the document (LSPSemanticTokensHighlightVisitor#L289).
  • Verified: for TEXT, TextMate and plugin file types without a language, the view provider stores one ConcurrentHashMap entry per character of every token (LSPSemanticTokensFileViewProviderHelper#L161-L177), refilled by each pass. The map is not cleared on a refresh without an edit (it's a cached value of the file), so tokens that a refresh removed stay in it (inferred).
  • Verified: the data arrives as List<Integer> (boxed), 5 per token.
  • Fix: decode once per answer into sorted int[] arrays (start, end, key index) kept with the result; reuse them across passes and for the view provider (binary search instead of a per-character map); no document-length array.
  • Test: a benchmark-style test with a file of 50,000 tokens: decode time, allocations, and that a second pass without changes doesn't decode again.

8. Correctness details

  • Verified: tokens are checked against the document length (L144) but not against their line: a token on the same line as the previous one is placed by adding the delta (L113-L114), with no clamp to the end of the line. Mostly harmless with the stamp checks, but a server bug spills colors onto the next line.
  • Verified: the PSI path keeps one token per start offset (L294-L296), so overlapping tokens from a server that sends them anyway are dropped silently.
  • Not checked: injected fragments and files with several PSI roots (template languages), where analyze may run once per root.
  • Test: unit tests of the decoder for tokens past the line end, past the document end, of length 0, with a type index outside the legend, and with modifier bits beyond the legend; and one with characters outside the BMP (emoji) before tokens on the same line.

9. Test coverage

What exists: one decoding test (LuaSemanticTokensTest, two tokens), three inspector tests, the file view provider tests (CSS, Go, JavaScript, TypeScript, plugin file type), the documentation provider, and a refresh-clears-the-cache test (EditorRefreshBatchTest). The mock server only answers semanticTokens/full, with a fixed answer (MockTextDocumentService#L691-L693).

Not covered: the fixes of #1437, #1440 and #1511, which came without tests; edits during a request; errors and cancellation; refresh end to end; several servers; dynamic registration with a legend; the PSI path (#1040); the color mapping; Unicode; large files. Each gap above lists its test. The tests should wait for events, not fixed times, as the rest of the suite does since #2252.

10. Inspector and docs

  • Verified: with the inspector open, each pass formats the tokens on a pooled thread through WriteCommandAction.runWriteCommandAction (SemanticTokensInspectorManager#L77), that is a write action on the UI thread, only to insert tags into a throwaway document. A StringBuilder does the same without a write action (which also cancels running highlighting passes). The inspector shows no server name, legend or raw data, and only the tokens of the one server used.
  • Docs: the limits above (one server, full only, refresh delay, uncolored custom types, how to disable) aren't documented. The Javadoc of LSPSemanticTokensFeature.shouldVisitPsiElement says the opposite of the code (L118-L120 vs L127-L129); LSPSemanticTokensHighlightVisitor#L265 still says "see issue #XXX" (it is IllegalArgumentException on inverted semantic token offsets caused by concurrent document edits #1440); LSPSemanticTokensSupport has comments copied from folding ranges (L67, L79).

Related open issues

Proposed plan

  1. Agree on the bar for dropping "experimental": gaps 1 to 4 fixed with tests, range-only servers working (first part of gap 5), the default colors reviewed (gap 6, without Add support for custom semantic token types and modifiers #871), and a manual check with a set of real servers (for example rust-analyzer, clangd, gopls, typescript-language-server, lua-language-server, jdtls, zls) in TextMate files and in one plugin with its own PSI.
  2. Correctness (gaps 1 and 2): keep the last good result, retry ServerCancelled/ContentModified, deterministic multi-server handling. Small, testable changes in LSPSemanticTokensSupport and the visitor.
  3. Threading and refresh (gaps 3 and 4): decide on Semantic tokens, document links and LSP intentions hold highlighting read actions while they wait for the language server #2017's non-blocking approach with a reproduction; never end a pass empty because the answer is pending; a refresh that applies promptly.
  4. Tests (gap 9) alongside 2 and 3: a mock server that can delay, fail, cancel and change its tokens; decoder unit tests (gap 8).
  5. Colors (gap 6): default mapping review, deprecated, client capabilities, Set the augmentsSyntaxTokens client capability if appropriate #1101, a per-server switch; Add support for custom semantic token types and modifiers #871 can follow.
  6. Then drop the label: update the User Guide and LSPSupport.md with what is supported and its limits, and remove "experimental".
  7. After that, as improvements: decode-once performance work (gap 7), LSP: support the textDocument/semanticTokens/range request #2240 visible range, LSP: support the textDocument/semanticTokens/full/delta request #2239 delta, inspector improvements (gap 10).

Activity

  1. added this to the 0.30.3 milestone on Nov 28, 2026
  2. lagergren commented on Dec 1, 2026

    @lagergren
    CollaboratorAuthor

    The live check that the plan asked for before dropping "experimental", with real servers and on both paths:

    Server File Path Tokens With a default color Highlighted Highlights that are no token
    clangd C TextMate 32 32 32 0
    rust-analyzer Rust, with a \u{1F600} TextMate 112 90 90 0
    typescript-language-server TypeScript, with an emoji TextMate 36 36 36 0
    jdtls Java, with a surrogate pair IntelliJ's own Java PSI 63 63 63 0
    • No token outside the text: none of the four put a token outside its lines.
    • rust-analyzer's other 22 tokens: they have types with no default color, so they show no color by design.
    • Not checked live: gopls, lua-language-server and zls, which aren't installed here.

    The "experimental" wording is already gone from the docs: LSPSupport.md lists textDocument/semanticTokens as ✅, and the user guide no longer calls it experimental.

    The gaps of the plan:

    So this plan is done. I'm closing it; #2017 and #2349 stay open for what's left.

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

    enhancementsemantic tokens

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions