From ec2d0233831cdd1d88a0d0be43534a014c3e4899 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 31 Mar 2022 14:50:33 +0200 Subject: [PATCH] Add "view file text" command to debug sync issues --- crates/rust-analyzer/src/handlers.rs | 8 +++++ crates/rust-analyzer/src/lsp_ext.rs | 8 +++++ crates/rust-analyzer/src/main_loop.rs | 1 + docs/dev/lsp-extensions.md | 13 +++++++- editors/code/package.json | 9 +++++ editors/code/src/commands.ts | 48 +++++++++++++++++++++++++++ editors/code/src/lsp_ext.ts | 2 ++ editors/code/src/main.ts | 1 + 8 files changed, 89 insertions(+), 1 deletion(-) diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 8f91f64a69..15f4978367 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -123,6 +123,14 @@ pub(crate) fn handle_view_hir( Ok(res) } +pub(crate) fn handle_view_file_text( + snap: GlobalStateSnapshot, + params: lsp_types::TextDocumentIdentifier, +) -> Result { + let file_id = from_proto::file_id(&snap, ¶ms.uri)?; + Ok(snap.analysis.file_text(file_id)?.to_string()) +} + pub(crate) fn handle_view_item_tree( snap: GlobalStateSnapshot, params: lsp_ext::ViewItemTreeParams, diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 2e131eeac9..b638a57111 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -70,6 +70,14 @@ impl Request for ViewHir { const METHOD: &'static str = "rust-analyzer/viewHir"; } +pub enum ViewFileText {} + +impl Request for ViewFileText { + type Params = lsp_types::TextDocumentIdentifier; + type Result = String; + const METHOD: &'static str = "rust-analyzer/viewFileText"; +} + #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] pub struct ViewCrateGraphParams { diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index b62a830803..2d898d76a6 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -590,6 +590,7 @@ impl GlobalState { .on::(handlers::handle_analyzer_status) .on::(handlers::handle_syntax_tree) .on::(handlers::handle_view_hir) + .on::(handlers::handle_view_file_text) .on::(handlers::handle_view_crate_graph) .on::(handlers::handle_view_item_tree) .on::(handlers::handle_expand_macro) diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 3091bdcbf0..516d1caccc 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -1,5 +1,5 @@