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 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 .tooltip(Tooltip::text(format!("Session {}", self.session_id(cx).0,)))
162 .ml(depth * px(16.0))
163 .gap_2()
164 .when_some(icon, |this, indicator| this.child(indicator))
165 .justify_between()
166 .child(
167 Label::new(label)
168 .size(LabelSize::Small)
169 .when(is_terminated, |this| this.strikethrough()),
170 )
171 .into_any_element()
172 }
173}
174
175impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
176
177impl Focusable for DebugSession {
178 fn focus_handle(&self, cx: &App) -> FocusHandle {
179 self.running_state.focus_handle(cx)
180 }
181}
182
183impl Item for DebugSession {
184 type Event = DebugPanelItemEvent;
185 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
186 "Debugger".into()
187 }
188}
189
190impl FollowableItem for DebugSession {
191 fn remote_id(&self) -> Option<workspace::ViewId> {
192 self.remote_id
193 }
194
195 fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
196 None
197 }
198
199 fn from_state_proto(
200 _workspace: Entity<Workspace>,
201 _remote_id: ViewId,
202 _state: &mut Option<proto::view::Variant>,
203 _window: &mut Window,
204 _cx: &mut App,
205 ) -> Option<gpui::Task<anyhow::Result<Entity<Self>>>> {
206 None
207 }
208
209 fn add_event_to_update_proto(
210 &self,
211 _event: &Self::Event,
212 _update: &mut Option<proto::update_view::Variant>,
213 _window: &Window,
214 _cx: &App,
215 ) -> bool {
216 // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
217
218 true
219 }
220
221 fn apply_update_proto(
222 &mut self,
223 _project: &Entity<project::Project>,
224 _message: proto::update_view::Variant,
225 _window: &mut Window,
226 _cx: &mut Context<Self>,
227 ) -> gpui::Task<anyhow::Result<()>> {
228 Task::ready(Ok(()))
229 }
230
231 fn set_leader_id(
232 &mut self,
233 _leader_id: Option<CollaboratorId>,
234 _window: &mut Window,
235 _cx: &mut Context<Self>,
236 ) {
237 }
238
239 fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
240 None
241 }
242
243 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
244 if existing.session_id(cx) == self.session_id(cx) {
245 Some(item::Dedup::KeepExisting)
246 } else {
247 None
248 }
249 }
250
251 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
252 true
253 }
254}
255
256impl Render for DebugSession {
257 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
258 self.running_state
259 .update(cx, |this, cx| this.render(window, cx).into_any_element())
260 }
261}