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},
|
|
|
|
RootDatabase,
|
|
|
|
};
|
|
|
|
use syntax::{ast, AstNode};
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2021-01-01 06:13:15 +00:00
|
|
|
use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
|
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
|
|
|
//
|
|
|
|
// Navigates to the impl block of structs, enums or traits. Also implemented as a code lens.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | 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-03-15 09:11:48 +00:00
|
|
|
let node = sema.find_node_at_offset_with_descend(&syntax, position.offset)?;
|
|
|
|
let def = match &node {
|
|
|
|
ast::NameLike::Name(name) => {
|
|
|
|
NameClass::classify(&sema, name).map(|class| class.referenced_or_defined(sema.db))
|
|
|
|
}
|
|
|
|
ast::NameLike::NameRef(name_ref) => {
|
|
|
|
NameRefClass::classify(&sema, name_ref).map(|class| class.referenced(sema.db))
|
|
|
|
}
|
|
|
|
ast::NameLike::Lifetime(_) => None,
|
|
|
|
}?;
|
2021-05-25 12:46:15 +00:00
|
|
|
|
2021-03-15 09:11:48 +00:00
|
|
|
let def = match def {
|
|
|
|
Definition::ModuleDef(def) => def,
|
|
|
|
_ => return None,
|
2019-01-28 14:26:32 +00:00
|
|
|
};
|
2021-03-15 09:11:48 +00:00
|
|
|
let navs = match def {
|
|
|
|
hir::ModuleDef::Trait(trait_) => impls_for_trait(&sema, trait_),
|
|
|
|
hir::ModuleDef::Adt(adt) => impls_for_ty(&sema, adt.ty(sema.db)),
|
|
|
|
hir::ModuleDef::TypeAlias(alias) => impls_for_ty(&sema, alias.ty(sema.db)),
|
|
|
|
hir::ModuleDef::BuiltinType(builtin) => {
|
|
|
|
let module = sema.to_module_def(position.file_id)?;
|
|
|
|
impls_for_ty(&sema, builtin.ty(sema.db, module))
|
|
|
|
}
|
2021-05-25 12:46:15 +00:00
|
|
|
hir::ModuleDef::Function(f) => {
|
|
|
|
let assoc = f.as_assoc_item(sema.db)?;
|
|
|
|
let name = assoc.name(sema.db)?;
|
|
|
|
let trait_ = assoc.containing_trait(sema.db)?;
|
|
|
|
impls_for_trait_fn(&sema, trait_, name)
|
|
|
|
}
|
2021-03-15 09:11:48 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
Some(RangeInfo { range: node.syntax().text_range(), info: navs })
|
2019-01-31 23:34:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:11:48 +00:00
|
|
|
fn impls_for_ty(sema: &Semantics<RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
|
|
|
|
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
|
|
|
|
2021-03-15 09:11:48 +00:00
|
|
|
fn impls_for_trait(sema: &Semantics<RootDatabase>, trait_: hir::Trait) -> Vec<NavigationTarget> {
|
|
|
|
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 12:46:15 +00:00
|
|
|
fn impls_for_trait_fn(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
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)?;
|
|
|
|
(itm_name == fun_name).then(|| itm.clone())
|
|
|
|
})?;
|
|
|
|
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;
|
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) {
|
2020-10-02 15:34:31 +00:00
|
|
|
let (analysis, position, annotations) = fixture::annotations(ra_fixture);
|
2020-06-30 11:20:16 +00:00
|
|
|
|
|
|
|
let navs = analysis.goto_implementation(position).unwrap().unwrap().info;
|
|
|
|
|
|
|
|
let key = |frange: &FileRange| (frange.file_id, frange.range.start());
|
|
|
|
|
|
|
|
let mut expected = annotations
|
|
|
|
.into_iter()
|
|
|
|
.map(|(range, data)| {
|
|
|
|
assert!(data.is_empty());
|
|
|
|
range
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
expected.sort_by_key(key);
|
|
|
|
|
|
|
|
let mut actual = navs
|
|
|
|
.into_iter()
|
2020-07-17 10:42:48 +00:00
|
|
|
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
|
2020-06-30 11:20:16 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
actual.sort_by_key(key);
|
|
|
|
|
|
|
|
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#"
|
|
|
|
#[derive(Copy)]
|
|
|
|
//^^^^^^^^^^^^^^^
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo$0;
|
2020-07-01 15:15:20 +00:00
|
|
|
|
|
|
|
mod marker {
|
|
|
|
trait Copy {}
|
|
|
|
}
|
2020-12-15 19:33:05 +00:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro Copy {}
|
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
|
|
|
|
#[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!");
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 11:20:16 +00:00
|
|
|
"#,
|
2020-01-12 11:24:34 +00:00
|
|
|
);
|
|
|
|
}
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|