Skip to content

Commit 64a73dc

Browse files
bors[bot]jhgg
andauthored
Merge #10785
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 <[email protected]> Co-authored-by: Jake <[email protected]>
2 parents fdd49b9 + aef8882 commit 64a73dc

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

crates/hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,10 @@ impl Const {
14571457
db.const_data(self.id).name.clone()
14581458
}
14591459

1460+
pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
1461+
self.source(db)?.value.body()
1462+
}
1463+
14601464
pub fn ty(self, db: &dyn HirDatabase) -> Type {
14611465
let data = db.const_data(self.id);
14621466
let resolver = self.id.resolver(db.upcast());

crates/ide/src/hover/render.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Logic for rendering the different hover messages
22
use either::Either;
3-
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
3+
use hir::{AsAssocItem, Const, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
44
use ide_db::{
55
base_db::SourceDatabase,
66
defs::Definition,
@@ -352,7 +352,7 @@ pub(super) fn definition(
352352
Definition::Function(it) => label_and_docs(db, it),
353353
Definition::Adt(it) => label_and_docs(db, it),
354354
Definition::Variant(it) => label_and_docs(db, it),
355-
Definition::Const(it) => label_and_docs(db, it),
355+
Definition::Const(it) => const_label_value_and_docs(db, it),
356356
Definition::Static(it) => label_and_docs(db, it),
357357
Definition::Trait(it) => label_and_docs(db, it),
358358
Definition::TypeAlias(it) => label_and_docs(db, it),
@@ -381,6 +381,21 @@ where
381381
(label, docs)
382382
}
383383

384+
fn const_label_value_and_docs(
385+
db: &RootDatabase,
386+
konst: Const,
387+
) -> (String, Option<hir::Documentation>) {
388+
let label = if let Some(expr) = konst.value(db) {
389+
format!("{} = {}", konst.display(db), expr)
390+
} else {
391+
konst.display(db).to_string()
392+
};
393+
394+
let docs = konst.attrs(db).docs();
395+
396+
(label, docs)
397+
}
398+
384399
fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
385400
if let Definition::GenericParam(_) = def {
386401
return None;

crates/ide/src/hover/tests.rs

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -503,17 +503,39 @@ fn hover_const_static() {
503503
check(
504504
r#"const foo$0: u32 = 123;"#,
505505
expect![[r#"
506-
*foo*
506+
*foo*
507507
508-
```rust
509-
test
510-
```
508+
```rust
509+
test
510+
```
511511
512-
```rust
513-
const foo: u32
514-
```
515-
"#]],
512+
```rust
513+
const foo: u32 = 123
514+
```
515+
"#]],
516516
);
517+
check(
518+
r#"
519+
const foo$0: u32 = {
520+
let x = foo();
521+
x + 100
522+
};"#,
523+
expect![[r#"
524+
*foo*
525+
526+
```rust
527+
test
528+
```
529+
530+
```rust
531+
const foo: u32 = {
532+
let x = foo();
533+
x + 100
534+
}
535+
```
536+
"#]],
537+
);
538+
517539
check(
518540
r#"static foo$0: u32 = 456;"#,
519541
expect![[r#"
@@ -788,16 +810,16 @@ fn main() {
788810
}
789811
"#,
790812
expect![[r#"
791-
*C*
813+
*C*
792814
793-
```rust
794-
test
795-
```
815+
```rust
816+
test
817+
```
796818
797-
```rust
798-
const C: u32
799-
```
800-
"#]],
819+
```rust
820+
const C: u32 = 1
821+
```
822+
"#]],
801823
)
802824
}
803825

@@ -3176,20 +3198,20 @@ fn foo() {
31763198
}
31773199
"#,
31783200
expect![[r#"
3179-
*FOO*
3201+
*FOO*
31803202
3181-
```rust
3182-
test
3183-
```
3203+
```rust
3204+
test
3205+
```
31843206
3185-
```rust
3186-
const FOO: usize
3187-
```
3207+
```rust
3208+
const FOO: usize = 3
3209+
```
31883210
3189-
---
3211+
---
31903212
3191-
This is a doc
3192-
"#]],
3213+
This is a doc
3214+
"#]],
31933215
);
31943216
}
31953217

0 commit comments

Comments
 (0)