diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs index feeedc96c..5de76c6cd 100644 --- a/helix-lsp/src/lib.rs +++ b/helix-lsp/src/lib.rs @@ -282,6 +282,8 @@ pub fn parse(method: &str, params: jsonrpc::Params) -> Result { pub enum Notification { // we inject this notification to signal the LSP is ready Initialized, + // and this notification to signal that the LSP exited + Exit, PublishDiagnostics(lsp::PublishDiagnosticsParams), ShowMessage(lsp::ShowMessageParams), LogMessage(lsp::LogMessageParams), @@ -294,6 +296,7 @@ pub fn parse(method: &str, params: jsonrpc::Params) -> Result { let notification = match method { lsp::notification::Initialized::METHOD => Self::Initialized, + lsp::notification::Exit::METHOD => Self::Exit, lsp::notification::PublishDiagnostics::METHOD => { let params: lsp::PublishDiagnosticsParams = params.parse()?; Self::PublishDiagnostics(params) @@ -350,6 +353,10 @@ pub fn get_by_id(&self, id: usize) -> Option<&Client> { .map(|(_, client)| client.as_ref()) } + pub fn remove_by_id(&mut self, id: usize) { + self.inner.retain(|_, (client_id, _)| client_id != &id) + } + pub fn restart( &mut self, language_config: &LanguageConfiguration, diff --git a/helix-lsp/src/transport.rs b/helix-lsp/src/transport.rs index 8aaeae3d9..68b3d15e6 100644 --- a/helix-lsp/src/transport.rs +++ b/helix-lsp/src/transport.rs @@ -250,6 +250,26 @@ async fn recv( } }; } + Err(Error::StreamClosed) => { + // Hack: inject a terminated notification so we trigger code that needs to happen after exit + use lsp_types::notification::Notification as _; + let notification = + ServerMessage::Call(jsonrpc::Call::Notification(jsonrpc::Notification { + jsonrpc: None, + method: lsp_types::notification::Exit::METHOD.to_string(), + params: jsonrpc::Params::None, + })); + match transport + .process_server_message(&client_tx, notification) + .await + { + Ok(_) => {} + Err(err) => { + error!("err: <- {:?}", err); + } + } + break; + } Err(err) => { error!("err: <- {:?}", err); break; diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 39a6532d7..99d3af182 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -871,6 +871,32 @@ pub async fn handle_language_server_message( Notification::ProgressMessage(_params) => { // do nothing } + Notification::Exit => { + self.editor.set_status("Language server exited"); + + // Clear any diagnostics for documents with this server open. + let urls: Vec<_> = self + .editor + .documents_mut() + .filter_map(|doc| { + if doc.language_server().map(|server| server.id()) + == Some(server_id) + { + doc.set_diagnostics(Vec::new()); + doc.url() + } else { + None + } + }) + .collect(); + + for url in urls { + self.editor.diagnostics.remove(&url); + } + + // Remove the language server from the registry. + self.editor.language_servers.remove_by_id(server_id); + } } } Call::MethodCall(helix_lsp::jsonrpc::MethodCall {