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 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: &mut App) -> Option<SharedString> {
114 let session = self.running_state.read(cx).session().clone();
115 session.update(cx, |session, cx| {
116 let session_label = session.label();
117 let quirks = session.quirks();
118 let mut single_thread_name = || {
119 let threads = session.threads(cx);
120 match threads.as_slice() {
121 [(thread, _)] => Some(SharedString::from(&thread.name)),
122 _ => None,
123 }
124 };
125 if quirks.prefer_thread_name {
126 single_thread_name().or(session_label)
127 } else {
128 session_label.or_else(single_thread_name)
129 }
130 })
131 }
132
133 pub fn running_state(&self) -> &Entity<RunningState> {
134 &self.running_state
135 }
136}
137
138impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
139
140impl Focusable for DebugSession {
141 fn focus_handle(&self, cx: &App) -> FocusHandle {
142 self.running_state.focus_handle(cx)
143 }
144}
145
146impl Item for DebugSession {
147 type Event = DebugPanelItemEvent;
148 fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
149 "Debugger".into()
150 }
151}
152
153impl FollowableItem for DebugSession {
154 fn remote_id(&self) -> Option<workspace::ViewId> {
155 self.remote_id
156 }
157
158 fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
159 None
160 }
161
162 fn from_state_proto(
163 _workspace: Entity<Workspace>,
164 _remote_id: ViewId,
165 _state: &mut Option<proto::view::Variant>,
166 _window: &mut Window,
167 _cx: &mut App,
168 ) -> Option<gpui::Task<anyhow::Result<Entity<Self>>>> {
169 None
170 }
171
172 fn add_event_to_update_proto(
173 &self,
174 _event: &Self::Event,
175 _update: &mut Option<proto::update_view::Variant>,
176 _window: &Window,
177 _cx: &App,
178 ) -> bool {
179 // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
180
181 true
182 }
183
184 fn apply_update_proto(
185 &mut self,
186 _project: &Entity<project::Project>,
187 _message: proto::update_view::Variant,
188 _window: &mut Window,
189 _cx: &mut Context<Self>,
190 ) -> gpui::Task<anyhow::Result<()>> {
191 Task::ready(Ok(()))
192 }
193
194 fn set_leader_id(
195 &mut self,
196 _leader_id: Option<CollaboratorId>,
197 _window: &mut Window,
198 _cx: &mut Context<Self>,
199 ) {
200 }
201
202 fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
203 None
204 }
205
206 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
207 if existing.session_id(cx) == self.session_id(cx) {
208 Some(item::Dedup::KeepExisting)
209 } else {
210 None
211 }
212 }
213
214 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
215 true
216 }
217}
218
219impl Render for DebugSession {
220 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
221 self.running_state
222 .update(cx, |this, cx| this.render(window, cx).into_any_element())
223 }
224}