diff options
author | Phil Ellison <[email protected]> | 2021-01-01 19:25:18 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2021-01-01 19:25:18 +0000 |
commit | db53db804604574337f80a614565fb964403a654 (patch) | |
tree | 91b5ef245f803183d33f3d41511330e77d59c3c9 /crates | |
parent | 077592a12fd982de3e69572a4c738dd4468617f9 (diff) |
Address review suggestion, fix tidy tests
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir/src/code_model.rs | 18 | ||||
-rw-r--r-- | crates/ide/src/view_hir.rs | 24 |
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 |
2 | use std::{iter, sync::Arc}; | 2 | use std::{fmt::Write, iter, sync::Arc}; |
3 | 3 | ||
4 | use arrayvec::ArrayVec; | 4 | use arrayvec::ArrayVec; |
5 | use base_db::{CrateDisplayName, CrateId, Edition, FileId}; | 5 | use 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)] |
731 | pub struct Function { | 731 | pub struct Function { |
732 | // DO NOT MERGE: this was previously pub(crate) | 732 | pub(crate) id: FunctionId, |
733 | pub id: FunctionId, | ||
734 | } | 733 | } |
735 | 734 | ||
736 | impl Function { | 735 | impl 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 @@ | |||
1 | use hir::{Function, Semantics}; | 1 | use hir::{Function, Semantics}; |
2 | use hir::db::DefDatabase; | ||
3 | use ide_db::base_db::FilePosition; | 2 | use ide_db::base_db::FilePosition; |
4 | use ide_db::RootDatabase; | 3 | use ide_db::RootDatabase; |
5 | use syntax::{AstNode, algo::find_node_at_offset, ast}; | 4 | use syntax::{algo::find_node_at_offset, ast, AstNode}; |
6 | use 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 | ||