2019-09-30 08:58:53 +00:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
use hir::{Crate, ImplBlock, Semantics};
|
2020-02-06 11:52:32 +00:00
|
|
|
use ra_ide_db::RootDatabase;
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2020-02-06 11:52:32 +00:00
|
|
|
use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo};
|
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
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
let krate = sema.to_module_def(position.file_id)?.krate();
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2019-07-19 09:56:47 +00:00
|
|
|
if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) {
|
2019-01-31 23:34:52 +00:00
|
|
|
return Some(RangeInfo::new(
|
2019-07-20 09:58:27 +00:00
|
|
|
nominal_def.syntax().text_range(),
|
2020-02-18 17:35:10 +00:00
|
|
|
impls_for_def(&sema, &nominal_def, krate)?,
|
2019-01-31 23:34:52 +00:00
|
|
|
));
|
2019-07-19 09:56:47 +00:00
|
|
|
} else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) {
|
2019-01-31 23:34:52 +00:00
|
|
|
return Some(RangeInfo::new(
|
2019-07-20 09:58:27 +00:00
|
|
|
trait_def.syntax().text_range(),
|
2020-02-18 17:35:10 +00:00
|
|
|
impls_for_trait(&sema, &trait_def, krate)?,
|
2019-01-31 23:34:52 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impls_for_def(
|
2020-02-18 17:35:10 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2019-01-31 23:34:52 +00:00
|
|
|
node: &ast::NominalDef,
|
2020-01-16 16:33:07 +00:00
|
|
|
krate: Crate,
|
2019-01-31 23:34:52 +00:00
|
|
|
) -> Option<Vec<NavigationTarget>> {
|
2019-08-19 11:13:58 +00:00
|
|
|
let ty = match node {
|
2020-02-18 17:35:10 +00:00
|
|
|
ast::NominalDef::StructDef(def) => sema.to_def(def)?.ty(sema.db),
|
|
|
|
ast::NominalDef::EnumDef(def) => sema.to_def(def)?.ty(sema.db),
|
|
|
|
ast::NominalDef::UnionDef(def) => sema.to_def(def)?.ty(sema.db),
|
2019-01-28 14:26:32 +00:00
|
|
|
};
|
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
let impls = ImplBlock::all_in_crate(sema.db, krate);
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2019-01-31 23:34:52 +00:00
|
|
|
Some(
|
|
|
|
impls
|
2019-11-26 12:27:33 +00:00
|
|
|
.into_iter()
|
2020-02-18 17:35:10 +00:00
|
|
|
.filter(|impl_block| ty.is_equal_for_find_impls(&impl_block.target_ty(sema.db)))
|
|
|
|
.map(|imp| imp.to_nav(sema.db))
|
2019-01-31 23:34:52 +00:00
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impls_for_trait(
|
2020-02-18 17:35:10 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2019-01-31 23:34:52 +00:00
|
|
|
node: &ast::TraitDef,
|
2020-01-16 16:33:07 +00:00
|
|
|
krate: Crate,
|
2019-01-31 23:34:52 +00:00
|
|
|
) -> Option<Vec<NavigationTarget>> {
|
2020-02-18 17:35:10 +00:00
|
|
|
let tr = sema.to_def(node)?;
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
let impls = ImplBlock::for_trait(sema.db, krate, tr);
|
2019-01-31 23:34:52 +00:00
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
Some(impls.into_iter().map(|imp| imp.to_nav(sema.db)).collect())
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::mock_analysis::analysis_and_position;
|
|
|
|
|
2019-02-11 16:18:27 +00:00
|
|
|
fn check_goto(fixture: &str, expected: &[&str]) {
|
|
|
|
let (analysis, pos) = analysis_and_position(fixture);
|
2019-01-28 14:26:32 +00:00
|
|
|
|
2019-03-14 10:27:21 +00:00
|
|
|
let mut navs = analysis.goto_implementation(pos).unwrap().unwrap().info;
|
2019-01-28 14:26:32 +00:00
|
|
|
assert_eq!(navs.len(), expected.len());
|
2019-03-14 10:27:21 +00:00
|
|
|
navs.sort_by_key(|nav| (nav.file_id(), nav.full_range().start()));
|
2019-02-08 11:49:43 +00:00
|
|
|
navs.into_iter().enumerate().for_each(|(i, nav)| nav.assert_match(expected[i]));
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo<|>;
|
|
|
|
impl Foo {}
|
|
|
|
",
|
|
|
|
&["impl IMPL_BLOCK FileId(1) [12; 23)"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_blocks() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo<|>;
|
|
|
|
impl Foo {}
|
|
|
|
impl Foo {}
|
|
|
|
",
|
2019-02-08 11:49:43 +00:00
|
|
|
&["impl IMPL_BLOCK FileId(1) [12; 23)", "impl IMPL_BLOCK FileId(1) [24; 35)"],
|
2019-01-28 14:26:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_mods() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo<|>;
|
|
|
|
mod a {
|
|
|
|
impl super::Foo {}
|
|
|
|
}
|
|
|
|
mod b {
|
|
|
|
impl super::Foo {}
|
|
|
|
}
|
|
|
|
",
|
2019-02-08 11:49:43 +00:00
|
|
|
&["impl IMPL_BLOCK FileId(1) [24; 42)", "impl IMPL_BLOCK FileId(1) [57; 75)"],
|
2019-01-28 14:26:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_works_multiple_files() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo<|>;
|
|
|
|
mod a;
|
|
|
|
mod b;
|
|
|
|
//- /a.rs
|
|
|
|
impl crate::Foo {}
|
|
|
|
//- /b.rs
|
|
|
|
impl crate::Foo {}
|
|
|
|
",
|
2019-02-08 11:49:43 +00:00
|
|
|
&["impl IMPL_BLOCK FileId(2) [0; 18)", "impl IMPL_BLOCK FileId(3) [0; 18)"],
|
2019-01-28 14:26:32 +00:00
|
|
|
);
|
|
|
|
}
|
2019-01-31 23:34:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_for_trait() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
trait T<|> {}
|
|
|
|
struct Foo;
|
|
|
|
impl T for Foo {}
|
|
|
|
",
|
|
|
|
&["impl IMPL_BLOCK FileId(1) [23; 40)"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_for_trait_multiple_files() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
trait T<|> {};
|
|
|
|
struct Foo;
|
|
|
|
mod a;
|
|
|
|
mod b;
|
|
|
|
//- /a.rs
|
|
|
|
impl crate::T for crate::Foo {}
|
|
|
|
//- /b.rs
|
|
|
|
impl crate::T for crate::Foo {}
|
|
|
|
",
|
2019-02-08 11:49:43 +00:00
|
|
|
&["impl IMPL_BLOCK FileId(2) [0; 31)", "impl IMPL_BLOCK FileId(3) [0; 31)"],
|
2019-01-31 23:34:52 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-16 13:34:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_all_impls() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
trait T {}
|
|
|
|
struct Foo<|>;
|
|
|
|
impl Foo {}
|
|
|
|
impl T for Foo {}
|
|
|
|
impl T for &Foo {}
|
|
|
|
",
|
|
|
|
&[
|
|
|
|
"impl IMPL_BLOCK FileId(1) [23; 34)",
|
|
|
|
"impl IMPL_BLOCK FileId(1) [35; 52)",
|
|
|
|
"impl IMPL_BLOCK FileId(1) [53; 71)",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2020-01-12 11:24:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_implementation_to_builtin_derive() {
|
|
|
|
check_goto(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
#[derive(Copy)]
|
2020-01-16 15:08:46 +00:00
|
|
|
struct Foo<|>;
|
2020-01-12 11:24:34 +00:00
|
|
|
",
|
|
|
|
&["impl IMPL_BLOCK FileId(1) [0; 15)"],
|
|
|
|
);
|
|
|
|
}
|
2019-01-28 14:26:32 +00:00
|
|
|
}
|