2021-05-25 12:46:15 +00:00
|
|
|
use hir::{AsAssocItem, Impl, Semantics};
|
2021-03-15 09:11:48 +00:00
|
|
|
use ide_db::{
|
|
|
|
defs::{Definition, NameClass, NameRefClass},
|
2021-09-01 12:51:37 +00:00
|
|
|
helpers::pick_best_token,
|
2021-03-15 09:11:48 +00:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-09-01 12:51:37 +00:00
|
|
|
use itertools::Itertools;
|
|
|
|
use syntax::{ast, AstNode, SyntaxKind::*, T};
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2021-11-22 12:04:28 +00:00
|
|
|
use crate::{FilePosition, NavigationTarget, RangeInfo, TryToNav};
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2020-05-31 07:45:41 +00:00
|
|
|
// Feature: Go to Implementation
|
2020-05-30 23:54:54 +00:00
|
|
|
//
|
2022-05-27 13:47:31 +00:00
|
|
|
// Navigates to the impl blocks of types.
|
2020-05-30 23:54:54 +00:00
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Shortcut
|
|
|
|
//
|
|
|
|
// | VS Code | kbd:[Ctrl+F12]
|
|
|
|
// |===
|
2021-03-30 23:08:10 +00:00
|
|
|
//
|
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113065566-02f85480-91b1-11eb-9288-aaad8abd8841.gif[]
|
2019-01-28 14:26:32 +00:00
|
|
|
pub(crate) fn goto_implementation(
|
|
|
|
db: &RootDatabase,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
2020-02-18 17:35:10 +00:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let source_file = sema.parse(position.file_id);
|
|
|
|
let syntax = source_file.syntax().clone();
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2021-09-01 12:51:37 +00:00
|
|
|
let original_token =
|
|
|
|
pick_best_token(syntax.token_at_offset(position.offset), |kind| match kind {
|
2022-08-05 12:59:26 +00:00
|
|
|
IDENT | T![self] | INT_NUMBER => 1,
|
2021-09-01 12:51:37 +00:00
|
|
|
_ => 0,
|
|
|
|
})?;
|
|
|
|
let range = original_token.text_range();
|
2021-11-10 21:02:50 +00:00
|
|
|
let navs = sema
|
|
|
|
.descend_into_macros(original_token)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|token| token.parent().and_then(ast::NameLike::cast))
|
|
|
|
.filter_map(|node| match &node {
|
|
|
|
ast::NameLike::Name(name) => {
|
|
|
|
NameClass::classify(&sema, name).map(|class| match class {
|
|
|
|
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
|
|
|
NameClass::PatFieldShorthand { local_def, field_ref: _ } => {
|
|
|
|
Definition::Local(local_def)
|
2021-09-01 12:51:37 +00:00
|
|
|
}
|
2021-11-10 21:02:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
ast::NameLike::NameRef(name_ref) => {
|
|
|
|
NameRefClass::classify(&sema, name_ref).map(|class| match class {
|
|
|
|
NameRefClass::Definition(def) => def,
|
|
|
|
NameRefClass::FieldShorthand { local_ref, field_ref: _ } => {
|
|
|
|
Definition::Local(local_ref)
|
2021-09-01 12:51:37 +00:00
|
|
|
}
|
2021-11-10 21:02:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
ast::NameLike::Lifetime(_) => None,
|
|
|
|
})
|
|
|
|
.unique()
|
|
|
|
.filter_map(|def| {
|
|
|
|
let navs = match def {
|
|
|
|
Definition::Trait(trait_) => impls_for_trait(&sema, trait_),
|
|
|
|
Definition::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
|
|
|
|
Definition::TypeAlias(alias) => impls_for_ty(&sema, alias.ty(sema.db)),
|
2022-03-26 20:22:35 +00:00
|
|
|
Definition::BuiltinType(builtin) => impls_for_ty(&sema, builtin.ty(sema.db)),
|
2021-11-10 21:02:50 +00:00
|
|
|
Definition::Function(f) => {
|
|
|
|
let assoc = f.as_assoc_item(sema.db)?;
|
|
|
|
let name = assoc.name(sema.db)?;
|
|
|
|
let trait_ = assoc.containing_trait_or_trait_impl(sema.db)?;
|
|
|
|
impls_for_trait_item(&sema, trait_, name)
|
|
|
|
}
|
|
|
|
Definition::Const(c) => {
|
|
|
|
let assoc = c.as_assoc_item(sema.db)?;
|
|
|
|
let name = assoc.name(sema.db)?;
|
|
|
|
let trait_ = assoc.containing_trait_or_trait_impl(sema.db)?;
|
|
|
|
impls_for_trait_item(&sema, trait_, name)
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
Some(navs)
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect();
|
2021-05-25 12:46:15 +00:00
|
|
|
|
2021-09-01 12:51:37 +00:00
|
|
|
Some(RangeInfo { range, info: navs })
|
2019-01-31 23:34:52 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 13:02:08 +00:00
|
|
|
fn impls_for_ty(sema: &Semantics<'_, RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
|
2021-03-15 09:11:48 +00:00
|
|
|
Impl::all_for_type(sema.db, ty).into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect()
|
|
|
|
}
|
2019-01-31 23:34:52 +00:00
|
|
|
|
2022-07-20 13:06:15 +00:00
|
|
|
fn impls_for_trait(
|
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
trait_: hir::Trait,
|
|
|
|
) -> Vec<NavigationTarget> {
|
2021-03-15 09:11:48 +00:00
|
|
|
Impl::all_for_trait(sema.db, trait_)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|imp| imp.try_to_nav(sema.db))
|
|
|
|
.collect()
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 13:27:41 +00:00
|
|
|
fn impls_for_trait_item(
|
2022-07-20 13:02:08 +00:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-05-25 12:46:15 +00:00
|
|
|
trait_: hir::Trait,
|
|
|
|
fun_name: hir::Name,
|
|
|
|
) -> Vec<NavigationTarget> {
|
|
|
|
Impl::all_for_trait(sema.db, trait_)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|imp| {
|
|
|
|
let item = imp.items(sema.db).iter().find_map(|itm| {
|
|
|
|
let itm_name = itm.name(sema.db)?;
|
2022-12-30 08:30:23 +00:00
|
|
|
(itm_name == fun_name).then_some(*itm)
|
2021-05-25 12:46:15 +00:00
|
|
|
})?;
|
|
|
|
item.try_to_nav(sema.db)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:26:32 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-24 08:39:57 +00:00
|
|
|
use ide_db::base_db::FileRange;
|
2021-07-15 19:28:30 +00:00
|
|
|
use itertools::Itertools;
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2020-10-02 15:34:31 +00:00
|
|
|
use crate::fixture;
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2020-06-30 11:20:16 +00:00
|
|
|
fn check(ra_fixture: &str) {
|
2021-07-15 19:28:30 +00:00
|
|
|
let (analysis, position, expected) = fixture::annotations(ra_fixture);
|
2020-06-30 11:20:16 +00:00
|
|
|
|
|
|
|
let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
|
|
|
|
|
2021-07-15 19:28:30 +00:00
|
|
|
let cmp = |frange: &FileRange| (frange.file_id, frange.range.start());
|
2020-06-30 11:20:16 +00:00
|
|
|
|
2021-07-15 19:28:30 +00:00
|
|
|
let actual = navs
|
2020-06-30 11:20:16 +00:00
|
|
|
.into_iter()
|
2020-07-17 10:42:48 +00:00
|
|
|
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
|
2021-07-15 19:28:30 +00:00
|
|
|
.sorted_by_key(cmp)
|
2020-06-30 11:20:16 +00:00
|
|
|
.collect::<Vec<_>>();
|
2021-07-15 19:28:30 +00:00
|
|
|
let expected =
|
|
|
|
expected.into_iter().map(|(range, _)| range).sorted_by_key(cmp).collect::<Vec<_>>();
|
2020-06-30 11:20:16 +00:00
|
|
|
assert_eq!(expected, actual);
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo$0;
|
2020-06-30 11:20:16 +00:00
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-01-28 14:26:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_blocks() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo$0;
|
2020-06-30 11:20:16 +00:00
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-01-28 14:26:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_mods() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo$0;
|
2020-06-30 11:20:16 +00:00
|
|
|
mod a {
|
|
|
|
impl super::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
}
|
|
|
|
mod b {
|
|
|
|
impl super::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-28 14:26:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_files() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo$0;
|
2020-06-30 11:20:16 +00:00
|
|
|
mod a;
|
|
|
|
mod b;
|
|
|
|
//- /a.rs
|
|
|
|
impl crate::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
//- /b.rs
|
|
|
|
impl crate::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
"#,
|
2019-01-28 14:26:32 +00:00
|
|
|
);
|
|
|
|
}
|
2019-01-31 23:34:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_for_trait() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
trait T$0 {}
|
2020-06-30 11:20:16 +00:00
|
|
|
struct Foo;
|
|
|
|
impl T for Foo {}
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-01-31 23:34:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_for_trait_multiple_files() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 20:15:48 +00:00
|
|
|
trait T$0 {};
|
2020-06-30 11:20:16 +00:00
|
|
|
struct Foo;
|
|
|
|
mod a;
|
|
|
|
mod b;
|
|
|
|
//- /a.rs
|
|
|
|
impl crate::T for crate::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
//- /b.rs
|
|
|
|
impl crate::T for crate::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
"#,
|
2019-01-31 23:34:52 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-16 13:34:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_all_impls() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
trait T {}
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo$0;
|
2020-06-30 11:20:16 +00:00
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
impl T for Foo {}
|
|
|
|
//^^^
|
|
|
|
impl T for &Foo {}
|
|
|
|
//^^^^
|
|
|
|
"#,
|
2019-08-16 13:34:47 +00:00
|
|
|
);
|
|
|
|
}
|
2020-01-12 11:24:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_to_builtin_derive() {
|
2020-06-30 11:20:16 +00:00
|
|
|
check(
|
|
|
|
r#"
|
2021-06-19 09:03:59 +00:00
|
|
|
//- minicore: copy, derive
|
2020-06-30 11:20:16 +00:00
|
|
|
#[derive(Copy)]
|
|
|
|
//^^^^^^^^^^^^^^^
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo$0;
|
2021-03-15 09:11:48 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_type_alias() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
type Bar$0 = Foo;
|
|
|
|
|
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
impl Bar {}
|
|
|
|
//^^^
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_adt_generic() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo$0<T>;
|
|
|
|
|
|
|
|
impl<T> Foo<T> {}
|
|
|
|
//^^^^^^
|
|
|
|
impl Foo<str> {}
|
|
|
|
//^^^^^^^^
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_builtin() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs crate:main deps:core
|
|
|
|
fn foo(_: bool$0) {{}}
|
|
|
|
//- /libcore.rs crate:core
|
2023-03-14 18:36:28 +00:00
|
|
|
#![rustc_coherence_is_core]
|
2021-03-15 09:11:48 +00:00
|
|
|
#[lang = "bool"]
|
|
|
|
impl bool {}
|
|
|
|
//^^^^
|
2021-05-25 12:46:15 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_trait_functions() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
fn f$0();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Tr for S {
|
|
|
|
fn f() {
|
|
|
|
//^
|
|
|
|
println!("Hello, world!");
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 13:27:41 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_trait_assoc_const() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
const C$0: usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Tr for S {
|
|
|
|
const C: usize = 4;
|
|
|
|
//^
|
|
|
|
}
|
2020-06-30 11:20:16 +00:00
|
|
|
"#,
|
2020-01-12 11:24:34 +00:00
|
|
|
);
|
|
|
|
}
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|