Skip to content

feat: Find Usages shows a reference that both the language plugin and the language server find once, on one line - #2228

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:feat/usages-merge-with-language-plugin
Nov 25, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:feat/usages-merge-with-language-plugin

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

Fixes #2227

This is the second request of #729, whose first was fixed in #2219.

What

When Find Usages searches a language plugin's element, such as a Scala class with Metals, IntelliJ runs the plugin's own search and LSP4IJ's CustomUsageSearcher (FindUsagesManager.createUsageSearcher). Usages on the same line are merged only within one group (GroupNode.tryMerge), and the view groups by usage type and by file structure:

  • LSP4IJ's reference had its own usage type, "References" (UsageType has identity equality);
  • its element (LSPUsageViewElement) had no file-structure parent, while the plugin's usage sits under its method or class.

So a reference that both found was listed twice, for example under "Method call" and under "References". That happened even with "Group by usage type" off, because the file-structure groups differed too.

Change

LSPUsageSearcher.toUsage(usage, searchedElement) handles a reference from the language server specially when the searched element is not LSP4IJ's own, that is when the plugin's handler searched it:

  • Matching: it looks for file.findReferenceAt(start). The reference must not be LSP4IJ's own PSI, such as the semantic tokens, and its getAbsoluteRange() must equal the language server's range exactly.
  • Reporting: if there is such a reference, the usage is reported the way the plugin's search reports it, as a UsageInfo of the reference's element converted with UsageInfoToUsageConverter.convert, as FindUsagesManager does. The usage view then puts both in the same groups and shows them on one line.
  • Access: a known read/write access from an integration is kept (ReadWriteAccessUsageInfo2UsageAdapter).
  • Everything else is unchanged: a reference that only the language server finds is still listed. Declarations, definitions, type definitions and implementations are untouched. Searches on LSP4IJ's own symbol, and files without a language plugin, are unchanged.

Dropping LSP4IJ's copy "when the plugin already reported it" isn't possible exactly. The plugin's usages go through a private UniqueProcessor, and its word-index results come after all custom searchers (processRequests(fastTrack)). So merging through the same groups is the safe way.

Not in this PR:

Tests

New LSPUsageMergeWithLanguagePluginTest, 3 tests:

  • The setup: a test language with a file type, a parser, PSI with PsiReferences and a "Method call" UsageTypeProvider, which gives Find Usages a plugin handler.
  • What it checks: it runs the real FindUsagesManager.createUsageSearcher, and renders the real UsageViewImpl tree.
  • Before: [Definitions 1 ArgNode, Method call 2 first(ArgNode), Method call 3 second(ArgNode), References 2 first(ArgNode), References 4 third(ArgNode), References 5 // ArgNode], with line 2 twice.
  • After: [Definitions 1 ArgNode, Method call 2 first(ArgNode), Method call 3 second(ArgNode), Method call 4 third(ArgNode), References 5 // ArgNode].
  • The other two tests: LSP4IJ's own symbol is listed as before, and a known access is kept.

The new class passes headless on IC 2024.2, 2025.3 and IU 2026.3. usages.* and features.references.* pass on all three: 30 tests each, 0 failures, 0 skipped.

Behaviour change

Before After
Find Usages of a language plugin's element lists a reference that both the plugin and the language server find twice: in the plugin's group, such as "Method call", and under "References" It is listed once, on one line, in the plugin's group. A reference that only the language server finds is still listed: in the plugin's group if the plugin has a reference at its range, otherwise under "References"

… the language server find once, on one line

Find Usages of a language plugin's element, such as a Scala class with
Metals as language server, lists the usages of the language plugin's own
search and those of the language server. The usage view merges the usages
of a line only within one group, and groups a usage by its usage type and
by the PSI around it, such as its method. A reference that both find was
therefore listed twice: in the language plugin's group, such as "Method
call", and under References.

A reference of the language server at the range of a reference of the
language plugin is now reported as the language plugin's search reports
that reference, a usage of the reference's element, so that the usage view
groups both alike and shows them on one line. Its read or write access, when
an integration gives it, is kept. A reference that only the language server
finds is still listed: in the language plugin's group when the language
plugin has a reference at its range, and under References otherwise. The
usages of LSP4IJ's own symbol, as in a file without a language plugin, are
listed as before.

This is the second request of redhat-developer#729.

Fixes redhat-developer#2227
@lagergren lagergren added this to the 0.30.2 milestone Nov 25, 2026
@lagergren lagergren added the enhancement New feature or request label Nov 25, 2026
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Find Usages now avoids showing duplicate results when both the language plugin and language server find the same reference.
    • Usage results preserve known read/write access and are grouped with the corresponding language-plugin results when appropriate.
    • Existing results for searches initiated by LSP4IJ continue to appear as before.

LSP usage conversion now uses an exact-range language-plugin reference when one is found for a searched element that is not an LSPPsiElement. Tests cover result grouping, duplicate references, and preservation of write access.

Changes: Language-plugin reference matching

Layer / File(s) Summary
Match LSP usages to plugin references
src/main/java/com/redhat/devtools/lsp4ij/usages/LSPUsageSearcher.java, src/test/java/com/redhat/devtools/lsp4ij/usages/LSPUsageMergeWithLanguagePluginTest.java
Fresh usages are converted with the searched element. For applicable searches, the conversion uses a language-plugin reference only when its absolute range exactly matches the LSP usage range. Tests cover shared and separate results, LSP4IJ-triggered searches, and write access preservation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Low

Sequence Diagram(s)

sequenceDiagram
  participant FindUsages
  participant LSPUsageSearcher
  participant PluginPSI
  participant UsageInfoToUsageConverter
  FindUsages->>LSPUsageSearcher: Convert fresh usage with searched element
  LSPUsageSearcher->>PluginPSI: Look up reference at usage start
  PluginPSI-->>LSPUsageSearcher: Return reference and absolute range
  alt Reference range matches LSP usage range
    LSPUsageSearcher->>UsageInfoToUsageConverter: Convert when reference access is unknown
  else No exact match
    LSPUsageSearcher->>LSPUsageSearcher: Use existing usage conversion
  end
Loading

Suggested reviewers: shiningpluto, angelozerr

Merge Risk: Minimal · up to 70f53

Find Usages now merges duplicate results when a language plugin and the language server both find the same reference. Unmatched language-server results still appear as before. No concrete defect was found, so the risk of merging is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Find Usages will show a reference found by both the language plugin and the language server once, on one line. The raw summary and PR description confirm this is the c…
Description check Passed The description is directly related to the changeset. It explains the duplicate-result cause, the exact-range matching in LSPUsageSearcher.toUsage, the retained read/write access, the scope exclusions…
Linked Issues check Passed Directly linked issue #2227 asks that Find Usages of a language plugin's element stop listing one reference twice, once under the plugin's group and once under "References". The change in LSPUsageSear…
Out of Scope Changes check Passed The whole-PR change set has two files: LSPUsageSearcher.java and the new LSPUsageMergeWithLanguagePluginTest.java. Both relate to the #2227 requirement. The new toUsage overload, the class Javadoc and…
Full details: Title check

Explanation

The title names the main change: Find Usages will show a reference found by both the language plugin and the language server once, on one line. The raw summary and PR description confirm this is the core change in LSPUsageSearcher and the new test class. The title is long (112 characters), but it stays specific and on topic.

Full details: Description check

Explanation

The description is directly related to the changeset. It explains the duplicate-result cause, the exact-range matching in LSPUsageSearcher.toUsage, the retained read/write access, the scope exclusions, and the three tests in LSPUsageMergeWithLanguagePluginTest.

Full details: Linked Issues check

Explanation

Directly linked issue #2227 asks that Find Usages of a language plugin's element stop listing one reference twice, once under the plugin's group and once under "References". The change in LSPUsageSearcher.toUsage(usage, searchedElement) addresses this. For a references usage, when the searched element is not an LSPPsiElement, it looks up file.findReferenceAt(start). It accepts the match only when the reference is not LSP4IJ-owned PSI and its getAbsoluteRange() equals the LSP range. A matched usage is reported as a UsageInfo of the plugin reference's element, so the usage view groups both on one line. Known access is kept through ReadWriteAccessUsageInfo2UsageAdapter. The issue's excluded cases are also excluded in the PR: LSP4IJ-own target searches, non-exact ranges, non-base language trees, and definitions, type definitions and implementations. The new test class LSPUsageMergeWithLanguagePluginTest is described with a before/after tree that shows the duplicate removed. Evidence limit: the diff output for the remaining part of LSPUsageSearcher.java was truncated, and the test file was not read directly. The verdict relies on the code shown and the PR's test description.

Full details: Out of Scope Changes check

Explanation

The whole-PR change set has two files: LSPUsageSearcher.java and the new LSPUsageMergeWithLanguagePluginTest.java. Both relate to the #2227 requirement. The new toUsage overload, the class Javadoc and the test class all target the duplicate-merge behavior. The existing toUsage(usage) wrapper and the cached-reference fast path keep their behavior. Declarations, definitions, type definitions and implementations are not changed in the code shown. The pre-existing removeRepeatedDeclarations logic is not altered by the shown hunks. Evidence limit: the rest of the LSPUsageSearcher.java diff was truncated, so unseen hunks were not checked line by line.

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: 7d3da59d-dc3c-48dd-a22f-8ee90a639bb8
Commits

Reviewing files that changed from the base of the PR and between 57bae55 and 70f53a0.

Files selected for processing: 2

  • src/main/java/com/redhat/devtools/lsp4ij/usages/LSPUsageSearcher.java
  • src/test/java/com/redhat/devtools/lsp4ij/usages/LSPUsageMergeWithLanguagePluginTest.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.

@lagergren

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

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 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.35294% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.56%. Comparing base (57bae55) to head (70f53a0).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...edhat/devtools/lsp4ij/usages/LSPUsageSearcher.java 82.35% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2228      +/-   ##
==========================================
+ Coverage   55.52%   55.56%   +0.03%     
==========================================
  Files         843      843              
  Lines       35939    35955      +16     
  Branches     5638     7437    +1799     
==========================================
+ Hits        19955    19978      +23     
+ Misses      13097    13083      -14     
- Partials     2887     2894       +7     

☔ 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
lagergren merged commit 30033b0 into redhat-developer:main Nov 25, 2026
9 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Find Usages: merge the language server's usages with those of the language plugin

1 participant