Skip to content

Commit

Permalink
Merge #1691
Browse files Browse the repository at this point in the history
1691: Show inherent and trait impls of structs and enums r=viorina a=viorina



Co-authored-by: Ekaterina Babshukova <ekaterina.babshukova@yandex.ru>
  • Loading branch information
bors[bot] and viorina authored Aug 16, 2019
2 parents af1052b + 35a04ec commit cd24349
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/ra_hir/src/ty/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ impl CrateImplBlocks {
)
}

pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplBlock> + 'a {
self.impls.values().chain(self.impls_by_trait.values()).flat_map(|i| i.iter()).map(
move |(module_id, impl_id)| {
let module = Module { krate: self.krate, module_id: *module_id };
ImplBlock::from_id(module, *impl_id)
},
)
}

fn collect_recursive(&mut self, db: &impl HirDatabase, module: Module) {
let module_impl_blocks = db.impls_in_module(module);

Expand Down
37 changes: 35 additions & 2 deletions crates/ra_ide_api/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hir::{db::HirDatabase, source_binder};
use hir::{db::HirDatabase, source_binder, ApplicationTy, Ty, TypeCtor};
use ra_db::SourceDatabase;
use ra_syntax::{algo::find_node_at_offset, ast, AstNode};

Expand Down Expand Up @@ -47,7 +47,8 @@ fn impls_for_def(

Some(
impls
.lookup_impl_blocks(&ty)
.all_impls()
.filter(|impl_block| is_equal_for_find_impls(&ty, &impl_block.target_ty(db)))
.map(|imp| NavigationTarget::from_impl_block(db, imp))
.collect(),
)
Expand All @@ -71,6 +72,19 @@ fn impls_for_trait(
)
}

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,
}
}

#[cfg(test)]
mod tests {
use crate::mock_analysis::analysis_and_position;
Expand Down Expand Up @@ -173,4 +187,23 @@ mod tests {
&["impl IMPL_BLOCK FileId(2) [0; 31)", "impl IMPL_BLOCK FileId(3) [0; 31)"],
);
}

#[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)",
],
);
}
}

0 comments on commit cd24349

Please sign in to comment.