From 38cb934d8f579663ff13496bf79293103863b60b Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Fri, 18 Jun 2021 05:42:34 +0200 Subject: Add unique id to each lsp client/server pair --- helix-lsp/src/lib.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'helix-lsp/src/lib.rs') diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs index be1e23a5..774de075 100644 --- a/helix-lsp/src/lib.rs +++ b/helix-lsp/src/lib.rs @@ -13,7 +13,10 @@ use helix_core::syntax::LanguageConfiguration; use std::{ collections::{hash_map::Entry, HashMap}, - sync::Arc, + sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, + }, }; use serde::{Deserialize, Serialize}; @@ -254,9 +257,10 @@ impl Notification { #[derive(Debug)] pub struct Registry { - inner: HashMap>, + inner: HashMap)>, - pub incoming: SelectAll>, + counter: AtomicUsize, + pub incoming: SelectAll>, } impl Default for Registry { @@ -269,10 +273,18 @@ impl Registry { pub fn new() -> Self { Self { inner: HashMap::new(), + counter: AtomicUsize::new(0), incoming: SelectAll::new(), } } + pub fn get_by_id(&mut self, id: usize) -> Option<&Client> { + self.inner + .values() + .find(|(client_id, _)| client_id == &id) + .map(|(_, client)| client.as_ref()) + } + pub fn get(&mut self, language_config: &LanguageConfiguration) -> Result> { if let Some(config) = &language_config.language_server { // avoid borrow issues @@ -280,16 +292,17 @@ impl Registry { let s_incoming = &mut self.incoming; match inner.entry(language_config.scope.clone()) { - Entry::Occupied(language_server) => Ok(language_server.get().clone()), + Entry::Occupied(entry) => Ok(entry.get().1.clone()), Entry::Vacant(entry) => { // initialize a new client - let (mut client, incoming) = Client::start(&config.command, &config.args)?; + let id = self.counter.fetch_add(1, Ordering::Relaxed); + let (mut client, incoming) = Client::start(&config.command, &config.args, id)?; // TODO: run this async without blocking futures_executor::block_on(client.initialize())?; s_incoming.push(UnboundedReceiverStream::new(incoming)); let client = Arc::new(client); - entry.insert(client.clone()); + entry.insert((id, client.clone())); Ok(client) } } -- cgit v1.2.3-70-g09d2