on master help sign in
dap: Reduce amount of block_on uses
fd0e4b1Blaž Hrastnik
helix-dap/src/client.rs+25−20
@@ -208,6 +208,16 @@ impl Client {
208 self.request_counter.fetch_add(1, Ordering::Relaxed)
208 self.request_counter.fetch_add(1, Ordering::Relaxed)
209 }
209 }
210
210
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+
211 /// Execute a RPC request on the debugger.
221 /// Execute a RPC request on the debugger.
212 pub fn call<R: crate::types::Request>(
222 pub fn call<R: crate::types::Request>(
213 &self,
223 &self,
@@ -321,8 +331,8 @@ impl Client {
321 Ok(())
331 Ok(())
322 }
332 }
323
333
324− pub async fn disconnect(&self) -> Result<()> {
334+ pub fn disconnect(&self) -> impl Future<Output = Result<Value>> {
325− self.request::<requests::Disconnect>(()).await
335+ self.call::<requests::Disconnect>(())
326 }
336 }
327
337
328 pub fn launch(&self, args: serde_json::Value) -> impl Future<Output = Result<Value>> {
338 pub fn launch(&self, args: serde_json::Value) -> impl Future<Output = Result<Value>> {
@@ -362,11 +372,10 @@ impl Client {
362 self.request::<requests::ConfigurationDone>(()).await
372 self.request::<requests::ConfigurationDone>(()).await
363 }
373 }
364
374
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>> {
366 let args = requests::ContinueArguments { thread_id };
376 let args = requests::ContinueArguments { thread_id };
367
377
368− let response = self.request::<requests::Continue>(args).await?;
378+ self.call::<requests::Continue>(args)
369− Ok(response.all_threads_continued)
370 }
379 }
371
380
372 pub async fn stack_trace(
381 pub async fn stack_trace(
@@ -408,38 +417,38 @@ impl Client {
408 Ok(response.variables)
417 Ok(response.variables)
409 }
418 }
410
419
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>> {
412 let args = requests::StepInArguments {
421 let args = requests::StepInArguments {
413 thread_id,
422 thread_id,
414 target_id: None,
423 target_id: None,
415 granularity: None,
424 granularity: None,
416 };
425 };
417
426
418− self.request::<requests::StepIn>(args).await
427+ self.call::<requests::StepIn>(args)
419 }
428 }
420
429
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>> {
422 let args = requests::StepOutArguments {
431 let args = requests::StepOutArguments {
423 thread_id,
432 thread_id,
424 granularity: None,
433 granularity: None,
425 };
434 };
426
435
427− self.request::<requests::StepOut>(args).await
436+ self.call::<requests::StepOut>(args)
428 }
437 }
429
438
430− pub async fn next(&self, thread_id: ThreadId) -> Result<()> {
439+ pub fn next(&self, thread_id: ThreadId) -> impl Future<Output = Result<Value>> {
431 let args = requests::NextArguments {
440 let args = requests::NextArguments {
432 thread_id,
441 thread_id,
433 granularity: None,
442 granularity: None,
434 };
443 };
435
444
436− self.request::<requests::Next>(args).await
445+ self.call::<requests::Next>(args)
437 }
446 }
438
447
439− pub async fn pause(&self, thread_id: ThreadId) -> Result<()> {
448+ pub fn pause(&self, thread_id: ThreadId) -> impl Future<Output = Result<Value>> {
440 let args = requests::PauseArguments { thread_id };
449 let args = requests::PauseArguments { thread_id };
441
450
442− self.request::<requests::Pause>(args).await
451+ self.call::<requests::Pause>(args)
443 }
452 }
444
453
445 pub async fn eval(
454 pub async fn eval(
@@ -457,16 +466,12 @@ impl Client {
457 self.request::<requests::Evaluate>(args).await
466 self.request::<requests::Evaluate>(args).await
458 }
467 }
459
468
460− pub async fn set_exception_breakpoints(
469+ pub fn set_exception_breakpoints(
461 &self,
470 &self,
462 filters: Vec<String>,
471 filters: Vec<String>,
463− ) -> Result<Option<Vec<Breakpoint>>> {
472+ ) -> impl Future<Output = Result<Value>> {
464 let args = requests::SetExceptionBreakpointsArguments { filters };
473 let args = requests::SetExceptionBreakpointsArguments { filters };
465
474
466− let response = self
467− .request::<requests::SetExceptionBreakpoints>(args)
475+ self.call::<requests::SetExceptionBreakpoints>(args)
468− .await;
469−
470− Ok(response.ok().map(|r| r.breakpoints).unwrap_or_default())
471 }
476 }
472}
477}
helix-term/src/application.rs+2−2
@@ -330,7 +330,7 @@ impl Application {
330 }
330 }
331
331
332 pub async fn handle_debugger_message(&mut self, payload: helix_dap::Payload) {
332 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};
334 use dap::requests::RunInTerminal;
334 use dap::requests::RunInTerminal;
335 use helix_dap::{events, Event};
335 use helix_dap::{events, Event};
336
336
@@ -392,7 +392,7 @@ impl Application {
392 .thread_states
392 .thread_states
393 .insert(thread_id, "running".to_owned());
393 .insert(thread_id, "running".to_owned());
394 if debugger.thread_id == Some(thread_id) {
394 if debugger.thread_id == Some(thread_id) {
395− resume_application(debugger)
395+ debugger.resume_application();
396 }
396 }
397 }
397 }
398 Event::Thread(_) => {
398 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
40 Some(pos)
40 Some(pos)
41}
41}
42
42
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−
54pub async fn select_thread_id(editor: &mut Editor, thread_id: ThreadId, force: bool) {
43pub async fn select_thread_id(editor: &mut Editor, thread_id: ThreadId, force: bool) {
55 let debugger = debugger!(editor);
44 let debugger = debugger!(editor);
56
45
@@ -518,11 +507,14 @@ pub fn dap_continue(cx: &mut Context) {
518
507
519 if let Some(thread_id) = debugger.thread_id {
508 if let Some(thread_id) = debugger.thread_id {
520 let request = debugger.continue_thread(thread_id);
509 let request = debugger.continue_thread(thread_id);
521− if let Err(e) = block_on(request) {
510+
522− cx.editor.set_error(format!("Failed to continue: {}", e));
511+ dap_callback(
523− return;
512+ cx.jobs,
524− }
513+ request,
525− resume_application(debugger);
514+ |editor, _compositor, _response: dap::requests::ContinueResponse| {
515+ debugger!(editor).resume_application();
516+ },
517+ );
526 } else {
518 } else {
527 cx.editor
519 cx.editor
528 .set_error("Currently active thread is not stopped. Switch the thread.".into());
520 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -545,11 +537,10 @@ pub fn dap_step_in(cx: &mut Context) {
545
537
546 if let Some(thread_id) = debugger.thread_id {
538 if let Some(thread_id) = debugger.thread_id {
547 let request = debugger.step_in(thread_id);
539 let request = debugger.step_in(thread_id);
548− if let Err(e) = block_on(request) {
540+
549− cx.editor.set_error(format!("Failed to continue: {}", e));
541+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
550− return;
551− }
552− resume_application(debugger);
542+ debugger!(editor).resume_application();
543+ });
553 } else {
544 } else {
554 cx.editor
545 cx.editor
555 .set_error("Currently active thread is not stopped. Switch the thread.".into());
546 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -561,11 +552,9 @@ pub fn dap_step_out(cx: &mut Context) {
561
552
562 if let Some(thread_id) = debugger.thread_id {
553 if let Some(thread_id) = debugger.thread_id {
563 let request = debugger.step_out(thread_id);
554 let request = debugger.step_out(thread_id);
564− if let Err(e) = block_on(request) {
555+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
565− cx.editor.set_error(format!("Failed to continue: {}", e));
566− return;
567− }
568− resume_application(debugger);
556+ debugger!(editor).resume_application();
557+ });
569 } else {
558 } else {
570 cx.editor
559 cx.editor
571 .set_error("Currently active thread is not stopped. Switch the thread.".into());
560 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -577,11 +566,9 @@ pub fn dap_next(cx: &mut Context) {
577
566
578 if let Some(thread_id) = debugger.thread_id {
567 if let Some(thread_id) = debugger.thread_id {
579 let request = debugger.next(thread_id);
568 let request = debugger.next(thread_id);
580− if let Err(e) = block_on(request) {
569+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
581− cx.editor.set_error(format!("Failed to continue: {}", e));
582− return;
583− }
584− resume_application(debugger);
570+ debugger!(editor).resume_application();
571+ });
585 } else {
572 } else {
586 cx.editor
573 cx.editor
587 .set_error("Currently active thread is not stopped. Switch the thread.".into());
574 .set_error("Currently active thread is not stopped. Switch the thread.".into());
@@ -658,11 +645,10 @@ pub fn dap_terminate(cx: &mut Context) {
658 let debugger = debugger!(cx.editor);
645 let debugger = debugger!(cx.editor);
659
646
660 let request = debugger.disconnect();
647 let request = debugger.disconnect();
661− if let Err(e) = block_on(request) {
648+ dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| {
662− cx.editor.set_error(format!("Failed to disconnect: {}", e));
649+ // editor.set_error(format!("Failed to disconnect: {}", e));
663− return;
664− }
665− cx.editor.debugger = None;
650+ editor.debugger = None;
651+ });
666}
652}
667
653
668pub fn dap_enable_exceptions(cx: &mut Context) {
654pub fn dap_enable_exceptions(cx: &mut Context) {
@@ -673,19 +659,29 @@ pub fn dap_enable_exceptions(cx: &mut Context) {
673 None => return,
659 None => return,
674 };
660 };
675
661
676− if let Err(e) = block_on(debugger.set_exception_breakpoints(filters)) {
662+ let request = debugger.set_exception_breakpoints(filters);
677− cx.editor
663+
664+ dap_callback(
665+ cx.jobs,
666+ request,
667+ |_editor, _compositor, _response: dap::requests::SetExceptionBreakpointsResponse| {
678− .set_error(format!("Failed to set up exception breakpoints: {}", e));
668+ // editor.set_error(format!("Failed to set up exception breakpoints: {}", e));
679− }
669+ },
670+ )
680}
671}
681
672
682pub fn dap_disable_exceptions(cx: &mut Context) {
673pub fn dap_disable_exceptions(cx: &mut Context) {
683 let debugger = debugger!(cx.editor);
674 let debugger = debugger!(cx.editor);
684
675
685− if let Err(e) = block_on(debugger.set_exception_breakpoints(Vec::new())) {
676+ let request = debugger.set_exception_breakpoints(Vec::new());
686− cx.editor
677+
678+ dap_callback(
679+ cx.jobs,
680+ request,
681+ |_editor, _compositor, _response: dap::requests::SetExceptionBreakpointsResponse| {
687− .set_error(format!("Failed to set up exception breakpoints: {}", e));
682+ // editor.set_error(format!("Failed to set up exception breakpoints: {}", e));
688− }
683+ },
684+ )
689}
685}
690
686
691// TODO: both edit condition and edit log need to be stable: we might get new breakpoints from the debugger which can change offsets
687// TODO: both edit condition and edit log need to be stable: we might get new breakpoints from the debugger which can change offsets