Add an HIR pretty-printer

This commit is contained in:
Jonas Schievink 2022-08-15 13:51:45 +02:00
parent b6d59f2bb4
commit dcbe892d7c
12 changed files with 914 additions and 203 deletions

View file

@ -1,4 +1,4 @@
use hir::{Function, Semantics};
use hir::{DefWithBody, Semantics};
use ide_db::base_db::FilePosition;
use ide_db::RootDatabase;
use syntax::{algo::find_node_at_offset, ast, AstNode};
@ -19,8 +19,12 @@ fn body_hir(db: &RootDatabase, position: FilePosition) -> Option<String> {
let sema = Semantics::new(db);
let source_file = sema.parse(position.file_id);
let function = find_node_at_offset::<ast::Fn>(source_file.syntax(), position.offset)?;
let function: Function = sema.to_def(&function)?;
Some(function.debug_hir(db))
let item = find_node_at_offset::<ast::Item>(source_file.syntax(), position.offset)?;
let def: DefWithBody = match item {
ast::Item::Fn(it) => sema.to_def(&it)?.into(),
ast::Item::Const(it) => sema.to_def(&it)?.into(),
ast::Item::Static(it) => sema.to_def(&it)?.into(),
_ => return None,
};
Some(def.debug_hir(db))
}