]> git.lizzy.rs Git - rust.git/commitdiff
ide: display static values in hover
authorJake Heinz <jh@discordapp.com>
Thu, 18 Nov 2021 04:47:07 +0000 (04:47 +0000)
committerJake Heinz <jh@discordapp.com>
Thu, 18 Nov 2021 06:00:51 +0000 (06:00 +0000)
crates/hir/src/lib.rs
crates/ide/src/hover/render.rs
crates/ide/src/hover/tests.rs

index a528b2bc2d1f066b906edaa70c9beb17a8174d5e..4fdcb88bbf0e4f21b466abf6963e2cb307bf5f03 100644 (file)
@@ -1497,6 +1497,10 @@ pub fn is_mut(self, db: &dyn HirDatabase) -> bool {
         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());
index 46fe7f2b7d2394fb9ca39b687172071e1b2b3c96..e872d9b1fb2f7f682b85e8af5868e2ac53b4df22 100644 (file)
@@ -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,
@@ -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) => {
@@ -381,18 +383,22 @@ fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Document
     (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)
 }
 
index 897f8d7b8312a3454aebdedd72bc386285b61ff6..91a4db33fb22f00f16e5de88220ed40fcc8dd5ec 100644 (file)
@@ -539,16 +539,16 @@ fn hover_const_static() {
     check(
         r#"static foo$0: u32 = 456;"#,
         expect![[r#"
-                *foo*
+            *foo*
 
-                ```rust
-                test
-                ```
+            ```rust
+            test
+            ```
 
-                ```rust
-                static foo: u32
-                ```
-            "#]],
+            ```rust
+            static foo: u32 = 456
+            ```
+        "#]],
     );
 }