on master help sign in
Add unique id to each lsp client/server pair
commit38cb934d8f579663ff13496bf79293103863b60b authorwojciechkepka <wojtek.kepka@protonmail.com> dateFri 18 Jun 2021, 05:42 (5 years ago) parent80b4a69
4 files changed+48−15
helix-lsp/src/client.rs+12−2 helix-lsp/src/lib.rs+19−6 helix-lsp/src/transport.rs+9−3 helix-term/src/application.rs+8−4
helix-lsp/src/client.rs+12−2
@@ -18,6 +18,7 @@ use tokio::{
1818
1919#[derive(Debug)]
2020pub struct Client {
21+ id: usize,
2122 _process: Child,
2223 server_tx: UnboundedSender<Payload>,
2324 request_counter: AtomicU64,
@@ -26,7 +27,11 @@ pub struct Client {
2627}
2728
2829impl Client {
29− pub fn start(cmd: &str, args: &[String]) -> Result<(Self, UnboundedReceiver<Call>)> {
30+ pub fn start(
31+ cmd: &str,
32+ args: &[String],
33+ id: usize,
34+ ) -> Result<(Self, UnboundedReceiver<(usize, Call)>)> {
3035 let process = Command::new(cmd)
3136 .args(args)
3237 .stdin(Stdio::piped())
@@ -43,9 +48,10 @@ impl Client {
4348 let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout"));
4449 let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr"));
4550
46− let (server_rx, server_tx) = Transport::start(reader, writer, stderr);
51+ let (server_rx, server_tx) = Transport::start(reader, writer, stderr, id);
4752
4853 let client = Self {
54+ id,
4955 _process: process,
5056 server_tx,
5157 request_counter: AtomicU64::new(0),
@@ -59,6 +65,10 @@ impl Client {
5965 Ok((client, server_rx))
6066 }
6167
68+ pub fn id(&self) -> usize {
69+ self.id
70+ }
71+
6272 fn next_request_id(&self) -> jsonrpc::Id {
6373 let id = self.request_counter.fetch_add(1, Ordering::Relaxed);
6474 jsonrpc::Id::Num(id)
helix-lsp/src/lib.rs+19−6
@@ -13,7 +13,10 @@ use helix_core::syntax::LanguageConfiguration;
1313
1414use std::{
1515 collections::{hash_map::Entry, HashMap},
16− sync::Arc,
16+ sync::{
17+ atomic::{AtomicUsize, Ordering},
18+ Arc,
19+ },
1720};
1821
1922use serde::{Deserialize, Serialize};
@@ -254,9 +257,10 @@ impl Notification {
254257
255258#[derive(Debug)]
256259pub struct Registry {
257− inner: HashMap<LanguageId, Arc<Client>>,
260+ inner: HashMap<LanguageId, (usize, Arc<Client>)>,
258261
259− pub incoming: SelectAll<UnboundedReceiverStream<Call>>,
262+ counter: AtomicUsize,
263+ pub incoming: SelectAll<UnboundedReceiverStream<(usize, Call)>>,
260264}
261265
262266impl Default for Registry {
@@ -269,10 +273,18 @@ impl Registry {
269273 pub fn new() -> Self {
270274 Self {
271275 inner: HashMap::new(),
276+ counter: AtomicUsize::new(0),
272277 incoming: SelectAll::new(),
273278 }
274279 }
275280
281+ pub fn get_by_id(&mut self, id: usize) -> Option<&Client> {
282+ self.inner
283+ .values()
284+ .find(|(client_id, _)| client_id == &id)
285+ .map(|(_, client)| client.as_ref())
286+ }
287+
276288 pub fn get(&mut self, language_config: &LanguageConfiguration) -> Result<Arc<Client>> {
277289 if let Some(config) = &language_config.language_server {
278290 // avoid borrow issues
@@ -280,16 +292,17 @@ impl Registry {
280292 let s_incoming = &mut self.incoming;
281293
282294 match inner.entry(language_config.scope.clone()) {
283− Entry::Occupied(language_server) => Ok(language_server.get().clone()),
295+ Entry::Occupied(entry) => Ok(entry.get().1.clone()),
284296 Entry::Vacant(entry) => {
285297 // initialize a new client
286− let (mut client, incoming) = Client::start(&config.command, &config.args)?;
298+ let id = self.counter.fetch_add(1, Ordering::Relaxed);
299+ let (mut client, incoming) = Client::start(&config.command, &config.args, id)?;
287300 // TODO: run this async without blocking
288301 futures_executor::block_on(client.initialize())?;
289302 s_incoming.push(UnboundedReceiverStream::new(incoming));
290303 let client = Arc::new(client);
291304
292− entry.insert(client.clone());
305+ entry.insert((id, client.clone()));
293306 Ok(client)
294307 }
295308 }
helix-lsp/src/transport.rs+9−3
@@ -33,7 +33,8 @@ enum ServerMessage {
3333
3434#[derive(Debug)]
3535pub struct Transport {
36− client_tx: UnboundedSender<jsonrpc::Call>,
36+ id: usize,
37+ client_tx: UnboundedSender<(usize, jsonrpc::Call)>,
3738 client_rx: UnboundedReceiver<Payload>,
3839
3940 pending_requests: HashMap<jsonrpc::Id, Sender<Result<Value>>>,
@@ -48,11 +49,16 @@ impl Transport {
4849 server_stdout: BufReader<ChildStdout>,
4950 server_stdin: BufWriter<ChildStdin>,
5051 server_stderr: BufReader<ChildStderr>,
51− ) -> (UnboundedReceiver<jsonrpc::Call>, UnboundedSender<Payload>) {
52+ id: usize,
53+ ) -> (
54+ UnboundedReceiver<(usize, jsonrpc::Call)>,
55+ UnboundedSender<Payload>,
56+ ) {
5257 let (client_tx, rx) = unbounded_channel();
5358 let (tx, client_rx) = unbounded_channel();
5459
5560 let transport = Self {
61+ id,
5662 server_stdout,
5763 server_stdin,
5864 server_stderr,
@@ -156,7 +162,7 @@ impl Transport {
156162 match msg {
157163 ServerMessage::Output(output) => self.process_request_response(output).await?,
158164 ServerMessage::Call(call) => {
159− self.client_tx.send(call).unwrap();
165+ self.client_tx.send((self.id, call)).unwrap();
160166 // let notification = Notification::parse(&method, params);
161167 }
162168 };
helix-term/src/application.rs+8−4
@@ -109,8 +109,8 @@ impl Application {
109109 event = reader.next() => {
110110 self.handle_terminal_events(event)
111111 }
112− Some(call) = self.editor.language_servers.incoming.next() => {
113− self.handle_language_server_message(call).await
112+ Some((id, call)) = self.editor.language_servers.incoming.next() => {
113+ self.handle_language_server_message(call, id).await
114114 }
115115 Some(callback) = &mut self.callbacks.next() => {
116116 self.handle_language_server_callback(callback)
@@ -153,8 +153,12 @@ impl Application {
153153 }
154154 }
155155
156− pub async fn handle_language_server_message(&mut self, call: helix_lsp::Call) {
157− use helix_lsp::{Call, Notification};
156+ pub async fn handle_language_server_message(
157+ &mut self,
158+ call: helix_lsp::Call,
159+ server_id: usize,
160+ ) {
161+ use helix_lsp::{Call, MethodCall, Notification};
158162 match call {
159163 Call::Notification(helix_lsp::jsonrpc::Notification { method, params, .. }) => {
160164 let notification = match Notification::parse(&method, params) {