session.rs

  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::{Indicator, 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
 46impl DebugSession {
 47    pub(crate) fn running(
 48        project: Entity<Project>,
 49        workspace: WeakEntity<Workspace>,
 50        session: Entity<Session>,
 51        _debug_panel: WeakEntity<DebugPanel>,
 52        window: &mut Window,
 53        cx: &mut App,
 54    ) -> Entity<Self> {
 55        let mode = cx.new(|cx| {
 56            RunningState::new(
 57                session.clone(),
 58                project.clone(),
 59                workspace.clone(),
 60                window,
 61                cx,
 62            )
 63        });
 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) -> SessionId {
 79        match &self.mode {
 80            DebugSessionState::Running(entity) => 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    pub(crate) fn label_element(&self, cx: &App) -> AnyElement {
112        let label = self.label(cx);
113
114        let (icon, color) = match &self.mode {
115            DebugSessionState::Running(state) => {
116                if state.read(cx).session().read(cx).is_terminated() {
117                    (Some(Indicator::dot().color(Color::Error)), Color::Error)
118                } else {
119                    match state.read(cx).thread_status(cx).unwrap_or_default() {
120                        project::debugger::session::ThreadStatus::Stopped => (
121                            Some(Indicator::dot().color(Color::Conflict)),
122                            Color::Conflict,
123                        ),
124                        _ => (Some(Indicator::dot().color(Color::Success)), Color::Success),
125                    }
126                }
127            }
128        };
129
130        h_flex()
131            .gap_2()
132            .when_some(icon, |this, indicator| this.child(indicator))
133            .justify_between()
134            .child(Label::new(label).color(color))
135            .into_any_element()
136    }
137}
138
139impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
140
141impl Focusable for DebugSession {
142    fn focus_handle(&self, cx: &App) -> FocusHandle {
143        match &self.mode {
144            DebugSessionState::Running(running_state) => running_state.focus_handle(cx),
145        }
146    }
147}
148
149impl Item for DebugSession {
150    type Event = DebugPanelItemEvent;
151}
152
153impl FollowableItem for DebugSession {
154    fn remote_id(&self) -> Option<workspace::ViewId> {
155        self.remote_id
156    }
157
158    fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
159        None
160    }
161
162    fn from_state_proto(
163        _workspace: Entity<Workspace>,
164        _remote_id: ViewId,
165        _state: &mut Option<proto::view::Variant>,
166        _window: &mut Window,
167        _cx: &mut App,
168    ) -> Option<gpui::Task<gpui::Result<Entity<Self>>>> {
169        None
170    }
171
172    fn add_event_to_update_proto(
173        &self,
174        _event: &Self::Event,
175        _update: &mut Option<proto::update_view::Variant>,
176        _window: &Window,
177        _cx: &App,
178    ) -> bool {
179        // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
180
181        true
182    }
183
184    fn apply_update_proto(
185        &mut self,
186        _project: &Entity<project::Project>,
187        _message: proto::update_view::Variant,
188        _window: &mut Window,
189        _cx: &mut Context<Self>,
190    ) -> gpui::Task<gpui::Result<()>> {
191        Task::ready(Ok(()))
192    }
193
194    fn set_leader_peer_id(
195        &mut self,
196        _leader_peer_id: Option<PeerId>,
197        _window: &mut Window,
198        _cx: &mut Context<Self>,
199    ) {
200    }
201
202    fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
203        None
204    }
205
206    fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
207        if existing.session_id(cx) == self.session_id(cx) {
208            Some(item::Dedup::KeepExisting)
209        } else {
210            None
211        }
212    }
213
214    fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
215        true
216    }
217}
218
219impl Render for DebugSession {
220    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
221        match &self.mode {
222            DebugSessionState::Running(running_state) => {
223                running_state.update(cx, |this, cx| this.render(window, cx).into_any_element())
224            }
225        }
226    }
227}