aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/code_model.rs18
-rw-r--r--crates/ide/src/view_hir.rs24
2 files changed, 20 insertions, 22 deletions
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 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use std::{iter, sync::Arc}; 2use std::{fmt::Write, iter, sync::Arc};
3 3
4use arrayvec::ArrayVec; 4use arrayvec::ArrayVec;
5use base_db::{CrateDisplayName, CrateId, Edition, FileId}; 5use base_db::{CrateDisplayName, CrateId, Edition, FileId};
@@ -729,8 +729,7 @@ impl DefWithBody {
729 729
730#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 730#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
731pub struct Function { 731pub struct Function {
732 // DO NOT MERGE: this was previously pub(crate) 732 pub(crate) id: FunctionId,
733 pub id: FunctionId,
734} 733}
735 734
736impl Function { 735impl Function {
@@ -798,6 +797,19 @@ impl Function {
798 pub fn has_body(self, db: &dyn HirDatabase) -> bool { 797 pub fn has_body(self, db: &dyn HirDatabase) -> bool {
799 db.function_data(self.id).has_body 798 db.function_data(self.id).has_body
800 } 799 }
800
801 /// A textual representation of the HIR of this function for debugging purposes.
802 pub fn debug_hir(self, db: &dyn HirDatabase) -> String {
803 let body = db.body(self.id.into());
804
805 let mut result = String::new();
806 writeln!(&mut result, "HIR expressions in the body of `{}`:", self.name(db)).unwrap();
807 for (id, expr) in body.exprs.iter() {
808 writeln!(&mut result, "{:?}: {:?}", id, expr).unwrap();
809 }
810
811 result
812 }
801} 813}
802 814
803// Note: logically, this belongs to `hir_ty`, but we are not using it there yet. 815// 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 @@
1use hir::{Function, Semantics}; 1use hir::{Function, Semantics};
2use hir::db::DefDatabase;
3use ide_db::base_db::FilePosition; 2use ide_db::base_db::FilePosition;
4use ide_db::RootDatabase; 3use ide_db::RootDatabase;
5use syntax::{AstNode, algo::find_node_at_offset, ast}; 4use syntax::{algo::find_node_at_offset, ast, AstNode};
6use std::fmt::Write;
7 5
8// Feature: View hir 6// Feature: View Hir
9// 7//
10// |=== 8// |===
11// | Editor | Action Name 9// | Editor | Action Name
@@ -20,20 +18,8 @@ fn body_hir(db: &RootDatabase, position: FilePosition) -> Option<String> {
20 let sema = Semantics::new(db); 18 let sema = Semantics::new(db);
21 let source_file = sema.parse(position.file_id); 19 let source_file = sema.parse(position.file_id);
22 20
23 let function = find_node_at_offset::<ast::Fn>( 21 let function = find_node_at_offset::<ast::Fn>(source_file.syntax(), position.offset)?;
24 source_file.syntax(),
25 position.offset,
26 )?;
27 22
28 let function: Function = sema.to_def(&function)?; 23 let function: Function = sema.to_def(&function)?;
29 let body = db.body(function.id.into()); 24 Some(function.debug_hir(db))
30 25}
31 let mut result = String::new();
32 writeln!(&mut result, "== Body expressions ==").ok()?;
33
34 for (id, expr) in body.exprs.iter() {
35 writeln!(&mut result, "{:?}: {:?}", id, expr).ok()?;
36 }
37
38 Some(result)
39} \ No newline at end of file