1pub(crate) mod breakpoint_list;
2pub(crate) mod console;
3pub(crate) mod loaded_source_list;
4pub(crate) mod module_list;
5pub mod stack_frame_list;
6pub mod variable_list;
7
8use std::{any::Any, ops::ControlFlow, sync::Arc, time::Duration};
9
10use crate::persistence::{self, DebuggerPaneItem, SerializedPaneLayout};
11
12use super::DebugPanelItemEvent;
13use breakpoint_list::BreakpointList;
14use collections::{HashMap, IndexMap};
15use console::Console;
16use dap::{Capabilities, Thread, client::SessionId, debugger_settings::DebuggerSettings};
17use gpui::{
18 Action as _, AnyView, AppContext, Entity, EntityId, EventEmitter, FocusHandle, Focusable,
19 NoAction, Pixels, Point, Subscription, Task, WeakEntity,
20};
21use loaded_source_list::LoadedSourceList;
22use module_list::ModuleList;
23use project::{
24 Project,
25 debugger::session::{Session, SessionEvent, ThreadId, ThreadStatus},
26};
27use rpc::proto::ViewId;
28use settings::Settings;
29use stack_frame_list::StackFrameList;
30use ui::{
31 ActiveTheme, AnyElement, App, Context, ContextMenu, DropdownMenu, FluentBuilder,
32 InteractiveElement, IntoElement, Label, LabelCommon as _, ParentElement, Render, SharedString,
33 StatefulInteractiveElement, Styled, Tab, Window, div, h_flex, v_flex,
34};
35use util::ResultExt;
36use variable_list::VariableList;
37use workspace::{
38 ActivePaneDecorator, DraggedTab, Item, Member, Pane, PaneGroup, Workspace,
39 item::TabContentParams, move_item, pane::Event,
40};
41
42pub struct RunningState {
43 session: Entity<Session>,
44 thread_id: Option<ThreadId>,
45 focus_handle: FocusHandle,
46 _remote_id: Option<ViewId>,
47 workspace: WeakEntity<Workspace>,
48 session_id: SessionId,
49 variable_list: Entity<variable_list::VariableList>,
50 _subscriptions: Vec<Subscription>,
51 stack_frame_list: Entity<stack_frame_list::StackFrameList>,
52 loaded_sources_list: Entity<LoadedSourceList>,
53 module_list: Entity<module_list::ModuleList>,
54 _console: Entity<Console>,
55 breakpoint_list: Entity<BreakpointList>,
56 panes: PaneGroup,
57 pane_close_subscriptions: HashMap<EntityId, Subscription>,
58 _schedule_serialize: Option<Task<()>>,
59}
60
61impl Render for RunningState {
62 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
63 let active = self.panes.panes().into_iter().next();
64 let x = if let Some(active) = active {
65 self.panes
66 .render(
67 None,
68 &ActivePaneDecorator::new(active, &self.workspace),
69 window,
70 cx,
71 )
72 .into_any_element()
73 } else {
74 div().into_any_element()
75 };
76 let thread_status = self
77 .thread_id
78 .map(|thread_id| self.session.read(cx).thread_status(thread_id))
79 .unwrap_or(ThreadStatus::Exited);
80
81 self.variable_list.update(cx, |this, cx| {
82 this.disabled(thread_status != ThreadStatus::Stopped, cx);
83 });
84 v_flex()
85 .size_full()
86 .key_context("DebugSessionItem")
87 .track_focus(&self.focus_handle(cx))
88 .child(h_flex().flex_1().child(x))
89 }
90}
91
92pub(crate) struct SubView {
93 inner: AnyView,
94 pane_focus_handle: FocusHandle,
95 kind: DebuggerPaneItem,
96 show_indicator: Box<dyn Fn(&App) -> bool>,
97}
98
99impl SubView {
100 pub(crate) fn new(
101 pane_focus_handle: FocusHandle,
102 view: AnyView,
103 kind: DebuggerPaneItem,
104 show_indicator: Option<Box<dyn Fn(&App) -> bool>>,
105 cx: &mut App,
106 ) -> Entity<Self> {
107 cx.new(|_| Self {
108 kind,
109 inner: view,
110 pane_focus_handle,
111 show_indicator: show_indicator.unwrap_or(Box::new(|_| false)),
112 })
113 }
114
115 pub(crate) fn view_kind(&self) -> DebuggerPaneItem {
116 self.kind
117 }
118}
119impl Focusable for SubView {
120 fn focus_handle(&self, _: &App) -> FocusHandle {
121 self.pane_focus_handle.clone()
122 }
123}
124impl EventEmitter<()> for SubView {}
125impl Item for SubView {
126 type Event = ();
127
128 /// This is used to serialize debugger pane layouts
129 /// A SharedString gets converted to a enum and back during serialization/deserialization.
130 fn tab_content_text(&self, _window: &Window, _cx: &App) -> Option<SharedString> {
131 Some(self.kind.to_shared_string())
132 }
133
134 fn tab_content(
135 &self,
136 params: workspace::item::TabContentParams,
137 _: &Window,
138 cx: &App,
139 ) -> AnyElement {
140 let label = Label::new(self.kind.to_shared_string())
141 .size(ui::LabelSize::Small)
142 .color(params.text_color())
143 .line_height_style(ui::LineHeightStyle::UiLabel);
144
145 if !params.selected && self.show_indicator.as_ref()(cx) {
146 return h_flex()
147 .justify_between()
148 .child(ui::Indicator::dot())
149 .gap_2()
150 .child(label)
151 .into_any_element();
152 }
153
154 label.into_any_element()
155 }
156}
157
158impl Render for SubView {
159 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
160 v_flex().size_full().child(self.inner.clone())
161 }
162}
163
164pub(crate) fn new_debugger_pane(
165 workspace: WeakEntity<Workspace>,
166 project: Entity<Project>,
167 window: &mut Window,
168 cx: &mut Context<RunningState>,
169) -> Entity<Pane> {
170 let weak_running = cx.weak_entity();
171 let custom_drop_handle = {
172 let workspace = workspace.clone();
173 let project = project.downgrade();
174 let weak_running = weak_running.clone();
175 move |pane: &mut Pane, any: &dyn Any, window: &mut Window, cx: &mut Context<Pane>| {
176 let Some(tab) = any.downcast_ref::<DraggedTab>() else {
177 return ControlFlow::Break(());
178 };
179 let Some(project) = project.upgrade() else {
180 return ControlFlow::Break(());
181 };
182 let this_pane = cx.entity().clone();
183 let item = if tab.pane == this_pane {
184 pane.item_for_index(tab.ix)
185 } else {
186 tab.pane.read(cx).item_for_index(tab.ix)
187 };
188 let Some(item) = item.filter(|item| item.downcast::<SubView>().is_some()) else {
189 return ControlFlow::Break(());
190 };
191
192 let source = tab.pane.clone();
193 let item_id_to_move = item.item_id();
194
195 let Ok(new_split_pane) = pane
196 .drag_split_direction()
197 .map(|split_direction| {
198 weak_running.update(cx, |running, cx| {
199 let new_pane =
200 new_debugger_pane(workspace.clone(), project.clone(), window, cx);
201 let _previous_subscription = running.pane_close_subscriptions.insert(
202 new_pane.entity_id(),
203 cx.subscribe_in(&new_pane, window, RunningState::handle_pane_event),
204 );
205 debug_assert!(_previous_subscription.is_none());
206 running
207 .panes
208 .split(&this_pane, &new_pane, split_direction)?;
209 anyhow::Ok(new_pane)
210 })
211 })
212 .transpose()
213 else {
214 return ControlFlow::Break(());
215 };
216
217 match new_split_pane.transpose() {
218 // Source pane may be the one currently updated, so defer the move.
219 Ok(Some(new_pane)) => cx
220 .spawn_in(window, async move |_, cx| {
221 cx.update(|window, cx| {
222 move_item(
223 &source,
224 &new_pane,
225 item_id_to_move,
226 new_pane.read(cx).active_item_index(),
227 window,
228 cx,
229 );
230 })
231 .ok();
232 })
233 .detach(),
234 // If we drop into existing pane or current pane,
235 // regular pane drop handler will take care of it,
236 // using the right tab index for the operation.
237 Ok(None) => return ControlFlow::Continue(()),
238 err @ Err(_) => {
239 err.log_err();
240 return ControlFlow::Break(());
241 }
242 };
243
244 ControlFlow::Break(())
245 }
246 };
247
248 let ret = cx.new(move |cx| {
249 let mut pane = Pane::new(
250 workspace.clone(),
251 project.clone(),
252 Default::default(),
253 None,
254 NoAction.boxed_clone(),
255 window,
256 cx,
257 );
258 pane.set_can_split(Some(Arc::new(move |pane, dragged_item, _window, cx| {
259 if let Some(tab) = dragged_item.downcast_ref::<DraggedTab>() {
260 let is_current_pane = tab.pane == cx.entity();
261 let Some(can_drag_away) = weak_running
262 .update(cx, |running_state, _| {
263 let current_panes = running_state.panes.panes();
264 !current_panes.contains(&&tab.pane)
265 || current_panes.len() > 1
266 || (!is_current_pane || pane.items_len() > 1)
267 })
268 .ok()
269 else {
270 return false;
271 };
272 if can_drag_away {
273 let item = if is_current_pane {
274 pane.item_for_index(tab.ix)
275 } else {
276 tab.pane.read(cx).item_for_index(tab.ix)
277 };
278 if let Some(item) = item {
279 return item.downcast::<SubView>().is_some();
280 }
281 }
282 }
283 false
284 })));
285 pane.display_nav_history_buttons(None);
286 pane.set_custom_drop_handle(cx, custom_drop_handle);
287 pane.set_should_display_tab_bar(|_, _| true);
288 pane.set_render_tab_bar_buttons(cx, |_, _, _| (None, None));
289 pane.set_render_tab_bar(cx, |pane, window, cx| {
290 let active_pane_item = pane.active_item();
291 h_flex()
292 .w_full()
293 .px_2()
294 .gap_1()
295 .h(Tab::container_height(cx))
296 .drag_over::<DraggedTab>(|bar, _, _, cx| {
297 bar.bg(cx.theme().colors().drop_target_background)
298 })
299 .on_drop(
300 cx.listener(move |this, dragged_tab: &DraggedTab, window, cx| {
301 this.drag_split_direction = None;
302 this.handle_tab_drop(dragged_tab, this.items_len(), window, cx)
303 }),
304 )
305 .bg(cx.theme().colors().tab_bar_background)
306 .border_b_1()
307 .border_color(cx.theme().colors().border)
308 .children(pane.items().enumerate().map(|(ix, item)| {
309 let selected = active_pane_item
310 .as_ref()
311 .map_or(false, |active| active.item_id() == item.item_id());
312 let item_ = item.boxed_clone();
313 div()
314 .id(SharedString::from(format!(
315 "debugger_tab_{}",
316 item.item_id().as_u64()
317 )))
318 .p_1()
319 .rounded_md()
320 .cursor_pointer()
321 .map(|this| {
322 if selected {
323 this.bg(cx.theme().colors().tab_active_background)
324 } else {
325 let hover_color = cx.theme().colors().element_hover;
326 this.hover(|style| style.bg(hover_color))
327 }
328 })
329 .on_click(cx.listener(move |this, _, window, cx| {
330 let index = this.index_for_item(&*item_);
331 if let Some(index) = index {
332 this.activate_item(index, true, true, window, cx);
333 }
334 }))
335 .child(item.tab_content(
336 TabContentParams {
337 selected,
338 ..Default::default()
339 },
340 window,
341 cx,
342 ))
343 .on_drop(
344 cx.listener(move |this, dragged_tab: &DraggedTab, window, cx| {
345 this.drag_split_direction = None;
346 this.handle_tab_drop(dragged_tab, ix, window, cx)
347 }),
348 )
349 .on_drag(
350 DraggedTab {
351 item: item.boxed_clone(),
352 pane: cx.entity().clone(),
353 detail: 0,
354 is_active: selected,
355 ix,
356 },
357 |tab, _, _, cx| cx.new(|_| tab.clone()),
358 )
359 }))
360 .into_any_element()
361 });
362 pane
363 });
364
365 ret
366}
367impl RunningState {
368 pub fn new(
369 session: Entity<Session>,
370 project: Entity<Project>,
371 workspace: WeakEntity<Workspace>,
372 serialized_pane_layout: Option<SerializedPaneLayout>,
373 window: &mut Window,
374 cx: &mut Context<Self>,
375 ) -> Self {
376 let focus_handle = cx.focus_handle();
377 let session_id = session.read(cx).session_id();
378 let weak_state = cx.weak_entity();
379 let stack_frame_list = cx.new(|cx| {
380 StackFrameList::new(workspace.clone(), session.clone(), weak_state, window, cx)
381 });
382
383 let variable_list =
384 cx.new(|cx| VariableList::new(session.clone(), stack_frame_list.clone(), window, cx));
385
386 let module_list = cx.new(|cx| ModuleList::new(session.clone(), workspace.clone(), cx));
387
388 let loaded_source_list = cx.new(|cx| LoadedSourceList::new(session.clone(), cx));
389
390 let console = cx.new(|cx| {
391 Console::new(
392 session.clone(),
393 stack_frame_list.clone(),
394 variable_list.clone(),
395 window,
396 cx,
397 )
398 });
399
400 let breakpoint_list = BreakpointList::new(session.clone(), workspace.clone(), &project, cx);
401
402 let _subscriptions = vec![
403 cx.observe(&module_list, |_, _, cx| cx.notify()),
404 cx.subscribe_in(&session, window, |this, _, event, window, cx| {
405 match event {
406 SessionEvent::Stopped(thread_id) => {
407 this.workspace
408 .update(cx, |workspace, cx| {
409 workspace.open_panel::<crate::DebugPanel>(window, cx);
410 })
411 .log_err();
412
413 if let Some(thread_id) = thread_id {
414 this.select_thread(*thread_id, window, cx);
415 }
416 }
417 SessionEvent::Threads => {
418 let threads = this.session.update(cx, |this, cx| this.threads(cx));
419 this.select_current_thread(&threads, window, cx);
420 }
421 SessionEvent::CapabilitiesLoaded => {
422 let capabilities = this.capabilities(cx);
423 if !capabilities.supports_modules_request.unwrap_or(false) {
424 this.remove_pane_item(DebuggerPaneItem::Modules, window, cx);
425 }
426 if !capabilities
427 .supports_loaded_sources_request
428 .unwrap_or(false)
429 {
430 this.remove_pane_item(DebuggerPaneItem::LoadedSources, window, cx);
431 }
432 }
433
434 _ => {}
435 }
436 cx.notify()
437 }),
438 cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
439 this.serialize_layout(window, cx);
440 }),
441 ];
442
443 let mut pane_close_subscriptions = HashMap::default();
444 let panes = if let Some(root) = serialized_pane_layout.and_then(|serialized_layout| {
445 persistence::deserialize_pane_layout(
446 serialized_layout,
447 &workspace,
448 &project,
449 &stack_frame_list,
450 &variable_list,
451 &module_list,
452 &console,
453 &breakpoint_list,
454 &loaded_source_list,
455 &mut pane_close_subscriptions,
456 window,
457 cx,
458 )
459 }) {
460 workspace::PaneGroup::with_root(root)
461 } else {
462 pane_close_subscriptions.clear();
463
464 let root = Self::default_pane_layout(
465 project,
466 &workspace,
467 &stack_frame_list,
468 &variable_list,
469 &module_list,
470 &loaded_source_list,
471 &console,
472 &breakpoint_list,
473 &mut pane_close_subscriptions,
474 window,
475 cx,
476 );
477
478 workspace::PaneGroup::with_root(root)
479 };
480
481 Self {
482 session,
483 workspace,
484 focus_handle,
485 variable_list,
486 _subscriptions,
487 thread_id: None,
488 _remote_id: None,
489 stack_frame_list,
490 session_id,
491 panes,
492 module_list,
493 _console: console,
494 breakpoint_list,
495 loaded_sources_list: loaded_source_list,
496 pane_close_subscriptions,
497 _schedule_serialize: None,
498 }
499 }
500
501 pub(crate) fn remove_pane_item(
502 &mut self,
503 item_kind: DebuggerPaneItem,
504 window: &mut Window,
505 cx: &mut Context<Self>,
506 ) {
507 if let Some((pane, item_id)) = self.panes.panes().iter().find_map(|pane| {
508 Some(pane).zip(
509 pane.read(cx)
510 .items()
511 .find(|item| {
512 item.act_as::<SubView>(cx)
513 .is_some_and(|view| view.read(cx).kind == item_kind)
514 })
515 .map(|item| item.item_id()),
516 )
517 }) {
518 pane.update(cx, |pane, cx| {
519 pane.remove_item(item_id, false, true, window, cx)
520 })
521 }
522 }
523
524 pub(crate) fn has_pane_at_position(&self, position: Point<Pixels>) -> bool {
525 self.panes.pane_at_pixel_position(position).is_some()
526 }
527
528 pub(crate) fn add_pane_item(
529 &mut self,
530 item_kind: DebuggerPaneItem,
531 position: Point<Pixels>,
532 window: &mut Window,
533 cx: &mut Context<Self>,
534 ) {
535 debug_assert!(
536 item_kind.is_supported(self.session.read(cx).capabilities()),
537 "We should only allow adding supported item kinds"
538 );
539
540 if let Some(pane) = self.panes.pane_at_pixel_position(position) {
541 let sub_view = match item_kind {
542 DebuggerPaneItem::Console => {
543 let weak_console = self._console.clone().downgrade();
544
545 Box::new(SubView::new(
546 pane.focus_handle(cx),
547 self._console.clone().into(),
548 item_kind,
549 Some(Box::new(move |cx| {
550 weak_console
551 .read_with(cx, |console, cx| console.show_indicator(cx))
552 .unwrap_or_default()
553 })),
554 cx,
555 ))
556 }
557 DebuggerPaneItem::Variables => Box::new(SubView::new(
558 self.variable_list.focus_handle(cx),
559 self.variable_list.clone().into(),
560 item_kind,
561 None,
562 cx,
563 )),
564 DebuggerPaneItem::BreakpointList => Box::new(SubView::new(
565 self.breakpoint_list.focus_handle(cx),
566 self.breakpoint_list.clone().into(),
567 item_kind,
568 None,
569 cx,
570 )),
571 DebuggerPaneItem::Frames => Box::new(SubView::new(
572 self.stack_frame_list.focus_handle(cx),
573 self.stack_frame_list.clone().into(),
574 item_kind,
575 None,
576 cx,
577 )),
578 DebuggerPaneItem::Modules => Box::new(SubView::new(
579 self.module_list.focus_handle(cx),
580 self.module_list.clone().into(),
581 item_kind,
582 None,
583 cx,
584 )),
585 DebuggerPaneItem::LoadedSources => Box::new(SubView::new(
586 self.loaded_sources_list.focus_handle(cx),
587 self.loaded_sources_list.clone().into(),
588 item_kind,
589 None,
590 cx,
591 )),
592 };
593
594 pane.update(cx, |pane, cx| {
595 pane.add_item(sub_view, false, false, None, window, cx);
596 })
597 }
598 }
599
600 pub(crate) fn pane_items_status(&self, cx: &App) -> IndexMap<DebuggerPaneItem, bool> {
601 let caps = self.session.read(cx).capabilities();
602 let mut pane_item_status = IndexMap::from_iter(
603 DebuggerPaneItem::all()
604 .iter()
605 .filter(|kind| kind.is_supported(&caps))
606 .map(|kind| (*kind, false)),
607 );
608 self.panes.panes().iter().for_each(|pane| {
609 pane.read(cx)
610 .items()
611 .filter_map(|item| item.act_as::<SubView>(cx))
612 .for_each(|view| {
613 pane_item_status.insert(view.read(cx).kind, true);
614 });
615 });
616
617 pane_item_status
618 }
619
620 pub(crate) fn serialize_layout(&mut self, window: &mut Window, cx: &mut Context<Self>) {
621 if self._schedule_serialize.is_none() {
622 self._schedule_serialize = Some(cx.spawn_in(window, async move |this, cx| {
623 cx.background_executor()
624 .timer(Duration::from_millis(100))
625 .await;
626
627 let Some((adapter_name, pane_group)) = this
628 .update(cx, |this, cx| {
629 let adapter_name = this.session.read(cx).adapter_name();
630 (
631 adapter_name,
632 persistence::build_serialized_pane_layout(&this.panes.root, cx),
633 )
634 })
635 .ok()
636 else {
637 return;
638 };
639
640 persistence::serialize_pane_layout(adapter_name, pane_group)
641 .await
642 .log_err();
643
644 this.update(cx, |this, _| {
645 this._schedule_serialize.take();
646 })
647 .ok();
648 }));
649 }
650 }
651
652 pub(crate) fn handle_pane_event(
653 this: &mut RunningState,
654 source_pane: &Entity<Pane>,
655 event: &Event,
656 window: &mut Window,
657 cx: &mut Context<RunningState>,
658 ) {
659 this.serialize_layout(window, cx);
660 if let Event::Remove { .. } = event {
661 let _did_find_pane = this.panes.remove(&source_pane).is_ok();
662 debug_assert!(_did_find_pane);
663 cx.notify();
664 }
665 }
666
667 pub(crate) fn go_to_selected_stack_frame(&self, window: &Window, cx: &mut Context<Self>) {
668 if self.thread_id.is_some() {
669 self.stack_frame_list
670 .update(cx, |list, cx| list.go_to_selected_stack_frame(window, cx));
671 }
672 }
673
674 pub(crate) fn has_open_context_menu(&self, cx: &App) -> bool {
675 self.variable_list.read(cx).has_open_context_menu()
676 }
677
678 pub fn session(&self) -> &Entity<Session> {
679 &self.session
680 }
681
682 pub fn session_id(&self) -> SessionId {
683 self.session_id
684 }
685
686 pub(crate) fn selected_stack_frame_id(&self, cx: &App) -> Option<dap::StackFrameId> {
687 self.stack_frame_list.read(cx).selected_stack_frame_id()
688 }
689
690 #[cfg(test)]
691 pub fn stack_frame_list(&self) -> &Entity<StackFrameList> {
692 &self.stack_frame_list
693 }
694
695 #[cfg(test)]
696 pub fn console(&self) -> &Entity<Console> {
697 &self._console
698 }
699
700 #[cfg(test)]
701 pub(crate) fn module_list(&self) -> &Entity<ModuleList> {
702 &self.module_list
703 }
704
705 #[cfg(test)]
706 pub(crate) fn activate_modules_list(&self, window: &mut Window, cx: &mut App) {
707 let (variable_list_position, pane) = self
708 .panes
709 .panes()
710 .into_iter()
711 .find_map(|pane| {
712 pane.read(cx)
713 .items_of_type::<SubView>()
714 .position(|view| view.read(cx).view_kind().to_shared_string() == *"Modules")
715 .map(|view| (view, pane))
716 })
717 .unwrap();
718 pane.update(cx, |this, cx| {
719 this.activate_item(variable_list_position, true, true, window, cx);
720 })
721 }
722 #[cfg(test)]
723 pub(crate) fn variable_list(&self) -> &Entity<VariableList> {
724 &self.variable_list
725 }
726
727 pub fn capabilities(&self, cx: &App) -> Capabilities {
728 self.session().read(cx).capabilities().clone()
729 }
730
731 pub fn select_current_thread(
732 &mut self,
733 threads: &Vec<(Thread, ThreadStatus)>,
734 window: &mut Window,
735 cx: &mut Context<Self>,
736 ) {
737 let selected_thread = self
738 .thread_id
739 .and_then(|thread_id| threads.iter().find(|(thread, _)| thread.id == thread_id.0))
740 .or_else(|| threads.first());
741
742 let Some((selected_thread, _)) = selected_thread else {
743 return;
744 };
745
746 if Some(ThreadId(selected_thread.id)) != self.thread_id {
747 self.select_thread(ThreadId(selected_thread.id), window, cx);
748 }
749 }
750
751 pub(crate) fn selected_thread_id(&self) -> Option<ThreadId> {
752 self.thread_id
753 }
754
755 pub fn thread_status(&self, cx: &App) -> Option<ThreadStatus> {
756 self.thread_id
757 .map(|id| self.session().read(cx).thread_status(id))
758 }
759
760 fn select_thread(&mut self, thread_id: ThreadId, window: &mut Window, cx: &mut Context<Self>) {
761 if self.thread_id.is_some_and(|id| id == thread_id) {
762 return;
763 }
764
765 self.thread_id = Some(thread_id);
766
767 self.stack_frame_list
768 .update(cx, |list, cx| list.schedule_refresh(true, window, cx));
769 }
770
771 pub fn continue_thread(&mut self, cx: &mut Context<Self>) {
772 let Some(thread_id) = self.thread_id else {
773 return;
774 };
775
776 self.session().update(cx, |state, cx| {
777 state.continue_thread(thread_id, cx);
778 });
779 }
780
781 pub fn step_over(&mut self, cx: &mut Context<Self>) {
782 let Some(thread_id) = self.thread_id else {
783 return;
784 };
785
786 let granularity = DebuggerSettings::get_global(cx).stepping_granularity;
787
788 self.session().update(cx, |state, cx| {
789 state.step_over(thread_id, granularity, cx);
790 });
791 }
792
793 pub(crate) fn step_in(&mut self, cx: &mut Context<Self>) {
794 let Some(thread_id) = self.thread_id else {
795 return;
796 };
797
798 let granularity = DebuggerSettings::get_global(cx).stepping_granularity;
799
800 self.session().update(cx, |state, cx| {
801 state.step_in(thread_id, granularity, cx);
802 });
803 }
804
805 pub(crate) fn step_out(&mut self, cx: &mut Context<Self>) {
806 let Some(thread_id) = self.thread_id else {
807 return;
808 };
809
810 let granularity = DebuggerSettings::get_global(cx).stepping_granularity;
811
812 self.session().update(cx, |state, cx| {
813 state.step_out(thread_id, granularity, cx);
814 });
815 }
816
817 pub(crate) fn step_back(&mut self, cx: &mut Context<Self>) {
818 let Some(thread_id) = self.thread_id else {
819 return;
820 };
821
822 let granularity = DebuggerSettings::get_global(cx).stepping_granularity;
823
824 self.session().update(cx, |state, cx| {
825 state.step_back(thread_id, granularity, cx);
826 });
827 }
828
829 pub fn restart_session(&self, cx: &mut Context<Self>) {
830 self.session().update(cx, |state, cx| {
831 state.restart(None, cx);
832 });
833 }
834
835 pub fn pause_thread(&self, cx: &mut Context<Self>) {
836 let Some(thread_id) = self.thread_id else {
837 return;
838 };
839
840 self.session().update(cx, |state, cx| {
841 state.pause_thread(thread_id, cx);
842 });
843 }
844
845 pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
846 self.workspace
847 .update(cx, |workspace, cx| {
848 workspace
849 .project()
850 .read(cx)
851 .breakpoint_store()
852 .update(cx, |store, cx| {
853 store.remove_active_position(Some(self.session_id), cx)
854 })
855 })
856 .log_err();
857
858 self.session.update(cx, |session, cx| {
859 session.shutdown(cx).detach();
860 })
861 }
862
863 pub fn stop_thread(&self, cx: &mut Context<Self>) {
864 let Some(thread_id) = self.thread_id else {
865 return;
866 };
867
868 self.workspace
869 .update(cx, |workspace, cx| {
870 workspace
871 .project()
872 .read(cx)
873 .breakpoint_store()
874 .update(cx, |store, cx| {
875 store.remove_active_position(Some(self.session_id), cx)
876 })
877 })
878 .log_err();
879
880 self.session().update(cx, |state, cx| {
881 state.terminate_threads(Some(vec![thread_id; 1]), cx);
882 });
883 }
884
885 #[expect(
886 unused,
887 reason = "Support for disconnecting a client is not wired through yet"
888 )]
889 pub fn disconnect_client(&self, cx: &mut Context<Self>) {
890 self.session().update(cx, |state, cx| {
891 state.disconnect_client(cx);
892 });
893 }
894
895 pub fn toggle_ignore_breakpoints(&mut self, cx: &mut Context<Self>) {
896 self.session.update(cx, |session, cx| {
897 session.toggle_ignore_breakpoints(cx).detach();
898 });
899 }
900
901 pub(crate) fn thread_dropdown(
902 &self,
903 window: &mut Window,
904 cx: &mut Context<'_, RunningState>,
905 ) -> DropdownMenu {
906 let state = cx.entity();
907 let threads = self.session.update(cx, |this, cx| this.threads(cx));
908 let selected_thread_name = threads
909 .iter()
910 .find(|(thread, _)| self.thread_id.map(|id| id.0) == Some(thread.id))
911 .map(|(thread, _)| thread.name.clone())
912 .unwrap_or("Threads".to_owned());
913 DropdownMenu::new(
914 ("thread-list", self.session_id.0),
915 selected_thread_name,
916 ContextMenu::build_eager(window, cx, move |mut this, _, _| {
917 for (thread, _) in threads {
918 let state = state.clone();
919 let thread_id = thread.id;
920 this = this.entry(thread.name, None, move |window, cx| {
921 state.update(cx, |state, cx| {
922 state.select_thread(ThreadId(thread_id), window, cx);
923 });
924 });
925 }
926 this
927 }),
928 )
929 }
930
931 fn default_pane_layout(
932 project: Entity<Project>,
933 workspace: &WeakEntity<Workspace>,
934 stack_frame_list: &Entity<StackFrameList>,
935 variable_list: &Entity<VariableList>,
936 module_list: &Entity<ModuleList>,
937 loaded_source_list: &Entity<LoadedSourceList>,
938 console: &Entity<Console>,
939 breakpoints: &Entity<BreakpointList>,
940 subscriptions: &mut HashMap<EntityId, Subscription>,
941 window: &mut Window,
942 cx: &mut Context<'_, RunningState>,
943 ) -> Member {
944 let leftmost_pane = new_debugger_pane(workspace.clone(), project.clone(), window, cx);
945 leftmost_pane.update(cx, |this, cx| {
946 this.add_item(
947 Box::new(SubView::new(
948 this.focus_handle(cx),
949 stack_frame_list.clone().into(),
950 DebuggerPaneItem::Frames,
951 None,
952 cx,
953 )),
954 true,
955 false,
956 None,
957 window,
958 cx,
959 );
960 this.add_item(
961 Box::new(SubView::new(
962 breakpoints.focus_handle(cx),
963 breakpoints.clone().into(),
964 DebuggerPaneItem::BreakpointList,
965 None,
966 cx,
967 )),
968 true,
969 false,
970 None,
971 window,
972 cx,
973 );
974 this.activate_item(0, false, false, window, cx);
975 });
976 let center_pane = new_debugger_pane(workspace.clone(), project.clone(), window, cx);
977
978 center_pane.update(cx, |this, cx| {
979 this.add_item(
980 Box::new(SubView::new(
981 variable_list.focus_handle(cx),
982 variable_list.clone().into(),
983 DebuggerPaneItem::Variables,
984 None,
985 cx,
986 )),
987 true,
988 false,
989 None,
990 window,
991 cx,
992 );
993 this.add_item(
994 Box::new(SubView::new(
995 module_list.focus_handle(cx),
996 module_list.clone().into(),
997 DebuggerPaneItem::Modules,
998 None,
999 cx,
1000 )),
1001 false,
1002 false,
1003 None,
1004 window,
1005 cx,
1006 );
1007
1008 this.add_item(
1009 Box::new(SubView::new(
1010 loaded_source_list.focus_handle(cx),
1011 loaded_source_list.clone().into(),
1012 DebuggerPaneItem::LoadedSources,
1013 None,
1014 cx,
1015 )),
1016 false,
1017 false,
1018 None,
1019 window,
1020 cx,
1021 );
1022 this.activate_item(0, false, false, window, cx);
1023 });
1024
1025 let rightmost_pane = new_debugger_pane(workspace.clone(), project.clone(), window, cx);
1026 rightmost_pane.update(cx, |this, cx| {
1027 let weak_console = console.downgrade();
1028 this.add_item(
1029 Box::new(SubView::new(
1030 this.focus_handle(cx),
1031 console.clone().into(),
1032 DebuggerPaneItem::Console,
1033 Some(Box::new(move |cx| {
1034 weak_console
1035 .read_with(cx, |console, cx| console.show_indicator(cx))
1036 .unwrap_or_default()
1037 })),
1038 cx,
1039 )),
1040 true,
1041 false,
1042 None,
1043 window,
1044 cx,
1045 );
1046 });
1047
1048 subscriptions.extend(
1049 [&leftmost_pane, ¢er_pane, &rightmost_pane]
1050 .into_iter()
1051 .map(|entity| {
1052 (
1053 entity.entity_id(),
1054 cx.subscribe_in(entity, window, Self::handle_pane_event),
1055 )
1056 }),
1057 );
1058
1059 let group_root = workspace::PaneAxis::new(
1060 gpui::Axis::Horizontal,
1061 [leftmost_pane, center_pane, rightmost_pane]
1062 .into_iter()
1063 .map(workspace::Member::Pane)
1064 .collect(),
1065 );
1066
1067 Member::Axis(group_root)
1068 }
1069}
1070
1071impl EventEmitter<DebugPanelItemEvent> for RunningState {}
1072
1073impl Focusable for RunningState {
1074 fn focus_handle(&self, _: &App) -> FocusHandle {
1075 self.focus_handle.clone()
1076 }
1077}