@@ -226,6 +226,8 @@ impl MethodCall {
226226
227227#[derive(Debug, PartialEq, Clone)]
228228pub enum Notification {
229+ // we inject this notification to signal the LSP is ready
230+ Initialized,
229231 PublishDiagnostics(lsp::PublishDiagnosticsParams),
230232 ShowMessage(lsp::ShowMessageParams),
231233 LogMessage(lsp::LogMessageParams),
@@ -237,6 +239,7 @@ impl Notification {
237239 use lsp::notification::Notification as _;
238240
239241 let notification = match method {
242+ lsp::notification::Initialized::METHOD => Self::Initialized,
240243 lsp::notification::PublishDiagnostics::METHOD => {
241244 let params: lsp::PublishDiagnosticsParams = params
242245 .parse()
@@ -294,7 +297,7 @@ impl Registry {
294297 }
295298 }
296299
297− pub fn get_by_id(&mut self, id: usize) -> Option<&Client> {
300+ pub fn get_by_id(&self, id: usize) -> Option<&Client> {
298301 self.inner
299302 .values()
300303 .find(|(client_id, _)| client_id == &id)
@@ -302,55 +305,52 @@ impl Registry {
302305 }
303306
304307 pub fn get(&mut self, language_config: &LanguageConfiguration) -> Result<Arc<Client>> {
305− if let Some(config) = &language_config.language_server {
306− // avoid borrow issues
307− let inner = &mut self.inner;
308− let s_incoming = &mut self.incoming;
309−
310− match inner.entry(language_config.scope.clone()) {
311− Entry::Occupied(entry) => Ok(entry.get().1.clone()),
312− Entry::Vacant(entry) => {
313− // initialize a new client
314− let id = self.counter.fetch_add(1, Ordering::Relaxed);
315− let (client, incoming, initialize_notify) = Client::start(
316− &config.command,
317− &config.args,
318− serde_json::from_str(language_config.config.as_deref().unwrap_or("")).ok(),
319− id,
320− )?;
321− s_incoming.push(UnboundedReceiverStream::new(incoming));
322− let client = Arc::new(client);
323−
324− let _client = client.clone();
325− // Initialize the client asynchronously
326− tokio::spawn(async move {
327− use futures_util::TryFutureExt;
328− let value = _client
329− .capabilities
330− .get_or_try_init(|| {
331− _client
332− .initialize()
333− .map_ok(|response| response.capabilities)
334− })
335− .await;
336−
337− value.expect("failed to initialize capabilities");
338−
339− // next up, notify<initialized>
340− _client
341− .notify::<lsp::notification::Initialized>(lsp::InitializedParams {})
342− .await
343− .unwrap();
344−
345− initialize_notify.notify_one();
346− });
347−
348− entry.insert((id, client.clone()));
349− Ok(client)
350− }
308+ let config = match &language_config.language_server {
309+ Some(config) => config,
310+ None => return Err(Error::LspNotDefined),
311+ };
312+
313+ match self.inner.entry(language_config.scope.clone()) {
314+ Entry::Occupied(entry) => Ok(entry.get().1.clone()),
315+ Entry::Vacant(entry) => {
316+ // initialize a new client
317+ let id = self.counter.fetch_add(1, Ordering::Relaxed);
318+ let (client, incoming, initialize_notify) = Client::start(
319+ &config.command,
320+ &config.args,
321+ serde_json::from_str(language_config.config.as_deref().unwrap_or("")).ok(),
322+ id,
323+ )?;
324+ self.incoming.push(UnboundedReceiverStream::new(incoming));
325+ let client = Arc::new(client);
326+
327+ // Initialize the client asynchronously
328+ let _client = client.clone();
329+ tokio::spawn(async move {
330+ use futures_util::TryFutureExt;
331+ let value = _client
332+ .capabilities
333+ .get_or_try_init(|| {
334+ _client
335+ .initialize()
336+ .map_ok(|response| response.capabilities)
337+ })
338+ .await;
339+
340+ value.expect("failed to initialize capabilities");
341+
342+ // next up, notify<initialized>
343+ _client
344+ .notify::<lsp::notification::Initialized>(lsp::InitializedParams {})
345+ .await
346+ .unwrap();
347+
348+ initialize_notify.notify_one();
349+ });
350+
351+ entry.insert((id, client.clone()));
352+ Ok(client)
351353 }
352− } else {
353− Err(Error::LspNotDefined)
354354 }
355355 }
356356