Skip to content

Commit 312eafe

Browse files
committed
maybe this is better??
1 parent 4fbc4b9 commit 312eafe

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

crates/hir/src/display.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,24 +401,14 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
401401

402402
impl HirDisplay for Const {
403403
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
404-
let module_id = self.module(f.db).id;
405-
write_visibility(module_id, self.visibility(f.db), f)?;
404+
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
406405
let data = f.db.const_data(self.id);
407406
write!(f, "const ")?;
408407
match &data.name {
409408
Some(name) => write!(f, "{}: ", name)?,
410409
None => write!(f, "_: ")?,
411410
}
412411
data.type_ref.hir_fmt(f)?;
413-
let ast_id_map = f.db.ast_id_map(data.file_id);
414-
let ast_node = ast_id_map.get(data.ast_id);
415-
if let Some(syntax_node) = f.db.parse_or_expand(data.file_id) {
416-
let ast_node = ast_node.to_node(&syntax_node);
417-
if let Some(body) = ast_node.body() {
418-
write!(f, " = {}", body)?;
419-
}
420-
}
421-
422412
Ok(())
423413
}
424414
}

crates/hir/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,17 @@ impl Const {
14571457
db.const_data(self.id).name.clone()
14581458
}
14591459

1460+
pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
1461+
let loc = self.id.lookup(db.upcast());
1462+
let item_tree = loc.id.item_tree(db.upcast());
1463+
let ast_id = item_tree[loc.id.value].ast_id;
1464+
let ast_id_map = db.ast_id_map(loc.id.file_id());
1465+
let ast_ptr = ast_id_map.get(ast_id);
1466+
let syntax_node = db.parse_or_expand(loc.id.file_id())?;
1467+
let ast_node = ast_ptr.to_node(&syntax_node);
1468+
ast_node.body()
1469+
}
1470+
14601471
pub fn ty(self, db: &dyn HirDatabase) -> Type {
14611472
let data = db.const_data(self.id);
14621473
let resolver = self.id.resolver(db.upcast());

crates/hir_def/src/data.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::sync::Arc;
44

5-
use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile};
5+
use hir_expand::{name::Name, InFile};
66
use syntax::ast;
77

88
use crate::{
@@ -255,8 +255,6 @@ pub struct ConstData {
255255
pub name: Option<Name>,
256256
pub type_ref: Interned<TypeRef>,
257257
pub visibility: RawVisibility,
258-
pub ast_id: FileAstId<ast::Const>,
259-
pub file_id: HirFileId,
260258
}
261259

262260
impl ConstData {
@@ -269,8 +267,6 @@ impl ConstData {
269267
name: konst.name.clone(),
270268
type_ref: konst.type_ref.clone(),
271269
visibility: item_tree[konst.visibility].clone(),
272-
ast_id: konst.ast_id.clone(),
273-
file_id: loc.id.file_id(),
274270
})
275271
}
276272
}

crates/ide/src/hover/render.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Logic for rendering the different hover messages
22
use either::Either;
3-
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
3+
use hir::{AsAssocItem, Const, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
44
use ide_db::{
55
base_db::SourceDatabase,
66
defs::Definition,
@@ -352,7 +352,7 @@ pub(super) fn definition(
352352
Definition::Function(it) => label_and_docs(db, it),
353353
Definition::Adt(it) => label_and_docs(db, it),
354354
Definition::Variant(it) => label_and_docs(db, it),
355-
Definition::Const(it) => label_and_docs(db, it),
355+
Definition::Const(it) => const_label_value_and_docs(db, it),
356356
Definition::Static(it) => label_and_docs(db, it),
357357
Definition::Trait(it) => label_and_docs(db, it),
358358
Definition::TypeAlias(it) => label_and_docs(db, it),
@@ -381,6 +381,21 @@ where
381381
(label, docs)
382382
}
383383

384+
fn const_label_value_and_docs(
385+
db: &RootDatabase,
386+
konst: Const,
387+
) -> (String, Option<hir::Documentation>) {
388+
let label = if let Some(expr) = konst.value(db) {
389+
format!("{} = {}", konst.display(db), expr)
390+
} else {
391+
konst.display(db).to_string()
392+
};
393+
394+
let docs = konst.attrs(db).docs();
395+
396+
(label, docs)
397+
}
398+
384399
fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
385400
if let Definition::GenericParam(_) = def {
386401
return None;

0 commit comments

Comments
 (0)