2020-12-17 05:36:15 -06:00
|
|
|
use hir::{Crate, Impl, Semantics};
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::RootDatabase;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{algo::find_node_at_offset, ast, AstNode};
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
|
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
|
|
|
//
|
|
|
|
// Navigates to the impl block of structs, enums or traits. Also implemented as a code lens.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Shortcut
|
|
|
|
//
|
|
|
|
// | VS Code | kbd:[Ctrl+F12]
|
|
|
|
// |===
|
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
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
let krate = sema.to_module_def(position.file_id)?.krate();
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2020-07-29 12:22:15 -05:00
|
|
|
if let Some(nominal_def) = find_node_at_offset::<ast::AdtDef>(&syntax, position.offset) {
|
2019-01-31 17:34:52 -06:00
|
|
|
return Some(RangeInfo::new(
|
2019-07-20 04:58:27 -05:00
|
|
|
nominal_def.syntax().text_range(),
|
2020-02-18 11:35:10 -06:00
|
|
|
impls_for_def(&sema, &nominal_def, krate)?,
|
2019-01-31 17:34:52 -06:00
|
|
|
));
|
2020-07-30 11:17:28 -05:00
|
|
|
} else if let Some(trait_def) = find_node_at_offset::<ast::Trait>(&syntax, position.offset) {
|
2019-01-31 17:34:52 -06:00
|
|
|
return Some(RangeInfo::new(
|
2019-07-20 04:58:27 -05:00
|
|
|
trait_def.syntax().text_range(),
|
2020-02-18 11:35:10 -06:00
|
|
|
impls_for_trait(&sema, &trait_def, krate)?,
|
2019-01-31 17:34:52 -06:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impls_for_def(
|
2020-02-18 11:35:10 -06:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-07-29 12:22:15 -05:00
|
|
|
node: &ast::AdtDef,
|
2020-01-16 10:33:07 -06:00
|
|
|
krate: Crate,
|
2019-01-31 17:34:52 -06:00
|
|
|
) -> Option<Vec<NavigationTarget>> {
|
2019-08-19 06:13:58 -05:00
|
|
|
let ty = match node {
|
2020-07-30 10:50:40 -05:00
|
|
|
ast::AdtDef::Struct(def) => sema.to_def(def)?.ty(sema.db),
|
2020-07-30 10:52:53 -05:00
|
|
|
ast::AdtDef::Enum(def) => sema.to_def(def)?.ty(sema.db),
|
2020-07-30 10:36:46 -05:00
|
|
|
ast::AdtDef::Union(def) => sema.to_def(def)?.ty(sema.db),
|
2019-01-28 08:26:32 -06:00
|
|
|
};
|
|
|
|
|
2020-12-17 05:36:15 -06:00
|
|
|
let impls = Impl::all_in_crate(sema.db, krate);
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2019-01-31 17:34:52 -06:00
|
|
|
Some(
|
|
|
|
impls
|
2019-11-26 06:27:33 -06:00
|
|
|
.into_iter()
|
2020-02-29 14:24:40 -06:00
|
|
|
.filter(|impl_def| ty.is_equal_for_find_impls(&impl_def.target_ty(sema.db)))
|
2021-01-01 00:13:15 -06:00
|
|
|
.filter_map(|imp| imp.try_to_nav(sema.db))
|
2019-01-31 17:34:52 -06:00
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impls_for_trait(
|
2020-02-18 11:35:10 -06:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-07-30 11:17:28 -05:00
|
|
|
node: &ast::Trait,
|
2020-01-16 10:33:07 -06:00
|
|
|
krate: Crate,
|
2019-01-31 17:34:52 -06:00
|
|
|
) -> Option<Vec<NavigationTarget>> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let tr = sema.to_def(node)?;
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2020-12-17 05:36:15 -06:00
|
|
|
let impls = Impl::for_trait(sema.db, krate, tr);
|
2019-01-31 17:34:52 -06:00
|
|
|
|
2021-01-01 00:13:15 -06:00
|
|
|
Some(impls.into_iter().filter_map(|imp| imp.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;
|
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) {
|
2020-10-02 10:34:31 -05:00
|
|
|
let (analysis, position, annotations) = fixture::annotations(ra_fixture);
|
2020-06-30 06:20:16 -05: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 05:42:48 -05:00
|
|
|
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
|
2020-06-30 06:20:16 -05:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
actual.sort_by_key(key);
|
|
|
|
|
|
|
|
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#"
|
|
|
|
#[derive(Copy)]
|
|
|
|
//^^^^^^^^^^^^^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0;
|
2020-07-01 10:15:20 -05:00
|
|
|
|
|
|
|
mod marker {
|
|
|
|
trait Copy {}
|
|
|
|
}
|
2020-12-15 13:33:05 -06:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro Copy {}
|
2020-06-30 06:20:16 -05:00
|
|
|
"#,
|
2020-01-12 05:24:34 -06:00
|
|
|
);
|
|
|
|
}
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|