From 4f26a3734ea3ca9da658c2c33c11ef4b87fddf89 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 2 Jul 2020 15:32:56 +0200 Subject: [PATCH] Indicate when project needs a reload --- crates/rust-analyzer/src/global_state.rs | 1 + crates/rust-analyzer/src/main_loop.rs | 38 ++++++++++++++++++- crates/rust-analyzer/src/reload.rs | 2 +- .../tests/heavy_tests/support.rs | 17 ++++++--- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 5e9cae3f84..640b3959df 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -32,6 +32,7 @@ pub(crate) enum Status { Loading, Ready, Invalid, + NeedsReload, } impl Default for Status { diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index a5a8c17a07..d4d18a8082 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -111,6 +111,35 @@ impl GlobalState { } fn run(mut self, inbox: Receiver) -> Result<()> { + let registration_options = lsp_types::TextDocumentRegistrationOptions { + document_selector: Some(vec![ + lsp_types::DocumentFilter { + language: None, + scheme: None, + pattern: Some("**/*.rs".into()), + }, + lsp_types::DocumentFilter { + language: None, + scheme: None, + pattern: Some("**/Cargo.toml".into()), + }, + lsp_types::DocumentFilter { + language: None, + scheme: None, + pattern: Some("**/Cargo.lock".into()), + }, + ]), + }; + let registration = lsp_types::Registration { + id: "textDocument/didSave".to_string(), + method: "textDocument/didSave".to_string(), + register_options: Some(serde_json::to_value(registration_options).unwrap()), + }; + self.send_request::( + lsp_types::RegistrationParams { registrations: vec![registration] }, + |_, _| (), + ); + self.reload(); while let Some(event) = self.next_event(&inbox) { @@ -281,6 +310,7 @@ impl GlobalState { Status::Loading => lsp_ext::Status::Loading, Status::Ready => lsp_ext::Status::Ready, Status::Invalid => lsp_ext::Status::Invalid, + Status::NeedsReload => lsp_ext::Status::NeedsReload, }; self.send_notification::(lsp_status); } @@ -395,10 +425,16 @@ impl GlobalState { ); Ok(()) })? - .on::(|this, _params| { + .on::(|this, params| { if let Some(flycheck) = &this.flycheck { flycheck.handle.update(); } + let uri = params.text_document.uri.as_str(); + if uri.ends_with("Cargo.toml") || uri.ends_with("Cargo.lock") { + if matches!(this.status, Status::Ready | Status::Invalid) { + this.transition(Status::NeedsReload); + } + } Ok(()) })? .on::(|this, _params| { diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs index 0c1fd1b8b5..523b04b97a 100644 --- a/crates/rust-analyzer/src/reload.rs +++ b/crates/rust-analyzer/src/reload.rs @@ -78,7 +78,7 @@ impl GlobalState { .collect(), }; let registration = lsp_types::Registration { - id: "file-watcher".to_string(), + id: "workspace/didChangeWatchedFiles".to_string(), method: "workspace/didChangeWatchedFiles".to_string(), register_options: Some(serde_json::to_value(registration_options).unwrap()), }; diff --git a/crates/rust-analyzer/tests/heavy_tests/support.rs b/crates/rust-analyzer/tests/heavy_tests/support.rs index 49f194f7ef..7bf687794c 100644 --- a/crates/rust-analyzer/tests/heavy_tests/support.rs +++ b/crates/rust-analyzer/tests/heavy_tests/support.rs @@ -176,12 +176,19 @@ impl Server { while let Some(msg) = self.recv() { match msg { Message::Request(req) => { - if req.method != "window/workDoneProgress/create" - && !(req.method == "client/registerCapability" - && req.params.to_string().contains("workspace/didChangeWatchedFiles")) - { - panic!("unexpected request: {:?}", req) + if req.method == "window/workDoneProgress/create" { + continue; } + if req.method == "client/registerCapability" { + let params = req.params.to_string(); + if ["workspace/didChangeWatchedFiles", "textDocument/didSave"] + .iter() + .any(|&it| params.contains(it)) + { + continue; + } + } + panic!("unexpected request: {:?}", req) } Message::Notification(_) => (), Message::Response(res) => {