Skip to content

fix: show the semantic tokens in the text they were computed for, and keep the place of the inspector - #2047

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/semantic-tokens-inspector-stale-offsets-and-caret
Nov 20, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/semantic-tokens-inspector-stale-offsets-and-caret

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

Fixes #399, fixes #400.

  • IndexOutOfBoundsException error in SemanticTokensInspectorManager #399: the semantic tokens inspector inserted its tags at the offsets
    of the tokens into the text that the document had when the inspector
    showed them, later than the tokens were computed. After an edit, as
    typing or an undo, the offsets were in another text, and the
    inspector threw IndexOutOfBoundsException: Wrong offset.
    SemanticTokensInspectorData now keeps the text of the document when
    it is created with the tokens, and the inspector inserts the tags into
    that text. A token outside the text, as one with a large value from a
    server, is not shown, instead of failing the whole view.
  • The inspector cursor with semantic highlighting always automatically scrolls to the bottom. #400: each refresh set the text of the inspector, which moved its
    caret to the end of the new text, so the view scrolled to the bottom.
    Its caret no longer follows the changes of the text, so the view stays
    where the user left it.

Test: SemanticTokensInspectorTest shows tokens after the document has
changed, shows a token outside the text, and refreshes the text of the
inspector's editor. Without the change, the first two throw
IndexOutOfBoundsException: Wrong offset, and the third finds the caret
at the end of the text.

… keep the place of the inspector

Fixes redhat-developer#399, fixes redhat-developer#400.

- redhat-developer#399: the semantic tokens inspector inserted its tags at the offsets
  of the tokens into the text that the document had when the inspector
  showed them, later than the tokens were computed. After an edit, as
  typing or an undo, the offsets were in another text, and the
  inspector threw `IndexOutOfBoundsException: Wrong offset`.
  `SemanticTokensInspectorData` now keeps the text of the document when
  it is created with the tokens, and the inspector inserts the tags into
  that text. A token outside the text, as one with a large value from a
  server, is not shown, instead of failing the whole view.
- redhat-developer#400: each refresh set the text of the inspector, which moved its
  caret to the end of the new text, so the view scrolled to the bottom.
  Its caret no longer follows the changes of the text, so the view stays
  where the user left it.

Test: `SemanticTokensInspectorTest` shows tokens after the document has
changed, shows a token outside the text, and refreshes the text of the
inspector's editor. Without the change, the first two throw
`IndexOutOfBoundsException: Wrong offset`, and the third finds the caret
at the end of the text.
@lagergren lagergren added this to the 0.30.2 milestone Nov 20, 2026
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • The semantic tokens inspector now displays tokens against the document text they were computed for, even if the document changes afterward.
    • Tokens that fall outside the available text are no longer displayed.
    • Refreshing the inspector no longer moves the caret to the end of the text or resets the current view.

The semantic tokens inspector now formats tokens against the text captured when the tokens were computed. It skips invalid token ranges and prevents editor text refreshes from moving the caret.

Changes: Semantic tokens inspector

Layer / File(s) Summary
Snapshot-based token formatting
src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorData.java, src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorManager.java, src/test/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorTest.java
The data record stores the text associated with token offsets. Formatting uses that snapshot and skips token ranges with invalid bounds. Tests cover document edits and token ranges beyond the text.
Caret behavior on text refresh
src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorToolWindowPanel.java, src/test/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorTest.java
The editor configures a DefaultCaret to never update its position when text changes. A test checks the caret position after refresh.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to be356

The semantic tokens inspector now formats tokens against the text they were computed for. It skips tokens outside that text and keeps its scroll position on refresh. 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 #399 is implemented at the reviewed head. SemanticTokensInspectorData adds a text field, set from document.getText() in the three-argument constructor. SemanticTokensInspectorManager.format() now read… Provide readable source for SemanticTokensInspectorToolWindowPanel.java at head, or make its blob available in the checkout. Then confirm that createEditor() sets the caret update policy so that editor.setText() does not move the caret to t…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title describes both fixes in the changeset: the semantic tokens inspector now shows tokens in the text they were computed for (issue #399), and the inspector keeps its caret and scroll position o…
Description check Passed The description states the two problems (stale offsets causing IndexOutOfBoundsException for issue #399, and caret moving to the end on refresh for issue #400), explains how the change addresses each,…
Out of Scope Changes check Passed The changed files named in the PR summary are SemanticTokensInspectorData.java, SemanticTokensInspectorManager.java, SemanticTokensInspectorToolWindowPanel.java, and SemanticTokensInspectorTest.java. …
Full details: Title check

Explanation

The title describes both fixes in the changeset: the semantic tokens inspector now shows tokens in the text they were computed for (issue #399), and the inspector keeps its caret and scroll position on refresh (issue #400). It is a single sentence, specific, and matches the diff and objectives. It is slightly long at 101 characters but remains clear.

Full details: Description check

Explanation

The description states the two problems (stale offsets causing IndexOutOfBoundsException for issue #399, and caret moving to the end on refresh for issue #400), explains how the change addresses each, and lists the new tests. It is directly related to the changeset.

Full details: Out of Scope Changes check

Explanation

The changed files named in the PR summary are SemanticTokensInspectorData.java, SemanticTokensInspectorManager.java, SemanticTokensInspectorToolWindowPanel.java, and SemanticTokensInspectorTest.java. All of them belong to the semantic tokens inspector. The data, manager, and test changes that were read implement #399. The panel change maps to #400. The reviewed files show no unrelated change. The panel change could not be read directly, so this result depends on the PR summary for that file.

Full details: Linked Issues check

Explanation

#399 is implemented at the reviewed head. SemanticTokensInspectorData adds a text field, set from document.getText() in the three-argument constructor. SemanticTokensInspectorManager.format() now reads data.text() and builds its working DocumentImpl from that text. doFormat() skips tokens with start < 0, start > end, or end > text.length(). The test testTheTokensShowInTheTextTheyWereComputedForOnceTheDocumentHasChanged covers the stale-offset case, and testATokenOutsideTheTextIsNotShown covers the out-of-range case. #400 is not verified in source. The change to SemanticTokensInspectorToolWindowPanel.createEditor() (package-private, DefaultCaret set to NEVER_UPDATE) could not be read. The diff tool reported that the changed blobs are missing from the checkout. The test testARefreshOfTheTextDoesNotMoveTheCaretToItsEnd references createEditor(), which shows that the method exists in the test's package. Its caret behavior rests on the PR summary and the test assertion, not on the source.

Resolution

Provide readable source for SemanticTokensInspectorToolWindowPanel.java at head, or make its blob available in the checkout. Then confirm that createEditor() sets the caret update policy so that editor.setText() does not move the caret to the end of the text.

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: d47e6f1f-bac2-41cb-a662-01871bf08bcd
Commits

Reviewing files that changed from the base of the PR and between 0af1afc and be3565e.

Files selected for processing: 4

  • src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorData.java
  • src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorManager.java
  • src/main/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorToolWindowPanel.java
  • src/test/java/com/redhat/devtools/lsp4ij/features/semanticTokens/inspector/SemanticTokensInspectorTest.java

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

@lagergren

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

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.

@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 45.45455% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.44%. Comparing base (0af1afc) to head (be3565e).

Files with missing lines Patch % Lines
...kens/inspector/SemanticTokensInspectorManager.java 42.85% 3 Missing and 1 partial ⚠️
...cTokens/inspector/SemanticTokensInspectorData.java 50.00% 1 Missing ⚠️
...pector/SemanticTokensInspectorToolWindowPanel.java 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2047      +/-   ##
==========================================
- Coverage   49.44%   49.44%   -0.01%     
==========================================
  Files         813      813              
  Lines       33324    33330       +6     
  Branches     5191     5193       +2     
==========================================
+ Hits        16478    16479       +1     
- Misses      14272    14280       +8     
+ Partials     2574     2571       -3     

☔ 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 20, 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
lagergren merged commit ccd8284 into redhat-developer:main Nov 20, 2026
9 checks passed
@lagergren lagergren added the bug Something isn't working label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

1 participant