-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[ty] Add initial implementation of goto definition for loads of local names #19123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
use ruff_db::files::{File, FilePath}; | ||
use ruff_db::source::line_index; | ||
use ruff_python_ast as ast; | ||
use ruff_python_ast::{self as ast, ExprContext}; | ||
use ruff_python_ast::{Expr, ExprRef, name::Name}; | ||
use ruff_source_file::LineIndex; | ||
|
||
use crate::Db; | ||
use crate::module_name::ModuleName; | ||
use crate::module_resolver::{KnownModule, Module, resolve_module}; | ||
use crate::semantic_index::ast_ids::HasScopedUseId; | ||
use crate::semantic_index::definition::Definition; | ||
use crate::semantic_index::place::FileScopeId; | ||
use crate::semantic_index::semantic_index; | ||
use crate::types::ide_support::all_declarations_and_bindings; | ||
|
@@ -175,6 +177,41 @@ pub struct Completion { | |
pub builtin: bool, | ||
} | ||
|
||
pub trait HasDefinition { | ||
/// Returns the definitions of `self`. | ||
/// | ||
/// ## Panics | ||
/// May panic if `self` is from another file than `model`. | ||
fn definitions<'db>(&self, model: &SemanticModel<'db>) -> Option<Vec<Definition<'db>>>; | ||
} | ||
|
||
impl HasDefinition for ast::ExprRef<'_> { | ||
fn definitions<'db>(&self, model: &SemanticModel<'db>) -> Option<Vec<Definition<'db>>> { | ||
match self { | ||
ExprRef::Name(name) => match name.ctx { | ||
ExprContext::Load => { | ||
let index = semantic_index(model.db, model.file); | ||
let file_scope = index.expression_scope_id(*self); | ||
let scope = file_scope.to_scope_id(model.db, model.file); | ||
let use_def = index.use_def_map(file_scope); | ||
let use_id = self.scoped_use_id(model.db, scope); | ||
|
||
Some( | ||
use_def | ||
.bindings_at_use(use_id) | ||
.filter_map(|binding| binding.binding.definition()) | ||
.collect(), | ||
) | ||
} | ||
ExprContext::Store => None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might a bit tricky because the definition that corresponds to this expression itself should also be included in the list of definitions. For example, here both definitions should be included: def _():
x = 1
x<CURSOR> = 2 |
||
ExprContext::Del => None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can include def _():
x = 1
del x<CURSOR> |
||
ExprContext::Invalid => None, | ||
}, | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
pub trait HasType { | ||
/// Returns the inferred type of `self`. | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::borrow::Cow; | ||
|
||
use lsp_types::GotoDefinitionParams; | ||
use lsp_types::request::GotoDefinition; | ||
use lsp_types::{GotoDefinitionResponse, Url}; | ||
use ruff_db::source::{line_index, source_text}; | ||
use ty_ide::goto_definition; | ||
use ty_project::ProjectDatabase; | ||
|
||
use crate::DocumentSnapshot; | ||
use crate::document::{PositionExt, ToLink}; | ||
use crate::server::api::traits::{ | ||
BackgroundDocumentRequestHandler, RequestHandler, RetriableRequestHandler, | ||
}; | ||
use crate::session::client::Client; | ||
|
||
pub(crate) struct GotoDefinitionRequestHandler; | ||
|
||
impl RequestHandler for GotoDefinitionRequestHandler { | ||
type RequestType = GotoDefinition; | ||
} | ||
Comment on lines
+17
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything in this file is a copy of goto_type_definition but changed to goto_definition |
||
|
||
impl BackgroundDocumentRequestHandler for GotoDefinitionRequestHandler { | ||
fn document_url(params: &GotoDefinitionParams) -> Cow<Url> { | ||
Cow::Borrowed(¶ms.text_document_position_params.text_document.uri) | ||
} | ||
|
||
fn run_with_snapshot( | ||
db: &ProjectDatabase, | ||
snapshot: DocumentSnapshot, | ||
_client: &Client, | ||
params: GotoDefinitionParams, | ||
) -> crate::server::Result<Option<GotoDefinitionResponse>> { | ||
if snapshot.client_settings().is_language_services_disabled() { | ||
return Ok(None); | ||
} | ||
|
||
let Some(file) = snapshot.file(db) else { | ||
tracing::debug!("Failed to resolve file for {:?}", params); | ||
return Ok(None); | ||
}; | ||
|
||
let source = source_text(db, file); | ||
let line_index = line_index(db, file); | ||
let offset = params.text_document_position_params.position.to_text_size( | ||
&source, | ||
&line_index, | ||
snapshot.encoding(), | ||
); | ||
|
||
let Some(ranged) = goto_definition(db, file, offset) else { | ||
return Ok(None); | ||
}; | ||
|
||
if snapshot | ||
.resolved_client_capabilities() | ||
.type_definition_link_support | ||
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the You'll need to add a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm what distinguishes this from goto_type_definition, which has the same impl? (or is this a "also fix goto_type_definition"?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are the client capabilities, so if a client has link support for goto type definition but not for goto definition (for whatever reason), the server needs to respect that. These capabilities are provided to the server during initialization where we make note of all the things that the client supports through which the server needs to make certain decisions. This is one of them. |
||
{ | ||
let src = Some(ranged.range); | ||
let links: Vec<_> = ranged | ||
.into_iter() | ||
.filter_map(|target| target.to_link(db, src, snapshot.encoding())) | ||
.collect(); | ||
|
||
Ok(Some(GotoDefinitionResponse::Link(links))) | ||
} else { | ||
let locations: Vec<_> = ranged | ||
.into_iter() | ||
.filter_map(|target| target.to_location(db, snapshot.encoding())) | ||
.collect(); | ||
|
||
Ok(Some(GotoDefinitionResponse::Array(locations))) | ||
} | ||
} | ||
} | ||
|
||
impl RetriableRequestHandler for GotoDefinitionRequestHandler {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh hmm, I see what you mean based on what you mentioned on Discord. So, this implementation would be limited to only provide definitions if they're defined in the current scope, right? The following doesn't work:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes correct.