aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/main.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-10 22:12:31 +0100
committerAleksey Kladov <[email protected]>2018-08-10 22:12:31 +0100
commit836e0c1863eaea5dffdf76a658c2ee9d7bc22e6f (patch)
treedb655764db63f160bf96a88796b2d21604b2af29 /crates/server/src/main.rs
parent46e4232cc69bc2e003dc8a14e604119bb36e9f9f (diff)
simplify
Diffstat (limited to 'crates/server/src/main.rs')
-rw-r--r--crates/server/src/main.rs97
1 files changed, 27 insertions, 70 deletions
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;
21mod req; 21mod req;
22mod dispatch; 22mod dispatch;
23mod handlers; 23mod handlers;
24 24mod util;
25use std::path::PathBuf;
26 25
27use threadpool::ThreadPool; 26use threadpool::ThreadPool;
28use crossbeam_channel::{bounded, Sender, Receiver}; 27use crossbeam_channel::{bounded, Sender, Receiver};
29use flexi_logger::Logger; 28use flexi_logger::Logger;
30use languageserver_types::{TextDocumentItem, VersionedTextDocumentIdentifier, TextDocumentIdentifier}; 29use url::Url;
31use libanalysis::{WorldState, World}; 30use libanalysis::{WorldState, World};
32 31
33use ::{ 32use ::{
34 io::{Io, RawMsg, RawRequest}, 33 io::{Io, RawMsg, RawRequest},
35 handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, 34 handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics},
35 util::{FilePath, FnBox}
36}; 36};
37 37
38pub type Result<T> = ::std::result::Result<T, ::failure::Error>; 38pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
@@ -198,21 +198,9 @@ fn main_loop(
198 dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| { 198 dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| {
199 let path = params.text_document.file_path()?; 199 let path = params.text_document.file_path()?;
200 world.change_overlay(path, Some(params.text_document.text)); 200 world.change_overlay(path, Some(params.text_document.text));
201 let world = world.snapshot(); 201 update_diagnostics_on_threadpool(
202 let sender = sender.clone(); 202 pool, world.snapshot(), sender.clone(), params.text_document.uri,
203 let uri = params.text_document.uri; 203 );
204 pool.execute(move || {
205 match publish_diagnostics(world, uri) {
206 Err(e) => {
207 error!("failed to compute diagnostics: {:?}", e)
208 }
209 Ok(params) => {
210 sender.send(Box::new(|io: &mut Io| {
211 dispatch::send_notification::<req::PublishDiagnostics>(io, params)
212 }))
213 }
214 }
215 });
216 Ok(()) 204 Ok(())
217 })?; 205 })?;
218 dispatch::handle_notification::<req::DidChangeTextDocument, _>(&mut not, |mut params| { 206 dispatch::handle_notification::<req::DidChangeTextDocument, _>(&mut not, |mut params| {
@@ -221,21 +209,9 @@ fn main_loop(
221 .ok_or_else(|| format_err!("empty changes"))? 209 .ok_or_else(|| format_err!("empty changes"))?
222 .text; 210 .text;
223 world.change_overlay(path, Some(text)); 211 world.change_overlay(path, Some(text));
224 let world = world.snapshot(); 212 update_diagnostics_on_threadpool(
225 let sender = sender.clone(); 213 pool, world.snapshot(), sender.clone(), params.text_document.uri,
226 let uri = params.text_document.uri; 214 );
227 pool.execute(move || {
228 match publish_diagnostics(world, uri) {
229 Err(e) => {
230 error!("failed to compute diagnostics: {:?}", e)
231 }
232 Ok(params) => {
233 sender.send(Box::new(|io: &mut Io| {
234 dispatch::send_notification::<req::PublishDiagnostics>(io, params)
235 }))
236 }
237 }
238 });
239 Ok(()) 215 Ok(())
240 })?; 216 })?;
241 dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| { 217 dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| {
@@ -278,41 +254,22 @@ fn handle_request_on_threadpool<R: req::ClientRequest>(
278 }) 254 })
279} 255}
280 256
281trait FnBox<A, R>: Send { 257fn update_diagnostics_on_threadpool(
282 fn call_box(self: Box<Self>, a: A) -> R; 258 pool: &ThreadPool,
283} 259 world: World,
284 260 sender: Sender<Thunk>,
285impl<A, R, F: FnOnce(A) -> R + Send> FnBox<A, R> for F { 261 uri: Url,
286 fn call_box(self: Box<F>, a: A) -> R { 262) {
287 (*self)(a) 263 pool.execute(move || {
288 } 264 match publish_diagnostics(world, uri) {
289} 265 Err(e) => {
290 266 error!("failed to compute diagnostics: {:?}", e)
291trait FilePath { 267 }
292 fn file_path(&self) -> Result<PathBuf>; 268 Ok(params) => {
293} 269 sender.send(Box::new(|io: &mut Io| {
294 270 dispatch::send_notification::<req::PublishDiagnostics>(io, params)
295impl FilePath for TextDocumentItem { 271 }))
296 fn file_path(&self) -> Result<PathBuf> { 272 }
297 self.uri.file_path() 273 }
298 } 274 });
299}
300
301impl FilePath for VersionedTextDocumentIdentifier {
302 fn file_path(&self) -> Result<PathBuf> {
303 self.uri.file_path()
304 }
305}
306
307impl FilePath for TextDocumentIdentifier {
308 fn file_path(&self) -> Result<PathBuf> {
309 self.uri.file_path()
310 }
311}
312
313impl FilePath for ::url::Url {
314 fn file_path(&self) -> Result<PathBuf> {
315 self.to_file_path()
316 .map_err(|()| format_err!("invalid uri: {}", self))
317 }
318} 275}