2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-11-26 06:27:33 -06:00
|
|
|
use hir::{ApplicationTy, FromSource, ImplBlock, Ty, TypeCtor};
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_db::SourceDatabase;
|
|
|
|
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2019-11-11 02:15:19 -06:00
|
|
|
use crate::{db::RootDatabase, display::ToNav, FilePosition, NavigationTarget, RangeInfo};
|
2019-01-28 08:26:32 -06:00
|
|
|
|
|
|
|
pub(crate) fn goto_implementation(
|
|
|
|
db: &RootDatabase,
|
|
|
|
position: FilePosition,
|
|
|
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = db.parse(position.file_id);
|
2019-07-19 04:56:47 -05:00
|
|
|
let syntax = parse.tree().syntax().clone();
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2019-09-16 05:48:54 -05:00
|
|
|
let src = hir::ModuleSource::from_position(db, position);
|
|
|
|
let module = hir::Module::from_definition(
|
|
|
|
db,
|
2019-11-20 00:40:36 -06:00
|
|
|
hir::Source { file_id: position.file_id.into(), value: src },
|
2019-09-16 05:48:54 -05:00
|
|
|
)?;
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&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(),
|
2019-09-16 05:48:54 -05:00
|
|
|
impls_for_def(db, position, &nominal_def, module)?,
|
2019-01-31 17:34:52 -06:00
|
|
|
));
|
2019-07-19 04:56:47 -05:00
|
|
|
} else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&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(),
|
2019-09-16 05:48:54 -05:00
|
|
|
impls_for_trait(db, position, &trait_def, module)?,
|
2019-01-31 17:34:52 -06:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impls_for_def(
|
|
|
|
db: &RootDatabase,
|
2019-09-16 05:48:54 -05:00
|
|
|
position: FilePosition,
|
2019-01-31 17:34:52 -06:00
|
|
|
node: &ast::NominalDef,
|
|
|
|
module: hir::Module,
|
|
|
|
) -> Option<Vec<NavigationTarget>> {
|
2019-08-19 06:13:58 -05:00
|
|
|
let ty = match node {
|
|
|
|
ast::NominalDef::StructDef(def) => {
|
2019-11-20 00:40:36 -06:00
|
|
|
let src = hir::Source { file_id: position.file_id.into(), value: def.clone() };
|
2019-09-16 05:48:54 -05:00
|
|
|
hir::Struct::from_source(db, src)?.ty(db)
|
|
|
|
}
|
|
|
|
ast::NominalDef::EnumDef(def) => {
|
2019-11-20 00:40:36 -06:00
|
|
|
let src = hir::Source { file_id: position.file_id.into(), value: def.clone() };
|
2019-09-16 05:48:54 -05:00
|
|
|
hir::Enum::from_source(db, src)?.ty(db)
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|
2019-11-25 08:30:50 -06:00
|
|
|
ast::NominalDef::UnionDef(def) => {
|
|
|
|
let src = hir::Source { file_id: position.file_id.into(), value: def.clone() };
|
|
|
|
hir::Union::from_source(db, src)?.ty(db)
|
|
|
|
}
|
2019-01-28 08:26:32 -06:00
|
|
|
};
|
|
|
|
|
2019-10-30 05:10:38 -05:00
|
|
|
let krate = module.krate();
|
2019-11-26 06:27:33 -06:00
|
|
|
let impls = ImplBlock::all_in_crate(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()
|
2019-08-16 08:34:47 -05:00
|
|
|
.filter(|impl_block| is_equal_for_find_impls(&ty, &impl_block.target_ty(db)))
|
2019-11-11 02:15:19 -06:00
|
|
|
.map(|imp| imp.to_nav(db))
|
2019-01-31 17:34:52 -06:00
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impls_for_trait(
|
|
|
|
db: &RootDatabase,
|
2019-09-16 05:48:54 -05:00
|
|
|
position: FilePosition,
|
2019-01-31 17:34:52 -06:00
|
|
|
node: &ast::TraitDef,
|
|
|
|
module: hir::Module,
|
|
|
|
) -> Option<Vec<NavigationTarget>> {
|
2019-11-20 00:40:36 -06:00
|
|
|
let src = hir::Source { file_id: position.file_id.into(), value: node.clone() };
|
2019-09-16 05:48:54 -05:00
|
|
|
let tr = hir::Trait::from_source(db, src)?;
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2019-10-30 05:10:38 -05:00
|
|
|
let krate = module.krate();
|
2019-11-26 06:27:33 -06:00
|
|
|
let impls = ImplBlock::for_trait(db, krate, tr);
|
2019-01-31 17:34:52 -06:00
|
|
|
|
2019-11-26 06:27:33 -06:00
|
|
|
Some(impls.into_iter().map(|imp| imp.to_nav(db)).collect())
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|
|
|
|
|
2019-08-16 08:34:47 -05:00
|
|
|
fn is_equal_for_find_impls(original_ty: &Ty, impl_ty: &Ty) -> bool {
|
|
|
|
match (original_ty, impl_ty) {
|
|
|
|
(Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor {
|
|
|
|
TypeCtor::Ref(..) => match parameters.as_single() {
|
|
|
|
Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => a_original_ty.ctor == *ctor,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:26:32 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::mock_analysis::analysis_and_position;
|
|
|
|
|
2019-02-11 10:18:27 -06:00
|
|
|
fn check_goto(fixture: &str, expected: &[&str]) {
|
|
|
|
let (analysis, pos) = analysis_and_position(fixture);
|
2019-01-28 08:26:32 -06:00
|
|
|
|
2019-03-14 05:27:21 -05:00
|
|
|
let mut navs = analysis.goto_implementation(pos).unwrap().unwrap().info;
|
2019-01-28 08:26:32 -06:00
|
|
|
assert_eq!(navs.len(), expected.len());
|
2019-03-14 05:27:21 -05:00
|
|
|
navs.sort_by_key(|nav| (nav.file_id(), nav.full_range().start()));
|
2019-02-08 05:49:43 -06:00
|
|
|
navs.into_iter().enumerate().for_each(|(i, nav)| nav.assert_match(expected[i]));
|
2019-01-28 08:26:32 -06: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 05:49:43 -06:00
|
|
|
&["impl IMPL_BLOCK FileId(1) [12; 23)", "impl IMPL_BLOCK FileId(1) [24; 35)"],
|
2019-01-28 08:26:32 -06: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 05:49:43 -06:00
|
|
|
&["impl IMPL_BLOCK FileId(1) [24; 42)", "impl IMPL_BLOCK FileId(1) [57; 75)"],
|
2019-01-28 08:26:32 -06: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 05:49:43 -06:00
|
|
|
&["impl IMPL_BLOCK FileId(2) [0; 18)", "impl IMPL_BLOCK FileId(3) [0; 18)"],
|
2019-01-28 08:26:32 -06:00
|
|
|
);
|
|
|
|
}
|
2019-01-31 17:34:52 -06: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 05:49:43 -06:00
|
|
|
&["impl IMPL_BLOCK FileId(2) [0; 31)", "impl IMPL_BLOCK FileId(3) [0; 31)"],
|
2019-01-31 17:34:52 -06:00
|
|
|
);
|
|
|
|
}
|
2019-08-16 08:34:47 -05: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)",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2019-01-28 08:26:32 -06:00
|
|
|
}
|