aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
authorPhil Ellison <[email protected]>2020-12-28 18:29:58 +0000
committerPhil Ellison <[email protected]>2020-12-28 18:29:58 +0000
commit077592a12fd982de3e69572a4c738dd4468617f9 (patch)
tree24bc738d02fb8c88ef662f85e4a0c9a4c8ab0fac /crates/ide
parent1d530756ed7ba175ec32ff71247072798dc9a748 (diff)
Initial implementation of view-hir command
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/lib.rs5
-rw-r--r--crates/ide/src/view_hir.rs39
2 files changed, 44 insertions, 0 deletions
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;
31mod goto_definition; 31mod goto_definition;
32mod goto_implementation; 32mod goto_implementation;
33mod goto_type_definition; 33mod goto_type_definition;
34mod view_hir;
34mod hover; 35mod hover;
35mod inlay_hints; 36mod inlay_hints;
36mod join_lines; 37mod join_lines;
@@ -271,6 +272,10 @@ impl Analysis {
271 self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) 272 self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range))
272 } 273 }
273 274
275 pub fn view_hir(&self, position: FilePosition) -> Cancelable<String> {
276 self.with_db(|db| view_hir::view_hir(&db, position))
277 }
278
274 pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> { 279 pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
275 self.with_db(|db| expand_macro::expand_macro(db, position)) 280 self.with_db(|db| expand_macro::expand_macro(db, position))
276 } 281 }
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 @@
1use hir::{Function, Semantics};
2use hir::db::DefDatabase;
3use ide_db::base_db::FilePosition;
4use ide_db::RootDatabase;
5use syntax::{AstNode, algo::find_node_at_offset, ast};
6use std::fmt::Write;
7
8// Feature: View hir
9//
10// |===
11// | Editor | Action Name
12//
13// | VS Code | **Rust Analyzer: View Hir**
14// |===
15pub(crate) fn view_hir(db: &RootDatabase, position: FilePosition) -> String {
16 body_hir(db, position).unwrap_or("Not inside a function body".to_string())
17}
18
19fn body_hir(db: &RootDatabase, position: FilePosition) -> Option<String> {
20 let sema = Semantics::new(db);
21 let source_file = sema.parse(position.file_id);
22
23 let function = find_node_at_offset::<ast::Fn>(
24 source_file.syntax(),
25 position.offset,
26 )?;
27
28 let function: Function = sema.to_def(&function)?;
29 let body = db.body(function.id.into());
30
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