feat: On assoc item name hover, render trait decl docs

This commit is contained in:
Lukas Wirth 2022-06-13 11:57:33 +02:00
parent 10e9f47dce
commit 9b9c13fc40
2 changed files with 44 additions and 1 deletions

View File

@ -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<Markup> {

View File

@ -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
"#]],
);
}