2021-05-25 07:46:15 -05:00
|
|
|
use hir::{AsAssocItem, Impl, Semantics};
|
2021-03-15 04:11:48 -05:00
|
|
|
use ide_db::{
|
|
|
|
defs::{Definition, NameClass, NameRefClass},
|
2021-09-01 07:51:37 -05:00
|
|
|
helpers::pick_best_token,
|
2021-03-15 04:11:48 -05:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-09-01 07:51:37 -05:00
|
|
|
use itertools::Itertools;
|
|
|
|
use syntax::{ast, AstNode, SyntaxKind::*, T};
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2021-11-22 06:04:28 -06:00
|
|
|
use crate::{FilePosition, NavigationTarget, RangeInfo, TryToNav};
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2020-05-31 02:45:41 -05:00
|
|
|
// Feature: Go to Implementation
|
2020-05-30 18:54:54 -05:00
|
|
|
//
|
2022-05-27 08:47:31 -05:00
|
|
|
// Navigates to the impl blocks of types.
|
2020-05-30 18:54:54 -05:00
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Shortcut
|
|
|
|
//
|
|
|
|
// | VS Code | kbd:[Ctrl+F12]
|
|
|
|
// |===
|
2021-03-30 18:08:10 -05:00
|
|
|
//
|
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113065566-02f85480-91b1-11eb-9288-aaad8abd8841.gif[]
|
2019-01-28 08:26:32 -06:00
|
|
|
pub(crate) fn goto_implementation(
|
|
|
|
db: &RootDatabase,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let source_file = sema.parse(position.file_id);
|
|
|
|
let syntax = source_file.syntax().clone();
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2021-09-01 07:51:37 -05:00
|
|
|
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();
|
2021-11-10 15:02:50 -06: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 07:51:37 -05:00
|
|
|
}
|
2021-11-10 15:02:50 -06: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 07:51:37 -05:00
|
|
|
}
|
2021-11-10 15:02:50 -06: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 15:22:35 -05:00
|
|
|
Definition::BuiltinType(builtin) => impls_for_ty(&sema, builtin.ty(sema.db)),
|
2021-11-10 15:02:50 -06: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 07:46:15 -05:00
|
|
|
|
2021-09-01 07:51:37 -05:00
|
|
|
Some(RangeInfo { range, info: navs })
|
2019-01-31 17:34:52 -06:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn impls_for_ty(sema: &Semantics<'_, RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
|
2021-03-15 04:11:48 -05:00
|
|
|
Impl::all_for_type(sema.db, ty).into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect()
|
|
|
|
}
|
2019-01-31 17:34:52 -06:00
|
|
|
|
2022-07-20 08:06:15 -05:00
|
|
|
fn impls_for_trait(
|
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
trait_: hir::Trait,
|
|
|
|
) -> Vec<NavigationTarget> {
|
2021-03-15 04:11:48 -05:00
|
|
|
Impl::all_for_trait(sema.db, trait_)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|imp| imp.try_to_nav(sema.db))
|
|
|
|
.collect()
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|
|
|
|
|
2021-05-25 08:27:41 -05:00
|
|
|
fn impls_for_trait_item(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-05-25 07:46:15 -05: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)?;
|
2021-06-12 22:57:19 -05:00
|
|
|
(itm_name == fun_name).then(|| *itm)
|
2021-05-25 07:46:15 -05:00
|
|
|
})?;
|
|
|
|
item.try_to_nav(sema.db)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:26:32 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-24 03:39:57 -05:00
|
|
|
use ide_db::base_db::FileRange;
|
2021-07-15 14:28:30 -05:00
|
|
|
use itertools::Itertools;
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2020-10-02 10:34:31 -05:00
|
|
|
use crate::fixture;
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2020-06-30 06:20:16 -05:00
|
|
|
fn check(ra_fixture: &str) {
|
2021-07-15 14:28:30 -05:00
|
|
|
let (analysis, position, expected) = fixture::annotations(ra_fixture);
|
2020-06-30 06:20:16 -05:00
|
|
|
|
|
|
|
let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
|
|
|
|
|
2021-07-15 14:28:30 -05:00
|
|
|
let cmp = |frange: &FileRange| (frange.file_id, frange.range.start());
|
2020-06-30 06:20:16 -05:00
|
|
|
|
2021-07-15 14:28:30 -05:00
|
|
|
let actual = navs
|
2020-06-30 06:20:16 -05:00
|
|
|
.into_iter()
|
2020-07-17 05:42:48 -05:00
|
|
|
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
|
2021-07-15 14:28:30 -05:00
|
|
|
.sorted_by_key(cmp)
|
2020-06-30 06:20:16 -05:00
|
|
|
.collect::<Vec<_>>();
|
2021-07-15 14:28:30 -05:00
|
|
|
let expected =
|
|
|
|
expected.into_iter().map(|(range, _)| range).sorted_by_key(cmp).collect::<Vec<_>>();
|
2020-06-30 06:20:16 -05:00
|
|
|
assert_eq!(expected, actual);
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0;
|
2020-06-30 06:20:16 -05:00
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-01-28 08:26:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_blocks() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0;
|
2020-06-30 06:20:16 -05:00
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-01-28 08:26:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_mods() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0;
|
2020-06-30 06:20:16 -05:00
|
|
|
mod a {
|
|
|
|
impl super::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
}
|
|
|
|
mod b {
|
|
|
|
impl super::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-28 08:26:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_files() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0;
|
2020-06-30 06:20:16 -05:00
|
|
|
mod a;
|
|
|
|
mod b;
|
|
|
|
//- /a.rs
|
|
|
|
impl crate::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
//- /b.rs
|
|
|
|
impl crate::Foo {}
|
|
|
|
//^^^^^^^^^^
|
|
|
|
"#,
|
2019-01-28 08:26:32 -06:00
|
|
|
);
|
|
|
|
}
|
2019-01-31 17:34:52 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_for_trait() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
trait T$0 {}
|
2020-06-30 06:20:16 -05:00
|
|
|
struct Foo;
|
|
|
|
impl T for Foo {}
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-01-31 17:34:52 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_for_trait_multiple_files() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
trait T$0 {};
|
2020-06-30 06:20:16 -05: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 17:34:52 -06:00
|
|
|
);
|
|
|
|
}
|
2019-08-16 08:34:47 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_all_impls() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
trait T {}
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0;
|
2020-06-30 06:20:16 -05:00
|
|
|
impl Foo {}
|
|
|
|
//^^^
|
|
|
|
impl T for Foo {}
|
|
|
|
//^^^
|
|
|
|
impl T for &Foo {}
|
|
|
|
//^^^^
|
|
|
|
"#,
|
2019-08-16 08:34:47 -05:00
|
|
|
);
|
|
|
|
}
|
2020-01-12 05:24:34 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_to_builtin_derive() {
|
2020-06-30 06:20:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-06-19 04:03:59 -05:00
|
|
|
//- minicore: copy, derive
|
2020-06-30 06:20:16 -05:00
|
|
|
#[derive(Copy)]
|
|
|
|
//^^^^^^^^^^^^^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0;
|
2021-03-15 04:11:48 -05: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
|
|
|
|
#[lang = "bool"]
|
|
|
|
impl bool {}
|
|
|
|
//^^^^
|
2021-05-25 07:46:15 -05: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 08:27:41 -05: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 06:20:16 -05:00
|
|
|
"#,
|
2020-01-12 05:24:34 -06:00
|
|
|
);
|
|
|
|
}
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|