From 6870bfd09951896b7115cff4c6817dd7c29da86f Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Thu, 2 Dec 2021 00:04:03 -0500 Subject: [PATCH] fixup: include more information for impls --- crates/ide/src/moniker.rs | 42 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/ide/src/moniker.rs b/crates/ide/src/moniker.rs index e691bfb06f1..c93c471f494 100644 --- a/crates/ide/src/moniker.rs +++ b/crates/ide/src/moniker.rs @@ -110,8 +110,23 @@ pub(crate) fn def_to_moniker( // Handle associated items within a trait if let Some(assoc) = def.as_assoc_item(db) { let container = assoc.container(db); - if let AssocItemContainer::Trait(parent_trait) = container { - path.push(parent_trait.name(db)); + match container { + AssocItemContainer::Trait(trait_) => { + // Because different traits can have functions with the same name, + // we have to include the trait name as part of the moniker for uniqueness. + path.push(trait_.name(db)); + } + AssocItemContainer::Impl(impl_) => { + // Because a struct can implement multiple traits, for implementations + // we add both the struct name and the trait name to the path + if let Some(adt) = impl_.self_ty(db).as_adt() { + path.push(adt.name(db)); + } + + if let Some(trait_) = impl_.trait_(db) { + path.push(trait_.name(db)); + } + } } } @@ -253,6 +268,29 @@ pub mod module { ); } + #[test] + fn moniker_for_trait_impl_function() { + check_moniker( + r#" +//- /foo/lib.rs crate:foo@CratesIo:0.1.0,https://a.b/foo.git +pub mod module { + pub trait MyTrait { + pub fn func() {} + } + + struct MyStruct {} + + impl MyTrait for MyStruct { + pub fn func$0() {} + } +} +"#, + "foo::module::MyStruct::MyTrait::func", + r#"PackageInformation { name: "foo", repo: "https://a.b/foo.git", version: "0.1.0" }"#, + MonikerKind::Export, + ); + } + #[test] fn moniker_for_field() { check_moniker(