From 1be7af26a83b79863efb0d66a77b1fb7c0235bd2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Aug 2018 18:01:59 +0300 Subject: cleaner shutdown --- codeless/server/src/main.rs | 42 ++++++++++++++++++++++++++---------------- codeless/server/src/req.rs | 1 + 2 files changed, 27 insertions(+), 16 deletions(-) (limited to 'codeless/server') diff --git a/codeless/server/src/main.rs b/codeless/server/src/main.rs index 92f6a400c..fdb2fe2d5 100644 --- a/codeless/server/src/main.rs +++ b/codeless/server/src/main.rs @@ -20,7 +20,6 @@ mod caps; mod req; mod dispatch; -use languageserver_types::InitializeResult; use threadpool::ThreadPool; use crossbeam_channel::{bounded, Sender, Receiver}; use flexi_logger::Logger; @@ -46,7 +45,7 @@ fn main() -> Result<()> { Err(_) => { error!("server panicked"); bail!("server panicked") - }, + } } } @@ -72,7 +71,7 @@ fn initialize(io: &mut Io) -> Result<()> { match io.recv()? { RawMsg::Request(req) => { if let Some((_params, resp)) = dispatch::expect::(io, req)? { - resp.result(io, InitializeResult { + resp.result(io, req::InitializeResult { capabilities: caps::SERVER_CAPABILITIES })?; match io.recv()? { @@ -107,6 +106,7 @@ fn initialized(io: &mut Io) -> Result<()> { let res = main_loop(io, &mut world, &mut pool, sender, receiver.clone()); info!("waiting for background jobs to finish..."); receiver.for_each(drop); + pool.join(); info!("...background jobs have finished"); res } @@ -148,19 +148,29 @@ fn main_loop( match msg { RawMsg::Request(req) => { - if let Some((params, resp)) = dispatch::expect::(io, req)? { - let world = world.snapshot(); - let sender = sender.clone(); - pool.execute(move || { - let res: Result = (|| { - let path = params.text_document.uri.to_file_path() - .map_err(|()| format_err!("invalid path"))?; - let file = world.file_syntax(&path)?; - Ok(libeditor::syntax_tree(&file)) - })(); - - sender.send(Box::new(|io: &mut Io| resp.response(io, res))) - }); + let req = match dispatch::parse_as::(req)? { + Ok((params, resp)) => { + let world = world.snapshot(); + let sender = sender.clone(); + pool.execute(move || { + let res: Result = (|| { + let path = params.text_document.uri.to_file_path() + .map_err(|()| format_err!("invalid path"))?; + let file = world.file_syntax(&path)?; + Ok(libeditor::syntax_tree(&file)) + })(); + + sender.send(Box::new(|io: &mut Io| resp.response(io, res))) + }); + continue; + } + Err(req) => req, + }; + + if let Some(((), resp)) = dispatch::expect::(io, req)? { + info!("shutdown request"); + resp.result(io, ())?; + return Ok(()); } } msg => { diff --git a/codeless/server/src/req.rs b/codeless/server/src/req.rs index bc54c1d33..35a20a229 100644 --- a/codeless/server/src/req.rs +++ b/codeless/server/src/req.rs @@ -1,5 +1,6 @@ use languageserver_types::TextDocumentIdentifier; pub use languageserver_types::request::*; +pub use languageserver_types::{InitializeResult}; pub enum SyntaxTree {} -- cgit v1.2.3