on master help sign in
dap: Extract thread_picker, make pause explicitly select a thread
commit42f9718f55b58283253b2be85818a0aa0d5e3596 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 3 Sep 2021, 11:40 (5 years ago) parent27c1b3f
3 files changed+46−45
helix-dap/src/client.rs+1 helix-term/src/application.rs+4−4 helix-term/src/commands/dap.rs+41−41
helix-dap/src/client.rs+1
@@ -80,6 +80,7 @@ impl Client {
8080 //
8181 breakpoints: HashMap::new(),
8282 stack_frames: HashMap::new(),
83+ thread_states: HashMap::new(),
8384 thread_id: None,
8485 active_frame: None,
8586 is_running: false,
helix-term/src/application.rs+4−4
@@ -258,7 +258,7 @@ impl Application {
258258 pub async fn handle_debugger_message(&mut self, payload: helix_dap::Payload) {
259259 use crate::commands::dap::{resume_application, select_thread_id};
260260 use helix_dap::{events, Event};
261− let mut debugger = match self.editor.debugger.as_mut() {
261+ let debugger = match self.editor.debugger.as_mut() {
262262 Some(debugger) => debugger,
263263 None => return,
264264 };
@@ -274,12 +274,12 @@ impl Application {
274274 ..
275275 }) => {
276276 if let Some(thread_id) = thread_id {
277− debugger.thread_states.insert(thread_id, reason); // TODO: dap uses "type" || "reason" here
277+ debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here
278+
278279 // whichever thread stops is made "current" (if no previously selected thread).
279280 select_thread_id(&mut self.editor, thread_id, false).await;
280281 }
281282
282−
283283 let scope = match thread_id {
284284 Some(id) => format!("Thread {}", id),
285285 None => "Target".to_owned(),
@@ -305,7 +305,7 @@ impl Application {
305305 }
306306 }
307307 Event::Thread(_) => {
308− // TODO: update thread_states, make threads request
308+ // TODO: update thread_states, make threads request
309309 }
310310 Event::Output(events::Output {
311311 category, output, ..
helix-term/src/commands/dap.rs+41−41
@@ -1,7 +1,7 @@
11use super::{align_view, Align, Context, Editor};
22use crate::ui::Picker;
33use helix_core::Selection;
4−use helix_dap::Client;
4+use helix_dap::{self as dap, Client};
55use helix_lsp::block_on;
66
77use serde_json::{to_value, Value};
@@ -20,7 +20,9 @@ pub fn dap_pos_to_pos(doc: &helix_core::Rope, line: usize, column: usize) -> Opt
2020
2121pub fn resume_application(debugger: &mut Client) {
2222 if let Some(thread_id) = debugger.thread_id {
23− debugger.thread_states.insert(thread_id, "running".to_string());
23+ debugger
24+ .thread_states
25+ .insert(thread_id, "running".to_string());
2426 debugger.stack_frames.remove(&thread_id);
2527 }
2628 debugger.active_frame = None;
@@ -80,7 +82,32 @@ pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
8082 align_view(doc, view, Align::Center);
8183}
8284
83−// DAP
85+fn thread_picker(cx: &mut Context, callback_fn: impl Fn(&mut Editor, &dap::Thread) + 'static) {
86+ let debugger = match &mut cx.editor.debugger {
87+ Some(debugger) => debugger,
88+ None => return,
89+ };
90+
91+ let threads = match block_on(debugger.threads()) {
92+ Ok(threads) => threads,
93+ Err(e) => {
94+ cx.editor
95+ .set_error(format!("Failed to retrieve threads: {:?}", e));
96+ return;
97+ }
98+ };
99+
100+ let picker = Picker::new(
101+ true,
102+ threads,
103+ |thread| thread.name.clone().into(),
104+ move |editor, thread, _action| callback_fn(editor, thread),
105+ );
106+ cx.push_layer(Box::new(picker))
107+}
108+
109+// -- DAP
110+
84111pub fn dap_start_impl(
85112 editor: &mut Editor,
86113 name: Option<&str>,
@@ -312,23 +339,17 @@ pub fn dap_continue(cx: &mut Context) {
312339}
313340
314341pub fn dap_pause(cx: &mut Context) {
315− let debugger = match &mut cx.editor.debugger {
342+ thread_picker(cx, |editor, thread| {
343+ let debugger = match &mut editor.debugger {
316344 Some(debugger) => debugger,
317345 None => return,
318346 };
319−
320− // TODO: this should instead open a picker to pause a selected thread
321−
322− if !debugger.is_running {
323− cx.editor.set_status("Debuggee is not running".to_owned());
324− return;
325− }
326−
327− // FIXME: correct number here
328− let request = debugger.pause(0);
329− if let Err(e) = block_on(request) {
330− cx.editor.set_error(format!("Failed to pause: {:?}", e));
331− }
347+ let request = debugger.pause(thread.id);
348+ // NOTE: we don't need to set active thread id here because DAP will emit a "stopped" event
349+ if let Err(e) = block_on(request) {
350+ editor.set_error(format!("Failed to pause: {:?}", e));
351+ }
352+ })
332353}
333354
334355pub fn dap_step_in(cx: &mut Context) {
@@ -456,28 +477,7 @@ pub fn dap_terminate(cx: &mut Context) {
456477}
457478
458479pub fn dap_switch_thread(cx: &mut Context) {
459− let debugger = match &mut cx.editor.debugger {
460− Some(debugger) => debugger,
461− None => return,
462− };
463−
464− let request = debugger.threads();
465− let threads = match block_on(request) {
466− Ok(threads) => threads,
467− Err(e) => {
468− cx.editor
469− .set_error(format!("Failed to retrieve threads: {:?}", e));
470− return;
471− }
472− };
473−
474− let picker = Picker::new(
475− true,
476− threads,
477− |thread| thread.name.clone().into(),
478− |editor, thread, _action| {
479− block_on(select_thread_id(editor, thread.id, true));
480− },
481− );
482− cx.push_layer(Box::new(picker))
480+ thread_picker(cx, |editor, thread| {
481+ block_on(select_thread_id(editor, thread.id, true));
482+ })
483483}