Add support for code highlighting using semantic tokens #4

Closed
Techatrix wants to merge 1 commit from semantic-tokens into main
Techatrix commented 2026-03-09 17:24:17 +01:00 (Migrated from github.com)

This adds support for highlighting using LSP semantic tokens. This feature must be enabled manually by implementing LSPClientConfig.highlightSemanticTokens which is where to specify how semantic tokens should be translated to codemirror decorations.

Updates are debounced to avoid requesting semantic tokens on every text edit and could also support client side cancellation in theory (more on that in the code comment). The implementation only really supports textDocument/semanticTokens/full but will fall back to requesting textDocument/semanticTokens/range on the entire document if unavailable.

This adds support for highlighting using LSP [semantic tokens](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens). This feature must be enabled manually by implementing `LSPClientConfig.highlightSemanticTokens` which is where to specify how semantic tokens should be translated to codemirror decorations. Updates are debounced to avoid requesting semantic tokens on every text edit and could also support client side cancellation in theory (more on that in the code comment). The implementation only really supports `textDocument/semanticTokens/full` but will fall back to requesting `textDocument/semanticTokens/range` on the entire document if unavailable.
Techatrix (Migrated from github.com) reviewed 2026-03-09 17:28:21 +01:00
@ -0,0 +38,4 @@
startRequest(plugin: LSPPlugin, view: EditorView): void {
if (this.pendingRequest != null) {
// There is a pending request on an older document state that should
// be cancelled here.
Techatrix (Migrated from github.com) commented 2026-03-09 17:28:21 +01:00

This is where there should be a call to plugin.client.cancelRequest but I encountered an issue while trying to do that. cancelRequest identifies requests based on the params using JS object equality which seems unusual. Is there are particular reason why it doesn't use the request id? The only usage of this function is for code completions so perhaps it could be reworked to use the id instead of params.

The cancelRequest implementation is also kind of broken. See #3

This is where there should be a call to `plugin.client.cancelRequest` but I encountered an issue while trying to do that. `cancelRequest` identifies requests based on the params using JS object equality which seems unusual. Is there are particular reason why it doesn't use the request id? The only usage of this function is for code completions so perhaps it could be reworked to use the id instead of params. The `cancelRequest` implementation is also kind of broken. See #3
marijnh commented 2026-04-07 20:49:34 +02:00 (Migrated from github.com)

Hi. I'm not interested in including support for semantic tokens in this package. They aren't a great fit in CodeMirror—client-side parsing is more responsive, and allows other extensions to also use the tree. I think it should be possible to implement this outside of this repository using the package's API (if not, feel free to open an issue about the problems you run into), so you could consider distributing it yourself as a separate package.

Hi. I'm not interested in including support for semantic tokens in this package. They aren't a great fit in CodeMirror—client-side parsing is more responsive, and allows other extensions to also use the tree. I think it should be possible to implement this outside of this repository using the package's API (if not, feel free to open an issue about the problems you run into), so you could consider distributing it yourself as a separate package.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
codemirror/lsp-client!4
No description provided.