@@ -13,7 +13,10 @@ use helix_core::syntax::LanguageConfiguration;
1313
1414use thiserror::Error;
1515
16−use std::{collections::HashMap, sync::Arc};
16+use std::{
17+ collections::{hash_map::Entry, HashMap},
18+ sync::Arc,
19+};
1720
1821use serde::{Deserialize, Serialize};
1922
@@ -29,6 +32,10 @@ pub enum Error {
2932 Parse(#[from] serde_json::Error),
3033 #[error("request timed out")]
3134 Timeout,
35+ #[error("server closed the stream")]
36+ StreamClosed,
37+ #[error("LSP not defined")]
38+ LspNotDefined,
3239 #[error(transparent)]
3340 Other(#[from] anyhow::Error),
3441}
@@ -173,7 +180,7 @@ type LanguageId = String;
173180use futures_util::stream::select_all::SelectAll;
174181
175182pub struct Registry {
176− inner: HashMap<LanguageId, Option<Arc<Client>>>,
183+ inner: HashMap<LanguageId, Arc<Client>>,
177184
178185 pub incoming: SelectAll<UnboundedReceiverStream<Call>>,
179186}
@@ -192,35 +199,29 @@ impl Registry {
192199 }
193200 }
194201
195− pub fn get(&mut self, language_config: &LanguageConfiguration) -> Option<Arc<Client>> {
196− // TODO: propagate the error
202+ pub fn get(&mut self, language_config: &LanguageConfiguration) -> Result<Arc<Client>> {
197203 if let Some(config) = &language_config.language_server {
198204 // avoid borrow issues
199205 let inner = &mut self.inner;
200206 let s_incoming = &mut self.incoming;
201207
202− let language_server = inner
203− .entry(language_config.scope.clone()) // can't use entry with Borrow keys: https://github.com/rust-lang/rfcs/pull/1769
204− .or_insert_with(|| {
205− // TODO: lookup defaults for id (name, args)
206−
208+ match inner.entry(language_config.scope.clone()) {
209+ Entry::Occupied(language_server) => Ok(language_server.get().clone()),
210+ Entry::Vacant(entry) => {
207211 // initialize a new client
208− let (mut client, incoming) =
209− Client::start(&config.command, &config.args).ok()?;
210−
212+ let (mut client, incoming) = Client::start(&config.command, &config.args)?;
211213 // TODO: run this async without blocking
212− futures_executor::block_on(client.initialize()).unwrap();
213−
214+ futures_executor::block_on(client.initialize())?;
214215 s_incoming.push(UnboundedReceiverStream::new(incoming));
216+ let client = Arc::new(client);
215217
216− Some(Arc::new(client))
217− })
218− .clone();
219−
220− return language_server;
218+ entry.insert(client.clone());
219+ Ok(client)
220+ }
221+ }
222+ } else {
223+ Err(Error::LspNotDefined)
221224 }
222−
223− None
224225 }
225226}
226227