From bf42a75f1e9a86247067611fadbed48ea9d379b0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Aug 2018 23:45:35 +0300 Subject: dedupe --- crates/server/src/main.rs | 53 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-) (limited to 'crates/server/src') diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 6018350e3..f521271fb 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -27,11 +27,13 @@ use std::path::PathBuf; use threadpool::ThreadPool; use crossbeam_channel::{bounded, Sender, Receiver}; use flexi_logger::Logger; -use libanalysis::WorldState; use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier}; +use serde::{ser::Serialize, de::DeserializeOwned}; +use libanalysis::{WorldState, World}; use ::{ - io::{Io, RawMsg}, + io::{Io, RawMsg, RawRequest}, + req::Request, handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, }; @@ -171,24 +173,12 @@ fn main_loop( match msg { RawMsg::Request(req) => { let mut req = Some(req); - dispatch::handle_request::(&mut req, |params, resp| { - let world = world.snapshot(); - let sender = sender.clone(); - pool.execute(move || { - let res = handle_syntax_tree(world, params); - sender.send(Box::new(|io: &mut Io| resp.response(io, res))) - }); - Ok(()) - })?; - dispatch::handle_request::(&mut req, |params, resp| { - let world = world.snapshot(); - let sender = sender.clone(); - pool.execute(move || { - let res = handle_extend_selection(world, params); - sender.send(Box::new(|io: &mut Io| resp.response(io, res))) - }); - Ok(()) - })?; + handle_request_on_threadpool::( + &mut req, pool, world, &sender, handle_syntax_tree + )?; + handle_request_on_threadpool::( + &mut req, pool, world, &sender, handle_extend_selection + )?; let mut shutdown = false; dispatch::handle_request::(&mut req, |(), resp| { resp.result(io, ())?; @@ -271,6 +261,29 @@ fn main_loop( } } +fn handle_request_on_threadpool( + req: &mut Option, + pool: &ThreadPool, + world: &WorldState, + sender: &Sender, + f: fn(World, R::Params) -> Result, +) -> Result<()> + where + R: Request + Send + 'static, + R::Params: DeserializeOwned + Send + 'static, + R::Result: Serialize + Send + 'static, +{ + dispatch::handle_request::(req, |params, resp| { + let world = world.snapshot(); + let sender = sender.clone(); + pool.execute(move || { + let res = f(world, params); + sender.send(Box::new(|io: &mut Io| resp.response(io, res))) + }); + Ok(()) + }) +} + trait FnBox: Send { fn call_box(self: Box, a: A) -> R; } -- cgit v1.2.3