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, prelude::*};
15use util::truncate_and_trailoff;
16use workspace::{
17 CollaboratorId, FollowableItem, ViewId, Workspace,
18 item::{self, Item},
19};
20
21pub struct DebugSession {
22 remote_id: Option<workspace::ViewId>,
23 running_state: Entity<RunningState>,
24 label: OnceLock<SharedString>,
25 stack_trace_view: OnceCell<Entity<StackTraceView>>,
26 _worktree_store: WeakEntity<WorktreeStore>,
27 workspace: WeakEntity<Workspace>,
28 _subscriptions: [Subscription; 1],
29}
30
31#[derive(Debug)]
32pub enum DebugPanelItemEvent {
33 Close,
34 Stopped { go_to_stack_frame: bool },
35}
36
37impl DebugSession {
38 pub(crate) fn running(
39 project: Entity<Project>,
40 workspace: WeakEntity<Workspace>,
41 parent_terminal: Option<Entity<DebugTerminal>>,
42 session: Entity<Session>,
43 serialized_layout: Option<SerializedLayout>,
44 dock_axis: Axis,
45 window: &mut Window,
46 cx: &mut App,
47 ) -> Entity<Self> {
48 let running_state = cx.new(|cx| {
49 RunningState::new(
50 session.clone(),
51 project.clone(),
52 workspace.clone(),
53 parent_terminal,
54 serialized_layout,
55 dock_axis,
56 window,
57 cx,
58 )
59 });
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 label: OnceLock::new(),
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 let stack_frame_view = cx.new(|cx| {
91 StackTraceView::new(
92 workspace.clone(),
93 project.clone(),
94 stackframe_list,
95 window,
96 cx,
97 )
98 });
99
100 stack_frame_view
101 })
102 }
103
104 pub fn session(&self, cx: &App) -> Entity<Session> {
105 self.running_state.read(cx).session().clone()
106 }
107
108 pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
109 self.running_state
110 .update(cx, |state, cx| state.shutdown(cx));
111 }
112
113 pub(crate) fn label(&self, cx: &App) -> SharedString {
114 if let Some(label) = self.label.get() {
115 return label.clone();
116 }
117
118 let session = self.running_state.read(cx).session();
119
120 self.label
121 .get_or_init(|| session.read(cx).label())
122 .to_owned()
123 }
124
125 pub(crate) fn running_state(&self) -> &Entity<RunningState> {
126 &self.running_state
127 }
128
129 pub(crate) fn label_element(&self, depth: usize, cx: &App) -> AnyElement {
130 const MAX_LABEL_CHARS: usize = 150;
131
132 let label = self.label(cx);
133 let label = truncate_and_trailoff(&label, MAX_LABEL_CHARS);
134
135 let is_terminated = self
136 .running_state
137 .read(cx)
138 .session()
139 .read(cx)
140 .is_terminated();
141 let icon = {
142 if is_terminated {
143 Some(Indicator::dot().color(Color::Error))
144 } else {
145 match self
146 .running_state
147 .read(cx)
148 .thread_status(cx)
149 .unwrap_or_default()
150 {
151 project::debugger::session::ThreadStatus::Stopped => {
152 Some(Indicator::dot().color(Color::Conflict))
153 }
154 _ => Some(Indicator::dot().color(Color::Success)),
155 }
156 }
157 };
158
159 h_flex()
160 .id("session-label")
161 .ml(depth * px(16.0))
162 .gap_2()
163 .when_some(icon, |this, indicator| this.child(indicator))
164 .justify_between()
165 .child(
166 Label::new(label)
167 .size(LabelSize::Small)
168 .when(is_terminated, |this| this.strikethrough()),
169 )
170 .into_any_element()
171 }
172}
173
174impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
175
176impl Focusable for DebugSession {
177 fn focus_handle(&self, cx: &App) -> FocusHandle {
178 self.running_state.focus_handle(cx)
179 }
180}
181
182impl Item for DebugSession {
183 type Event = DebugPanelItemEvent;
184 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
185 "Debugger".into()
186 }
187}
188
189impl FollowableItem for DebugSession {
190 fn remote_id(&self) -> Option<workspace::ViewId> {
191 self.remote_id
192 }
193
194 fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
195 None
196 }
197
198 fn from_state_proto(
199 _workspace: Entity<Workspace>,
200 _remote_id: ViewId,
201 _state: &mut Option<proto::view::Variant>,
202 _window: &mut Window,
203 _cx: &mut App,
204 ) -> Option<gpui::Task<anyhow::Result<Entity<Self>>>> {
205 None
206 }
207
208 fn add_event_to_update_proto(
209 &self,
210 _event: &Self::Event,
211 _update: &mut Option<proto::update_view::Variant>,
212 _window: &Window,
213 _cx: &App,
214 ) -> bool {
215 // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
216
217 true
218 }
219
220 fn apply_update_proto(
221 &mut self,
222 _project: &Entity<project::Project>,
223 _message: proto::update_view::Variant,
224 _window: &mut Window,
225 _cx: &mut Context<Self>,
226 ) -> gpui::Task<anyhow::Result<()>> {
227 Task::ready(Ok(()))
228 }
229
230 fn set_leader_id(
231 &mut self,
232 _leader_id: Option<CollaboratorId>,
233 _window: &mut Window,
234 _cx: &mut Context<Self>,
235 ) {
236 }
237
238 fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
239 None
240 }
241
242 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
243 if existing.session_id(cx) == self.session_id(cx) {
244 Some(item::Dedup::KeepExisting)
245 } else {
246 None
247 }
248 }
249
250 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
251 true
252 }
253}
254
255impl Render for DebugSession {
256 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
257 self.running_state
258 .update(cx, |this, cx| this.render(window, cx).into_any_element())
259 }
260}