aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/main_loop/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/main_loop/mod.rs')
-rw-r--r--crates/ra_lsp_server/src/main_loop/mod.rs38
1 files changed, 17 insertions, 21 deletions
diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs
index 165f2e78f..b35ebd38b 100644
--- a/crates/ra_lsp_server/src/main_loop/mod.rs
+++ b/crates/ra_lsp_server/src/main_loop/mod.rs
@@ -8,9 +8,9 @@ use gen_lsp_server::{
8 handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, 8 handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse,
9}; 9};
10use languageserver_types::NumberOrString; 10use languageserver_types::NumberOrString;
11use ra_analysis::{FileId, JobHandle, JobToken, LibraryData}; 11use ra_analysis::{FileId, LibraryData};
12use rayon::{self, ThreadPool}; 12use rayon::{self, ThreadPool};
13use rustc_hash::FxHashMap; 13use rustc_hash::FxHashSet;
14use serde::{de::DeserializeOwned, Serialize}; 14use serde::{de::DeserializeOwned, Serialize};
15 15
16use crate::{ 16use crate::{
@@ -47,7 +47,7 @@ pub fn main_loop(
47 info!("server initialized, serving requests"); 47 info!("server initialized, serving requests");
48 let mut state = ServerWorldState::new(); 48 let mut state = ServerWorldState::new();
49 49
50 let mut pending_requests = FxHashMap::default(); 50 let mut pending_requests = FxHashSet::default();
51 let mut subs = Subscriptions::new(); 51 let mut subs = Subscriptions::new();
52 let main_res = main_loop_inner( 52 let main_res = main_loop_inner(
53 internal_mode, 53 internal_mode,
@@ -92,7 +92,7 @@ fn main_loop_inner(
92 fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, 92 fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
93 ws_worker: Worker<PathBuf, Result<CargoWorkspace>>, 93 ws_worker: Worker<PathBuf, Result<CargoWorkspace>>,
94 state: &mut ServerWorldState, 94 state: &mut ServerWorldState,
95 pending_requests: &mut FxHashMap<u64, JobHandle>, 95 pending_requests: &mut FxHashSet<u64>,
96 subs: &mut Subscriptions, 96 subs: &mut Subscriptions,
97) -> Result<()> { 97) -> Result<()> {
98 let (libdata_sender, libdata_receiver) = unbounded(); 98 let (libdata_sender, libdata_receiver) = unbounded();
@@ -204,14 +204,13 @@ fn main_loop_inner(
204fn on_task( 204fn on_task(
205 task: Task, 205 task: Task,
206 msg_sender: &Sender<RawMessage>, 206 msg_sender: &Sender<RawMessage>,
207 pending_requests: &mut FxHashMap<u64, JobHandle>, 207 pending_requests: &mut FxHashSet<u64>,
208) { 208) {
209 match task { 209 match task {
210 Task::Respond(response) => { 210 Task::Respond(response) => {
211 if let Some(handle) = pending_requests.remove(&response.id) { 211 if pending_requests.remove(&response.id) {
212 assert!(handle.has_completed()); 212 msg_sender.send(RawMessage::Response(response))
213 } 213 }
214 msg_sender.send(RawMessage::Response(response))
215 } 214 }
216 Task::Notify(n) => msg_sender.send(RawMessage::Notification(n)), 215 Task::Notify(n) => msg_sender.send(RawMessage::Notification(n)),
217 } 216 }
@@ -219,7 +218,7 @@ fn on_task(
219 218
220fn on_request( 219fn on_request(
221 world: &mut ServerWorldState, 220 world: &mut ServerWorldState,
222 pending_requests: &mut FxHashMap<u64, JobHandle>, 221 pending_requests: &mut FxHashSet<u64>,
223 pool: &ThreadPool, 222 pool: &ThreadPool,
224 sender: &Sender<Task>, 223 sender: &Sender<Task>,
225 req: RawRequest, 224 req: RawRequest,
@@ -253,8 +252,8 @@ fn on_request(
253 .on::<req::References>(handlers::handle_references)? 252 .on::<req::References>(handlers::handle_references)?
254 .finish(); 253 .finish();
255 match req { 254 match req {
256 Ok((id, handle)) => { 255 Ok(id) => {
257 let inserted = pending_requests.insert(id, handle).is_none(); 256 let inserted = pending_requests.insert(id);
258 assert!(inserted, "duplicate request: {}", id); 257 assert!(inserted, "duplicate request: {}", id);
259 Ok(None) 258 Ok(None)
260 } 259 }
@@ -265,7 +264,7 @@ fn on_request(
265fn on_notification( 264fn on_notification(
266 msg_sender: &Sender<RawMessage>, 265 msg_sender: &Sender<RawMessage>,
267 state: &mut ServerWorldState, 266 state: &mut ServerWorldState,
268 pending_requests: &mut FxHashMap<u64, JobHandle>, 267 pending_requests: &mut FxHashSet<u64>,
269 subs: &mut Subscriptions, 268 subs: &mut Subscriptions,
270 not: RawNotification, 269 not: RawNotification,
271) -> Result<()> { 270) -> Result<()> {
@@ -277,9 +276,7 @@ fn on_notification(
277 panic!("string id's not supported: {:?}", id); 276 panic!("string id's not supported: {:?}", id);
278 } 277 }
279 }; 278 };
280 if let Some(handle) = pending_requests.remove(&id) { 279 pending_requests.remove(&id);
281 handle.cancel();
282 }
283 return Ok(()); 280 return Ok(());
284 } 281 }
285 Err(not) => not, 282 Err(not) => not,
@@ -336,7 +333,7 @@ fn on_notification(
336 333
337struct PoolDispatcher<'a> { 334struct PoolDispatcher<'a> {
338 req: Option<RawRequest>, 335 req: Option<RawRequest>,
339 res: Option<(u64, JobHandle)>, 336 res: Option<u64>,
340 pool: &'a ThreadPool, 337 pool: &'a ThreadPool,
341 world: &'a ServerWorldState, 338 world: &'a ServerWorldState,
342 sender: &'a Sender<Task>, 339 sender: &'a Sender<Task>,
@@ -345,7 +342,7 @@ struct PoolDispatcher<'a> {
345impl<'a> PoolDispatcher<'a> { 342impl<'a> PoolDispatcher<'a> {
346 fn on<'b, R>( 343 fn on<'b, R>(
347 &'b mut self, 344 &'b mut self,
348 f: fn(ServerWorld, R::Params, JobToken) -> Result<R::Result>, 345 f: fn(ServerWorld, R::Params) -> Result<R::Result>,
349 ) -> Result<&'b mut Self> 346 ) -> Result<&'b mut Self>
350 where 347 where
351 R: req::Request, 348 R: req::Request,
@@ -358,11 +355,10 @@ impl<'a> PoolDispatcher<'a> {
358 }; 355 };
359 match req.cast::<R>() { 356 match req.cast::<R>() {
360 Ok((id, params)) => { 357 Ok((id, params)) => {
361 let (handle, token) = JobHandle::new();
362 let world = self.world.snapshot(); 358 let world = self.world.snapshot();
363 let sender = self.sender.clone(); 359 let sender = self.sender.clone();
364 self.pool.spawn(move || { 360 self.pool.spawn(move || {
365 let resp = match f(world, params, token) { 361 let resp = match f(world, params) {
366 Ok(resp) => RawResponse::ok::<R>(id, &resp), 362 Ok(resp) => RawResponse::ok::<R>(id, &resp),
367 Err(e) => { 363 Err(e) => {
368 RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()) 364 RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string())
@@ -371,14 +367,14 @@ impl<'a> PoolDispatcher<'a> {
371 let task = Task::Respond(resp); 367 let task = Task::Respond(resp);
372 sender.send(task); 368 sender.send(task);
373 }); 369 });
374 self.res = Some((id, handle)); 370 self.res = Some(id);
375 } 371 }
376 Err(req) => self.req = Some(req), 372 Err(req) => self.req = Some(req),
377 } 373 }
378 Ok(self) 374 Ok(self)
379 } 375 }
380 376
381 fn finish(&mut self) -> ::std::result::Result<(u64, JobHandle), RawRequest> { 377 fn finish(&mut self) -> ::std::result::Result<u64, RawRequest> {
382 match (self.res.take(), self.req.take()) { 378 match (self.res.take(), self.req.take()) {
383 (Some(res), None) => Ok(res), 379 (Some(res), None) => Ok(res),
384 (None, Some(req)) => Err(req), 380 (None, Some(req)) => Err(req),