Skip to content

Commit 737b03c

Browse files
committed
Refactor go_to_stack_frame & clear_hightlights
1 parent 1b42dd5 commit 737b03c

File tree

1 file changed

+103
-106
lines changed

1 file changed

+103
-106
lines changed

crates/debugger_ui/src/debugger_panel.rs

Lines changed: 103 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -152,133 +152,123 @@ impl DebugPanel {
152152
}
153153
}
154154

155-
pub fn go_to_stack_frame(
156-
stack_frame: &StackFrame,
155+
pub async fn go_to_stack_frame(
156+
workspace: WeakView<Workspace>,
157+
stack_frame: StackFrame,
157158
client: Arc<DebugAdapterClient>,
158159
clear_highlights: bool,
159-
cx: &mut ViewContext<Self>,
160-
) -> Task<Result<()>> {
160+
mut cx: AsyncWindowContext,
161+
) -> Result<()> {
161162
let path = stack_frame.clone().source.unwrap().path.unwrap().clone();
162163
let row = (stack_frame.line.saturating_sub(1)) as u32;
163164
let column = (stack_frame.column.saturating_sub(1)) as u32;
164165

165-
cx.spawn(move |this, mut cx| async move {
166-
if clear_highlights {
167-
// TODO:
168-
// this.update(&mut cx, |this, cx| this.remove_highlights(client, cx))?
169-
// .await?;
170-
}
166+
if clear_highlights {
167+
Self::remove_highlights(workspace.clone(), client, cx.clone()).await?;
168+
}
171169

172-
let task = this.update(&mut cx, |this, cx| {
173-
this.workspace.update(cx, |workspace, cx| {
174-
let project_path = workspace.project().read_with(cx, |project, cx| {
175-
project.project_path_for_absolute_path(&Path::new(&path), cx)
176-
});
170+
let task = workspace.update(&mut cx, |workspace, cx| {
171+
let project_path = workspace.project().read_with(cx, |project, cx| {
172+
project.project_path_for_absolute_path(&Path::new(&path), cx)
173+
});
177174

178-
if let Some(project_path) = project_path {
179-
workspace.open_path_preview(project_path, None, false, true, cx)
180-
} else {
181-
Task::ready(Err(anyhow::anyhow!(
182-
"No project path found for path: {}",
183-
path
184-
)))
185-
}
186-
})
187-
})??;
188-
189-
let editor = task.await?.downcast::<Editor>().unwrap();
190-
191-
this.update(&mut cx, |this, cx| {
192-
this.workspace.update(cx, |_, cx| {
193-
editor.update(cx, |editor, cx| {
194-
editor.go_to_line::<DebugCurrentRowHighlight>(
195-
row,
196-
column,
197-
Some(cx.theme().colors().editor_highlighted_line_background),
198-
cx,
199-
);
200-
})
201-
})
202-
})??;
175+
if let Some(project_path) = project_path {
176+
workspace.open_path_preview(project_path, None, false, true, cx)
177+
} else {
178+
Task::ready(Err(anyhow::anyhow!(
179+
"No project path found for path: {}",
180+
path
181+
)))
182+
}
183+
})?;
203184

204-
anyhow::Ok(())
185+
let editor = task.await?.downcast::<Editor>().unwrap();
186+
187+
workspace.update(&mut cx, |_, cx| {
188+
editor.update(cx, |editor, cx| {
189+
editor.go_to_line::<DebugCurrentRowHighlight>(
190+
row,
191+
column,
192+
Some(cx.theme().colors().editor_highlighted_line_background),
193+
cx,
194+
);
195+
})
205196
})
206197
}
207198

208-
fn remove_highlights(
199+
async fn remove_highlights(
200+
workspace: WeakView<Workspace>,
209201
client: Arc<DebugAdapterClient>,
210-
cx: &mut ViewContext<Self>,
211-
) -> Task<Result<()>> {
202+
cx: AsyncWindowContext,
203+
) -> Result<()> {
212204
let mut tasks = Vec::new();
213205
for thread_state in client.thread_states().values() {
214206
for stack_frame in thread_state.stack_frames.clone() {
215-
tasks.push(Self::remove_editor_highlight(&stack_frame, cx));
207+
tasks.push(Self::remove_editor_highlight(
208+
workspace.clone(),
209+
stack_frame,
210+
cx.clone(),
211+
));
216212
}
217213
}
218214

219-
if tasks.is_empty() {
220-
return Task::ready(Ok(()));
221-
}
222-
223-
cx.spawn(|_, _| async move {
215+
if !tasks.is_empty() {
224216
try_join_all(tasks).await?;
217+
}
225218

226-
anyhow::Ok(())
227-
})
219+
anyhow::Ok(())
228220
}
229221

230-
fn remove_highlights_for_thread(
222+
async fn remove_highlights_for_thread(
223+
workspace: WeakView<Workspace>,
231224
client: Arc<DebugAdapterClient>,
232225
thread_id: u64,
233-
cx: &mut ViewContext<Self>,
234-
) -> Task<Result<()>> {
226+
cx: AsyncWindowContext,
227+
) -> Result<()> {
235228
let mut tasks = Vec::new();
236229
if let Some(thread_state) = client.thread_states().get(&thread_id) {
237230
for stack_frame in thread_state.stack_frames.clone() {
238-
tasks.push(Self::remove_editor_highlight(&stack_frame, cx));
231+
tasks.push(Self::remove_editor_highlight(
232+
workspace.clone(),
233+
stack_frame.clone(),
234+
cx.clone(),
235+
));
239236
}
240237
}
241238

242-
if tasks.is_empty() {
243-
return Task::ready(Ok(()));
244-
}
245-
246-
cx.spawn(|_, _| async move {
239+
if !tasks.is_empty() {
247240
try_join_all(tasks).await?;
241+
}
248242

249-
anyhow::Ok(())
250-
})
243+
anyhow::Ok(())
251244
}
252245

253-
fn remove_editor_highlight(
254-
stack_frame: &StackFrame,
255-
cx: &mut ViewContext<Self>,
256-
) -> Task<Result<()>> {
246+
async fn remove_editor_highlight(
247+
workspace: WeakView<Workspace>,
248+
stack_frame: StackFrame,
249+
mut cx: AsyncWindowContext,
250+
) -> Result<()> {
257251
let path = stack_frame.clone().source.unwrap().path.unwrap().clone();
258252

259-
cx.spawn(|this, mut cx| async move {
260-
let task = this.update(&mut cx, |this, cx| {
261-
this.workspace.update(cx, |workspace, cx| {
262-
let project_path = workspace.project().read_with(cx, |project, cx| {
263-
project.project_path_for_absolute_path(&Path::new(&path), cx)
264-
});
253+
let task = workspace.update(&mut cx, |workspace, cx| {
254+
let project_path = workspace.project().read_with(cx, |project, cx| {
255+
project.project_path_for_absolute_path(&Path::new(&path), cx)
256+
});
265257

266-
if let Some(project_path) = project_path {
267-
workspace.open_path(project_path, None, false, cx)
268-
} else {
269-
Task::ready(Err(anyhow::anyhow!(
270-
"No project path found for path: {}",
271-
path
272-
)))
273-
}
274-
})
275-
})??;
258+
if let Some(project_path) = project_path {
259+
workspace.open_path(project_path, None, false, cx)
260+
} else {
261+
Task::ready(Err(anyhow::anyhow!(
262+
"No project path found for path: {}",
263+
path
264+
)))
265+
}
266+
})?;
276267

277-
let editor = task.await?.downcast::<Editor>().unwrap();
268+
let editor = task.await?.downcast::<Editor>().unwrap();
278269

279-
editor.update(&mut cx, |editor, _| {
280-
editor.clear_row_highlights::<DebugCurrentRowHighlight>();
281-
})
270+
editor.update(&mut cx, |editor, _| {
271+
editor.clear_row_highlights::<DebugCurrentRowHighlight>();
282272
})
283273
}
284274

@@ -394,12 +384,18 @@ impl DebugPanel {
394384
if let Some(item) = this.pane.read(cx).active_item() {
395385
if let Some(pane) = item.downcast::<DebugPanelItem>() {
396386
if pane.read(cx).thread_id() == thread_id {
397-
return Self::go_to_stack_frame(
398-
&current_stack_frame,
399-
client.clone(),
400-
true,
401-
cx,
402-
);
387+
let workspace = this.workspace.clone();
388+
let client = client.clone();
389+
return cx.spawn(|_, cx| async move {
390+
Self::go_to_stack_frame(
391+
workspace,
392+
current_stack_frame.clone(),
393+
client,
394+
true,
395+
cx,
396+
)
397+
.await
398+
});
403399
}
404400
}
405401
}
@@ -429,16 +425,17 @@ impl DebugPanel {
429425
client.update_thread_state_status(thread_id.clone(), ThreadStatus::Ended);
430426

431427
// TODO: we want to figure out for witch clients/threads we should remove the highlights
432-
// cx.spawn({
433-
// let client = client.clone();
434-
// |this, mut cx| async move {
435-
// this.update(&mut cx, |_, cx| {
436-
// Self::remove_highlights_for_thread(client, thread_id, cx)
437-
// })?
438-
// .await
439-
// }
440-
// })
441-
// .detach_and_log_err(cx);
428+
cx.spawn({
429+
let client = client.clone();
430+
|this, mut cx| async move {
431+
let workspace = this.update(&mut cx, |this, _| this.workspace.clone())?;
432+
433+
Self::remove_highlights_for_thread(workspace, client, thread_id, cx).await?;
434+
435+
anyhow::Ok(())
436+
}
437+
})
438+
.detach_and_log_err(cx);
442439
}
443440

444441
cx.emit(DebugPanelEvent::Thread((*client_id, event.clone())));
@@ -452,12 +449,12 @@ impl DebugPanel {
452449
) {
453450
let restart_args = event.clone().and_then(|e| e.restart);
454451
let client = this.debug_client_by_id(*client_id, cx);
452+
let workspace = this.workspace.clone();
455453

456-
cx.spawn(|this, mut cx| async move {
454+
cx.spawn(|_, cx| async move {
457455
let should_restart = restart_args.is_some();
458456

459-
this.update(&mut cx, |_, cx| Self::remove_highlights(client.clone(), cx))?
460-
.await?;
457+
Self::remove_highlights(workspace, client.clone(), cx).await?;
461458

462459
client
463460
.request::<Disconnect>(DisconnectArguments {

0 commit comments

Comments
 (0)