From db53db804604574337f80a614565fb964403a654 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Fri, 1 Jan 2021 19:25:18 +0000 Subject: Address review suggestion, fix tidy tests --- crates/hir/src/code_model.rs | 18 +++++++++++++++--- crates/ide/src/view_hir.rs | 24 +++++------------------- 2 files changed, 20 insertions(+), 22 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 9b78944c6..f68299d3a 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -1,5 +1,5 @@ //! FIXME: write short doc here -use std::{iter, sync::Arc}; +use std::{fmt::Write, iter, sync::Arc}; use arrayvec::ArrayVec; use base_db::{CrateDisplayName, CrateId, Edition, FileId}; @@ -729,8 +729,7 @@ impl DefWithBody { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Function { - // DO NOT MERGE: this was previously pub(crate) - pub id: FunctionId, + pub(crate) id: FunctionId, } impl Function { @@ -798,6 +797,19 @@ impl Function { pub fn has_body(self, db: &dyn HirDatabase) -> bool { db.function_data(self.id).has_body } + + /// A textual representation of the HIR of this function for debugging purposes. + pub fn debug_hir(self, db: &dyn HirDatabase) -> String { + let body = db.body(self.id.into()); + + let mut result = String::new(); + writeln!(&mut result, "HIR expressions in the body of `{}`:", self.name(db)).unwrap(); + for (id, expr) in body.exprs.iter() { + writeln!(&mut result, "{:?}: {:?}", id, expr).unwrap(); + } + + result + } } // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. diff --git a/crates/ide/src/view_hir.rs b/crates/ide/src/view_hir.rs index e48f2cfe0..cfcfb7cfb 100644 --- a/crates/ide/src/view_hir.rs +++ b/crates/ide/src/view_hir.rs @@ -1,11 +1,9 @@ use hir::{Function, Semantics}; -use hir::db::DefDatabase; use ide_db::base_db::FilePosition; use ide_db::RootDatabase; -use syntax::{AstNode, algo::find_node_at_offset, ast}; -use std::fmt::Write; +use syntax::{algo::find_node_at_offset, ast, AstNode}; -// Feature: View hir +// Feature: View Hir // // |=== // | Editor | Action Name @@ -20,20 +18,8 @@ fn body_hir(db: &RootDatabase, position: FilePosition) -> Option { let sema = Semantics::new(db); let source_file = sema.parse(position.file_id); - let function = find_node_at_offset::( - source_file.syntax(), - position.offset, - )?; + let function = find_node_at_offset::(source_file.syntax(), position.offset)?; let function: Function = sema.to_def(&function)?; - let body = db.body(function.id.into()); - - let mut result = String::new(); - writeln!(&mut result, "== Body expressions ==").ok()?; - - for (id, expr) in body.exprs.iter() { - writeln!(&mut result, "{:?}: {:?}", id, expr).ok()?; - } - - Some(result) -} \ No newline at end of file + Some(function.debug_hir(db)) +} -- cgit v1.2.3