Skip to content

feat: ide: display static values in hover #10796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,10 @@ impl Static {
db.static_data(self.id).mutable
}

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.static_data(self.id);
let resolver = self.id.resolver(db.upcast());
Expand Down
30 changes: 18 additions & 12 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Logic for rendering the different hover messages
use std::fmt::Display;

use either::Either;
use hir::{AsAssocItem, Const, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
use ide_db::{
base_db::SourceDatabase,
defs::Definition,
Expand Down Expand Up @@ -352,8 +354,8 @@ 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) => const_label_value_and_docs(db, it),
Definition::Static(it) => label_and_docs(db, it),
Definition::Const(it) => label_value_and_docs(db, it, |it| it.value(db)),
Definition::Static(it) => label_value_and_docs(db, it, |it| it.value(db)),
Definition::Trait(it) => label_and_docs(db, it),
Definition::TypeAlias(it) => label_and_docs(db, it),
Definition::BuiltinType(it) => {
Expand Down Expand Up @@ -381,18 +383,22 @@ where
(label, docs)
}

fn const_label_value_and_docs(
fn label_value_and_docs<D, E, V>(
db: &RootDatabase,
konst: Const,
) -> (String, Option<hir::Documentation>) {
let label = if let Some(expr) = konst.value(db) {
format!("{} = {}", konst.display(db), expr)
def: D,
value_extractor: E,
) -> (String, Option<hir::Documentation>)
where
D: HasAttrs + HirDisplay,
E: Fn(&D) -> Option<V>,
V: Display,
{
let label = if let Some(value) = (value_extractor)(&def) {
format!("{} = {}", def.display(db), value)
} else {
konst.display(db).to_string()
def.display(db).to_string()
};

let docs = konst.attrs(db).docs();

let docs = def.attrs(db).docs();
(label, docs)
}

Expand Down
16 changes: 8 additions & 8 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,16 +539,16 @@ const foo$0: u32 = {
check(
r#"static foo$0: u32 = 456;"#,
expect![[r#"
*foo*
*foo*
```rust
test
```
```rust
test
```
```rust
static foo: u32
```
"#]],
```rust
static foo: u32 = 456
```
"#]],
);
}

Expand Down