Skip to content

feat: the first highlighting of a file shows the semantic tokens of its visible range while those of the whole file are computed - #2380

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:feat/semantic-tokens-visible-range
Nov 30, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:feat/semantic-tokens-visible-range

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

Fixes #2240.

When a large file opens, its semantic colors wait for the language server's first textDocument/semanticTokens/full answer, which takes a while on a large file. VS Code asks for the visible part of the file meanwhile, with textDocument/semanticTokens/range. LSP4IJ now does the same.

#2338 already handles #2240's other case: a language server with semantic tokens for a range only gets the range of the whole file.

What changes

  • When it applies: at the first highlighting of a file whose whole text isn't visible, when the semantic tokens of the whole file haven't come after 100 ms. This is the delay VS Code waits before it asks for the viewport, so a quick language server is asked once. The highlighting then also asks the language servers for the semantic tokens of the visible range. Only language servers that have both full and range are asked, and only when every language server with semantic tokens for the file has both. The answer comes from the same language server as for the whole file: the first one, in the order of the mappings, that answers with tokens.
  • The visible range: the priority range of IntelliJ's highlighting, AnnotationSession.getPriorityRange(), which is public and not deprecated from 2024.2 to 2026.3.
  • When the visible range comes first:
    • its tokens show, and become the colors shown last;
    • once the tokens of the whole file come, the highlighting of the file restarts, without an edit, through restartDaemonCodeAnalyzerForSemanticTokensRefresh, so they replace them;
    • if the whole file comes first, or the visible range has no tokens or no answer, the highlighting waits for the whole file as before.
  • Later highlightings show the colors shown last while they wait, as before, and don't ask for the visible range.
  • Cancellation: an edit cancels the visible-range request, as it cancels the request for the whole file.
  • Decoding: a range answer is decoded from the start of the file, as a full answer is. The live check confirms that this is how language servers encode it.
  • Docs: docs/LSPSupport.md and docs/UserGuide.md. The User Guide's "not supported yet" now names the one thing missing: asking again when the editor scrolls before the whole file has come.

Behaviour changes

Before After
A large file opened with no semantic colors until the whole file's tokens came The visible part gets its colors first, from a language server that has range too, when the whole file takes more than 100 ms
One request at the first highlighting of a file Two, a full and a range, when the full one takes more than 100 ms and the file isn't visible whole

Live check

Decoding. Range tokens decoded from the start of the file were compared with the full tokens of the same lines:

Language server Lines Range answer Equal to the full tokens of those lines
rust-analyzer 0.3.3138 (2026-11-29) 10–13 of 15 23 tokens yes
typescript-language-server 6.1.0 10–15 of 16 6 tokens yes

Timing. rust-analyzer on a 12,000-line Rust file, right after an edit, with both requests sent at once:

  • range for the first 40 lines: 67 ms, 356 tokens;
  • full: 446 ms, 105,026 tokens.

With the 100 ms wait, the visible lines get their colors about 250 ms sooner.

Tests

SemanticTokensVisibleRangeTest, with the mock language server and an editor that shows 5 of the file's 40 lines (EditorTestUtil.setEditorVisibleSize):

  • a slow full answer: the visible range shows first. Its request covers the visible lines only. While the daemon runs, the whole file's tokens replace it without an edit;
  • a quick language server is asked for the whole file only;
  • a later highlighting keeps the colors shown last and doesn't ask for the visible range;
  • a language server without range is asked for the whole file only;
  • a file that is visible whole is asked for the whole file only;
  • a visible range without tokens waits for the whole file.

The existing semantic tokens tests pass unchanged, since their language servers answer within 100 ms.

…ts visible range while those of the whole file are computed

When the semantic tokens of the whole file haven't come after 100 ms,
the first highlighting of a file that is not visible whole asks the
language servers that have both full and range requests for the
semantic tokens of its visible range, as VS Code does, shows them if
they come first, and restarts once those of the whole file come.

Fixes redhat-developer#2240
@lagergren lagergren added this to the 0.30.3 milestone Nov 30, 2026
@lagergren lagergren added enhancement New feature or request semantic tokens labels Nov 30, 2026
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • For partially visible files, semantic highlighting can now show colors for the visible range if whole-file colors take longer than 100 ms and the language server supports both request types. Whole-file colors replace the visible-range colors when they arrive; later highlighting keeps the last shown colors while waiting.
  • Documentation
    • Updated semantic token documentation to describe visible-range highlighting, replacement by whole-file results, and behavior when scrolling before whole-file colors arrive.

The change adds visible-range semantic-token requests during initial highlighting when full-file tokens are delayed and the server supports both full and range requests. Full-file tokens replace visible-range tokens when they arrive. Tests and documentation cover this behavior and its limits.

Changes: Initial Semantic Highlighting

Layer / File(s) Summary
Range request support
src/main/java/com/redhat/devtools/lsp4ij/server/capabilities/SemanticTokensCapabilityRegistry.java, src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LSPSemanticTokensSupport.java
The capability registry checks for full and range support. Semantic token support requests visible-range tokens from eligible servers and cancels any prior range request.
Initial highlighting flow
src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LSPSemanticTokensHighlightVisitor.java, src/test/java/com/redhat/devtools/lsp4ij/features/semanticTokens/SemanticTokensVisibleRangeTest.java, docs/LSPSupport.md, docs/UserGuide.md
During initial highlighting, the visitor requests visible-range tokens if the full-file request remains pending for 100 ms and no tokens are already shown. It uses the range result if it arrives first, then restarts analysis when full-file tokens arrive. Tests cover request conditions and result ordering. The documentation describes the behavior and unsupported cases.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Feature · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant LSPSemanticTokensHighlightVisitor
  participant LSPSemanticTokensSupport
  participant LanguageServer
  LSPSemanticTokensHighlightVisitor->>LSPSemanticTokensSupport: Request full-file tokens
  LSPSemanticTokensSupport->>LanguageServer: Send full-file token request
  LSPSemanticTokensHighlightVisitor->>LSPSemanticTokensSupport: Request visible-range tokens after 100 ms
  LSPSemanticTokensSupport->>LanguageServer: Send visible-range token request
  LanguageServer-->>LSPSemanticTokensSupport: Return visible-range tokens
  LSPSemanticTokensSupport-->>LSPSemanticTokensHighlightVisitor: Return visible-range tokens
  LSPSemanticTokensHighlightVisitor->>LSPSemanticTokensHighlightVisitor: Show visible-range tokens
  LanguageServer-->>LSPSemanticTokensSupport: Return full-file tokens
  LSPSemanticTokensSupport-->>LSPSemanticTokensHighlightVisitor: Return full-file tokens
  LSPSemanticTokensHighlightVisitor->>LSPSemanticTokensHighlightVisitor: Restart analysis
Loading

Suggested reviewers: angelozerr

Merge Risk: Minimal · up to 63be5

This adds an early visible-range semantic-token request so colors appear sooner when opening large files. Whole-file tokens still replace the range result, and every fallback path falls back to the existing full-file behavior. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Issue #2240 has four coding parts. (1) Visible-range first paint for servers with both full and range: the PR adds this (LSPSemanticTokensSupport, LSPSemanticTokensHighlightVisitor, SemanticTokensCapa… Establish the state at the reviewed head of the range-only path (the #2338 work) and of the client capability for semanticTokens.requests.range, for example by reading the semanticTokens section of ClientCapabilitiesFactory.java at head and…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title states the main change: the first highlighting of a file shows semantic tokens for the visible range while the whole-file tokens are computed. This matches the changeset, which adds visible-…
Description check Passed The description is directly related to the changeset. It explains the visible-range semantic token request for large files, the 100 ms delay, the full and range support conditions, the daemon restart …
Out of Scope Changes check Passed All changed areas relate to the semantic-token visible-range feature. These are: the highlight visitor and support classes, the capability check method for full and range support, the test class, and …
Full details: Title check

Explanation

The title states the main change: the first highlighting of a file shows semantic tokens for the visible range while the whole-file tokens are computed. This matches the changeset, which adds visible-range semantic token requests on first highlighting, with range-support checks, daemon restart, and tests. The title is a single sentence and specific to the change.

Full details: Description check

Explanation

The description is directly related to the changeset. It explains the visible-range semantic token request for large files, the 100 ms delay, the full and range support conditions, the daemon restart on whole-file arrival, cancellation, decoding, documentation updates, and the new test suite. It also links the issue and gives a behaviour table.

Full details: Out of Scope Changes check

Explanation

All changed areas relate to the semantic-token visible-range feature. These are: the highlight visitor and support classes, the capability check method for full and range support, the test class, and the LSPSupport and UserGuide docs. The docs describe the new behavior and its limit. The 100 ms delay, the priority-range source, and the daemon restart on full-token arrival all serve the visible-range objective of #2240. No change was found that lacks a connection to the linked issues. The file-level summaries were not checked against the full diff, so a small unlisted change could exist.

Full details: Linked Issues check

Explanation

Issue #2240 has four coding parts. (1) Visible-range first paint for servers with both full and range: the PR adds this (LSPSemanticTokensSupport, LSPSemanticTokensHighlightVisitor, SemanticTokensCapabilityRegistry.isSemanticTokensFullAndRangeSupported). It uses AnnotationSession.getPriorityRange() as the visible range, shows range tokens first, restarts the daemon when full tokens arrive, and decodes range answers from the file start. SemanticTokensVisibleRangeTest covers these cases. The PR summary supports this part. The live check supports the decoding choice, which differs from the issue's suggestion to decode from the range start line. (2) Range-only servers: the PR description assigns this to #2338. The changed-file list has no range-only code. The reviewed head was not verified for that code, so this part is unresolved. (3) Announcing range: true in client capabilities: the issue requires it. ClientCapabilitiesFactory is not in the changed-file list. Its semanticTokens section was not readable (output truncated, and the base read of LSPSemanticTokensSupport.java failed), so whether range is announced at head is unknown. (4) Test coverage for the both-support case is present.

Resolution

Establish the state at the reviewed head of the range-only path (the #2338 work) and of the client capability for semanticTokens.requests.range, for example by reading the semanticTokens section of ClientCapabilitiesFactory.java at head and base. If range-only support is absent at head and is meant for this issue, the PR or a linked change must add it. If #2338 is merged into the base, record that as existing behavior and then reassess.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a18e1850-3d50-4169-a6a9-703c858025bd
Commits

Reviewing files that changed from the base of the PR and between f74b72d and 63be5be.

Files selected for processing: 6

  • docs/LSPSupport.md
  • docs/UserGuide.md
  • src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LSPSemanticTokensHighlightVisitor.java
  • src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/LSPSemanticTokensSupport.java
  • src/main/java/com/redhat/devtools/lsp4ij/server/capabilities/SemanticTokensCapabilityRegistry.java
  • src/test/java/com/redhat/devtools/lsp4ij/features/semanticTokens/SemanticTokensVisibleRangeTest.java

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.05882% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.80%. Comparing base (f74b72d) to head (63be5be).

Files with missing lines Patch % Lines
...anticTokens/LSPSemanticTokensHighlightVisitor.java 62.85% 5 Missing and 8 partials ⚠️
...tures/semanticTokens/LSPSemanticTokensSupport.java 86.66% 2 Missing and 2 partials ⚠️
...capabilities/SemanticTokensCapabilityRegistry.java 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2380      +/-   ##
==========================================
+ Coverage   59.77%   59.80%   +0.03%     
==========================================
  Files         858      858              
  Lines       37428    37495      +67     
  Branches     5883     7776    +1893     
==========================================
+ Hits        22373    22425      +52     
- Misses      11991    11997       +6     
- Partials     3064     3073       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lagergren

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@lagergren

Copy link
Copy Markdown
Collaborator Author

@coderabbitai About the inconclusive "Linked Issues" check: the two other parts of #2240 are already on main, from #2338. That's why this PR doesn't change them.

  • Range-only servers: 1a6ef71, "fix: a language server with semantic tokens for a range only gets textDocument/semanticTokens/range, and its colors show". LSPSemanticTokensSupport sends the range of the whole file when SemanticTokensCapabilityRegistry.isSemanticTokensRangeOnly(file) is true (LSPSemanticTokensSupport.java:423 on main). SemanticTokensRangeTest.testARangeOnlyServerGetsTheRangeOfTheWholeFile covers it.
  • range: true in the client capabilities: ClientCapabilitiesFactory.java:348-349 declares new SemanticTokensClientCapabilitiesRequests(new SemanticTokensClientCapabilitiesRequestsFull(Boolean.TRUE), Boolean.TRUE), so full with delta (feat: semantic tokens ask a language server that has delta for the edits since its last answer #2377) and range. SemanticTokensClientCapabilitiesTest.testTheClientDeclaresFullDeltaAndRangeRequests checks the initialize request.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

Analysis chain

Script executed:

#!/bin/bash
set -u

echo '=== PR head and base ==='
gh api repos/redhat-developer/lsp4ij/pulls/2380 --jq '{head: .head.sha, base: .base.sha, base_ref: .base.ref}'
base_sha=$(gh api repos/redhat-developer/lsp4ij/pulls/2380 --jq '.base.sha')
echo "Checked-out revision: $(git rev-parse HEAD)"

echo '=== PR #2338 ==='
gh api repos/redhat-developer/lsp4ij/pulls/2338 --jq '{state: .state, merged_at: .merged_at, merge_commit_sha: .merge_commit_sha, base_ref: .base.ref}'

echo '=== Cited source and test files ==='
fd -t f '^(LSPSemanticTokensSupport|SemanticTokensCapabilityRegistry|ClientCapabilitiesFactory|SemanticTokensRangeTest|SemanticTokensClientCapabilitiesTest)\.java$' | while IFS= read -r file; do
  printf '\n=== %s: PR head ===\n' "$file"
  ast-grep outline "$file" --items all 2>/dev/null | head -100
  rg -n -C 22 \
    -e 'isSemanticTokensRangeOnly\s*\(' \
    -e 'SemanticTokensClientCapabilitiesRequests' \
    -e 'testARangeOnlyServerGetsTheRangeOfTheWholeFile' \
    -e 'testTheClientDeclaresFullDeltaAndRangeRequests' \
    "$file" || true

  printf '\n=== %s: PR base %s ===\n' "$file" "$base_sha"
  if git cat-file -e "${base_sha}:${file}" 2>/dev/null; then
    git show "${base_sha}:${file}" | rg -n -C 22 \
      -e 'isSemanticTokensRangeOnly\s*\(' \
      -e 'SemanticTokensClientCapabilitiesRequests' \
      -e 'testARangeOnlyServerGetsTheRangeOfTheWholeFile' \
      -e 'testTheClientDeclaresFullDeltaAndRangeRequests' || true
  else
    gh api -H 'Accept: application/vnd.github.raw+json' "repos/redhat-developer/lsp4ij/contents/${file}?ref=${base_sha}" | rg -n -C 22 \
      -e 'isSemanticTokensRangeOnly\s*\(' \
      -e 'SemanticTokensClientCapabilitiesRequests' \
      -e 'testARangeOnlyServerGetsTheRangeOfTheWholeFile' \
      -e 'testTheClientDeclaresFullDeltaAndRangeRequests' || true
  fi
done

Length of output: 34840


@lagergren Thanks for the clarification. I checked the PR head and its main base. Both contain:

  • The whole-file range request for range-only servers in LSPSemanticTokensSupport.java at Line 483 on the PR head.
  • The range and full-delta declarations in ClientCapabilitiesFactory.java at Line 348.

Both cited tests are also present. I inspected them but did not run them.

This resolves the two unknowns behind the saved inconclusive assessment. I did not rerun the check, so this reply does not update its saved result.

You are interacting with an AI system.

@lagergren
lagergren merged commit 4a42009 into redhat-developer:main Nov 30, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request semantic tokens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LSP: support the textDocument/semanticTokens/range request

1 participant