From c5a2fd5da394b5b16695af9f2eb437e29be010f0 Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Sat, 19 Jun 2021 05:14:40 +0200 Subject: [PATCH] Add `close_language_servers` method on `Editor` --- Cargo.lock | 1 + helix-term/src/application.rs | 11 +---------- helix-view/Cargo.toml | 1 + helix-view/src/editor.rs | 20 ++++++++++++++++++++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12a03dac1..24c277e12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,6 +342,7 @@ version = "0.2.0" dependencies = [ "anyhow", "crossterm", + "futures-util", "helix-core", "helix-lsp", "helix-tui", diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 1f02ac4f0..2fae467fd 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -406,16 +406,7 @@ pub async fn run(&mut self) -> Result<(), Error> { self.event_loop().await; - tokio::time::timeout( - Duration::from_millis(500), - future::join_all( - self.editor - .language_servers - .iter_clients() - .map(|client| client.force_shutdown()), - ), - ) - .await; + self.editor.close_language_servers(None).await; // reset cursor shape write!(stdout, "\x1B[2 q"); diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index 13539a5a2..7f18e9a2a 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -27,6 +27,7 @@ once_cell = "1.8" url = "2" tokio = { version = "1", features = ["full"] } +futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false } slotmap = "1" diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index a1c93f755..db8ae87ab 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -2,7 +2,9 @@ use tui::layout::Rect; use tui::terminal::CursorKind; +use futures_util::future; use std::path::PathBuf; +use std::time::Duration; use slotmap::SlotMap; @@ -270,4 +272,22 @@ pub fn cursor(&self) -> (Option, CursorKind) { (None, CursorKind::Hidden) } } + + /// Closes language servers with timeout. The default timeout is 500 ms, use + /// `timeout` parameter to override this. + pub async fn close_language_servers( + &self, + timeout: Option, + ) -> Result<(), tokio::time::error::Elapsed> { + tokio::time::timeout( + Duration::from_millis(timeout.unwrap_or(500)), + future::join_all( + self.language_servers + .iter_clients() + .map(|client| client.force_shutdown()), + ), + ) + .await + .map(|_| ()) + } }