1use std::any::TypeId;
2
3use dap::debugger_settings::DebuggerSettings;
4use debugger_panel::DebugPanel;
5use editor::Editor;
6use gpui::{App, DispatchPhase, EntityInputHandler, actions};
7use new_process_modal::{NewProcessModal, NewProcessMode};
8use onboarding_modal::DebuggerOnboardingModal;
9use project::debugger::{self, breakpoint_store::SourceBreakpoint, session::ThreadStatus};
10use session::DebugSession;
11use settings::Settings;
12use stack_trace_view::StackTraceView;
13use tasks_ui::{Spawn, TaskOverrides};
14use ui::{FluentBuilder, InteractiveElement};
15use util::maybe;
16use workspace::{ItemHandle, ShutdownDebugAdapters, Workspace};
17use zed_actions::ToggleFocus;
18use zed_actions::debugger::OpenOnboardingModal;
19
20pub mod attach_modal;
21pub mod debugger_panel;
22mod dropdown_menus;
23mod new_process_modal;
24mod onboarding_modal;
25mod persistence;
26pub(crate) mod session;
27mod stack_trace_view;
28
29#[cfg(any(test, feature = "test-support"))]
30pub mod tests;
31
32actions!(
33 debugger,
34 [
35 Start,
36 Continue,
37 Detach,
38 Pause,
39 Restart,
40 StepInto,
41 StepOver,
42 StepOut,
43 StepBack,
44 Stop,
45 ToggleIgnoreBreakpoints,
46 ClearAllBreakpoints,
47 FocusConsole,
48 FocusVariables,
49 FocusBreakpointList,
50 FocusFrames,
51 FocusModules,
52 FocusLoadedSources,
53 FocusTerminal,
54 ShowStackTrace,
55 ToggleThreadPicker,
56 ToggleSessionPicker,
57 RerunLastSession,
58 ToggleExpandItem,
59 ]
60);
61
62actions!(dev, [CopyDebugAdapterArguments]);
63
64pub fn init(cx: &mut App) {
65 DebuggerSettings::register(cx);
66 workspace::FollowableViewRegistry::register::<DebugSession>(cx);
67
68 cx.observe_new(|workspace: &mut Workspace, _, _| {
69 workspace
70 .register_action(spawn_task_or_modal)
71 .register_action(|workspace, _: &ToggleFocus, window, cx| {
72 workspace.toggle_panel_focus::<DebugPanel>(window, cx);
73 })
74 .register_action(|workspace: &mut Workspace, _: &Start, window, cx| {
75 NewProcessModal::show(workspace, window, NewProcessMode::Debug, None, cx);
76 })
77 .register_action(
78 |workspace: &mut Workspace, _: &RerunLastSession, window, cx| {
79 let Some(debug_panel) = workspace.panel::<DebugPanel>(cx) else {
80 return;
81 };
82
83 debug_panel.update(cx, |debug_panel, cx| {
84 debug_panel.rerun_last_session(workspace, window, cx);
85 })
86 },
87 )
88 .register_action(
89 |workspace: &mut Workspace, _: &ShutdownDebugAdapters, _window, cx| {
90 workspace.project().update(cx, |project, cx| {
91 project.dap_store().update(cx, |store, cx| {
92 store.shutdown_sessions(cx).detach();
93 })
94 })
95 },
96 )
97 .register_action(|workspace, _: &OpenOnboardingModal, window, cx| {
98 DebuggerOnboardingModal::toggle(workspace, window, cx)
99 })
100 .register_action_renderer(|div, workspace, _, cx| {
101 let Some(debug_panel) = workspace.panel::<DebugPanel>(cx) else {
102 return div;
103 };
104 let Some(active_item) = debug_panel
105 .read(cx)
106 .active_session()
107 .map(|session| session.read(cx).running_state().clone())
108 else {
109 return div;
110 };
111 let running_state = active_item.read(cx);
112 if running_state.session().read(cx).is_terminated() {
113 return div;
114 }
115
116 let caps = running_state.capabilities(cx);
117 let supports_step_back = caps.supports_step_back.unwrap_or_default();
118 let supports_detach = running_state.session().read(cx).is_attached();
119 let status = running_state.thread_status(cx);
120
121 let active_item = active_item.downgrade();
122 div.when(status == Some(ThreadStatus::Running), |div| {
123 let active_item = active_item.clone();
124 div.on_action(move |_: &Pause, _, cx| {
125 active_item
126 .update(cx, |item, cx| item.pause_thread(cx))
127 .ok();
128 })
129 })
130 .when(status == Some(ThreadStatus::Stopped), |div| {
131 div.on_action({
132 let active_item = active_item.clone();
133 move |_: &StepInto, _, cx| {
134 active_item.update(cx, |item, cx| item.step_in(cx)).ok();
135 }
136 })
137 .on_action({
138 let active_item = active_item.clone();
139 move |_: &StepOver, _, cx| {
140 active_item.update(cx, |item, cx| item.step_over(cx)).ok();
141 }
142 })
143 .on_action({
144 let active_item = active_item.clone();
145 move |_: &StepOut, _, cx| {
146 active_item.update(cx, |item, cx| item.step_out(cx)).ok();
147 }
148 })
149 .when(supports_step_back, |div| {
150 let active_item = active_item.clone();
151 div.on_action(move |_: &StepBack, _, cx| {
152 active_item.update(cx, |item, cx| item.step_back(cx)).ok();
153 })
154 })
155 .on_action({
156 let active_item = active_item.clone();
157 move |_: &Continue, _, cx| {
158 active_item
159 .update(cx, |item, cx| item.continue_thread(cx))
160 .ok();
161 }
162 })
163 .on_action(cx.listener(
164 |workspace, _: &ShowStackTrace, window, cx| {
165 let Some(debug_panel) = workspace.panel::<DebugPanel>(cx) else {
166 return;
167 };
168
169 if let Some(existing) = workspace.item_of_type::<StackTraceView>(cx) {
170 let is_active = workspace
171 .active_item(cx)
172 .is_some_and(|item| item.item_id() == existing.item_id());
173 workspace.activate_item(&existing, true, !is_active, window, cx);
174 } else {
175 let Some(active_session) = debug_panel.read(cx).active_session()
176 else {
177 return;
178 };
179
180 let project = workspace.project();
181
182 let stack_trace_view = active_session.update(cx, |session, cx| {
183 session.stack_trace_view(project, window, cx).clone()
184 });
185
186 workspace.add_item_to_active_pane(
187 Box::new(stack_trace_view),
188 None,
189 true,
190 window,
191 cx,
192 );
193 }
194 },
195 ))
196 })
197 .when(supports_detach, |div| {
198 let active_item = active_item.clone();
199 div.on_action(move |_: &Detach, _, cx| {
200 active_item
201 .update(cx, |item, cx| item.detach_client(cx))
202 .ok();
203 })
204 })
205 .on_action({
206 let active_item = active_item.clone();
207 move |_: &Restart, _, cx| {
208 active_item
209 .update(cx, |item, cx| item.restart_session(cx))
210 .ok();
211 }
212 })
213 .on_action({
214 let active_item = active_item.clone();
215 move |_: &Stop, _, cx| {
216 active_item.update(cx, |item, cx| item.stop_thread(cx)).ok();
217 }
218 })
219 .on_action({
220 let active_item = active_item.clone();
221 move |_: &ToggleIgnoreBreakpoints, _, cx| {
222 active_item
223 .update(cx, |item, cx| item.toggle_ignore_breakpoints(cx))
224 .ok();
225 }
226 })
227 });
228 })
229 .detach();
230
231 cx.observe_new({
232 move |editor: &mut Editor, _, _| {
233 editor
234 .register_action_renderer(move |editor, window, cx| {
235 let Some(workspace) = editor.workspace() else {
236 return;
237 };
238 let Some(debug_panel) = workspace.read(cx).panel::<DebugPanel>(cx) else {
239 return;
240 };
241 let Some(active_session) = debug_panel
242 .clone()
243 .update(cx, |panel, _| panel.active_session())
244 else {
245 return;
246 };
247 let editor = cx.entity().downgrade();
248 window.on_action(TypeId::of::<editor::actions::RunToCursor>(), {
249 let editor = editor.clone();
250 let active_session = active_session.clone();
251 move |_, phase, _, cx| {
252 if phase != DispatchPhase::Bubble {
253 return;
254 }
255 maybe!({
256 let (buffer, position, _) = editor
257 .update(cx, |editor, cx| {
258 let cursor_point: language::Point =
259 editor.selections.newest(cx).head();
260
261 editor
262 .buffer()
263 .read(cx)
264 .point_to_buffer_point(cursor_point, cx)
265 })
266 .ok()??;
267
268 let path =
269 debugger::breakpoint_store::BreakpointStore::abs_path_from_buffer(
270 &buffer, cx,
271 )?;
272
273 let source_breakpoint = SourceBreakpoint {
274 row: position.row,
275 path,
276 message: None,
277 condition: None,
278 hit_condition: None,
279 state: debugger::breakpoint_store::BreakpointState::Enabled,
280 };
281
282 active_session.update(cx, |session, cx| {
283 session.running_state().update(cx, |state, cx| {
284 if let Some(thread_id) = state.selected_thread_id() {
285 state.session().update(cx, |session, cx| {
286 session.run_to_position(
287 source_breakpoint,
288 thread_id,
289 cx,
290 );
291 })
292 }
293 });
294 });
295
296 Some(())
297 });
298 }
299 });
300
301 window.on_action(
302 TypeId::of::<editor::actions::EvaluateSelectedText>(),
303 move |_, _, window, cx| {
304 maybe!({
305 let text = editor
306 .update(cx, |editor, cx| {
307 editor.text_for_range(
308 editor.selections.newest(cx).range(),
309 &mut None,
310 window,
311 cx,
312 )
313 })
314 .ok()??;
315
316 active_session.update(cx, |session, cx| {
317 session.running_state().update(cx, |state, cx| {
318 let stack_id = state.selected_stack_frame_id(cx);
319
320 state.session().update(cx, |session, cx| {
321 session
322 .evaluate(text, None, stack_id, None, cx)
323 .detach();
324 });
325 });
326 });
327
328 Some(())
329 });
330 },
331 );
332 })
333 .detach();
334 }
335 })
336 .detach();
337}
338
339fn spawn_task_or_modal(
340 workspace: &mut Workspace,
341 action: &Spawn,
342 window: &mut ui::Window,
343 cx: &mut ui::Context<Workspace>,
344) {
345 match action {
346 Spawn::ByName {
347 task_name,
348 reveal_target,
349 } => {
350 let overrides = reveal_target.map(|reveal_target| TaskOverrides {
351 reveal_target: Some(reveal_target),
352 });
353 let name = task_name.clone();
354 tasks_ui::spawn_tasks_filtered(
355 move |(_, task)| task.label.eq(&name),
356 overrides,
357 window,
358 cx,
359 )
360 .detach_and_log_err(cx)
361 }
362 Spawn::ByTag {
363 task_tag,
364 reveal_target,
365 } => {
366 let overrides = reveal_target.map(|reveal_target| TaskOverrides {
367 reveal_target: Some(reveal_target),
368 });
369 let tag = task_tag.clone();
370 tasks_ui::spawn_tasks_filtered(
371 move |(_, task)| task.tags.contains(&tag),
372 overrides,
373 window,
374 cx,
375 )
376 .detach_and_log_err(cx)
377 }
378 Spawn::ViaModal { reveal_target } => {
379 NewProcessModal::show(workspace, window, NewProcessMode::Task, *reveal_target, cx);
380 }
381 }
382}