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/global_state.rs | |
parent | 1893289e5c7cebeeb9705c031c996fc29d8c5b54 (diff) |
Type safer requests
Diffstat (limited to 'crates/rust-analyzer/src/global_state.rs')
-rw-r--r-- | crates/rust-analyzer/src/global_state.rs | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 41659f99d..3c9752259 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs | |||
@@ -7,7 +7,7 @@ use std::{sync::Arc, time::Instant}; | |||
7 | 7 | ||
8 | use crossbeam_channel::{unbounded, Receiver, Sender}; | 8 | use crossbeam_channel::{unbounded, Receiver, Sender}; |
9 | use flycheck::FlycheckHandle; | 9 | use flycheck::FlycheckHandle; |
10 | use lsp_types::Url; | 10 | use lsp_types::{request::Request as _, Url}; |
11 | use parking_lot::RwLock; | 11 | use parking_lot::RwLock; |
12 | use ra_db::{CrateId, VfsPath}; | 12 | use ra_db::{CrateId, VfsPath}; |
13 | use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FileId}; | 13 | use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FileId}; |
@@ -18,6 +18,7 @@ use crate::{ | |||
18 | diagnostics::{CheckFixes, DiagnosticCollection}, | 18 | diagnostics::{CheckFixes, DiagnosticCollection}, |
19 | from_proto, | 19 | from_proto, |
20 | line_endings::LineEndings, | 20 | line_endings::LineEndings, |
21 | lsp_utils::notification_new, | ||
21 | main_loop::Task, | 22 | main_loop::Task, |
22 | reload::SourceRootConfig, | 23 | reload::SourceRootConfig, |
23 | request_metrics::{LatestRequests, RequestMetrics}, | 24 | request_metrics::{LatestRequests, RequestMetrics}, |
@@ -57,6 +58,7 @@ pub(crate) type ReqQueue = lsp_server::ReqQueue<(String, Instant), ReqHandler>; | |||
57 | /// Note that this struct has more than on impl in various modules! | 58 | /// Note that this struct has more than on impl in various modules! |
58 | pub(crate) struct GlobalState { | 59 | pub(crate) struct GlobalState { |
59 | sender: Sender<lsp_server::Message>, | 60 | sender: Sender<lsp_server::Message>, |
61 | req_queue: ReqQueue, | ||
60 | pub(crate) task_pool: Handle<TaskPool<Task>, Receiver<Task>>, | 62 | pub(crate) task_pool: Handle<TaskPool<Task>, Receiver<Task>>, |
61 | pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>, | 63 | pub(crate) loader: Handle<Box<dyn vfs::loader::Handle>, Receiver<vfs::loader::Message>>, |
62 | pub(crate) flycheck: Option<Handle<FlycheckHandle, Receiver<flycheck::Message>>>, | 64 | pub(crate) flycheck: Option<Handle<FlycheckHandle, Receiver<flycheck::Message>>>, |
@@ -66,7 +68,6 @@ pub(crate) struct GlobalState { | |||
66 | pub(crate) mem_docs: FxHashSet<VfsPath>, | 68 | pub(crate) mem_docs: FxHashSet<VfsPath>, |
67 | pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>, | 69 | pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>, |
68 | pub(crate) status: Status, | 70 | pub(crate) status: Status, |
69 | pub(crate) req_queue: ReqQueue, | ||
70 | pub(crate) source_root_config: SourceRootConfig, | 71 | pub(crate) source_root_config: SourceRootConfig, |
71 | pub(crate) proc_macro_client: ProcMacroClient, | 72 | pub(crate) proc_macro_client: ProcMacroClient, |
72 | pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>, | 73 | pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>, |
@@ -102,16 +103,16 @@ impl GlobalState { | |||
102 | let analysis_host = AnalysisHost::new(config.lru_capacity); | 103 | let analysis_host = AnalysisHost::new(config.lru_capacity); |
103 | GlobalState { | 104 | GlobalState { |
104 | sender, | 105 | sender, |
106 | req_queue: ReqQueue::default(), | ||
105 | task_pool, | 107 | task_pool, |
106 | loader, | 108 | loader, |
109 | flycheck: None, | ||
107 | config, | 110 | config, |
108 | analysis_host, | 111 | analysis_host, |
109 | flycheck: None, | ||
110 | diagnostics: Default::default(), | 112 | diagnostics: Default::default(), |
111 | mem_docs: FxHashSet::default(), | 113 | mem_docs: FxHashSet::default(), |
112 | vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))), | 114 | vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))), |
113 | status: Status::default(), | 115 | status: Status::default(), |
114 | req_queue: ReqQueue::default(), | ||
115 | source_root_config: SourceRootConfig::default(), | 116 | source_root_config: SourceRootConfig::default(), |
116 | proc_macro_client: ProcMacroClient::dummy(), | 117 | proc_macro_client: ProcMacroClient::dummy(), |
117 | workspaces: Arc::new(Vec::new()), | 118 | workspaces: Arc::new(Vec::new()), |
@@ -168,8 +169,39 @@ impl GlobalState { | |||
168 | } | 169 | } |
169 | } | 170 | } |
170 | 171 | ||
171 | pub(crate) fn send(&mut self, message: lsp_server::Message) { | 172 | pub(crate) fn send_request<R: lsp_types::request::Request>( |
172 | self.sender.send(message).unwrap() | 173 | &mut self, |
174 | params: R::Params, | ||
175 | handler: ReqHandler, | ||
176 | ) { | ||
177 | let request = self.req_queue.outgoing.register( | ||
178 | lsp_types::request::WorkDoneProgressCreate::METHOD.to_string(), | ||
179 | params, | ||
180 | handler, | ||
181 | ); | ||
182 | self.send(request.into()); | ||
183 | } | ||
184 | pub(crate) fn complete_request(&mut self, response: lsp_server::Response) { | ||
185 | let handler = self.req_queue.outgoing.complete(response.id.clone()); | ||
186 | handler(self, response) | ||
187 | } | ||
188 | |||
189 | pub(crate) fn send_notification<N: lsp_types::notification::Notification>( | ||
190 | &mut self, | ||
191 | params: N::Params, | ||
192 | ) { | ||
193 | let not = notification_new::<N>(params); | ||
194 | self.send(not.into()); | ||
195 | } | ||
196 | |||
197 | pub(crate) fn register_request( | ||
198 | &mut self, | ||
199 | request: &lsp_server::Request, | ||
200 | request_received: Instant, | ||
201 | ) { | ||
202 | self.req_queue | ||
203 | .incoming | ||
204 | .register(request.id.clone(), (request.method.clone(), request_received)); | ||
173 | } | 205 | } |
174 | pub(crate) fn respond(&mut self, response: lsp_server::Response) { | 206 | pub(crate) fn respond(&mut self, response: lsp_server::Response) { |
175 | if let Some((method, start)) = self.req_queue.incoming.complete(response.id.clone()) { | 207 | if let Some((method, start)) = self.req_queue.incoming.complete(response.id.clone()) { |
@@ -181,6 +213,15 @@ impl GlobalState { | |||
181 | self.send(response.into()); | 213 | self.send(response.into()); |
182 | } | 214 | } |
183 | } | 215 | } |
216 | pub(crate) fn cancel(&mut self, request_id: lsp_server::RequestId) { | ||
217 | if let Some(response) = self.req_queue.incoming.cancel(request_id) { | ||
218 | self.send(response.into()); | ||
219 | } | ||
220 | } | ||
221 | |||
222 | fn send(&mut self, message: lsp_server::Message) { | ||
223 | self.sender.send(message).unwrap() | ||
224 | } | ||
184 | } | 225 | } |
185 | 226 | ||
186 | impl Drop for GlobalState { | 227 | impl Drop for GlobalState { |