Skip to content

Commit bf8cf09

Browse files
bors[bot]jhgg
andauthored
Merge #10796
10796: ide: display static values in hover r=Veykril a=jhgg Continuation from #10785 - does the same thing, but for `static`'s as well. Co-authored-by: Jake Heinz <[email protected]>
2 parents b844d45 + e8d0989 commit bf8cf09

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

crates/hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ impl Static {
14971497
db.static_data(self.id).mutable
14981498
}
14991499

1500+
pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
1501+
self.source(db)?.value.body()
1502+
}
1503+
15001504
pub fn ty(self, db: &dyn HirDatabase) -> Type {
15011505
let data = db.static_data(self.id);
15021506
let resolver = self.id.resolver(db.upcast());

crates/ide/src/hover/render.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Logic for rendering the different hover messages
2+
use std::fmt::Display;
3+
24
use either::Either;
3-
use hir::{AsAssocItem, Const, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
5+
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
46
use ide_db::{
57
base_db::SourceDatabase,
68
defs::Definition,
@@ -352,8 +354,8 @@ pub(super) fn definition(
352354
Definition::Function(it) => label_and_docs(db, it),
353355
Definition::Adt(it) => label_and_docs(db, it),
354356
Definition::Variant(it) => label_and_docs(db, it),
355-
Definition::Const(it) => const_label_value_and_docs(db, it),
356-
Definition::Static(it) => label_and_docs(db, it),
357+
Definition::Const(it) => label_value_and_docs(db, it, |it| it.value(db)),
358+
Definition::Static(it) => label_value_and_docs(db, it, |it| it.value(db)),
357359
Definition::Trait(it) => label_and_docs(db, it),
358360
Definition::TypeAlias(it) => label_and_docs(db, it),
359361
Definition::BuiltinType(it) => {
@@ -381,18 +383,22 @@ where
381383
(label, docs)
382384
}
383385

384-
fn const_label_value_and_docs(
386+
fn label_value_and_docs<D, E, V>(
385387
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)
388+
def: D,
389+
value_extractor: E,
390+
) -> (String, Option<hir::Documentation>)
391+
where
392+
D: HasAttrs + HirDisplay,
393+
E: Fn(&D) -> Option<V>,
394+
V: Display,
395+
{
396+
let label = if let Some(value) = (value_extractor)(&def) {
397+
format!("{} = {}", def.display(db), value)
390398
} else {
391-
konst.display(db).to_string()
399+
def.display(db).to_string()
392400
};
393-
394-
let docs = konst.attrs(db).docs();
395-
401+
let docs = def.attrs(db).docs();
396402
(label, docs)
397403
}
398404

crates/ide/src/hover/tests.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,16 +539,16 @@ const foo$0: u32 = {
539539
check(
540540
r#"static foo$0: u32 = 456;"#,
541541
expect![[r#"
542-
*foo*
542+
*foo*
543543
544-
```rust
545-
test
546-
```
544+
```rust
545+
test
546+
```
547547
548-
```rust
549-
static foo: u32
550-
```
551-
"#]],
548+
```rust
549+
static foo: u32 = 456
550+
```
551+
"#]],
552552
);
553553
}
554554

0 commit comments

Comments
 (0)