pub mod running; use dap::client::SessionId; use gpui::{App, Entity, EventEmitter, FocusHandle, Focusable, Subscription, Task, WeakEntity}; use project::Project; use project::debugger::{dap_store::DapStore, session::Session}; use project::worktree_store::WorktreeStore; use rpc::proto::{self, PeerId}; use running::RunningState; use ui::prelude::*; use workspace::{ FollowableItem, ViewId, Workspace, item::{self, Item}, }; use crate::debugger_panel::DebugPanel; pub(crate) enum DebugSessionState { Running(Entity), } impl DebugSessionState { pub(crate) fn as_running(&self) -> Option<&Entity> { match &self { DebugSessionState::Running(entity) => Some(entity), } } } pub struct DebugSession { remote_id: Option, mode: DebugSessionState, dap_store: WeakEntity, _debug_panel: WeakEntity, _worktree_store: WeakEntity, _workspace: WeakEntity, _subscriptions: [Subscription; 1], } #[derive(Debug)] pub enum DebugPanelItemEvent { Close, Stopped { go_to_stack_frame: bool }, } impl DebugSession { pub(crate) fn running( project: Entity, workspace: WeakEntity, session: Entity, _debug_panel: WeakEntity, window: &mut Window, cx: &mut App, ) -> Entity { let mode = cx.new(|cx| { RunningState::new( session.clone(), project.clone(), workspace.clone(), window, cx, ) }); cx.new(|cx| Self { _subscriptions: [cx.subscribe(&mode, |_, _, _, cx| { cx.notify(); })], remote_id: None, mode: DebugSessionState::Running(mode), dap_store: project.read(cx).dap_store().downgrade(), _debug_panel, _worktree_store: project.read(cx).worktree_store().downgrade(), _workspace: workspace, }) } pub(crate) fn session_id(&self, cx: &App) -> Option { match &self.mode { DebugSessionState::Running(entity) => Some(entity.read(cx).session_id()), } } #[expect(unused)] pub(crate) fn shutdown(&mut self, cx: &mut Context) { match &self.mode { DebugSessionState::Running(state) => state.update(cx, |state, cx| state.shutdown(cx)), } } pub(crate) fn mode(&self) -> &DebugSessionState { &self.mode } pub(crate) fn label(&self, cx: &App) -> String { let session_id = match &self.mode { DebugSessionState::Running(running_state) => running_state.read(cx).session_id(), }; let Ok(Some(session)) = self .dap_store .read_with(cx, |store, _| store.session_by_id(session_id)) else { return "".to_owned(); }; session .read(cx) .as_local() .expect("Remote Debug Sessions are not implemented yet") .label() } } impl EventEmitter for DebugSession {} impl Focusable for DebugSession { fn focus_handle(&self, cx: &App) -> FocusHandle { match &self.mode { DebugSessionState::Running(running_state) => running_state.focus_handle(cx), } } } impl Item for DebugSession { type Event = DebugPanelItemEvent; } impl FollowableItem for DebugSession { fn remote_id(&self) -> Option { self.remote_id } fn to_state_proto(&self, _window: &Window, _cx: &App) -> Option { None } fn from_state_proto( _workspace: Entity, _remote_id: ViewId, _state: &mut Option, _window: &mut Window, _cx: &mut App, ) -> Option>>> { None } fn add_event_to_update_proto( &self, _event: &Self::Event, _update: &mut Option, _window: &Window, _cx: &App, ) -> bool { // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default())); true } fn apply_update_proto( &mut self, _project: &Entity, _message: proto::update_view::Variant, _window: &mut Window, _cx: &mut Context, ) -> gpui::Task> { Task::ready(Ok(())) } fn set_leader_peer_id( &mut self, _leader_peer_id: Option, _window: &mut Window, _cx: &mut Context, ) { } fn to_follow_event(_event: &Self::Event) -> Option { None } fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option { if existing.session_id(cx) == self.session_id(cx) { Some(item::Dedup::KeepExisting) } else { None } } fn is_project_item(&self, _window: &Window, _cx: &App) -> bool { true } } impl Render for DebugSession { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { match &self.mode { DebugSessionState::Running(running_state) => { running_state.update(cx, |this, cx| this.render(window, cx).into_any_element()) } } } }