session.rs

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