2024-07-27 20:19:57 +04:00
|
|
|
use crate::{
|
|
|
|
jsonrpc,
|
|
|
|
lsp::{self, notification::Notification as _},
|
|
|
|
Error, LanguageServerId, Result,
|
|
|
|
};
|
2021-08-29 13:38:28 +04:00
|
|
|
use anyhow::Context;
|
2021-08-31 11:48:59 +04:00
|
|
|
use log::{error, info};
|
2021-06-07 23:11:17 +04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-10-19 11:09:44 +04:00
|
|
|
use serde_json::Value;
|
2021-06-07 23:11:17 +04:00
|
|
|
use std::collections::HashMap;
|
2021-08-07 10:02:12 +04:00
|
|
|
use std::sync::Arc;
|
2021-05-06 08:56:34 +04:00
|
|
|
use tokio::{
|
|
|
|
io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
2020-10-19 11:09:44 +04:00
|
|
|
process::{ChildStderr, ChildStdin, ChildStdout},
|
2021-08-07 10:02:12 +04:00
|
|
|
sync::{
|
|
|
|
mpsc::{unbounded_channel, Sender, UnboundedReceiver, UnboundedSender},
|
2021-08-31 11:48:59 +04:00
|
|
|
Mutex, Notify,
|
2021-08-07 10:02:12 +04:00
|
|
|
},
|
2020-10-19 11:09:44 +04:00
|
|
|
};
|
|
|
|
|
2021-05-06 08:56:34 +04:00
|
|
|
#[derive(Debug)]
|
2021-03-31 18:42:16 +04:00
|
|
|
pub enum Payload {
|
2020-10-19 11:09:44 +04:00
|
|
|
Request {
|
2020-10-21 11:42:45 +04:00
|
|
|
chan: Sender<Result<Value>>,
|
2020-10-19 11:09:44 +04:00
|
|
|
value: jsonrpc::MethodCall,
|
|
|
|
},
|
|
|
|
Notification(jsonrpc::Notification),
|
2020-10-23 13:48:03 +04:00
|
|
|
Response(jsonrpc::Output),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A type representing all possible values sent from the server to the client.
|
|
|
|
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[serde(untagged)]
|
2021-06-07 23:11:17 +04:00
|
|
|
enum ServerMessage {
|
2020-10-23 13:48:03 +04:00
|
|
|
/// A regular JSON-RPC request output (single response).
|
|
|
|
Output(jsonrpc::Output),
|
|
|
|
/// A JSON-RPC request or notification.
|
|
|
|
Call(jsonrpc::Call),
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
|
2021-06-07 18:34:19 +04:00
|
|
|
#[derive(Debug)]
|
2021-03-31 18:42:16 +04:00
|
|
|
pub struct Transport {
|
2024-04-08 04:46:32 +04:00
|
|
|
id: LanguageServerId,
|
2022-05-23 20:10:48 +04:00
|
|
|
name: String,
|
2021-08-07 10:02:12 +04:00
|
|
|
pending_requests: Mutex<HashMap<jsonrpc::Id, Sender<Result<Value>>>>,
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Transport {
|
|
|
|
pub fn start(
|
2021-06-07 23:11:17 +04:00
|
|
|
server_stdout: BufReader<ChildStdout>,
|
|
|
|
server_stdin: BufWriter<ChildStdin>,
|
|
|
|
server_stderr: BufReader<ChildStderr>,
|
2024-04-08 04:46:32 +04:00
|
|
|
id: LanguageServerId,
|
2022-05-23 20:10:48 +04:00
|
|
|
name: String,
|
2021-06-18 07:42:34 +04:00
|
|
|
) -> (
|
2024-04-08 04:46:32 +04:00
|
|
|
UnboundedReceiver<(LanguageServerId, jsonrpc::Call)>,
|
2021-06-18 07:42:34 +04:00
|
|
|
UnboundedSender<Payload>,
|
2021-08-31 11:48:59 +04:00
|
|
|
Arc<Notify>,
|
2021-06-18 07:42:34 +04:00
|
|
|
) {
|
2021-06-07 23:11:17 +04:00
|
|
|
let (client_tx, rx) = unbounded_channel();
|
|
|
|
let (tx, client_rx) = unbounded_channel();
|
2021-08-31 11:48:59 +04:00
|
|
|
let notify = Arc::new(Notify::new());
|
2020-10-19 11:09:44 +04:00
|
|
|
|
|
|
|
let transport = Self {
|
2021-06-18 07:42:34 +04:00
|
|
|
id,
|
2022-05-23 20:10:48 +04:00
|
|
|
name,
|
2021-08-07 10:02:12 +04:00
|
|
|
pending_requests: Mutex::new(HashMap::default()),
|
2020-10-19 11:09:44 +04:00
|
|
|
};
|
|
|
|
|
2021-08-07 10:02:12 +04:00
|
|
|
let transport = Arc::new(transport);
|
|
|
|
|
2021-09-02 08:55:08 +04:00
|
|
|
tokio::spawn(Self::recv(
|
|
|
|
transport.clone(),
|
|
|
|
server_stdout,
|
|
|
|
client_tx.clone(),
|
|
|
|
));
|
2021-08-07 10:02:12 +04:00
|
|
|
tokio::spawn(Self::err(transport.clone(), server_stderr));
|
2021-08-31 11:48:59 +04:00
|
|
|
tokio::spawn(Self::send(
|
|
|
|
transport,
|
|
|
|
server_stdin,
|
2021-09-02 08:55:08 +04:00
|
|
|
client_tx,
|
2021-08-31 11:48:59 +04:00
|
|
|
client_rx,
|
|
|
|
notify.clone(),
|
|
|
|
));
|
|
|
|
|
|
|
|
(rx, tx, notify)
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
async fn recv_server_message(
|
2021-05-06 12:20:00 +04:00
|
|
|
reader: &mut (impl AsyncBufRead + Unpin + Send),
|
2021-06-07 23:11:17 +04:00
|
|
|
buffer: &mut String,
|
2022-05-23 20:10:48 +04:00
|
|
|
language_server_name: &str,
|
2021-06-07 23:11:17 +04:00
|
|
|
) -> Result<ServerMessage> {
|
|
|
|
let mut content_length = None;
|
2020-10-19 11:09:44 +04:00
|
|
|
loop {
|
2021-06-07 23:11:17 +04:00
|
|
|
buffer.truncate(0);
|
2021-08-31 09:27:55 +04:00
|
|
|
if reader.read_line(buffer).await? == 0 {
|
|
|
|
return Err(Error::StreamClosed);
|
|
|
|
};
|
|
|
|
|
|
|
|
// debug!("<- header {:?}", buffer);
|
2020-10-19 11:09:44 +04:00
|
|
|
|
2021-08-31 11:48:59 +04:00
|
|
|
if buffer == "\r\n" {
|
|
|
|
// look for an empty CRLF line
|
2020-10-19 11:09:44 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-31 09:27:55 +04:00
|
|
|
let header = buffer.trim();
|
2021-06-07 23:11:17 +04:00
|
|
|
|
2021-08-29 07:40:21 +04:00
|
|
|
let parts = header.split_once(": ");
|
|
|
|
|
|
|
|
match parts {
|
|
|
|
Some(("Content-Length", value)) => {
|
2021-06-19 06:57:05 +04:00
|
|
|
content_length = Some(value.parse().context("invalid content length")?);
|
2021-06-07 23:11:17 +04:00
|
|
|
}
|
2021-08-29 07:40:21 +04:00
|
|
|
Some((_, _)) => {}
|
2021-08-29 13:38:28 +04:00
|
|
|
None => {
|
|
|
|
// Workaround: Some non-conformant language servers will output logging and other garbage
|
|
|
|
// into the same stream as JSON-RPC messages. This can also happen from shell scripts that spawn
|
|
|
|
// the server. Skip such lines and log a warning.
|
2021-08-31 11:48:59 +04:00
|
|
|
|
|
|
|
// warn!("Failed to parse header: {:?}", header);
|
2021-08-29 13:38:28 +04:00
|
|
|
}
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:57:05 +04:00
|
|
|
let content_length = content_length.context("missing content length")?;
|
2020-10-19 11:09:44 +04:00
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
//TODO: reuse vector
|
2020-10-19 11:09:44 +04:00
|
|
|
let mut content = vec![0; content_length];
|
|
|
|
reader.read_exact(&mut content).await?;
|
2021-08-07 10:02:12 +04:00
|
|
|
let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?;
|
2020-10-19 11:09:44 +04:00
|
|
|
|
2022-05-23 20:10:48 +04:00
|
|
|
info!("{language_server_name} <- {msg}");
|
2020-12-23 12:03:20 +04:00
|
|
|
|
2020-10-19 11:09:44 +04:00
|
|
|
// try parsing as output (server response) or call (server request)
|
2021-08-07 10:02:12 +04:00
|
|
|
let output: serde_json::Result<ServerMessage> = serde_json::from_str(msg);
|
2020-10-19 11:09:44 +04:00
|
|
|
|
|
|
|
Ok(output?)
|
|
|
|
}
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
async fn recv_server_error(
|
2021-05-06 12:20:00 +04:00
|
|
|
err: &mut (impl AsyncBufRead + Unpin + Send),
|
2021-06-07 23:11:17 +04:00
|
|
|
buffer: &mut String,
|
2022-05-23 20:10:48 +04:00
|
|
|
language_server_name: &str,
|
2021-06-07 23:11:17 +04:00
|
|
|
) -> Result<()> {
|
|
|
|
buffer.truncate(0);
|
2021-08-31 09:27:55 +04:00
|
|
|
if err.read_line(buffer).await? == 0 {
|
|
|
|
return Err(Error::StreamClosed);
|
|
|
|
};
|
2022-05-23 20:10:48 +04:00
|
|
|
error!("{language_server_name} err <- {buffer:?}");
|
2021-02-15 16:16:25 +04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-08-07 10:02:12 +04:00
|
|
|
async fn send_payload_to_server(
|
|
|
|
&self,
|
|
|
|
server_stdin: &mut BufWriter<ChildStdin>,
|
|
|
|
payload: Payload,
|
|
|
|
) -> Result<()> {
|
2021-06-07 23:11:17 +04:00
|
|
|
//TODO: reuse string
|
|
|
|
let json = match payload {
|
2020-10-19 11:09:44 +04:00
|
|
|
Payload::Request { chan, value } => {
|
2021-08-07 10:02:12 +04:00
|
|
|
self.pending_requests
|
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.insert(value.id.clone(), chan);
|
2021-06-07 23:11:17 +04:00
|
|
|
serde_json::to_string(&value)?
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
2021-06-07 23:11:17 +04:00
|
|
|
Payload::Notification(value) => serde_json::to_string(&value)?,
|
|
|
|
Payload::Response(error) => serde_json::to_string(&error)?,
|
|
|
|
};
|
2022-05-23 20:10:48 +04:00
|
|
|
self.send_string_to_server(server_stdin, json, &self.name)
|
|
|
|
.await
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
|
2021-08-07 10:02:12 +04:00
|
|
|
async fn send_string_to_server(
|
|
|
|
&self,
|
|
|
|
server_stdin: &mut BufWriter<ChildStdin>,
|
|
|
|
request: String,
|
2022-05-23 20:10:48 +04:00
|
|
|
language_server_name: &str,
|
2021-08-07 10:02:12 +04:00
|
|
|
) -> Result<()> {
|
2022-05-23 20:10:48 +04:00
|
|
|
info!("{language_server_name} -> {request}");
|
2020-10-19 11:09:44 +04:00
|
|
|
|
|
|
|
// send the headers
|
2021-08-07 10:02:12 +04:00
|
|
|
server_stdin
|
2020-10-19 11:09:44 +04:00
|
|
|
.write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// send the body
|
2021-08-07 10:02:12 +04:00
|
|
|
server_stdin.write_all(request.as_bytes()).await?;
|
2020-10-19 11:09:44 +04:00
|
|
|
|
2021-08-07 10:02:12 +04:00
|
|
|
server_stdin.flush().await?;
|
2020-10-19 11:09:44 +04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-08-07 10:02:12 +04:00
|
|
|
async fn process_server_message(
|
|
|
|
&self,
|
2024-04-08 04:46:32 +04:00
|
|
|
client_tx: &UnboundedSender<(LanguageServerId, jsonrpc::Call)>,
|
2021-08-07 10:02:12 +04:00
|
|
|
msg: ServerMessage,
|
2022-05-23 20:10:48 +04:00
|
|
|
language_server_name: &str,
|
2021-08-07 10:02:12 +04:00
|
|
|
) -> Result<()> {
|
2020-10-19 11:09:44 +04:00
|
|
|
match msg {
|
2022-05-23 20:10:48 +04:00
|
|
|
ServerMessage::Output(output) => {
|
|
|
|
self.process_request_response(output, language_server_name)
|
|
|
|
.await?
|
|
|
|
}
|
2021-06-07 23:11:17 +04:00
|
|
|
ServerMessage::Call(call) => {
|
2021-08-07 10:02:12 +04:00
|
|
|
client_tx
|
2021-06-19 06:57:05 +04:00
|
|
|
.send((self.id, call))
|
|
|
|
.context("failed to send a message to server")?;
|
2020-10-23 13:48:03 +04:00
|
|
|
// let notification = Notification::parse(&method, params);
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:10:48 +04:00
|
|
|
async fn process_request_response(
|
|
|
|
&self,
|
|
|
|
output: jsonrpc::Output,
|
|
|
|
language_server_name: &str,
|
|
|
|
) -> Result<()> {
|
2021-02-22 07:42:56 +04:00
|
|
|
let (id, result) = match output {
|
2020-10-19 11:09:44 +04:00
|
|
|
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
info!("{language_server_name} <- {}", result);
|
2021-02-22 07:42:56 +04:00
|
|
|
(id, Ok(result))
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
2020-10-21 11:42:45 +04:00
|
|
|
jsonrpc::Output::Failure(jsonrpc::Failure { id, error, .. }) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
error!("{language_server_name} <- {error}");
|
2021-02-22 07:42:56 +04:00
|
|
|
(id, Err(error.into()))
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
2021-02-22 07:42:56 +04:00
|
|
|
};
|
|
|
|
|
2022-05-17 09:45:34 +04:00
|
|
|
if let Some(tx) = self.pending_requests.lock().await.remove(&id) {
|
|
|
|
match tx.send(result).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => error!(
|
|
|
|
"Tried sending response into a closed channel (id={:?}), original request likely timed out",
|
|
|
|
id
|
|
|
|
),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
log::error!(
|
|
|
|
"Discarding Language Server response without a request (id={:?}) {:?}",
|
|
|
|
id,
|
|
|
|
result
|
|
|
|
);
|
|
|
|
}
|
2021-02-22 07:42:56 +04:00
|
|
|
|
2020-10-19 11:09:44 +04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-08-07 10:02:12 +04:00
|
|
|
async fn recv(
|
|
|
|
transport: Arc<Self>,
|
|
|
|
mut server_stdout: BufReader<ChildStdout>,
|
2024-04-08 04:46:32 +04:00
|
|
|
client_tx: UnboundedSender<(LanguageServerId, jsonrpc::Call)>,
|
2021-08-07 10:02:12 +04:00
|
|
|
) {
|
2021-06-07 23:11:17 +04:00
|
|
|
let mut recv_buffer = String::new();
|
2020-10-19 11:09:44 +04:00
|
|
|
loop {
|
2022-05-23 20:10:48 +04:00
|
|
|
match Self::recv_server_message(&mut server_stdout, &mut recv_buffer, &transport.name)
|
|
|
|
.await
|
|
|
|
{
|
2021-08-07 10:02:12 +04:00
|
|
|
Ok(msg) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
match transport
|
|
|
|
.process_server_message(&client_tx, msg, &transport.name)
|
|
|
|
.await
|
|
|
|
{
|
2021-08-23 13:04:22 +04:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
error!("{} err: <- {err:?}", transport.name);
|
2021-08-23 13:04:22 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
2024-01-17 18:49:25 +04:00
|
|
|
Err(err) => {
|
|
|
|
if !matches!(err, Error::StreamClosed) {
|
|
|
|
error!(
|
|
|
|
"Exiting {} after unexpected error: {err:?}",
|
|
|
|
&transport.name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-24 06:07:42 +04:00
|
|
|
// Close any outstanding requests.
|
|
|
|
for (id, tx) in transport.pending_requests.lock().await.drain() {
|
|
|
|
match tx.send(Err(Error::StreamClosed)).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => {
|
|
|
|
error!("Could not close request on a closed channel (id={:?})", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 08:14:36 +04:00
|
|
|
// Hack: inject a terminated notification so we trigger code that needs to happen after exit
|
|
|
|
let notification =
|
|
|
|
ServerMessage::Call(jsonrpc::Call::Notification(jsonrpc::Notification {
|
|
|
|
jsonrpc: None,
|
2024-07-27 20:19:57 +04:00
|
|
|
method: lsp::notification::Exit::METHOD.to_string(),
|
2022-11-19 08:14:36 +04:00
|
|
|
params: jsonrpc::Params::None,
|
|
|
|
}));
|
|
|
|
match transport
|
2022-05-23 20:10:48 +04:00
|
|
|
.process_server_message(&client_tx, notification, &transport.name)
|
2022-11-19 08:14:36 +04:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
error!("err: <- {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-07 10:02:12 +04:00
|
|
|
|
2022-05-23 20:10:48 +04:00
|
|
|
async fn err(transport: Arc<Self>, mut server_stderr: BufReader<ChildStderr>) {
|
2021-08-07 10:02:12 +04:00
|
|
|
let mut recv_buffer = String::new();
|
|
|
|
loop {
|
2022-05-23 20:10:48 +04:00
|
|
|
match Self::recv_server_error(&mut server_stderr, &mut recv_buffer, &transport.name)
|
|
|
|
.await
|
|
|
|
{
|
2021-08-07 10:02:12 +04:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
error!("{} err: <- {err:?}", transport.name);
|
2021-08-07 10:02:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn send(
|
|
|
|
transport: Arc<Self>,
|
|
|
|
mut server_stdin: BufWriter<ChildStdin>,
|
2024-04-08 04:46:32 +04:00
|
|
|
client_tx: UnboundedSender<(LanguageServerId, jsonrpc::Call)>,
|
2021-08-07 10:02:12 +04:00
|
|
|
mut client_rx: UnboundedReceiver<Payload>,
|
2021-08-31 11:48:59 +04:00
|
|
|
initialize_notify: Arc<Notify>,
|
2021-08-07 10:02:12 +04:00
|
|
|
) {
|
2021-08-31 11:48:59 +04:00
|
|
|
let mut pending_messages: Vec<Payload> = Vec::new();
|
|
|
|
let mut is_pending = true;
|
|
|
|
|
|
|
|
// Determine if a message is allowed to be sent early
|
|
|
|
fn is_initialize(payload: &Payload) -> bool {
|
2024-07-27 20:19:57 +04:00
|
|
|
use lsp::{
|
|
|
|
notification::Initialized,
|
2021-08-31 11:48:59 +04:00
|
|
|
request::{Initialize, Request},
|
|
|
|
};
|
|
|
|
match payload {
|
|
|
|
Payload::Request {
|
|
|
|
value: jsonrpc::MethodCall { method, .. },
|
|
|
|
..
|
|
|
|
} if method == Initialize::METHOD => true,
|
|
|
|
Payload::Notification(jsonrpc::Notification { method, .. })
|
|
|
|
if method == Initialized::METHOD =>
|
|
|
|
{
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 19:25:23 +04:00
|
|
|
fn is_shutdown(payload: &Payload) -> bool {
|
2024-07-27 20:19:57 +04:00
|
|
|
use lsp::request::{Request, Shutdown};
|
2023-06-29 19:25:23 +04:00
|
|
|
matches!(payload, Payload::Request { value: jsonrpc::MethodCall { method, .. }, .. } if method == Shutdown::METHOD)
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:48:59 +04:00
|
|
|
// TODO: events that use capabilities need to do the right thing
|
|
|
|
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
biased;
|
|
|
|
_ = initialize_notify.notified() => { // TODO: notified is technically not cancellation safe
|
|
|
|
// server successfully initialized
|
|
|
|
is_pending = false;
|
2021-09-02 08:55:08 +04:00
|
|
|
|
|
|
|
// Hack: inject an initialized notification so we trigger code that needs to happen after init
|
|
|
|
let notification = ServerMessage::Call(jsonrpc::Call::Notification(jsonrpc::Notification {
|
|
|
|
jsonrpc: None,
|
|
|
|
|
2024-07-27 20:19:57 +04:00
|
|
|
method: lsp::notification::Initialized::METHOD.to_string(),
|
2021-09-02 08:55:08 +04:00
|
|
|
params: jsonrpc::Params::None,
|
|
|
|
}));
|
2022-05-23 20:10:48 +04:00
|
|
|
let language_server_name = &transport.name;
|
|
|
|
match transport.process_server_message(&client_tx, notification, language_server_name).await {
|
2021-09-02 08:55:08 +04:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
error!("{language_server_name} err: <- {err:?}");
|
2021-09-02 08:55:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:48:59 +04:00
|
|
|
// drain the pending queue and send payloads to server
|
|
|
|
for msg in pending_messages.drain(..) {
|
|
|
|
log::info!("Draining pending message {:?}", msg);
|
|
|
|
match transport.send_payload_to_server(&mut server_stdin, msg).await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
error!("{language_server_name} err: <- {err:?}");
|
2021-08-31 11:48:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg = client_rx.recv() => {
|
|
|
|
if let Some(msg) = msg {
|
2023-06-29 19:25:23 +04:00
|
|
|
if is_pending && is_shutdown(&msg) {
|
|
|
|
log::info!("Language server not initialized, shutting down");
|
|
|
|
break;
|
|
|
|
} else if is_pending && !is_initialize(&msg) {
|
2021-09-02 08:55:08 +04:00
|
|
|
// ignore notifications
|
|
|
|
if let Payload::Notification(_) = msg {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:48:59 +04:00
|
|
|
log::info!("Language server not initialized, delaying request");
|
|
|
|
pending_messages.push(msg);
|
|
|
|
} else {
|
|
|
|
match transport.send_payload_to_server(&mut server_stdin, msg).await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
2022-05-23 20:10:48 +04:00
|
|
|
error!("{} err: <- {err:?}", transport.name);
|
2021-08-31 11:48:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// channel closed
|
|
|
|
break;
|
|
|
|
}
|
2021-08-23 13:04:22 +04:00
|
|
|
}
|
|
|
|
}
|
2021-08-07 10:02:12 +04:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|