Unnamed repository; edit this file 'description' to name the repository.
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:

Co-authored-by: Jake Heinz <[email protected]>
Co-authored-by: Jake <[email protected]>
| -rw-r--r-- | crates/hir/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/ide/src/hover/render.rs | 19 | ||||
| -rw-r--r-- | crates/ide/src/hover/tests.rs | 74 |
3 files changed, 69 insertions, 28 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d59c26438b..a528b2bc2d 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -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()); diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 59068028ed..46fe7f2b7d 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -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; diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 2aa54fc33f..897f8d7b83 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -503,18 +503,40 @@ 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#" *foo* @@ -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 + "#]], ); } |