aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/view_hir.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-03 09:03:15 +0000
committerGitHub <[email protected]>2021-01-03 09:03:15 +0000
commit1cc73d60bbd7149773f2eb57296d5611cbe941b1 (patch)
tree631a683b73784e633baf84c826a81903922095a7 /crates/ide/src/view_hir.rs
parent520b8a5a4dde032ba6118efb02801611191acc4e (diff)
parentee7c3f79e29bf140fe6faaf52bee63dba2fc29b1 (diff)
Merge #7068
7068: Add VSCode command to view the hir of a function body r=theotherphil a=theotherphil Will fix https://github.com/rust-analyzer/rust-analyzer/issues/7061. Very rough initial version just to work out where I needed to wire everything up. @matklad would you be happy merging a hir visualiser of some kind? If so, do you have any thoughts on what you'd like it show, and how? I've spent very little time on this thus far, so I'm fine with throwing away the contents of this PR, but I want to avoid taking the time to make this more polished/interactive/useful only to discover that no-one else has any interest in this functionality. ![image](https://user-images.githubusercontent.com/1974256/103236081-bb58f700-493b-11eb-9d12-55ae1b870f8f.png) Co-authored-by: Phil Ellison <[email protected]>
Diffstat (limited to 'crates/ide/src/view_hir.rs')
-rw-r--r--crates/ide/src/view_hir.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/crates/ide/src/view_hir.rs b/crates/ide/src/view_hir.rs
new file mode 100644
index 000000000..cfcfb7cfb
--- /dev/null
+++ b/crates/ide/src/view_hir.rs
@@ -0,0 +1,25 @@
1use hir::{Function, Semantics};
2use ide_db::base_db::FilePosition;
3use ide_db::RootDatabase;
4use syntax::{algo::find_node_at_offset, ast, AstNode};
5
6// Feature: View Hir
7//
8// |===
9// | Editor | Action Name
10//
11// | VS Code | **Rust Analyzer: View Hir**
12// |===
13pub(crate) fn view_hir(db: &RootDatabase, position: FilePosition) -> String {
14 body_hir(db, position).unwrap_or("Not inside a function body".to_string())
15}
16
17fn body_hir(db: &RootDatabase, position: FilePosition) -> Option<String> {
18 let sema = Semantics::new(db);
19 let source_file = sema.parse(position.file_id);
20
21 let function = find_node_at_offset::<ast::Fn>(source_file.syntax(), position.offset)?;
22
23 let function: Function = sema.to_def(&function)?;
24 Some(function.debug_hir(db))
25}