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::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    #[expect(unused)]
 85    pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
 86        match &self.mode {
 87            DebugSessionState::Running(state) => state.update(cx, |state, cx| state.shutdown(cx)),
 88        }
 89    }
 90
 91    pub(crate) fn mode(&self) -> &DebugSessionState {
 92        &self.mode
 93    }
 94
 95    pub(crate) fn label(&self, cx: &App) -> String {
 96        let session_id = match &self.mode {
 97            DebugSessionState::Running(running_state) => running_state.read(cx).session_id(),
 98        };
 99        let Ok(Some(session)) = self
100            .dap_store
101            .read_with(cx, |store, _| store.session_by_id(session_id))
102        else {
103            return "".to_owned();
104        };
105        session
106            .read(cx)
107            .as_local()
108            .expect("Remote Debug Sessions are not implemented yet")
109            .label()
110    }
111}
112
113impl EventEmitter<DebugPanelItemEvent> for DebugSession {}
114
115impl Focusable for DebugSession {
116    fn focus_handle(&self, cx: &App) -> FocusHandle {
117        match &self.mode {
118            DebugSessionState::Running(running_state) => running_state.focus_handle(cx),
119        }
120    }
121}
122
123impl Item for DebugSession {
124    type Event = DebugPanelItemEvent;
125}
126
127impl FollowableItem for DebugSession {
128    fn remote_id(&self) -> Option<workspace::ViewId> {
129        self.remote_id
130    }
131
132    fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option<proto::view::Variant> {
133        None
134    }
135
136    fn from_state_proto(
137        _workspace: Entity<Workspace>,
138        _remote_id: ViewId,
139        _state: &mut Option<proto::view::Variant>,
140        _window: &mut Window,
141        _cx: &mut App,
142    ) -> Option<gpui::Task<gpui::Result<Entity<Self>>>> {
143        None
144    }
145
146    fn add_event_to_update_proto(
147        &self,
148        _event: &Self::Event,
149        _update: &mut Option<proto::update_view::Variant>,
150        _window: &Window,
151        _cx: &App,
152    ) -> bool {
153        // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
154
155        true
156    }
157
158    fn apply_update_proto(
159        &mut self,
160        _project: &Entity<project::Project>,
161        _message: proto::update_view::Variant,
162        _window: &mut Window,
163        _cx: &mut Context<Self>,
164    ) -> gpui::Task<gpui::Result<()>> {
165        Task::ready(Ok(()))
166    }
167
168    fn set_leader_peer_id(
169        &mut self,
170        _leader_peer_id: Option<PeerId>,
171        _window: &mut Window,
172        _cx: &mut Context<Self>,
173    ) {
174    }
175
176    fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
177        None
178    }
179
180    fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
181        if existing.session_id(cx) == self.session_id(cx) {
182            Some(item::Dedup::KeepExisting)
183        } else {
184            None
185        }
186    }
187
188    fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
189        true
190    }
191}
192
193impl Render for DebugSession {
194    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
195        match &self.mode {
196            DebugSessionState::Running(running_state) => {
197                running_state.update(cx, |this, cx| this.render(window, cx).into_any_element())
198            }
199        }
200    }
201}