From 077592a12fd982de3e69572a4c738dd4468617f9 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Mon, 28 Dec 2020 18:29:58 +0000 Subject: Initial implementation of view-hir command --- crates/hir/src/code_model.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index b7ded3478..9b78944c6 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -729,7 +729,8 @@ impl DefWithBody { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Function { - pub(crate) id: FunctionId, + // DO NOT MERGE: this was previously pub(crate) + pub id: FunctionId, } impl Function { -- cgit v1.2.3 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 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'crates/hir/src') 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. -- cgit v1.2.3 From ee7c3f79e29bf140fe6faaf52bee63dba2fc29b1 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Sun, 3 Jan 2021 08:54:33 +0000 Subject: Use stdx::format_to instead of writeln --- crates/hir/src/code_model.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index f68299d3a..804cdb143 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::{fmt::Write, iter, sync::Arc}; +use std::{iter, sync::Arc}; use arrayvec::ArrayVec; use base_db::{CrateDisplayName, CrateId, Edition, FileId}; @@ -39,7 +39,7 @@ use hir_ty::{ TyDefId, TyKind, TypeCtor, }; use rustc_hash::FxHashSet; -use stdx::impl_from; +use stdx::{format_to, impl_from}; use syntax::{ ast::{self, AttrsOwner, NameOwner}, AstNode, SmolStr, @@ -803,9 +803,9 @@ impl Function { 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(); + format_to!(result, "HIR expressions in the body of `{}`:\n", self.name(db)); for (id, expr) in body.exprs.iter() { - writeln!(&mut result, "{:?}: {:?}", id, expr).unwrap(); + format_to!(result, "{:?}: {:?}\n", id, expr); } result -- cgit v1.2.3