session.rs

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