Skip to content

DocumentSymbolData.equals: compare symbols only, and deprecate matchesPsiElement, which the structure view no longer needs since #1531 #2285

Backlog

Description

@lagergren

Split out of #2281, as it needs an API deprecation, not a late 0.30.2 change.

What

DocumentSymbolData.equals (DocumentSymbolData.java:120-137) is true for another DocumentSymbolData with an equal symbol, and also for any PsiElement that LSPDocumentSymbolFeature.matchesPsiElement accepts. hashCode uses only the symbol. So equals isn't symmetric, and an object equal to a symbol can have another hash code.

Why the PsiElement branch is no longer needed

Checked in the bytecode of IntelliJ 2024.2 (IC 242.20224.300) and 2026.3 (IU 263.7307.200):

  • The editor element is always a symbol. Since fix: Always Select Opened Element should select parent DocumentSymbol #1531 (0.20.0), findAcceptableElement returns the containing DocumentSymbolData. So TextEditorBasedStructureViewModel.getCurrentEditorElement() gives a DocumentSymbolData or null.

  • Every selection path compares a symbol with a symbol: the structure view's Always Select Opened Element (StructureViewComponent.visitPathForElementSelection, Comparing.equal(value, element)), Select In → Structure, the File Structure popup, and the 2026.3 popup backend (StructureViewNodeKey).

  • The branch from fix: In Structure tool window, Always Select Opened Element did not work #1381 is still reached only when a symbol is compared with the file's PsiFile:

    • in the PsiTreeUtil.isAncestor walk, with the same result either way;
    • in StructureViewComponent.MyNodeWrapper.equals;
    • in the old popup's speed search.

    It is true there only for a symbol whose selection range is the whole file, such as a File or Module symbol, where it is a spurious match.

  • Hash-based collections are safe: none lets the asymmetry decide anything, since synchronizeChildren keys on tree elements whose equals checks the class, and 2026.3's StructureViewNodeKey includes the element class.

  • No other uses: breadcrumbs and sticky lines compare symbols only, and LSP4IJ registers no navigation bar extension.

  • matchesPsiElement: it is public and documented in docs/LSPApi.md, but this equals is its only caller. No client on GitHub overrides it.

  • Dead code: LSP4IJ's getSuitableClasses override from fix: In Structure tool window, Always Select Opened Element did not work #1381 is unused, since its only user is the default findAcceptableElement, which LSP4IJ replaces.

Plan

For 0.31.0

  • Compare symbols only in equals, which makes it symmetric and consistent with hashCode.
  • Remove the dead getSuitableClasses override; matchesPsiElement is no longer called, and can be removed in a later release.
  • First add tests that pin the structure view's selection: the caret, the containing symbol, nested symbols, and two symbols of the same name. They should pass before and after. They need a StructureViewComponent with a mock language server.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions