diff options
author | Phil Ellison <[email protected]> | 2020-12-28 18:29:58 +0000 |
---|---|---|
committer | Phil Ellison <[email protected]> | 2020-12-28 18:29:58 +0000 |
commit | 077592a12fd982de3e69572a4c738dd4468617f9 (patch) | |
tree | 24bc738d02fb8c88ef662f85e4a0c9a4c8ab0fac /crates/ide | |
parent | 1d530756ed7ba175ec32ff71247072798dc9a748 (diff) |
Initial implementation of view-hir command
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/lib.rs | 5 | ||||
-rw-r--r-- | crates/ide/src/view_hir.rs | 39 |
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; | |||
31 | mod goto_definition; | 31 | mod goto_definition; |
32 | mod goto_implementation; | 32 | mod goto_implementation; |
33 | mod goto_type_definition; | 33 | mod goto_type_definition; |
34 | mod view_hir; | ||
34 | mod hover; | 35 | mod hover; |
35 | mod inlay_hints; | 36 | mod inlay_hints; |
36 | mod join_lines; | 37 | mod 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 @@ | |||
1 | use hir::{Function, Semantics}; | ||
2 | use hir::db::DefDatabase; | ||
3 | use ide_db::base_db::FilePosition; | ||
4 | use ide_db::RootDatabase; | ||
5 | use syntax::{AstNode, algo::find_node_at_offset, ast}; | ||
6 | use std::fmt::Write; | ||
7 | |||
8 | // Feature: View hir | ||
9 | // | ||
10 | // |=== | ||
11 | // | Editor | Action Name | ||
12 | // | ||
13 | // | VS Code | **Rust Analyzer: View Hir** | ||
14 | // |=== | ||
15 | pub(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 | |||
19 | fn 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 | ||