10785: ide: show const value in hover r=jhgg a=jhgg

fixes #10783

I think my original attempt was incorrect, because it looks like `HirDisplay` is used in more places than just the hover.  

So, I've attempted it again in 312eafe, this time specifically just rendering the value in `hover::render`

pictoral:

![image](https://user-images.githubusercontent.com/5489149/142163890-b6aa2ab4-7bd0-4dd3-b35d-5eaa83fffb7f.png)


Co-authored-by: Jake Heinz <jh@discordapp.com>
Co-authored-by: Jake <jh@discordapp.com>
This commit is contained in:
bors[bot] 2021-11-18 04:17:16 +00:00 committed by GitHub
commit 64a73dcfba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 28 deletions

View File

@ -1457,6 +1457,10 @@ impl Const {
db.const_data(self.id).name.clone()
}
pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
self.source(db)?.value.body()
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
let data = db.const_data(self.id);
let resolver = self.id.resolver(db.upcast());

View File

@ -1,6 +1,6 @@
//! Logic for rendering the different hover messages
use either::Either;
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
use hir::{AsAssocItem, Const, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
use ide_db::{
base_db::SourceDatabase,
defs::Definition,
@ -352,7 +352,7 @@ pub(super) fn definition(
Definition::Function(it) => label_and_docs(db, it),
Definition::Adt(it) => label_and_docs(db, it),
Definition::Variant(it) => label_and_docs(db, it),
Definition::Const(it) => label_and_docs(db, it),
Definition::Const(it) => const_label_value_and_docs(db, it),
Definition::Static(it) => label_and_docs(db, it),
Definition::Trait(it) => label_and_docs(db, it),
Definition::TypeAlias(it) => label_and_docs(db, it),
@ -381,6 +381,21 @@ where
(label, docs)
}
fn const_label_value_and_docs(
db: &RootDatabase,
konst: Const,
) -> (String, Option<hir::Documentation>) {
let label = if let Some(expr) = konst.value(db) {
format!("{} = {}", konst.display(db), expr)
} else {
konst.display(db).to_string()
};
let docs = konst.attrs(db).docs();
(label, docs)
}
fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
if let Definition::GenericParam(_) = def {
return None;

View File

@ -503,17 +503,39 @@ fn hover_const_static() {
check(
r#"const foo$0: u32 = 123;"#,
expect![[r#"
*foo*
*foo*
```rust
test
```
```rust
test
```
```rust
const foo: u32
```
"#]],
```rust
const foo: u32 = 123
```
"#]],
);
check(
r#"
const foo$0: u32 = {
let x = foo();
x + 100
};"#,
expect![[r#"
*foo*
```rust
test
```
```rust
const foo: u32 = {
let x = foo();
x + 100
}
```
"#]],
);
check(
r#"static foo$0: u32 = 456;"#,
expect![[r#"
@ -788,16 +810,16 @@ fn main() {
}
"#,
expect![[r#"
*C*
*C*
```rust
test
```
```rust
test
```
```rust
const C: u32
```
"#]],
```rust
const C: u32 = 1
```
"#]],
)
}
@ -3176,20 +3198,20 @@ fn foo() {
}
"#,
expect![[r#"
*FOO*
*FOO*
```rust
test
```
```rust
test
```
```rust
const FOO: usize
```
```rust
const FOO: usize = 3
```
---
---
This is a doc
"#]],
This is a doc
"#]],
);
}