Almost there

This commit is contained in:
OleStrohm 2022-08-06 22:12:33 +02:00
parent b63234e20b
commit 2f84b6e2e5
5 changed files with 141 additions and 22 deletions

View file

@ -3,7 +3,7 @@ use std::fmt::Display;
use either::Either;
use hir::{
db::HirDatabase, AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo,
db::HirDatabase, AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, StructKind, TypeInfo,
};
use ide_db::{
base_db::SourceDatabase,
@ -348,12 +348,12 @@ pub(super) fn definition(
Definition::Module(it) => label_and_docs(db, it),
Definition::Function(it) => label_and_docs(db, it),
Definition::Adt(it) => label_and_docs(db, it),
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
let body = it.eval(db);
match body {
Ok(x) => Some(format!("{}", x)),
Err(_) => it.value(db).map(|x| format!("{}", x)),
}
Definition::Variant(it) => label_value_and_docs(db, it, |&it| match it.kind(db) {
StructKind::Unit => match it.eval(db) {
Ok(x) => Some(format!("{}", x.enum_value().unwrap_or(x))),
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
},
_ => None,
}),
Definition::Const(it) => label_value_and_docs(db, it, |it| {
let body = it.eval(db);

View file

@ -3529,6 +3529,31 @@ impl<const LEN: usize> Foo<LEN$0> {}
#[test]
fn hover_const_eval_variant() {
check(
r#"
#[repr(u8)]
enum E {
A = 4,
/// This is a doc
B$0 = E::A as u8 + 1,
}
"#,
expect![[r#"
*B*
```rust
test::E
```
```rust
B = 5
```
---
This is a doc
"#]],
);
// show hex for <10
check(
r#"
@ -3586,7 +3611,7 @@ enum E {
enum E {
A = 1,
/// This is a doc
B$0 = E::A + 1,
B$0 = E::A as u8 + 1,
}
"#,
expect![[r#"
@ -3602,6 +3627,32 @@ enum E {
---
This is a doc
"#]],
);
// unspecified variant should increment by one
check(
r#"
#[repr(u8)]
enum E {
A = 4,
/// This is a doc
B$0,
}
"#,
expect![[r#"
*B*
```rust
test::E
```
```rust
B = 5
```
---
This is a doc
"#]],
);