diff options
author | Aleksey Kladov <[email protected]> | 2020-06-26 16:07:14 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-26 16:07:14 +0100 |
commit | 9d15e8fc4f875f6da2cd2bd89c62bb96fa64ef1b (patch) | |
tree | 8d782ee4c736b57c8eaab0e65ee9746c3880a4e4 /crates/rust-analyzer/src/main_loop.rs | |
parent | 1893289e5c7cebeeb9705c031c996fc29d8c5b54 (diff) |
Type safer requests
Diffstat (limited to 'crates/rust-analyzer/src/main_loop.rs')
-rw-r--r-- | crates/rust-analyzer/src/main_loop.rs | 40 |
1 files changed, 16 insertions, 24 deletions
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index ae3c7e30e..e5194fe41 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -7,7 +7,7 @@ use std::{ | |||
7 | 7 | ||
8 | use crossbeam_channel::{never, select, Receiver}; | 8 | use crossbeam_channel::{never, select, Receiver}; |
9 | use lsp_server::{Connection, Notification, Request, Response}; | 9 | use lsp_server::{Connection, Notification, Request, Response}; |
10 | use lsp_types::{notification::Notification as _, request::Request as _}; | 10 | use lsp_types::notification::Notification as _; |
11 | use ra_db::VfsPath; | 11 | use ra_db::VfsPath; |
12 | use ra_ide::{Canceled, FileId}; | 12 | use ra_ide::{Canceled, FileId}; |
13 | use ra_prof::profile; | 13 | use ra_prof::profile; |
@@ -18,7 +18,7 @@ use crate::{ | |||
18 | from_proto, | 18 | from_proto, |
19 | global_state::{file_id_to_url, url_to_file_id, GlobalState, Status}, | 19 | global_state::{file_id_to_url, url_to_file_id, GlobalState, Status}, |
20 | handlers, lsp_ext, | 20 | handlers, lsp_ext, |
21 | lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new, Progress}, | 21 | lsp_utils::{apply_document_changes, is_canceled, notification_is, Progress}, |
22 | Result, | 22 | Result, |
23 | }; | 23 | }; |
24 | 24 | ||
@@ -143,10 +143,7 @@ impl GlobalState { | |||
143 | lsp_server::Message::Notification(not) => { | 143 | lsp_server::Message::Notification(not) => { |
144 | self.on_notification(not)?; | 144 | self.on_notification(not)?; |
145 | } | 145 | } |
146 | lsp_server::Message::Response(resp) => { | 146 | lsp_server::Message::Response(resp) => self.complete_request(resp), |
147 | let handler = self.req_queue.outgoing.complete(resp.id.clone()); | ||
148 | handler(self, resp) | ||
149 | } | ||
150 | }, | 147 | }, |
151 | Event::Task(task) => { | 148 | Event::Task(task) => { |
152 | match task { | 149 | match task { |
@@ -250,10 +247,9 @@ impl GlobalState { | |||
250 | for file_id in diagnostic_changes { | 247 | for file_id in diagnostic_changes { |
251 | let url = file_id_to_url(&self.vfs.read().0, file_id); | 248 | let url = file_id_to_url(&self.vfs.read().0, file_id); |
252 | let diagnostics = self.diagnostics.diagnostics_for(file_id).cloned().collect(); | 249 | let diagnostics = self.diagnostics.diagnostics_for(file_id).cloned().collect(); |
253 | let params = | 250 | self.send_notification::<lsp_types::notification::PublishDiagnostics>( |
254 | lsp_types::PublishDiagnosticsParams { uri: url, diagnostics, version: None }; | 251 | lsp_types::PublishDiagnosticsParams { uri: url, diagnostics, version: None }, |
255 | let not = notification_new::<lsp_types::notification::PublishDiagnostics>(params); | 252 | ); |
256 | self.send(not.into()); | ||
257 | } | 253 | } |
258 | } | 254 | } |
259 | 255 | ||
@@ -271,7 +267,7 @@ impl GlobalState { | |||
271 | } | 267 | } |
272 | 268 | ||
273 | fn on_request(&mut self, request_received: Instant, req: Request) -> Result<()> { | 269 | fn on_request(&mut self, request_received: Instant, req: Request) -> Result<()> { |
274 | self.req_queue.incoming.register(req.id.clone(), (req.method.clone(), request_received)); | 270 | self.register_request(&req, request_received); |
275 | 271 | ||
276 | RequestDispatcher { req: Some(req), global_state: self } | 272 | RequestDispatcher { req: Some(req), global_state: self } |
277 | .on_sync::<lsp_ext::CollectGarbage>(|s, ()| Ok(s.analysis_host.collect_garbage()))? | 273 | .on_sync::<lsp_ext::CollectGarbage>(|s, ()| Ok(s.analysis_host.collect_garbage()))? |
@@ -335,9 +331,7 @@ impl GlobalState { | |||
335 | lsp_types::NumberOrString::Number(id) => id.into(), | 331 | lsp_types::NumberOrString::Number(id) => id.into(), |
336 | lsp_types::NumberOrString::String(id) => id.into(), | 332 | lsp_types::NumberOrString::String(id) => id.into(), |
337 | }; | 333 | }; |
338 | if let Some(response) = this.req_queue.incoming.cancel(id) { | 334 | this.cancel(id); |
339 | this.send(response.into()); | ||
340 | } | ||
341 | Ok(()) | 335 | Ok(()) |
342 | })? | 336 | })? |
343 | .on::<lsp_types::notification::DidOpenTextDocument>(|this, params| { | 337 | .on::<lsp_types::notification::DidOpenTextDocument>(|this, params| { |
@@ -372,13 +366,13 @@ impl GlobalState { | |||
372 | this.loader.handle.invalidate(path.to_path_buf()); | 366 | this.loader.handle.invalidate(path.to_path_buf()); |
373 | } | 367 | } |
374 | } | 368 | } |
375 | let params = lsp_types::PublishDiagnosticsParams { | 369 | this.send_notification::<lsp_types::notification::PublishDiagnostics>( |
376 | uri: params.text_document.uri, | 370 | lsp_types::PublishDiagnosticsParams { |
377 | diagnostics: Vec::new(), | 371 | uri: params.text_document.uri, |
378 | version: None, | 372 | diagnostics: Vec::new(), |
379 | }; | 373 | version: None, |
380 | let not = notification_new::<lsp_types::notification::PublishDiagnostics>(params); | 374 | }, |
381 | this.send(not.into()); | 375 | ); |
382 | Ok(()) | 376 | Ok(()) |
383 | })? | 377 | })? |
384 | .on::<lsp_types::notification::DidSaveTextDocument>(|this, _params| { | 378 | .on::<lsp_types::notification::DidSaveTextDocument>(|this, _params| { |
@@ -390,8 +384,7 @@ impl GlobalState { | |||
390 | .on::<lsp_types::notification::DidChangeConfiguration>(|this, _params| { | 384 | .on::<lsp_types::notification::DidChangeConfiguration>(|this, _params| { |
391 | // As stated in https://github.com/microsoft/language-server-protocol/issues/676, | 385 | // As stated in https://github.com/microsoft/language-server-protocol/issues/676, |
392 | // this notification's parameters should be ignored and the actual config queried separately. | 386 | // this notification's parameters should be ignored and the actual config queried separately. |
393 | let request = this.req_queue.outgoing.register( | 387 | this.send_request::<lsp_types::request::WorkspaceConfiguration>( |
394 | lsp_types::request::WorkspaceConfiguration::METHOD.to_string(), | ||
395 | lsp_types::ConfigurationParams { | 388 | lsp_types::ConfigurationParams { |
396 | items: vec![lsp_types::ConfigurationItem { | 389 | items: vec![lsp_types::ConfigurationItem { |
397 | scope_uri: None, | 390 | scope_uri: None, |
@@ -419,7 +412,6 @@ impl GlobalState { | |||
419 | } | 412 | } |
420 | }, | 413 | }, |
421 | ); | 414 | ); |
422 | this.send(request.into()); | ||
423 | 415 | ||
424 | return Ok(()); | 416 | return Ok(()); |
425 | })? | 417 | })? |