From 941c34a7fc86e613b41887174c699be52ab202a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 6 Jan 2021 17:48:14 +0900 Subject: [PATCH] lsp: Move timeouts into client.request --- Cargo.lock | 1 + helix-lsp/Cargo.toml | 1 + helix-lsp/src/client.rs | 9 ++++++++- helix-term/src/commands.rs | 14 ++++---------- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bc77034..a76c6669 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -527,6 +527,7 @@ dependencies = [ "serde_json", "shellexpand", "smol", + "smol-timeout", "thiserror", "url", ] diff --git a/helix-lsp/Cargo.toml b/helix-lsp/Cargo.toml index 18aa90d0..caf9287a 100644 --- a/helix-lsp/Cargo.toml +++ b/helix-lsp/Cargo.toml @@ -13,6 +13,7 @@ once_cell = "1.4" lsp-types = { version = "0.86", features = ["proposed"] } smol = "1.2" +smol-timeout = "0.6" url = "2" pathdiff = "0.2" shellexpand = "2.0" diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 207e2970..e3f72a56 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -114,7 +114,14 @@ impl Client { .await .map_err(|e| Error::Other(e.into()))?; - let response = rx.recv().await.map_err(|e| Error::Other(e.into()))??; + use smol_timeout::TimeoutExt; + use std::time::Duration; + + let response = match rx.recv().timeout(Duration::from_secs(2)).await { + Some(response) => response, + None => return Err(Error::Timeout), + } + .map_err(|e| Error::Other(e.into()))??; let response = serde_json::from_value(response)?; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 4f250247..8216437d 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -857,21 +857,15 @@ pub fn completion(cx: &mut Context) { let language_server = cx.language_servers.get("rust", &cx.executor).unwrap(); use log::info; - use smol_timeout::TimeoutExt; - use std::time::Duration; - // TODO: blocking here is not ideal let pos = helix_lsp::util::pos_to_lsp_pos( &cx.view.doc.text().slice(..), cx.view.doc.selection().cursor(), ); - let res = smol::block_on( - language_server - .completion(cx.view.doc.identifier(), pos) - .timeout(Duration::from_secs(2)), - ) - .expect("completion failed!") - .unwrap_or_default(); // if timeout, just return + + // TODO: handle fails + let res = smol::block_on(language_server.completion(cx.view.doc.identifier(), pos)) + .unwrap_or_default(); // TODO: if no completion, show some message or something if !res.is_empty() {