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/ide/src/lib.rs | 5 +++++ crates/ide/src/view_hir.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 crates/ide/src/view_hir.rs (limited to 'crates/ide') diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 41eb139d1..25c2047ca 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -31,6 +31,7 @@ mod folding_ranges; mod goto_definition; mod goto_implementation; mod goto_type_definition; +mod view_hir; mod hover; mod inlay_hints; mod join_lines; @@ -271,6 +272,10 @@ impl Analysis { self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) } + pub fn view_hir(&self, position: FilePosition) -> Cancelable { + self.with_db(|db| view_hir::view_hir(&db, position)) + } + pub fn expand_macro(&self, position: FilePosition) -> Cancelable> { self.with_db(|db| expand_macro::expand_macro(db, position)) } diff --git a/crates/ide/src/view_hir.rs b/crates/ide/src/view_hir.rs new file mode 100644 index 000000000..e48f2cfe0 --- /dev/null +++ b/crates/ide/src/view_hir.rs @@ -0,0 +1,39 @@ +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; + +// Feature: View hir +// +// |=== +// | Editor | Action Name +// +// | VS Code | **Rust Analyzer: View Hir** +// |=== +pub(crate) fn view_hir(db: &RootDatabase, position: FilePosition) -> String { + body_hir(db, position).unwrap_or("Not inside a function body".to_string()) +} + +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: 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 -- 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/ide/src/view_hir.rs | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'crates/ide') 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