From 35a04ec066b1a4ece2a6aa5e74dd8beae52e68f1 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Fri, 16 Aug 2019 16:34:47 +0300 Subject: show inherent and trait impls of structs and enums --- crates/ra_hir/src/ty/method_resolution.rs | 9 ++++++++ crates/ra_ide_api/src/impls.rs | 37 +++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 88d012a74..3f4c43aee 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -68,6 +68,15 @@ impl CrateImplBlocks { ) } + pub fn all_impls<'a>(&'a self) -> impl Iterator + '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); diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index f5b6ffd61..638f4cbde 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs @@ -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}; @@ -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(), ) @@ -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; @@ -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)", + ], + ); + } } -- cgit v1.2.3