From 271ec6b990523c79f93468a5b0ab5e1aceab50f6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 21 May 2021 23:59:52 +0200 Subject: Add a "Debug ItemTree" LSP request --- crates/ide/src/lib.rs | 5 +++++ crates/ide/src/view_item_tree.rs | 16 ++++++++++++++++ crates/rust-analyzer/src/handlers.rs | 10 ++++++++++ crates/rust-analyzer/src/lsp_ext.rs | 14 ++++++++++++++ crates/rust-analyzer/src/main_loop.rs | 1 + 5 files changed, 46 insertions(+) create mode 100644 crates/ide/src/view_item_tree.rs (limited to 'crates') diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index f4b90db3a..ff2a54117 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -50,6 +50,7 @@ mod typing; mod markdown_remove; mod doc_links; mod view_crate_graph; +mod view_item_tree; use std::sync::Arc; @@ -288,6 +289,10 @@ impl Analysis { self.with_db(|db| view_hir::view_hir(&db, position)) } + pub fn view_item_tree(&self, file_id: FileId) -> Cancelable { + self.with_db(|db| view_item_tree::view_item_tree(&db, file_id)) + } + /// Renders the crate graph to GraphViz "dot" syntax. pub fn view_crate_graph(&self) -> Cancelable> { self.with_db(|db| view_crate_graph::view_crate_graph(&db)) diff --git a/crates/ide/src/view_item_tree.rs b/crates/ide/src/view_item_tree.rs new file mode 100644 index 000000000..3dc03085d --- /dev/null +++ b/crates/ide/src/view_item_tree.rs @@ -0,0 +1,16 @@ +use hir::db::DefDatabase; +use ide_db::base_db::FileId; +use ide_db::RootDatabase; + +// Feature: Debug ItemTree +// +// Displays the ItemTree of the currently open file, for debugging. +// +// |=== +// | Editor | Action Name +// +// | VS Code | **Rust Analyzer: Debug ItemTree** +// |=== +pub(crate) fn view_item_tree(db: &RootDatabase, file_id: FileId) -> String { + db.file_item_tree(file_id.into()).pretty_print() +} diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 51041d7a0..aa12fd94b 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -117,6 +117,16 @@ pub(crate) fn handle_view_hir( Ok(res) } +pub(crate) fn handle_view_item_tree( + snap: GlobalStateSnapshot, + params: lsp_ext::ViewItemTreeParams, +) -> Result { + let _p = profile::span("handle_view_item_tree"); + let file_id = from_proto::file_id(&snap, ¶ms.text_document.uri)?; + let res = snap.analysis.view_item_tree(file_id)?; + Ok(res) +} + pub(crate) fn handle_view_crate_graph(snap: GlobalStateSnapshot, (): ()) -> Result { let _p = profile::span("handle_view_crate_graph"); let dot = snap.analysis.view_crate_graph()??; diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 34b53a7a8..905048793 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -70,6 +70,20 @@ impl Request for ViewCrateGraph { const METHOD: &'static str = "rust-analyzer/viewCrateGraph"; } +#[derive(Deserialize, Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct ViewItemTreeParams { + pub text_document: TextDocumentIdentifier, +} + +pub enum ViewItemTree {} + +impl Request for ViewItemTree { + type Params = ViewItemTreeParams; + type Result = String; + const METHOD: &'static str = "rust-analyzer/viewItemTree"; +} + 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 4e0791611..f837b89dd 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -514,6 +514,7 @@ impl GlobalState { .on::(handlers::handle_syntax_tree) .on::(handlers::handle_view_hir) .on::(handlers::handle_view_crate_graph) + .on::(handlers::handle_view_item_tree) .on::(handlers::handle_expand_macro) .on::(handlers::handle_parent_module) .on::(handlers::handle_runnables) -- cgit v1.2.3