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, 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            .gap_2()
157            .when_some(icon, |this, indicator| this.child(indicator))
158            .justify_between()
159            .child(
160                Label::new(label)
161                    .size(LabelSize::Small)
162                    .when(is_terminated, |this| this.strikethrough()),
163            )
164            .into_any_element()
165    }
166}
167
168impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
169
170impl Focusable for DebugSession {
171    fn focus_handle(&self, cx: &App) -> FocusHandle {
172        self.running_state.focus_handle(cx)
173    }
174}
175
176impl Item for DebugSession {
177    type Event = DebugPanelItemEvent;
178    fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
179        "Debugger".into()
180    }
181}
182
183impl FollowableItem for DebugSession {
184    fn remote_id(&self) -> Option<workspace::ViewId> {
185        self.remote_id
186    }
187
188    fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
189        None
190    }
191
192    fn from_state_proto(
193        _workspace: Entity<Workspace>,
194        _remote_id: ViewId,
195        _state: &mut Option<proto::view::Variant>,
196        _window: &mut Window,
197        _cx: &mut App,
198    ) -> Option<gpui::Task<anyhow::Result<Entity<Self>>>> {
199        None
200    }
201
202    fn add_event_to_update_proto(
203        &self,
204        _event: &Self::Event,
205        _update: &mut Option<proto::update_view::Variant>,
206        _window: &Window,
207        _cx: &App,
208    ) -> bool {
209        // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
210
211        true
212    }
213
214    fn apply_update_proto(
215        &mut self,
216        _project: &Entity<project::Project>,
217        _message: proto::update_view::Variant,
218        _window: &mut Window,
219        _cx: &mut Context<Self>,
220    ) -> gpui::Task<anyhow::Result<()>> {
221        Task::ready(Ok(()))
222    }
223
224    fn set_leader_id(
225        &mut self,
226        _leader_id: Option<CollaboratorId>,
227        _window: &mut Window,
228        _cx: &mut Context<Self>,
229    ) {
230    }
231
232    fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
233        None
234    }
235
236    fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
237        if existing.session_id(cx) == self.session_id(cx) {
238            Some(item::Dedup::KeepExisting)
239        } else {
240            None
241        }
242    }
243
244    fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
245        true
246    }
247}
248
249impl Render for DebugSession {
250    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
251        self.running_state
252            .update(cx, |this, cx| this.render(window, cx).into_any_element())
253    }
254}