From 4fbc4b935679c142a6b45d9ebaf27e05254e8081 Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Wed, 17 Nov 2021 05:49:27 +0000 Subject: [PATCH 1/4] 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 e9ace855fc3..4c37d17aa4d 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 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { 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 f4d0c3af90b..6f071164989 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 @@ pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc Date: Wed, 17 Nov 2021 07:00:24 +0000 Subject: [PATCH 2/4] maybe this is better?? --- crates/hir/src/display.rs | 12 +----------- crates/hir/src/lib.rs | 11 +++++++++++ crates/hir_def/src/data.rs | 6 +----- crates/ide/src/hover/render.rs | 19 +++++++++++++++++-- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 4c37d17aa4d..e9ace855fc3 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -401,8 +401,7 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir impl HirDisplay for Const { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { - let module_id = self.module(f.db).id; - write_visibility(module_id, self.visibility(f.db), f)?; + write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; let data = f.db.const_data(self.id); write!(f, "const ")?; match &data.name { @@ -410,15 +409,6 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { 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/src/lib.rs b/crates/hir/src/lib.rs index d59c26438b4..ee7270f2959 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1457,6 +1457,17 @@ pub fn name(self, db: &dyn HirDatabase) -> Option { db.const_data(self.id).name.clone() } + pub fn value(self, db: &dyn HirDatabase) -> Option { + let loc = self.id.lookup(db.upcast()); + let item_tree = loc.id.item_tree(db.upcast()); + let ast_id = item_tree[loc.id.value].ast_id; + let ast_id_map = db.ast_id_map(loc.id.file_id()); + let ast_ptr = ast_id_map.get(ast_id); + let syntax_node = db.parse_or_expand(loc.id.file_id())?; + let ast_node = ast_ptr.to_node(&syntax_node); + ast_node.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/hir_def/src/data.rs b/crates/hir_def/src/data.rs index 6f071164989..f4d0c3af90b 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::{ast_id_map::FileAstId, name::Name, HirFileId, InFile}; +use hir_expand::{name::Name, InFile}; use syntax::ast; use crate::{ @@ -255,8 +255,6 @@ pub struct ConstData { pub name: Option, pub type_ref: Interned, pub visibility: RawVisibility, - pub ast_id: FileAstId, - pub file_id: HirFileId, } impl ConstData { @@ -269,8 +267,6 @@ pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc 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 @@ fn label_and_docs(db: &RootDatabase, def: D) -> (String, Option (String, Option) { + 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 { if let Definition::GenericParam(_) = def { return None; From d5de7c21b27e7ad2ba92c6ce8f386d226bcc6adc Mon Sep 17 00:00:00 2001 From: Jake Date: Wed, 17 Nov 2021 09:53:24 -0800 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Lukas Wirth --- crates/hir/src/lib.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index ee7270f2959..a528b2bc2d1 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1458,14 +1458,7 @@ pub fn name(self, db: &dyn HirDatabase) -> Option { } pub fn value(self, db: &dyn HirDatabase) -> Option { - let loc = self.id.lookup(db.upcast()); - let item_tree = loc.id.item_tree(db.upcast()); - let ast_id = item_tree[loc.id.value].ast_id; - let ast_id_map = db.ast_id_map(loc.id.file_id()); - let ast_ptr = ast_id_map.get(ast_id); - let syntax_node = db.parse_or_expand(loc.id.file_id())?; - let ast_node = ast_ptr.to_node(&syntax_node); - ast_node.body() + self.source(db)?.value.body() } pub fn ty(self, db: &dyn HirDatabase) -> Type { From aef8882254e1443fb68d449be5f8006c46533c5a Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Thu, 18 Nov 2021 04:14:11 +0000 Subject: [PATCH 4/4] more complicated const test --- crates/ide/src/hover/tests.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 329e6b177f4..897f8d7b831 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -514,6 +514,28 @@ fn hover_const_static() { ``` "#]], ); + 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#"