aboutsummaryrefslogtreecommitdiff
path: root/helix-lsp/src/transport.rs
diff options
context:
space:
mode:
authorBlaž Hrastnik2023-05-19 00:39:35 +0000
committerGitHub2023-05-19 00:39:35 +0000
commit53f47bc47771c94dab51626ca025be28e62eba0c (patch)
treec8f5c59d40d1ecde227c209f898cc7afd6da5477 /helix-lsp/src/transport.rs
parent7f5940be80eaa3aec7903903072b7108f41dd97b (diff)
parent2a512f7c487f0a707a7eb158e24bd478433bcd91 (diff)
Merge pull request #2507 from Philipp-M/multiple-language-servers
Add support for multiple language servers per language
Diffstat (limited to 'helix-lsp/src/transport.rs')
-rw-r--r--helix-lsp/src/transport.rs63
1 files changed, 43 insertions, 20 deletions
diff --git a/helix-lsp/src/transport.rs b/helix-lsp/src/transport.rs
index 3e3e06ee..8c38c177 100644
--- a/helix-lsp/src/transport.rs
+++ b/helix-lsp/src/transport.rs
@@ -38,6 +38,7 @@ enum ServerMessage {
#[derive(Debug)]
pub struct Transport {
id: usize,
+ name: String,
pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>,
}
@@ -47,6 +48,7 @@ impl Transport {
server_stdin: BufWriter<ChildStdin>,
server_stderr: BufReader<ChildStderr>,
id: usize,
+ name: String,
) -> (
UnboundedReceiver<(usize, jsonrpc::Call)>,
UnboundedSender<Payload>,
@@ -58,6 +60,7 @@ impl Transport {
let transport = Self {
id,
+ name,
pending_requests: Mutex::new(HashMap::default()),
};
@@ -83,6 +86,7 @@ impl Transport {
async fn recv_server_message(
reader: &mut (impl AsyncBufRead + Unpin + Send),
buffer: &mut String,
+ language_server_name: &str,
) -> Result<ServerMessage> {
let mut content_length = None;
loop {
@@ -124,7 +128,7 @@ impl Transport {
reader.read_exact(&mut content).await?;
let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?;
- info!("<- {}", msg);
+ info!("{language_server_name} <- {msg}");
// try parsing as output (server response) or call (server request)
let output: serde_json::Result<ServerMessage> = serde_json::from_str(msg);
@@ -135,12 +139,13 @@ impl Transport {
async fn recv_server_error(
err: &mut (impl AsyncBufRead + Unpin + Send),
buffer: &mut String,
+ language_server_name: &str,
) -> Result<()> {
buffer.truncate(0);
if err.read_line(buffer).await? == 0 {
return Err(Error::StreamClosed);
};
- error!("err <- {:?}", buffer);
+ error!("{language_server_name} err <- {buffer:?}");
Ok(())
}
@@ -162,15 +167,17 @@ impl Transport {
Payload::Notification(value) => serde_json::to_string(&value)?,
Payload::Response(error) => serde_json::to_string(&error)?,
};
- self.send_string_to_server(server_stdin, json).await
+ self.send_string_to_server(server_stdin, json, &self.name)
+ .await
}
async fn send_string_to_server(
&self,
server_stdin: &mut BufWriter<ChildStdin>,
request: String,
+ language_server_name: &str,
) -> Result<()> {
- info!("-> {}", request);
+ info!("{language_server_name} -> {request}");
// send the headers
server_stdin
@@ -189,9 +196,13 @@ impl Transport {
&self,
client_tx: &UnboundedSender<(usize, jsonrpc::Call)>,
msg: ServerMessage,
+ language_server_name: &str,
) -> Result<()> {
match msg {
- ServerMessage::Output(output) => self.process_request_response(output).await?,
+ ServerMessage::Output(output) => {
+ self.process_request_response(output, language_server_name)
+ .await?
+ }
ServerMessage::Call(call) => {
client_tx
.send((self.id, call))
@@ -202,14 +213,18 @@ impl Transport {
Ok(())
}
- async fn process_request_response(&self, output: jsonrpc::Output) -> Result<()> {
+ async fn process_request_response(
+ &self,
+ output: jsonrpc::Output,
+ language_server_name: &str,
+ ) -> Result<()> {
let (id, result) = match output {
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
- info!("<- {}", result);
+ info!("{language_server_name} <- {}", result);
(id, Ok(result))
}
jsonrpc::Output::Failure(jsonrpc::Failure { id, error, .. }) => {
- error!("<- {}", error);
+ error!("{language_server_name} <- {error}");
(id, Err(error.into()))
}
};
@@ -240,12 +255,17 @@ impl Transport {
) {
let mut recv_buffer = String::new();
loop {
- match Self::recv_server_message(&mut server_stdout, &mut recv_buffer).await {
+ match Self::recv_server_message(&mut server_stdout, &mut recv_buffer, &transport.name)
+ .await
+ {
Ok(msg) => {
- match transport.process_server_message(&client_tx, msg).await {
+ match transport
+ .process_server_message(&client_tx, msg, &transport.name)
+ .await
+ {
Ok(_) => {}
Err(err) => {
- error!("err: <- {:?}", err);
+ error!("{} err: <- {err:?}", transport.name);
break;
}
};
@@ -270,7 +290,7 @@ impl Transport {
params: jsonrpc::Params::None,
}));
match transport
- .process_server_message(&client_tx, notification)
+ .process_server_message(&client_tx, notification, &transport.name)
.await
{
Ok(_) => {}
@@ -281,20 +301,22 @@ impl Transport {
break;
}
Err(err) => {
- error!("err: <- {:?}", err);
+ error!("{} err: <- {err:?}", transport.name);
break;
}
}
}
}
- async fn err(_transport: Arc<Self>, mut server_stderr: BufReader<ChildStderr>) {
+ async fn err(transport: Arc<Self>, mut server_stderr: BufReader<ChildStderr>) {
let mut recv_buffer = String::new();
loop {
- match Self::recv_server_error(&mut server_stderr, &mut recv_buffer).await {
+ match Self::recv_server_error(&mut server_stderr, &mut recv_buffer, &transport.name)
+ .await
+ {
Ok(_) => {}
Err(err) => {
- error!("err: <- {:?}", err);
+ error!("{} err: <- {err:?}", transport.name);
break;
}
}
@@ -348,10 +370,11 @@ impl Transport {
method: lsp_types::notification::Initialized::METHOD.to_string(),
params: jsonrpc::Params::None,
}));
- match transport.process_server_message(&client_tx, notification).await {
+ let language_server_name = &transport.name;
+ match transport.process_server_message(&client_tx, notification, language_server_name).await {
Ok(_) => {}
Err(err) => {
- error!("err: <- {:?}", err);
+ error!("{language_server_name} err: <- {err:?}");
}
}
@@ -361,7 +384,7 @@ impl Transport {
match transport.send_payload_to_server(&mut server_stdin, msg).await {
Ok(_) => {}
Err(err) => {
- error!("err: <- {:?}", err);
+ error!("{language_server_name} err: <- {err:?}");
}
}
}
@@ -380,7 +403,7 @@ impl Transport {
match transport.send_payload_to_server(&mut server_stdin, msg).await {
Ok(_) => {}
Err(err) => {
- error!("err: <- {:?}", err);
+ error!("{} err: <- {err:?}", transport.name);
}
}
}