1pub mod running;
2
3use crate::{StackTraceView, persistence::SerializedLayout, session::running::DebugTerminal};
4use dap::client::SessionId;
5use gpui::{
6 App, Axis, Entity, EventEmitter, FocusHandle, Focusable, Subscription, Task, WeakEntity,
7};
8use project::debugger::session::Session;
9use project::worktree_store::WorktreeStore;
10use project::{Project, debugger::session::SessionQuirks};
11use rpc::proto;
12use running::RunningState;
13use std::cell::OnceCell;
14use ui::prelude::*;
15use workspace::{
16 CollaboratorId, FollowableItem, ViewId, Workspace,
17 item::{self, Item},
18};
19
20pub struct DebugSession {
21 remote_id: Option<workspace::ViewId>,
22 pub(crate) running_state: Entity<RunningState>,
23 pub(crate) quirks: SessionQuirks,
24 stack_trace_view: OnceCell<Entity<StackTraceView>>,
25 _worktree_store: WeakEntity<WorktreeStore>,
26 workspace: WeakEntity<Workspace>,
27 _subscriptions: [Subscription; 1],
28}
29
30#[derive(Debug)]
31pub enum DebugPanelItemEvent {
32 Close,
33 Stopped { go_to_stack_frame: bool },
34}
35
36impl DebugSession {
37 pub(crate) fn running(
38 project: Entity<Project>,
39 workspace: WeakEntity<Workspace>,
40 parent_terminal: Option<Entity<DebugTerminal>>,
41 session: Entity<Session>,
42 serialized_layout: Option<SerializedLayout>,
43 dock_axis: Axis,
44 window: &mut Window,
45 cx: &mut App,
46 ) -> Entity<Self> {
47 let running_state = cx.new(|cx| {
48 RunningState::new(
49 session.clone(),
50 project.clone(),
51 workspace.clone(),
52 parent_terminal,
53 serialized_layout,
54 dock_axis,
55 window,
56 cx,
57 )
58 });
59 let quirks = session.read(cx).quirks();
60
61 cx.new(|cx| Self {
62 _subscriptions: [cx.subscribe(&running_state, |_, _, _, cx| {
63 cx.notify();
64 })],
65 remote_id: None,
66 running_state,
67 quirks,
68 stack_trace_view: OnceCell::new(),
69 _worktree_store: project.read(cx).worktree_store().downgrade(),
70 workspace,
71 })
72 }
73
74 pub(crate) fn session_id(&self, cx: &App) -> SessionId {
75 self.running_state.read(cx).session_id()
76 }
77
78 pub(crate) fn stack_trace_view(
79 &mut self,
80 project: &Entity<Project>,
81 window: &mut Window,
82 cx: &mut Context<Self>,
83 ) -> &Entity<StackTraceView> {
84 let workspace = self.workspace.clone();
85 let running_state = self.running_state.clone();
86
87 self.stack_trace_view.get_or_init(|| {
88 let stackframe_list = running_state.read(cx).stack_frame_list().clone();
89
90 cx.new(|cx| {
91 StackTraceView::new(
92 workspace.clone(),
93 project.clone(),
94 stackframe_list,
95 window,
96 cx,
97 )
98 })
99 })
100 }
101
102 pub fn session(&self, cx: &App) -> Entity<Session> {
103 self.running_state.read(cx).session().clone()
104 }
105
106 pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
107 self.running_state
108 .update(cx, |state, cx| state.shutdown(cx));
109 }
110
111 pub(crate) fn label(&self, cx: &mut App) -> Option<SharedString> {
112 let session = self.running_state.read(cx).session().clone();
113 session.update(cx, |session, cx| {
114 let session_label = session.label();
115 let quirks = session.quirks();
116 let mut single_thread_name = || {
117 let threads = session.threads(cx);
118 match threads.as_slice() {
119 [(thread, _)] => Some(SharedString::from(&thread.name)),
120 _ => None,
121 }
122 };
123 if quirks.prefer_thread_name {
124 single_thread_name().or(session_label)
125 } else {
126 session_label.or_else(single_thread_name)
127 }
128 })
129 }
130
131 pub fn running_state(&self) -> &Entity<RunningState> {
132 &self.running_state
133 }
134}
135
136impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
137
138impl Focusable for DebugSession {
139 fn focus_handle(&self, cx: &App) -> FocusHandle {
140 self.running_state.focus_handle(cx)
141 }
142}
143
144impl Item for DebugSession {
145 type Event = DebugPanelItemEvent;
146 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
147 "Debugger".into()
148 }
149}
150
151impl FollowableItem for DebugSession {
152 fn remote_id(&self) -> Option<workspace::ViewId> {
153 self.remote_id
154 }
155
156 fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
157 None
158 }
159
160 fn from_state_proto(
161 _workspace: Entity<Workspace>,
162 _remote_id: ViewId,
163 _state: &mut Option<proto::view::Variant>,
164 _window: &mut Window,
165 _cx: &mut App,
166 ) -> Option<gpui::Task<anyhow::Result<Entity<Self>>>> {
167 None
168 }
169
170 fn add_event_to_update_proto(
171 &self,
172 _event: &Self::Event,
173 _update: &mut Option<proto::update_view::Variant>,
174 _window: &Window,
175 _cx: &App,
176 ) -> bool {
177 // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
178
179 true
180 }
181
182 fn apply_update_proto(
183 &mut self,
184 _project: &Entity<project::Project>,
185 _message: proto::update_view::Variant,
186 _window: &mut Window,
187 _cx: &mut Context<Self>,
188 ) -> gpui::Task<anyhow::Result<()>> {
189 Task::ready(Ok(()))
190 }
191
192 fn set_leader_id(
193 &mut self,
194 _leader_id: Option<CollaboratorId>,
195 _window: &mut Window,
196 _cx: &mut Context<Self>,
197 ) {
198 }
199
200 fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
201 None
202 }
203
204 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
205 if existing.session_id(cx) == self.session_id(cx) {
206 Some(item::Dedup::KeepExisting)
207 } else {
208 None
209 }
210 }
211
212 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
213 true
214 }
215}
216
217impl Render for DebugSession {
218 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
219 self.running_state
220 .update(cx, |this, cx| this.render(window, cx).into_any_element())
221 }
222}