Unnamed repository; edit this file 'description' to name the repository.
LSP: Eagerly send requests in `Client::request`
This is a similar change to the parent commit but for `request`. The requests should be sent eagerly so that the ordering stays consistent. Co-authored-by: Pascal Kuthe <[email protected]>
Michael Davis 2025-02-02
parent 5532ef3 · commit f0fa905
-rw-r--r--helix-lsp/src/client.rs41
1 files changed, 22 insertions, 19 deletions
diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs
index 31742804..3a50f20a 100644
--- a/helix-lsp/src/client.rs
+++ b/helix-lsp/src/client.rs
@@ -426,29 +426,32 @@ impl Client {
let server_tx = self.server_tx.clone();
let id = self.next_request_id();
- let params = serde_json::to_value(params);
+ // It's important that this is not part of the future so that it gets executed right away
+ // and the request order stays consistent.
+ let rx = serde_json::to_value(params)
+ .map_err(Error::from)
+ .and_then(|params| {
+ let request = jsonrpc::MethodCall {
+ jsonrpc: Some(jsonrpc::Version::V2),
+ id: id.clone(),
+ method: R::METHOD.to_string(),
+ params: Self::value_into_params(params),
+ };
+ let (tx, rx) = channel::<Result<Value>>(1);
+ server_tx
+ .send(Payload::Request {
+ chan: tx,
+ value: request,
+ })
+ .map_err(|e| Error::Other(e.into()))?;
+ Ok(rx)
+ });
+
async move {
use std::time::Duration;
use tokio::time::timeout;
-
- let request = jsonrpc::MethodCall {
- jsonrpc: Some(jsonrpc::Version::V2),
- id: id.clone(),
- method: R::METHOD.to_string(),
- params: Self::value_into_params(params?),
- };
-
- let (tx, mut rx) = channel::<Result<Value>>(1);
-
- server_tx
- .send(Payload::Request {
- chan: tx,
- value: request,
- })
- .map_err(|e| Error::Other(e.into()))?;
-
// TODO: delay other calls until initialize success
- timeout(Duration::from_secs(timeout_secs), rx.recv())
+ timeout(Duration::from_secs(timeout_secs), rx?.recv())
.await
.map_err(|_| Error::Timeout(id))? // return Timeout
.ok_or(Error::StreamClosed)?