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/hir/src/code_model.rs | 3 ++- crates/ide/src/lib.rs | 5 +++++ crates/ide/src/view_hir.rs | 39 +++++++++++++++++++++++++++++++++++ crates/rust-analyzer/src/handlers.rs | 10 +++++++++ crates/rust-analyzer/src/lsp_ext.rs | 8 +++++++ crates/rust-analyzer/src/main_loop.rs | 1 + 6 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 crates/ide/src/view_hir.rs (limited to 'crates') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index b7ded3478..9b78944c6 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -729,7 +729,8 @@ impl DefWithBody { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Function { - pub(crate) id: FunctionId, + // DO NOT MERGE: this was previously pub(crate) + pub id: FunctionId, } impl Function { 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 diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 374fb5302..85f1f81ad 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -104,6 +104,16 @@ pub(crate) fn handle_syntax_tree( Ok(res) } +pub(crate) fn handle_view_hir( + snap: GlobalStateSnapshot, + params: lsp_types::TextDocumentPositionParams, +) -> Result { + let _p = profile::span("handle_view_hir"); + let position = from_proto::file_position(&snap, params)?; + let res = snap.analysis.view_hir(position)?; + Ok(res) +} + pub(crate) fn handle_expand_macro( snap: GlobalStateSnapshot, params: lsp_ext::ExpandMacroParams, diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 93ac45415..a85978737 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -53,6 +53,14 @@ pub struct SyntaxTreeParams { pub range: Option, } +pub enum ViewHir {} + +impl Request for ViewHir { + type Params = lsp_types::TextDocumentPositionParams; + type Result = String; + const METHOD: &'static str = "rust-analyzer/viewHir"; +} + pub enum ExpandMacro {} impl Request for ExpandMacro { diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 5d55dc96e..8eca79f7e 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -443,6 +443,7 @@ impl GlobalState { .on_sync::(|s, p| handlers::handle_memory_usage(s, p))? .on::(handlers::handle_analyzer_status) .on::(handlers::handle_syntax_tree) + .on::(handlers::handle_view_hir) .on::(handlers::handle_expand_macro) .on::(handlers::handle_parent_module) .on::(handlers::handle_runnables) -- cgit v1.2.3