mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #1691
1691: Show inherent and trait impls of structs and enums r=viorina a=viorina Co-authored-by: Ekaterina Babshukova <ekaterina.babshukova@yandex.ru>
This commit is contained in:
commit
cd24349997
2 changed files with 44 additions and 2 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)",
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue