Skip to content

fix Implement type hierarchy #2264#2278

Closed
asukaminato0721 wants to merge 3 commits intofacebook:mainfrom
asukaminato0721:2264
Closed

fix Implement type hierarchy #2264#2278
asukaminato0721 wants to merge 3 commits intofacebook:mainfrom
asukaminato0721:2264

Conversation

@asukaminato0721
Copy link
Contributor

@asukaminato0721 asukaminato0721 commented Jan 31, 2026

Summary

Fixes #2264

Added LSP type hierarchy support end‑to‑end: prepare/supertypes/subtypes handlers backed by class MRO + reverse‑dep scanning, and injected typeHierarchyProvider into the initialize response so clients can enable the feature.

Test Plan

added lsp test.

@meta-cla meta-cla bot added the cla signed label Jan 31, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review January 31, 2026 16:31
Copilot AI review requested due to automatic review settings January 31, 2026 16:31
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds end-to-end LSP type hierarchy support (prepare + supertypes + subtypes) and wires up capability advertisement so LSP clients can enable the feature.

Changes:

  • Implement textDocument/typeHierarchy handlers in the non-wasm LSP server (prepare/supertypes/subtypes).
  • Add AST helpers for locating/collecting class definitions to support hierarchy queries.
  • Add an LSP integration test and fixture Python file for type hierarchy behavior.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pyrefly/lib/lsp/non_wasm/server.rs Adds type hierarchy request handling and injects typeHierarchyProvider into initialize response.
pyrefly/lib/lsp/non_wasm/type_hierarchy.rs Adds helpers to find class defs at positions and build TypeHierarchyItems.
pyrefly/lib/lsp/non_wasm/mod.rs Exposes the new type_hierarchy module.
pyrefly/lib/commands/lsp.rs Passes a type_hierarchy_provider flag into initialization.
pyrefly/lib/commands/tsp.rs Passes a type_hierarchy_provider flag into initialization for TSP mode.
pyrefly/lib/test/lsp/lsp_interaction/type_hierarchy.rs Adds an integration test covering prepare/supertypes/subtypes.
pyrefly/lib/test/lsp/lsp_interaction/test_files/type_hierarchy_test/classes.py Adds a simple inheritance fixture (A <- B <- C).
pyrefly/lib/test/lsp/lsp_interaction/mod.rs Registers the new test module.
Cargo.lock Updates lockfile due to dependency resolution changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 651 to 656
#[derive(Serialize)]
struct InitializeResultWithTypeHierarchy<'a> {
capabilities: ServerCapabilitiesWithTypeHierarchy<'a>,
#[serde(skip_serializing_if = "Option::is_none")]
server_info: Option<ServerInfo>,
}
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InitializeResultWithTypeHierarchy is missing #[serde(rename_all = "camelCase")] (or a field-level rename) so server_info will serialize as server_info instead of LSP's required serverInfo. This breaks the initialize response schema for clients that read serverInfo.

Add #[serde(rename_all = "camelCase")] to InitializeResultWithTypeHierarchy (or #[serde(rename = "serverInfo")] on the field) to keep the JSON wire format compliant.

Copilot uses AI. Check for mistakes.
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

&uri,
params.item.selection_range.start,
FindPreference::default(),
move |transaction, handle, definition| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to break this up or move it into a helper function. This is over 100 lines and at this point becoming a bit difficult to read.

@github-actions

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Feb 2, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

indexing_mode: IndexingMode,
initialization_params: &InitializeParams,
) -> ServerCapabilities {
) -> ServerCapabilitiesWithTypeHierarchy {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry maybe I was not clear but this is not exactly what I meant. On line 842 of fbcode/pyrefly/pyrefly/lib/lsp/non_wasm/server.rs we add the call hierarchy provider. We should be able to add the TypeHierarchyProvider in a very similar way. Once we do that we will not need ServerCapabilitiesWithTypeHierarchy since type hierarchy will already be included in the existing ServerCapabilities

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is:

we have https://docs.rs/lsp-types/latest/lsp_types/enum.CallHierarchyServerCapability.html

but we don't have TypeHierarchyServerCapability in lsp_types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch! In that case I think this is fine for now.

pub fn initialize_finish(
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerCapabilitiesWithTypeHierarchy {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we can implement this in a way where these structs are not required. See my other comment.

@meta-codesync
Copy link

meta-codesync bot commented Feb 3, 2026

@jvansch1 has imported this pull request. If you are a Meta employee, you can view this in D92156168.

Copy link
Contributor

@kinto0 kinto0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync
Copy link

meta-codesync bot commented Feb 4, 2026

@jvansch1 merged this pull request in 6969675.

@asukaminato0721 asukaminato0721 deleted the 2264 branch February 4, 2026 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement type hierarchy

4 participants