session.rs

  1pub mod running;
  2
  3use crate::{persistence::SerializedLayout, session::running::DebugTerminal};
  4use dap::client::SessionId;
  5use gpui::{App, Axis, Entity, EventEmitter, FocusHandle, Focusable, Task, WeakEntity};
  6use project::debugger::session::Session;
  7
  8use project::{Project, debugger::session::SessionQuirks};
  9use rpc::proto;
 10use running::RunningState;
 11use ui::prelude::*;
 12use workspace::{
 13    CollaboratorId, FollowableItem, ViewId, Workspace,
 14    item::{self, Item},
 15};
 16
 17pub struct DebugSession {
 18    remote_id: Option<workspace::ViewId>,
 19    pub(crate) running_state: Entity<RunningState>,
 20    pub(crate) quirks: SessionQuirks,
 21}
 22
 23impl DebugSession {
 24    pub(crate) fn running(
 25        project: Entity<Project>,
 26        workspace: WeakEntity<Workspace>,
 27        parent_terminal: Option<Entity<DebugTerminal>>,
 28        session: Entity<Session>,
 29        serialized_layout: Option<SerializedLayout>,
 30        dock_axis: Axis,
 31        window: &mut Window,
 32        cx: &mut App,
 33    ) -> Entity<Self> {
 34        let running_state = cx.new(|cx| {
 35            RunningState::new(
 36                session.clone(),
 37                project.clone(),
 38                workspace.clone(),
 39                parent_terminal,
 40                serialized_layout,
 41                dock_axis,
 42                window,
 43                cx,
 44            )
 45        });
 46        let quirks = session.read(cx).quirks();
 47
 48        cx.new(|_| Self {
 49            remote_id: None,
 50            running_state,
 51            quirks,
 52        })
 53    }
 54
 55    pub(crate) fn session_id(&self, cx: &App) -> SessionId {
 56        self.running_state.read(cx).session_id()
 57    }
 58
 59    pub fn session(&self, cx: &App) -> Entity<Session> {
 60        self.running_state.read(cx).session().clone()
 61    }
 62
 63    pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
 64        self.running_state
 65            .update(cx, |state, cx| state.shutdown(cx));
 66    }
 67
 68    pub(crate) fn label(&self, cx: &mut App) -> Option<SharedString> {
 69        let session = self.running_state.read(cx).session().clone();
 70        session.update(cx, |session, cx| {
 71            let session_label = session.label();
 72            let quirks = session.quirks();
 73            let mut single_thread_name = || {
 74                let threads = session.threads(cx);
 75                match threads.as_slice() {
 76                    [(thread, _)] => Some(SharedString::from(&thread.name)),
 77                    _ => None,
 78                }
 79            };
 80            if quirks.prefer_thread_name {
 81                single_thread_name().or(session_label)
 82            } else {
 83                session_label.or_else(single_thread_name)
 84            }
 85        })
 86    }
 87
 88    pub fn running_state(&self) -> &Entity<RunningState> {
 89        &self.running_state
 90    }
 91}
 92
 93impl EventEmitter<()> for DebugSession {}
 94
 95impl Focusable for DebugSession {
 96    fn focus_handle(&self, cx: &App) -> FocusHandle {
 97        self.running_state.focus_handle(cx)
 98    }
 99}
100
101impl Item for DebugSession {
102    type Event = ();
103    fn tab_content_text(&self, _detail: usize, _cx: &App) -> SharedString {
104        "Debugger".into()
105    }
106}
107
108impl FollowableItem for DebugSession {
109    fn remote_id(&self) -> Option<workspace::ViewId> {
110        self.remote_id
111    }
112
113    fn to_state_proto(&self, _window: &mut Window, _cx: &mut App) -> Option<proto::view::Variant> {
114        None
115    }
116
117    fn from_state_proto(
118        _workspace: Entity<Workspace>,
119        _remote_id: ViewId,
120        _state: &mut Option<proto::view::Variant>,
121        _window: &mut Window,
122        _cx: &mut App,
123    ) -> Option<gpui::Task<anyhow::Result<Entity<Self>>>> {
124        None
125    }
126
127    fn add_event_to_update_proto(
128        &self,
129        _event: &Self::Event,
130        _update: &mut Option<proto::update_view::Variant>,
131        _window: &mut Window,
132        _cx: &mut App,
133    ) -> bool {
134        // update.get_or_insert_with(|| proto::update_view::Variant::DebugPanel(Default::default()));
135
136        true
137    }
138
139    fn apply_update_proto(
140        &mut self,
141        _project: &Entity<project::Project>,
142        _message: proto::update_view::Variant,
143        _window: &mut Window,
144        _cx: &mut Context<Self>,
145    ) -> gpui::Task<anyhow::Result<()>> {
146        Task::ready(Ok(()))
147    }
148
149    fn set_leader_id(
150        &mut self,
151        _leader_id: Option<CollaboratorId>,
152        _window: &mut Window,
153        _cx: &mut Context<Self>,
154    ) {
155    }
156
157    fn to_follow_event(_event: &Self::Event) -> Option<workspace::item::FollowEvent> {
158        None
159    }
160
161    fn dedup(&self, existing: &Self, _window: &Window, cx: &App) -> Option<workspace::item::Dedup> {
162        if existing.session_id(cx) == self.session_id(cx) {
163            Some(item::Dedup::KeepExisting)
164        } else {
165            None
166        }
167    }
168
169    fn is_project_item(&self, _window: &Window, _cx: &App) -> bool {
170        true
171    }
172}
173
174impl Render for DebugSession {
175    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
176        self.running_state
177            .update(cx, |this, cx| this.render(window, cx).into_any_element())
178    }
179}