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::Project;
9use project::debugger::session::Session;
10use project::worktree_store::WorktreeStore;
11use rpc::proto;
12use running::RunningState;
13use std::{cell::OnceCell, sync::OnceLock};
14use ui::{Indicator, Tooltip, prelude::*};
15use workspace::{
16 CollaboratorId, FollowableItem, ViewId, Workspace,
17 item::{self, Item},
18};
19
20pub struct DebugSession {
21 remote_id: Option<workspace::ViewId>,
22 running_state: Entity<RunningState>,
23 label: OnceLock<SharedString>,
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
60 cx.new(|cx| Self {
61 _subscriptions: [cx.subscribe(&running_state, |_, _, _, cx| {
62 cx.notify();
63 })],
64 remote_id: None,
65 running_state,
66 label: OnceLock::new(),
67 stack_trace_view: OnceCell::new(),
68 _worktree_store: project.read(cx).worktree_store().downgrade(),
69 workspace,
70 })
71 }
72
73 pub(crate) fn session_id(&self, cx: &App) -> SessionId {
74 self.running_state.read(cx).session_id()
75 }
76
77 pub(crate) fn stack_trace_view(
78 &mut self,
79 project: &Entity<Project>,
80 window: &mut Window,
81 cx: &mut Context<Self>,
82 ) -> &Entity<StackTraceView> {
83 let workspace = self.workspace.clone();
84 let running_state = self.running_state.clone();
85
86 self.stack_trace_view.get_or_init(|| {
87 let stackframe_list = running_state.read(cx).stack_frame_list().clone();
88
89 let stack_frame_view = cx.new(|cx| {
90 StackTraceView::new(
91 workspace.clone(),
92 project.clone(),
93 stackframe_list,
94 window,
95 cx,
96 )
97 });
98
99 stack_frame_view
100 })
101 }
102
103 pub fn session(&self, cx: &App) -> Entity<Session> {
104 self.running_state.read(cx).session().clone()
105 }
106
107 pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
108 self.running_state
109 .update(cx, |state, cx| state.shutdown(cx));
110 }
111
112 pub(crate) fn label(&self, cx: &App) -> SharedString {
113 if let Some(label) = self.label.get() {
114 return label.clone();
115 }
116
117 let session = self.running_state.read(cx).session();
118
119 self.label
120 .get_or_init(|| session.read(cx).label())
121 .to_owned()
122 }
123
124 pub(crate) fn running_state(&self) -> &Entity<RunningState> {
125 &self.running_state
126 }
127
128 pub(crate) fn label_element(&self, depth: usize, cx: &App) -> AnyElement {
129 let label = self.label(cx);
130
131 let is_terminated = self
132 .running_state
133 .read(cx)
134 .session()
135 .read(cx)
136 .is_terminated();
137 let icon = {
138 if is_terminated {
139 Some(Indicator::dot().color(Color::Error))
140 } else {
141 match self
142 .running_state
143 .read(cx)
144 .thread_status(cx)
145 .unwrap_or_default()
146 {
147 project::debugger::session::ThreadStatus::Stopped => {
148 Some(Indicator::dot().color(Color::Conflict))
149 }
150 _ => Some(Indicator::dot().color(Color::Success)),
151 }
152 }
153 };
154
155 h_flex()
156 .id("session-label")
157 .tooltip(Tooltip::text(format!("Session {}", self.session_id(cx).0,)))
158 .ml(depth * px(16.0))
159 .gap_2()
160 .when_some(icon, |this, indicator| this.child(indicator))
161 .justify_between()
162 .child(
163 Label::new(label)
164 .size(LabelSize::Small)
165 .when(is_terminated, |this| this.strikethrough()),
166 )
167 .into_any_element()
168 }
169}
170
171impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
172
173impl Focusable for DebugSession {
174 fn focus_handle(&self, cx: &App) -> FocusHandle {
175 self.running_state.focus_handle(cx)
176 }
177}
178
179impl Item for DebugSession {
180 type Event = DebugPanelItemEvent;
181 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
182 "Debugger".into()
183 }
184}
185
186impl FollowableItem for DebugSession {
187 fn remote_id(&self) -> Option<workspace::ViewId> {
188 self.remote_id
189 }
190
191 fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
192 None
193 }
194
195 fn from_state_proto(
196 _workspace: Entity<Workspace>,
197 _remote_id: ViewId,
198 _state: &mut Option<proto::view::Variant>,
199 _window: &mut Window,
200 _cx: &mut App,
201 ) -> Option<gpui::Task<anyhow::Result<Entity<Self>>>> {
202 None
203 }
204
205 fn add_event_to_update_proto(
206 &self,
207 _event: &Self::Event,
208 _update: &mut Option<proto::update_view::Variant>,
209 _window: &Window,
210 _cx: &App,
211 ) -> bool {
212 // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
213
214 true
215 }
216
217 fn apply_update_proto(
218 &mut self,
219 _project: &Entity<project::Project>,
220 _message: proto::update_view::Variant,
221 _window: &mut Window,
222 _cx: &mut Context<Self>,
223 ) -> gpui::Task<anyhow::Result<()>> {
224 Task::ready(Ok(()))
225 }
226
227 fn set_leader_id(
228 &mut self,
229 _leader_id: Option<CollaboratorId>,
230 _window: &mut Window,
231 _cx: &mut Context<Self>,
232 ) {
233 }
234
235 fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
236 None
237 }
238
239 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
240 if existing.session_id(cx) == self.session_id(cx) {
241 Some(item::Dedup::KeepExisting)
242 } else {
243 None
244 }
245 }
246
247 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
248 true
249 }
250}
251
252impl Render for DebugSession {
253 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
254 self.running_state
255 .update(cx, |this, cx| this.render(window, cx).into_any_element())
256 }
257}