mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 14:03:35 +00:00
Merge #10108
10108: fix: make `goto_implementation` multi-token mapping aware r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
1b8a294555
2 changed files with 66 additions and 47 deletions
|
@ -1,9 +1,11 @@
|
||||||
use hir::{AsAssocItem, Impl, Semantics};
|
use hir::{AsAssocItem, Impl, Semantics};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
defs::{Definition, NameClass, NameRefClass},
|
defs::{Definition, NameClass, NameRefClass},
|
||||||
|
helpers::pick_best_token,
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use syntax::{ast, AstNode};
|
use itertools::Itertools;
|
||||||
|
use syntax::{ast, AstNode, SyntaxKind::*, T};
|
||||||
|
|
||||||
use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
|
use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
|
||||||
|
|
||||||
|
@ -26,29 +28,43 @@ pub(crate) fn goto_implementation(
|
||||||
let source_file = sema.parse(position.file_id);
|
let source_file = sema.parse(position.file_id);
|
||||||
let syntax = source_file.syntax().clone();
|
let syntax = source_file.syntax().clone();
|
||||||
|
|
||||||
let node = sema.find_node_at_offset_with_descend(&syntax, position.offset)?;
|
let original_token =
|
||||||
|
pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
|
||||||
|
IDENT | T![self] => 1,
|
||||||
|
_ => 0,
|
||||||
|
})?;
|
||||||
|
let range = original_token.text_range();
|
||||||
|
let navs =
|
||||||
|
sema.descend_into_macros_many(original_token)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|token| token.parent().and_then(ast::NameLike::cast))
|
||||||
|
.filter_map(|node| {
|
||||||
let def = match &node {
|
let def = match &node {
|
||||||
ast::NameLike::Name(name) => NameClass::classify(&sema, name).map(|class| match class {
|
ast::NameLike::Name(name) => {
|
||||||
|
NameClass::classify(&sema, name).map(|class| match class {
|
||||||
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
||||||
NameClass::PatFieldShorthand { local_def, field_ref: _ } => {
|
NameClass::PatFieldShorthand { local_def, field_ref: _ } => {
|
||||||
Definition::Local(local_def)
|
Definition::Local(local_def)
|
||||||
}
|
}
|
||||||
}),
|
})
|
||||||
ast::NameLike::NameRef(name_ref) => {
|
}
|
||||||
NameRefClass::classify(&sema, name_ref).map(|class| match class {
|
ast::NameLike::NameRef(name_ref) => NameRefClass::classify(&sema, name_ref)
|
||||||
|
.map(|class| match class {
|
||||||
NameRefClass::Definition(def) => def,
|
NameRefClass::Definition(def) => def,
|
||||||
NameRefClass::FieldShorthand { local_ref, field_ref: _ } => {
|
NameRefClass::FieldShorthand { local_ref, field_ref: _ } => {
|
||||||
Definition::Local(local_ref)
|
Definition::Local(local_ref)
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
}
|
|
||||||
ast::NameLike::Lifetime(_) => None,
|
ast::NameLike::Lifetime(_) => None,
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let def = match def {
|
match def {
|
||||||
Definition::ModuleDef(def) => def,
|
Definition::ModuleDef(def) => Some(def),
|
||||||
_ => return None,
|
_ => None,
|
||||||
};
|
}
|
||||||
|
})
|
||||||
|
.unique()
|
||||||
|
.filter_map(|def| {
|
||||||
let navs = match def {
|
let navs = match def {
|
||||||
hir::ModuleDef::Trait(trait_) => impls_for_trait(&sema, trait_),
|
hir::ModuleDef::Trait(trait_) => impls_for_trait(&sema, trait_),
|
||||||
hir::ModuleDef::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
|
hir::ModuleDef::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
|
||||||
|
@ -71,7 +87,12 @@ pub(crate) fn goto_implementation(
|
||||||
}
|
}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
Some(RangeInfo { range: node.syntax().text_range(), info: navs })
|
Some(navs)
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Some(RangeInfo { range, info: navs })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn impls_for_ty(sema: &Semantics<RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
|
fn impls_for_ty(sema: &Semantics<RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
|
||||||
|
|
|
@ -442,9 +442,7 @@ impl<'a> FindUsages<'a> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ast::NameLike::NameRef(name_ref)) =
|
for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) {
|
||||||
sema.find_node_at_offset_with_descend(&tree, offset)
|
|
||||||
{
|
|
||||||
if self.found_self_module_name_ref(&name_ref, sink) {
|
if self.found_self_module_name_ref(&name_ref, sink) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue