Improve hover message for inert attributes

This commit is contained in:
Lukas Wirth 2021-12-03 20:28:15 +01:00
parent d174158abc
commit 8da850b6d5
4 changed files with 75 additions and 7 deletions

View file

@ -2,7 +2,7 @@
use std::fmt::Display;
use either::Either;
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
use ide_db::{
base_db::SourceDatabase,
defs::Definition,
@ -370,13 +370,31 @@ pub(super) fn definition(
Definition::GenericParam(it) => label_and_docs(db, it),
Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))),
// FIXME: We should be able to show more info about these
Definition::BuiltinAttr(it) => return Some(Markup::fenced_block(&it.name(db))),
Definition::BuiltinAttr(it) => return render_builtin_attr(db, it),
Definition::ToolModule(it) => return Some(Markup::fenced_block(&it.name(db))),
};
markup(docs.filter(|_| config.documentation.is_some()).map(Into::into), label, mod_path)
}
fn render_builtin_attr(db: &RootDatabase, attr: hir::BuiltinAttr) -> Option<Markup> {
let name = attr.name(db);
let desc = format!("#[{}]", name);
let AttributeTemplate { word, list, name_value_str } = attr.template(db);
let mut docs = "Valid forms are:".to_owned();
if word {
format_to!(docs, "\n - #\\[{}]", name);
}
if let Some(list) = list {
format_to!(docs, "\n - #\\[{}({})]", name, list);
}
if let Some(name_value_str) = name_value_str {
format_to!(docs, "\n - #\\[{} = {}]", name, name_value_str);
}
markup(Some(docs.replace('*', "\\*")), desc, None)
}
fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Documentation>)
where
D: HasAttrs + HirDisplay,

View file

@ -4277,3 +4277,46 @@ pub struct Foo;
"#]],
);
}
#[test]
fn hover_inert_attr() {
check(
r#"
#[doc$0 = ""]
pub struct Foo;
"#,
expect![[r##"
*doc*
```rust
#[doc]
```
---
Valid forms are:
* \#\[doc(hidden|inline|...)\]
* \#\[doc = string\]
"##]],
);
check(
r#"
#[allow$0()]
pub struct Foo;
"#,
expect![[r##"
*allow*
```rust
#[allow]
```
---
Valid forms are:
* \#\[allow(lint1, lint2, ..., /\*opt\*/ reason = "...")\]
"##]],
);
}