maybe this is better??

This commit is contained in:
Jake Heinz 2021-11-17 07:00:24 +00:00
parent 4fbc4b9356
commit 312eafe916
4 changed files with 30 additions and 18 deletions

View File

@ -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(())
}
}

View File

@ -1457,6 +1457,17 @@ pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
db.const_data(self.id).name.clone()
}
pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
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());

View File

@ -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<Name>,
pub type_ref: Interned<TypeRef>,
pub visibility: RawVisibility,
pub ast_id: FileAstId<ast::Const>,
pub file_id: HirFileId,
}
impl ConstData {
@ -269,8 +267,6 @@ pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc<Cons
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(),
})
}
}

View File

@ -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 @@ fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Document
(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;