session.rs

  1pub mod running;
  2
  3use crate::{StackTraceView, debugger_panel::DebugPanel, persistence::SerializedLayout};
  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    _debug_panel: WeakEntity<DebugPanel>,
 26    _worktree_store: WeakEntity<WorktreeStore>,
 27    workspace: WeakEntity<Workspace>,
 28    _subscriptions: [Subscription; 1],
 29}
 30
 31#[derive(Debug)]
 32pub enum DebugPanelItemEvent {
 33    Close,
 34    Stopped { go_to_stack_frame: bool },
 35}
 36
 37impl DebugSession {
 38    pub(crate) fn running(
 39        project: Entity<Project>,
 40        workspace: WeakEntity<Workspace>,
 41        session: Entity<Session>,
 42        _debug_panel: WeakEntity<DebugPanel>,
 43        serialized_layout: Option<SerializedLayout>,
 44        dock_axis: Axis,
 45        window: &mut Window,
 46        cx: &mut App,
 47    ) -> Entity<Self> {
 48        let running_state = cx.new(|cx| {
 49            RunningState::new(
 50                session.clone(),
 51                project.clone(),
 52                workspace.clone(),
 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            _debug_panel,
 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: &App) -> SharedString {
114        if let Some(label) = self.label.get() {
115            return label.clone();
116        }
117
118        let session = self.running_state.read(cx).session();
119
120        self.label
121            .get_or_init(|| session.read(cx).label())
122            .to_owned()
123    }
124
125    pub(crate) fn running_state(&self) -> &Entity<RunningState> {
126        &self.running_state
127    }
128
129    pub(crate) fn label_element(&self, cx: &App) -> AnyElement {
130        let label = self.label(cx);
131
132        let is_terminated = self
133            .running_state
134            .read(cx)
135            .session()
136            .read(cx)
137            .is_terminated();
138        let icon = {
139            if is_terminated {
140                Some(Indicator::dot().color(Color::Error))
141            } else {
142                match self
143                    .running_state
144                    .read(cx)
145                    .thread_status(cx)
146                    .unwrap_or_default()
147                {
148                    project::debugger::session::ThreadStatus::Stopped => {
149                        Some(Indicator::dot().color(Color::Conflict))
150                    }
151                    _ => Some(Indicator::dot().color(Color::Success)),
152                }
153            }
154        };
155
156        h_flex()
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}