From 4fbc4b935679c142a6b45d9ebaf27e05254e8081 Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Wed, 17 Nov 2021 05:49:27 +0000 Subject: [PATCH] hir: show const value in hover --- crates/hir/src/display.rs | 12 +++++++- crates/hir_def/src/data.rs | 6 +++- crates/ide/src/hover/tests.rs | 52 +++++++++++++++++------------------ 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index e9ace855fc..4c37d17aa4 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -401,7 +401,8 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir impl HirDisplay for Const { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { - write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; + let module_id = self.module(f.db).id; + write_visibility(module_id, self.visibility(f.db), f)?; let data = f.db.const_data(self.id); write!(f, "const ")?; match &data.name { @@ -409,6 +410,15 @@ impl HirDisplay for Const { None => write!(f, "_: ")?, } data.type_ref.hir_fmt(f)?; + let ast_id_map = f.db.ast_id_map(data.file_id); + let ast_node = ast_id_map.get(data.ast_id); + if let Some(syntax_node) = f.db.parse_or_expand(data.file_id) { + let ast_node = ast_node.to_node(&syntax_node); + if let Some(body) = ast_node.body() { + write!(f, " = {}", body)?; + } + } + Ok(()) } } diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index f4d0c3af90..6f07116498 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs @@ -2,7 +2,7 @@ use std::sync::Arc; -use hir_expand::{name::Name, InFile}; +use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile}; use syntax::ast; use crate::{ @@ -255,6 +255,8 @@ pub struct ConstData { pub name: Option, pub type_ref: Interned, pub visibility: RawVisibility, + pub ast_id: FileAstId, + pub file_id: HirFileId, } impl ConstData { @@ -267,6 +269,8 @@ impl ConstData { name: konst.name.clone(), type_ref: konst.type_ref.clone(), visibility: item_tree[konst.visibility].clone(), + ast_id: konst.ast_id.clone(), + file_id: loc.id.file_id(), }) } } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 2aa54fc33f..329e6b177f 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -503,16 +503,16 @@ 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#"static foo$0: u32 = 456;"#, @@ -788,16 +788,16 @@ fn main() { } "#, expect![[r#" - *C* + *C* - ```rust - test - ``` + ```rust + test + ``` - ```rust - const C: u32 - ``` - "#]], + ```rust + const C: u32 = 1 + ``` + "#]], ) } @@ -3176,20 +3176,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 + "#]], ); }