on master help sign in
dap: Reduce amount of block_on uses
commitfd0e4b1159e2e96548a5fa962298fe8f2a80fc5c authorBlaž Hrastnik <blaz@mxxn.io> dateTue 15 Feb 2022, 16:30 (5 years ago) parenta629343
3 files changed+67−66
helix-dap/src/client.rs+25−20 helix-term/src/application.rs+2−2 helix-term/src/commands/dap.rs+40−44
helix-dap/src/client.rs+25−20
@@ -208,6 +208,16 @@ impl Client {
208208 self.request_counter.fetch_add(1, Ordering::Relaxed)
209209 }
210210
211+ // Internal, called by specific DAP commands when resuming
212+ pub fn resume_application(&mut self) {
213+ if let Some(thread_id) = self.thread_id {
214+ self.thread_states.insert(thread_id, "running".to_string());
215+ self.stack_frames.remove(&thread_id);
216+ }
217+ self.active_frame = None;
218+ self.thread_id = None;
219+ }
220+
211221 /// Execute a RPC request on the debugger.
212222 pub fn call<R: crate::types::Request>(
213223 &self,
@@ -321,8 +331,8 @@ impl Client {
321331 Ok(())
322332 }
323333
324− pub async fn disconnect(&self) -> Result<()> {
325− self.request::<requests::Disconnect>(()).await
334+ pub fn disconnect(&self) -> impl Future<Output = Result<Value>> {
335+ self.call::<requests::Disconnect>(())
326336 }
327337
328338 pub fn launch(&self, args: serde_json::Value) -> impl Future<Output = Result<Value>> {
@@ -362,11 +372,10 @@ impl Client {
362372 self.request::<requests::ConfigurationDone>(()).await
363373 }
364374
365− pub async fn continue_thread(&self, thread_id: ThreadId) -> Result<Option<bool>> {
375+ pub fn continue_thread(&self, thread_id: ThreadId) -> impl Future<Output = Result<Value>> {
366376 let args = requests::ContinueArguments { thread_id };
367377
368− let response = self.request::<requests::Continue>(args).await?;
369− Ok(response.all_threads_continued)
378+ self.call::<requests::Continue>(args)
370379 }
371380
372381 pub async fn stack_trace(
@@ -408,38 +417,38 @@ impl Client {
408417 Ok(response.variables)
409418 }
410419
411− pub async fn step_in(&self, thread_id: ThreadId) -> Result<()> {
420+ pub fn step_in(&self, thread_id: ThreadId) -> impl Future<Output = Result<Value>> {
412421 let args = requests::StepInArguments {
413422 thread_id,
414423 target_id: None,
415424 granularity: None,
416425 };
417426
418− self.request::<requests::StepIn>(args).await
427+ self.call::<requests::StepIn>(args)
419428 }
420429
421− pub async fn step_out(&self, thread_id: ThreadId) -> Result<()> {
430+ pub fn step_out(&self, thread_id: ThreadId) -> impl Future<Output = Result<Value>> {
422431 let args = requests::StepOutArguments {
423432 thread_id,
424433 granularity: None,
425434 };
426435
427− self.request::<requests::StepOut>(args).await
436+ self.call::<requests::StepOut>(args)
428437 }
429438
430− pub async fn next(&self, thread_id: ThreadId) -> Result<()> {
439+ pub fn next(&self, thread_id: ThreadId) -> impl Future<Output = Result<Value>> {
431440 let args = requests::NextArguments {
432441 thread_id,
433442 granularity: None,
434443 };
435444
436− self.request::<requests::Next>(args).await
445+ self.call::<requests::Next>(args)
437446 }
438447
439− pub async fn pause(&self, thread_id: ThreadId) -> Result<()> {
448+ pub fn pause(&self, thread_id: ThreadId) -> impl Future<Output = Result<Value>> {
440449 let args = requests::PauseArguments { thread_id };
441450
442− self.request::<requests::Pause>(args).await
451+ self.call::<requests::Pause>(args)
443452 }
444453
445454 pub async fn eval(
@@ -457,16 +466,12 @@ impl Client {
457466 self.request::<requests::Evaluate>(args).await
458467 }
459468
460− pub async fn set_exception_breakpoints(
469+ pub fn set_exception_breakpoints(
461470 &self,
462471 filters: Vec<String>,
463− ) -> Result<Option<Vec<Breakpoint>>> {
472+ ) -> impl Future<Output = Result<Value>> {
464473 let args = requests::SetExceptionBreakpointsArguments { filters };
465474
466− let response = self
467− .request::<requests::SetExceptionBreakpoints>(args)
468− .await;
469−
470− Ok(response.ok().map(|r| r.breakpoints).unwrap_or_default())
475+ self.call::<requests::SetExceptionBreakpoints>(args)
471476 }
472477}
helix-term/src/application.rs+2−2
@@ -330,7 +330,7 @@ impl Application {
330330 }
331331
332332 pub async fn handle_debugger_message(&mut self, payload: helix_dap::Payload) {
333− use crate::commands::dap::{breakpoints_changed, resume_application, select_thread_id};
333+ use crate::commands::dap::{breakpoints_changed, select_thread_id};
334334 use dap::requests::RunInTerminal;
335335 use helix_dap::{events, Event};
336336
@@ -392,7 +392,7 @@ impl Application {
392392 .thread_states
393393 .insert(thread_id, "running".to_owned());
394394 if debugger.thread_id == Some(thread_id) {
395− resume_application(debugger)
395+ debugger.resume_application();
396396 }
397397 }
398398 Event::Thread(_) => {
helix-term/src/commands/dap.rs+40−44
@@ -40,17 +40,6 @@ pub fn dap_pos_to_pos(doc: &helix_core::Rope, line: usize, column: usize) -> Opt
4040 Some(pos)
4141}
4242
43−pub fn resume_application(debugger: &mut Client) {
44− if let Some(thread_id) = debugger.thread_id {
45− debugger
46− .thread_states
47− .insert(thread_id, "running".to_string());
48− debugger.stack_frames.remove(&thread_id);
49− }
50− debugger.active_frame = None;
51− debugger.thread_id = None;
52−}
53−
5443pub async fn select_thread_id(editor: &mut Editor, thread_id: ThreadId, force: bool) {
5544 let debugger = debugger!(editor);
5645
@@ -518,11 +507,14 @@ pub fn dap_continue(cx: &mut Context) {
518507
519508 if let Some(thread_id) = debugger.thread_id {
520509 let request = debugger.continue_thread(thread_id);
521− if let Err(e) = block_on(request) {
522− cx.editor.set_error(format!("Failed to continue: {}", e));
523− return;
524− }
525− resume_application(debugger);
510+
511+ dap_callback(
512+ cx.jobs,
513+ request,
514+ |editor, _compositor, _response: dap::requests::ContinueResponse| {
515+ debugger!(editor).resume_application();
516+ },
517+ );
526518 } else {
527519 cx.editor
528520 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -545,11 +537,10 @@ pub fn dap_step_in(cx: &mut Context) {
545537
546538 if let Some(thread_id) = debugger.thread_id {
547539 let request = debugger.step_in(thread_id);
548− if let Err(e) = block_on(request) {
549− cx.editor.set_error(format!("Failed to continue: {}", e));
550− return;
551− }
552− resume_application(debugger);
540+
541+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
542+ debugger!(editor).resume_application();
543+ });
553544 } else {
554545 cx.editor
555546 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -561,11 +552,9 @@ pub fn dap_step_out(cx: &mut Context) {
561552
562553 if let Some(thread_id) = debugger.thread_id {
563554 let request = debugger.step_out(thread_id);
564− if let Err(e) = block_on(request) {
565− cx.editor.set_error(format!("Failed to continue: {}", e));
566− return;
567− }
568− resume_application(debugger);
555+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
556+ debugger!(editor).resume_application();
557+ });
569558 } else {
570559 cx.editor
571560 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -577,11 +566,9 @@ pub fn dap_next(cx: &mut Context) {
577566
578567 if let Some(thread_id) = debugger.thread_id {
579568 let request = debugger.next(thread_id);
580− if let Err(e) = block_on(request) {
581− cx.editor.set_error(format!("Failed to continue: {}", e));
582− return;
583− }
584− resume_application(debugger);
569+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
570+ debugger!(editor).resume_application();
571+ });
585572 } else {
586573 cx.editor
587574 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -658,11 +645,10 @@ pub fn dap_terminate(cx: &mut Context) {
658645 let debugger = debugger!(cx.editor);
659646
660647 let request = debugger.disconnect();
661− if let Err(e) = block_on(request) {
662− cx.editor.set_error(format!("Failed to disconnect: {}", e));
663− return;
664− }
665− cx.editor.debugger = None;
648+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
649+ // editor.set_error(format!("Failed to disconnect: {}", e));
650+ editor.debugger = None;
651+ });
666652}
667653
668654pub fn dap_enable_exceptions(cx: &mut Context) {
@@ -673,19 +659,29 @@ pub fn dap_enable_exceptions(cx: &mut Context) {
673659 None => return,
674660 };
675661
676− if let Err(e) = block_on(debugger.set_exception_breakpoints(filters)) {
677− cx.editor
678− .set_error(format!("Failed to set up exception breakpoints: {}", e));
679− }
662+ let request = debugger.set_exception_breakpoints(filters);
663+
664+ dap_callback(
665+ cx.jobs,
666+ request,
667+ |_editor, _compositor, _response: dap::requests::SetExceptionBreakpointsResponse| {
668+ // editor.set_error(format!("Failed to set up exception breakpoints: {}", e));
669+ },
670+ )
680671}
681672
682673pub fn dap_disable_exceptions(cx: &mut Context) {
683674 let debugger = debugger!(cx.editor);
684675
685− if let Err(e) = block_on(debugger.set_exception_breakpoints(Vec::new())) {
686− cx.editor
687− .set_error(format!("Failed to set up exception breakpoints: {}", e));
688− }
676+ let request = debugger.set_exception_breakpoints(Vec::new());
677+
678+ dap_callback(
679+ cx.jobs,
680+ request,
681+ |_editor, _compositor, _response: dap::requests::SetExceptionBreakpointsResponse| {
682+ // editor.set_error(format!("Failed to set up exception breakpoints: {}", e));
683+ },
684+ )
689685}
690686
691687// TODO: both edit condition and edit log need to be stable: we might get new breakpoints from the debugger which can change offsets