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

View file

@ -1457,6 +1457,17 @@ impl Const {
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());