on master help sign in
Make thread_picker non-blocking
commitdc8df7ba219d9a403ee77027dd5a698f08a1dcce authorBlaž Hrastnik <blaz@mxxn.io> dateSun 5 Dec 2021, 14:52 (5 years ago) parent2b4de41
3 files changed+54−47
helix-dap/src/client.rs+4−5 helix-term/src/application.rs+2−2 helix-term/src/commands/dap.rs+48−40
helix-dap/src/client.rs+4−5
@@ -209,7 +209,7 @@ impl Client {
209209 }
210210
211211 /// Execute a RPC request on the debugger.
212− fn call<R: crate::types::Request>(
212+ pub fn call<R: crate::types::Request>(
213213 &self,
214214 arguments: R::Arguments,
215215 ) -> impl Future<Output = Result<Value>>
@@ -248,7 +248,7 @@ impl Client {
248248 }
249249 }
250250
251− async fn request<R: crate::types::Request>(&self, params: R::Arguments) -> Result<R::Result>
251+ pub async fn request<R: crate::types::Request>(&self, params: R::Arguments) -> Result<R::Result>
252252 where
253253 R::Arguments: serde::Serialize,
254254 R::Result: core::fmt::Debug, // TODO: temporary
@@ -384,9 +384,8 @@ impl Client {
384384 Ok((response.stack_frames, response.total_frames))
385385 }
386386
387− pub async fn threads(&self) -> Result<Vec<Thread>> {
388− let response = self.request::<requests::Threads>(()).await?;
389− Ok(response.threads)
387+ pub fn threads(&self) -> impl Future<Output = Result<Value>> {
388+ self.call::<requests::Threads>(())
390389 }
391390
392391 pub async fn scopes(&self, frame_id: usize) -> Result<Vec<Scope>> {
helix-term/src/application.rs+2−2
@@ -340,8 +340,8 @@ impl Application {
340340 let all_threads_stopped = all_threads_stopped.unwrap_or_default();
341341
342342 if all_threads_stopped {
343− if let Ok(threads) = debugger.threads().await {
344− for thread in threads {
343+ if let Ok(response) = debugger.request::<dap::requests::Threads>(()).await {
344+ for thread in response.threads {
345345 fetch_stack_trace(debugger, thread.id).await;
346346 }
347347 select_thread_id(
helix-term/src/commands/dap.rs+48−40
@@ -19,7 +19,7 @@ use std::collections::HashMap;
1919use std::future::Future;
2020use std::path::PathBuf;
2121
22−use anyhow::bail;
22+use anyhow::{anyhow, bail};
2323
2424// general utils:
2525pub fn dap_pos_to_pos(doc: &helix_core::Rope, line: usize, column: usize) -> Option<usize> {
@@ -99,53 +99,61 @@ pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
9999 align_view(doc, view, Align::Center);
100100}
101101
102−fn thread_picker(cx: &mut Context, callback_fn: impl Fn(&mut Editor, &dap::Thread) + 'static) {
102+fn thread_picker(
103+ cx: &mut Context,
104+ callback_fn: impl Fn(&mut Editor, &dap::Thread) + Send + 'static,
105+) {
103106 let debugger = match &mut cx.editor.debugger {
104107 Some(debugger) => debugger,
105108 None => return,
106109 };
107110
108− let threads = match block_on(debugger.threads()) {
109− Ok(threads) => threads,
110− Err(e) => {
111− cx.editor
112− .set_error(format!("Failed to retrieve threads: {}", e));
113− return;
114− }
115− };
116−
117− if threads.len() == 1 {
118− callback_fn(cx.editor, &threads[0]);
119− return;
120− }
111+ let future = debugger.threads();
112+ dap_callback(
113+ cx.jobs,
114+ future,
115+ move |editor: &mut Editor,
116+ compositor: &mut Compositor,
117+ response: dap::requests::ThreadsResponse| {
118+ let threads = response.threads;
119+ if threads.len() == 1 {
120+ callback_fn(editor, &threads[0]);
121+ return;
122+ }
123+ let debugger = match &mut editor.debugger {
124+ Some(debugger) => debugger,
125+ None => return,
126+ };
121127
122− let thread_states = debugger.thread_states.clone();
123− let picker = FilePicker::new(
124− threads,
125− move |thread| {
126− format!(
127− "{} ({})",
128− thread.name,
129− thread_states
130− .get(&thread.id)
131− .map(|state| state.as_str())
132− .unwrap_or("unknown")
133− )
134− .into()
135− },
136− move |cx, thread, _action| callback_fn(cx.editor, thread),
137− move |editor, thread| {
138− let frames = editor.debugger.as_ref()?.stack_frames.get(&thread.id)?;
139− let frame = frames.get(0)?;
140− let path = frame.source.as_ref()?.path.clone()?;
141− let pos = Some((
142− frame.line.saturating_sub(1),
143− frame.end_line.unwrap_or(frame.line).saturating_sub(1),
144− ));
145− Some((path, pos))
128+ let thread_states = debugger.thread_states.clone();
129+ let picker = FilePicker::new(
130+ threads,
131+ move |thread| {
132+ format!(
133+ "{} ({})",
134+ thread.name,
135+ thread_states
136+ .get(&thread.id)
137+ .map(|state| state.as_str())
138+ .unwrap_or("unknown")
139+ )
140+ .into()
141+ },
142+ move |cx, thread, _action| callback_fn(cx.editor, thread),
143+ move |editor, thread| {
144+ let frames = editor.debugger.as_ref()?.stack_frames.get(&thread.id)?;
145+ let frame = frames.get(0)?;
146+ let path = frame.source.as_ref()?.path.clone()?;
147+ let pos = Some((
148+ frame.line.saturating_sub(1),
149+ frame.end_line.unwrap_or(frame.line).saturating_sub(1),
150+ ));
151+ Some((path, pos))
152+ },
153+ );
154+ compositor.push(Box::new(picker));
146155 },
147156 );
148− cx.push_layer(Box::new(picker))
149157}
150158
151159fn get_breakpoint_at_current_line(editor: &mut Editor) -> Option<(usize, Breakpoint)> {