From 836e0c1863eaea5dffdf76a658c2ee9d7bc22e6f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Aug 2018 00:12:31 +0300 Subject: simplify --- crates/server/src/main.rs | 97 +++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 70 deletions(-) (limited to 'crates/server/src/main.rs') diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index dfde8afb1..0e4f5f86a 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -21,18 +21,18 @@ mod caps; mod req; mod dispatch; mod handlers; - -use std::path::PathBuf; +mod util; use threadpool::ThreadPool; use crossbeam_channel::{bounded, Sender, Receiver}; use flexi_logger::Logger; -use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier}; +use url::Url; use libanalysis::{WorldState, World}; use ::{ io::{Io, RawMsg, RawRequest}, handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, + util::{FilePath, FnBox} }; pub type Result = ::std::result::Result; @@ -198,21 +198,9 @@ fn main_loop( dispatch::handle_notification::(&mut not, |params| { let path = params.text_document.file_path()?; world.change_overlay(path, Some(params.text_document.text)); - let world = world.snapshot(); - let sender = sender.clone(); - let uri = params.text_document.uri; - pool.execute(move || { - match publish_diagnostics(world, uri) { - Err(e) => { - error!("failed to compute diagnostics: {:?}", e) - } - Ok(params) => { - sender.send(Box::new(|io: &mut Io| { - dispatch::send_notification::(io, params) - })) - } - } - }); + update_diagnostics_on_threadpool( + pool, world.snapshot(), sender.clone(), params.text_document.uri, + ); Ok(()) })?; dispatch::handle_notification::(&mut not, |mut params| { @@ -221,21 +209,9 @@ fn main_loop( .ok_or_else(|| format_err!("empty changes"))? .text; world.change_overlay(path, Some(text)); - let world = world.snapshot(); - let sender = sender.clone(); - let uri = params.text_document.uri; - pool.execute(move || { - match publish_diagnostics(world, uri) { - Err(e) => { - error!("failed to compute diagnostics: {:?}", e) - } - Ok(params) => { - sender.send(Box::new(|io: &mut Io| { - dispatch::send_notification::(io, params) - })) - } - } - }); + update_diagnostics_on_threadpool( + pool, world.snapshot(), sender.clone(), params.text_document.uri, + ); Ok(()) })?; dispatch::handle_notification::(&mut not, |params| { @@ -278,41 +254,22 @@ fn handle_request_on_threadpool( }) } -trait FnBox: Send { - fn call_box(self: Box, a: A) -> R; -} - -impl R + Send> FnBox for F { - fn call_box(self: Box, a: A) -> R { - (*self)(a) - } -} - -trait FilePath { - fn file_path(&self) -> Result; -} - -impl FilePath for TextDocumentItem { - fn file_path(&self) -> Result { - self.uri.file_path() - } -} - -impl FilePath for VersionedTextDocumentIdentifier { - fn file_path(&self) -> Result { - self.uri.file_path() - } -} - -impl FilePath for TextDocumentIdentifier { - fn file_path(&self) -> Result { - self.uri.file_path() - } -} - -impl FilePath for ::url::Url { - fn file_path(&self) -> Result { - self.to_file_path() - .map_err(|()| format_err!("invalid uri: {}", self)) - } +fn update_diagnostics_on_threadpool( + pool: &ThreadPool, + world: World, + sender: Sender, + uri: Url, +) { + pool.execute(move || { + match publish_diagnostics(world, uri) { + Err(e) => { + error!("failed to compute diagnostics: {:?}", e) + } + Ok(params) => { + sender.send(Box::new(|io: &mut Io| { + dispatch::send_notification::(io, params) + })) + } + } + }); } -- cgit v1.2.3