Skip to content

Commit 7db7387

Browse files
committed
Auto merge of rust-lang#12519 - Veykril:hover-assoc, r=Veykril
feat: On assoc item name hover, render trait decl docs
2 parents 366bd72 + 9b9c13f commit 7db7387

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

crates/ide/src/hover/render.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,20 @@ pub(super) fn definition(
372372
Definition::ToolModule(it) => return Some(Markup::fenced_block(&it.name(db))),
373373
};
374374

375-
markup(docs.filter(|_| config.documentation.is_some()).map(Into::into), label, mod_path)
375+
let docs = match config.documentation {
376+
Some(_) => docs.or_else(|| {
377+
// docs are missing, for assoc items of trait impls try to fall back to the docs of the
378+
// original item of the trait
379+
let assoc = def.as_assoc_item(db)?;
380+
let trait_ = assoc.containing_trait_impl(db)?;
381+
let name = Some(assoc.name(db)?);
382+
let item = trait_.items(db).into_iter().find(|it| it.name(db) == name)?;
383+
item.docs(db)
384+
}),
385+
None => None,
386+
};
387+
let docs = docs.filter(|_| config.documentation.is_some()).map(Into::into);
388+
markup(docs, label, mod_path)
376389
}
377390

378391
fn render_builtin_attr(db: &RootDatabase, attr: hir::BuiltinAttr) -> Option<Markup> {

crates/ide/src/hover/tests.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4888,3 +4888,33 @@ enum Enum {
48884888
"#]],
48894889
);
48904890
}
4891+
4892+
#[test]
4893+
fn hover_trait_impl_assoc_item_def_doc_forwarding() {
4894+
check(
4895+
r#"
4896+
trait T {
4897+
/// Trait docs
4898+
fn func() {}
4899+
}
4900+
impl T for () {
4901+
fn func$0() {}
4902+
}
4903+
"#,
4904+
expect![[r#"
4905+
*func*
4906+
4907+
```rust
4908+
test
4909+
```
4910+
4911+
```rust
4912+
fn func()
4913+
```
4914+
4915+
---
4916+
4917+
Trait docs
4918+
"#]],
4919+
);
4920+
}

0 commit comments

Comments
 (0)