Skip to content

LSP: support the textDocument/semanticTokens/full/delta request #2239

Done

Description

@lagergren

What

textDocument/semanticTokens/full/delta (3.16) sends the resultId of the last full or delta answer as previousResultId. The server answers with only the edits to the token array (SemanticTokensDelta), or with full tokens.

  • The client announces it with textDocument.semanticTokens.requests.full.delta: true.
  • The server announces it with semanticTokensProvider.full.delta: true.

Why

  • rust-analyzer: full: { delta: true } (crates/rust-analyzer/src/lsp/capabilities.rs).
  • clangd: "full": {"delta": true} (ClangdLSPServer.cpp), with its handler onSemanticTokensDelta.
  • No delta: gopls (its SemanticTokensFullDelta is in unimplemented.go), jdtls (SemanticTokensServerFull(false)), typescript-language-server.

What users miss: on large files, every keystroke re-sends and re-parses all tokens of the file. That is 5 integers per token, so tens of thousands of integers for a big C++ or Rust file, on each highlighting pass. With delta it is a few edits.

Today

How it could be done

  1. Keep the last result per server and document: the resultId and the int[] data. They belong to the opened document of that server, and are dropped on didClose, on a server restart and on workspace/semanticTokens/refresh.
  2. Send delta when the server announces full.delta and a previous result with a resultId exists. Otherwise send full.
  3. Apply the SemanticTokensEdits (start, deleteCount, data) to a copy of the previous array, in order. Build SemanticTokensData from it as today. If the answer is full SemanticTokens, use it as it is.
  4. Error handling: if an edit doesn't fit the previous array (a server bug), drop the cache and send full once.
  5. Announce full: { delta: true } (SemanticTokensClientCapabilitiesRequestsFull, in LSP4J 1.0).

No IntelliJ API changes. LSPSemanticTokensHighlightVisitor keeps working on the full array.

Test

Unit tests of the edit application:

  • insert, delete, replace;
  • several edits;
  • an edit past the end, which falls back to full.

Plus a fixture test with a mock server that answers full and then delta.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions