From 9b9c13fc40876bdd94ca73ab135cc7efc36ec95e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 13 Jun 2022 11:57:33 +0200 Subject: [PATCH] feat: On assoc item name hover, render trait decl docs --- crates/ide/src/hover/render.rs | 15 ++++++++++++++- crates/ide/src/hover/tests.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 06762202720..1cb67c6657a 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -372,7 +372,20 @@ pub(super) fn definition( Definition::ToolModule(it) => return Some(Markup::fenced_block(&it.name(db))), }; - markup(docs.filter(|_| config.documentation.is_some()).map(Into::into), label, mod_path) + let docs = match config.documentation { + Some(_) => docs.or_else(|| { + // docs are missing, for assoc items of trait impls try to fall back to the docs of the + // original item of the trait + let assoc = def.as_assoc_item(db)?; + let trait_ = assoc.containing_trait_impl(db)?; + let name = Some(assoc.name(db)?); + let item = trait_.items(db).into_iter().find(|it| it.name(db) == name)?; + item.docs(db) + }), + None => None, + }; + let docs = docs.filter(|_| config.documentation.is_some()).map(Into::into); + markup(docs, label, mod_path) } fn render_builtin_attr(db: &RootDatabase, attr: hir::BuiltinAttr) -> Option { diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index c95a0e40054..8eb16aab293 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -4888,3 +4888,33 @@ enum Enum { "#]], ); } + +#[test] +fn hover_trait_impl_assoc_item_def_doc_forwarding() { + check( + r#" +trait T { + /// Trait docs + fn func() {} +} +impl T for () { + fn func$0() {} +} +"#, + expect![[r#" + *func* + + ```rust + test + ``` + + ```rust + fn func() + ``` + + --- + + Trait docs + "#]], + ); +}