1pub mod running;
2
3use dap::client::SessionId;
4use gpui::{App, Entity, EventEmitter, FocusHandle, Focusable, Subscription, Task, WeakEntity};
5use project::Project;
6use project::debugger::{dap_store::DapStore, session::Session};
7use project::worktree_store::WorktreeStore;
8use rpc::proto::{self, PeerId};
9use running::RunningState;
10use ui::prelude::*;
11use workspace::{
12 FollowableItem, ViewId, Workspace,
13 item::{self, Item},
14};
15
16use crate::debugger_panel::DebugPanel;
17
18pub(crate) enum DebugSessionState {
19 Running(Entity<running::RunningState>),
20}
21
22impl DebugSessionState {
23 pub(crate) fn as_running(&self) -> Option<&Entity<running::RunningState>> {
24 match &self {
25 DebugSessionState::Running(entity) => Some(entity),
26 }
27 }
28}
29
30pub struct DebugSession {
31 remote_id: Option<workspace::ViewId>,
32 mode: DebugSessionState,
33 dap_store: WeakEntity<DapStore>,
34 _debug_panel: WeakEntity<DebugPanel>,
35 _worktree_store: WeakEntity<WorktreeStore>,
36 _workspace: WeakEntity<Workspace>,
37 _subscriptions: [Subscription; 1],
38}
39
40#[derive(Debug)]
41pub enum DebugPanelItemEvent {
42 Close,
43 Stopped { go_to_stack_frame: bool },
44}
45
46#[derive(Clone, Copy, PartialEq, Eq, Debug)]
47pub enum ThreadItem {
48 Console,
49 LoadedSource,
50 Modules,
51 Variables,
52}
53
54impl DebugSession {
55 pub(crate) fn running(
56 project: Entity<Project>,
57 workspace: WeakEntity<Workspace>,
58 session: Entity<Session>,
59 _debug_panel: WeakEntity<DebugPanel>,
60 window: &mut Window,
61 cx: &mut App,
62 ) -> Entity<Self> {
63 let mode = cx.new(|cx| RunningState::new(session.clone(), workspace.clone(), window, cx));
64
65 cx.new(|cx| Self {
66 _subscriptions: [cx.subscribe(&mode, |_, _, _, cx| {
67 cx.notify();
68 })],
69 remote_id: None,
70 mode: DebugSessionState::Running(mode),
71 dap_store: project.read(cx).dap_store().downgrade(),
72 _debug_panel,
73 _worktree_store: project.read(cx).worktree_store().downgrade(),
74 _workspace: workspace,
75 })
76 }
77
78 pub(crate) fn session_id(&self, cx: &App) -> Option<SessionId> {
79 match &self.mode {
80 DebugSessionState::Running(entity) => Some(entity.read(cx).session_id()),
81 }
82 }
83
84 pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
85 match &self.mode {
86 DebugSessionState::Running(state) => state.update(cx, |state, cx| state.shutdown(cx)),
87 }
88 }
89
90 pub(crate) fn mode(&self) -> &DebugSessionState {
91 &self.mode
92 }
93
94 pub(crate) fn label(&self, cx: &App) -> String {
95 let session_id = match &self.mode {
96 DebugSessionState::Running(running_state) => running_state.read(cx).session_id(),
97 };
98 let Ok(Some(session)) = self
99 .dap_store
100 .read_with(cx, |store, _| store.session_by_id(session_id))
101 else {
102 return "".to_owned();
103 };
104 session
105 .read(cx)
106 .as_local()
107 .expect("Remote Debug Sessions are not implemented yet")
108 .label()
109 }
110}
111
112impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
113
114impl Focusable for DebugSession {
115 fn focus_handle(&self, cx: &App) -> FocusHandle {
116 match &self.mode {
117 DebugSessionState::Running(running_state) => running_state.focus_handle(cx),
118 }
119 }
120}
121
122impl Item for DebugSession {
123 type Event = DebugPanelItemEvent;
124}
125
126impl FollowableItem for DebugSession {
127 fn remote_id(&self) -> Option<workspace::ViewId> {
128 self.remote_id
129 }
130
131 fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
132 None
133 }
134
135 fn from_state_proto(
136 _workspace: Entity<Workspace>,
137 _remote_id: ViewId,
138 _state: &mut Option<proto::view::Variant>,
139 _window: &mut Window,
140 _cx: &mut App,
141 ) -> Option<gpui::Task<gpui::Result<Entity<Self>>>> {
142 None
143 }
144
145 fn add_event_to_update_proto(
146 &self,
147 _event: &Self::Event,
148 _update: &mut Option<proto::update_view::Variant>,
149 _window: &Window,
150 _cx: &App,
151 ) -> bool {
152 // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
153
154 true
155 }
156
157 fn apply_update_proto(
158 &mut self,
159 _project: &Entity<project::Project>,
160 _message: proto::update_view::Variant,
161 _window: &mut Window,
162 _cx: &mut Context<Self>,
163 ) -> gpui::Task<gpui::Result<()>> {
164 Task::ready(Ok(()))
165 }
166
167 fn set_leader_peer_id(
168 &mut self,
169 _leader_peer_id: Option<PeerId>,
170 _window: &mut Window,
171 _cx: &mut Context<Self>,
172 ) {
173 }
174
175 fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
176 None
177 }
178
179 fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
180 if existing.session_id(cx) == self.session_id(cx) {
181 Some(item::Dedup::KeepExisting)
182 } else {
183 None
184 }
185 }
186
187 fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
188 true
189 }
190}
191
192impl Render for DebugSession {
193 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
194 match &self.mode {
195 DebugSessionState::Running(running_state) => {
196 running_state.update(cx, |this, cx| this.render(window, cx).into_any_element())
197 }
198 }
199 }
200}