on master help sign in
dap: Split call/request in the same way LSP does
032aaffBlaž Hrastnik
helix-dap/src/client.rs+50−24
@@ -5,10 +5,13 @@ use crate::{
5};
5};
6use helix_core::syntax::DebuggerQuirks;
6use helix_core::syntax::DebuggerQuirks;
7
7
8+use serde_json::Value;
9+
8use anyhow::anyhow;
10use anyhow::anyhow;
9pub use log::{error, info};
11pub use log::{error, info};
10use std::{
12use std::{
11 collections::HashMap,
13 collections::HashMap,
14+ future::Future,
12 net::{IpAddr, Ipv4Addr, SocketAddr},
15 net::{IpAddr, Ipv4Addr, SocketAddr},
13 path::PathBuf,
16 path::PathBuf,
14 process::Stdio,
17 process::Stdio,
@@ -205,27 +208,52 @@ impl Client {
205 self.request_counter.fetch_add(1, Ordering::Relaxed)
208 self.request_counter.fetch_add(1, Ordering::Relaxed)
206 }
209 }
207
210
211+ /// Execute a RPC request on the debugger.
208− async fn request<R: crate::types::Request>(
212+ fn call<R: crate::types::Request>(
209 &self,
213 &self,
210 arguments: R::Arguments,
214 arguments: R::Arguments,
211− ) -> Result<R::Result> {
215+ ) -> impl Future<Output = Result<Value>>
216+ where
217+ R::Arguments: serde::Serialize,
218+ {
219+ let server_tx = self.server_tx.clone();
220+ let id = self.next_request_id();
221+
222+ async move {
223+ use std::time::Duration;
224+ use tokio::time::timeout;
225+
226+ let arguments = Some(serde_json::to_value(arguments)?);
227+
212− let (callback_tx, mut callback_rx) = channel(1);
228+ let (callback_tx, mut callback_rx) = channel(1);
213−
229+
214− let arguments = Some(serde_json::to_value(arguments)?);
215−
216− let req = Request {
230+ let req = Request {
217− back_ch: Some(callback_tx),
231+ back_ch: Some(callback_tx),
218− seq: self.next_request_id(),
232+ seq: id,
219− command: R::COMMAND.to_string(),
233+ command: R::COMMAND.to_string(),
220− arguments,
234+ arguments,
221− };
235+ };
222−
236+
223− self.server_tx
237+ server_tx.send(req).map_err(|e| Error::Other(e.into()))?;
224− .send(req)
238+
225− .expect("Failed to send request to debugger");
239+ // TODO: specifiable timeout, delay other calls until initialize success
240+ timeout(Duration::from_secs(20), callback_rx.recv())
241+ .await
242+ .map_err(|_| Error::Timeout)? // return Timeout
243+ .ok_or(Error::StreamClosed)?
244+ .map(|response| response.body.unwrap_or_default())
245+ // TODO: check response.success
246+ }
247+ }
226
248
227− let response = callback_rx.recv().await.unwrap()?;
249+ async fn request<R: crate::types::Request>(&self, params: R::Arguments) -> Result<R::Result>
250+ where
251+ R::Arguments: serde::Serialize,
252+ R::Result: core::fmt::Debug, // TODO: temporary
253+ {
254+ // a future that resolves into the response
255+ let json = self.call::<R>(params).await?;
228− let response = serde_json::from_value(response.body.unwrap_or_default())?;
256+ let response = serde_json::from_value(json)?;
229 Ok(response)
257 Ok(response)
230 }
258 }
231
259
@@ -260,18 +288,16 @@ impl Client {
260 self.request::<requests::Disconnect>(()).await
288 self.request::<requests::Disconnect>(()).await
261 }
289 }
262
290
263− pub async fn launch(&mut self, args: serde_json::Value) -> Result<()> {
291+ pub async fn launch(&mut self, args: serde_json::Value) -> Result<Value> {
264 let response = self.request::<requests::Launch>(args).await?;
292 let response = self.request::<requests::Launch>(args).await?;
265 log::error!("launch response {}", response);
293 log::error!("launch response {}", response);
266−
294+ Ok(response)
267− Ok(())
268 }
295 }
269
296
270− pub async fn attach(&mut self, args: serde_json::Value) -> Result<()> {
297+ pub async fn attach(&mut self, args: serde_json::Value) -> Result<Value> {
271 let response = self.request::<requests::Attach>(args).await?;
298 let response = self.request::<requests::Attach>(args).await?;
272 log::error!("attach response {}", response);
299 log::error!("attach response {}", response);
273−
300+ Ok(response)
274− Ok(())
275 }
301 }
276
302
277 pub async fn set_breakpoints(
303 pub async fn set_breakpoints(