mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-26 14:29:05 +08:00
## Summary https://github.com/astral-sh/ty/issues/3514 Implements support for `textDocument/implementation` 1. Finds one or more class roots: - for an attribute access, normalize the receiver type into class roots - for a method declaration, use the containing class as the root 2. For each class root, collect: - the method definition selected by normal MRO lookup for that root - same-named methods defined directly on known transitive subclasses 3. Deduplicate targets + preserve discovery order ## Test Plan - New snapshot tests - E2E in VSCode and Neovim <img width="492" height="176" alt="Screenshot 2026-05-26 at 23 16 45" src="https://github.com/user-attachments/assets/aaa9c4d2-aed1-42c3-88be-829b369c1910" /> <img width="1058" height="394" alt="Screenshot 2026-05-26 at 23 16 28" src="https://github.com/user-attachments/assets/931615d1-c327-4c90-9c7e-4a6070b24fc4" /> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Lérè <contact@lrbr.dev>
117 lines
3.5 KiB
Rust
117 lines
3.5 KiB
Rust
use anyhow::Result;
|
|
use lsp_types::{
|
|
Definition, ImplementationParams, ImplementationProvider, ImplementationRequest,
|
|
ImplementationResponse, PartialResultParams, Position, Range, TextDocumentIdentifier,
|
|
TextDocumentPositionParams, WorkDoneProgressParams,
|
|
};
|
|
|
|
use crate::TestServerBuilder;
|
|
|
|
const CONTENT: &str = r#"class Animal:
|
|
def speak(self): ...
|
|
|
|
class Dog(Animal):
|
|
def speak(self): ...
|
|
|
|
class Cat(Animal):
|
|
def speak(self): ...
|
|
|
|
def f(animal: Animal):
|
|
animal.speak()
|
|
"#;
|
|
|
|
#[test]
|
|
fn implementation_provider_is_advertised() -> Result<()> {
|
|
let server = TestServerBuilder::new()?
|
|
.build()
|
|
.wait_until_workspaces_are_initialized();
|
|
|
|
let initialization_result = server.initialization_result().unwrap();
|
|
assert_eq!(
|
|
initialization_result.capabilities.implementation_provider,
|
|
Some(ImplementationProvider::Bool(true))
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn implementation_locations_without_link_support() -> Result<()> {
|
|
let mut server = TestServerBuilder::new()?
|
|
.with_file("foo.py", CONTENT)?
|
|
.enable_implementations_link_support(false)
|
|
.build()
|
|
.wait_until_workspaces_are_initialized();
|
|
|
|
server.open_text_document("foo.py", CONTENT, 1);
|
|
|
|
let response = implementation(&mut server, "foo.py", Position::new(10, 13)).unwrap();
|
|
let ImplementationResponse::Definition(Definition::LocationList(locations)) = response else {
|
|
panic!("Expected Location[] response, got {response:#?}");
|
|
};
|
|
|
|
let ranges: Vec<_> = locations.iter().map(|location| location.range).collect();
|
|
assert_eq!(
|
|
ranges,
|
|
vec![
|
|
Range::new(Position::new(1, 8), Position::new(1, 13)),
|
|
Range::new(Position::new(4, 8), Position::new(4, 13)),
|
|
Range::new(Position::new(7, 8), Position::new(7, 13)),
|
|
]
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn implementation_location_links_with_link_support() -> Result<()> {
|
|
let mut server = TestServerBuilder::new()?
|
|
.with_file("foo.py", CONTENT)?
|
|
.enable_implementations_link_support(true)
|
|
.build()
|
|
.wait_until_workspaces_are_initialized();
|
|
|
|
server.open_text_document("foo.py", CONTENT, 1);
|
|
|
|
let response = implementation(&mut server, "foo.py", Position::new(10, 13)).unwrap();
|
|
let ImplementationResponse::DefinitionLinkList(links) = response else {
|
|
panic!("Expected LocationLink[] response, got {response:#?}");
|
|
};
|
|
|
|
let selection_ranges: Vec<_> = links
|
|
.iter()
|
|
.map(|link| link.target_selection_range)
|
|
.collect();
|
|
assert_eq!(
|
|
selection_ranges,
|
|
vec![
|
|
Range::new(Position::new(1, 8), Position::new(1, 13)),
|
|
Range::new(Position::new(4, 8), Position::new(4, 13)),
|
|
Range::new(Position::new(7, 8), Position::new(7, 13)),
|
|
]
|
|
);
|
|
assert!(links.iter().all(|link| {
|
|
link.origin_selection_range
|
|
== Some(Range::new(Position::new(10, 11), Position::new(10, 16)))
|
|
}));
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn implementation(
|
|
server: &mut crate::TestServer,
|
|
path: impl AsRef<ruff_db::system::SystemPath>,
|
|
position: Position,
|
|
) -> Option<ImplementationResponse> {
|
|
server.send_request_await::<ImplementationRequest>(ImplementationParams {
|
|
text_document_position_params: TextDocumentPositionParams {
|
|
text_document: TextDocumentIdentifier {
|
|
uri: server.file_uri(path),
|
|
},
|
|
position,
|
|
},
|
|
work_done_progress_params: WorkDoneProgressParams::default(),
|
|
partial_result_params: PartialResultParams::default(),
|
|
})
|
|
}
|