debugger: Extract running state from DebugSession mode and remove mode field (#29646)

DebugSession.mode is no longer needed because project::debugger::Session
manages its own state now (booting, running, terminated), and removing
mode simplifies a lot of the code that uses running state.

I used Zed AI to do a good chunk of the refactor, but I doubled-checked
everything it did and changed a good amount of its updates.

Release Notes:

- N/A

Co-authored-by: Zed AI <ai@zed.dev>
This commit is contained in:
Anthony Eid 2025-04-30 01:57:53 -04:00 committed by GitHub
parent edf78e770d
commit 9767033985
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 158 additions and 288 deletions

View file

@ -5,7 +5,7 @@ use std::sync::OnceLock;
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::debugger::session::Session;
use project::worktree_store::WorktreeStore;
use rpc::proto::{self, PeerId};
use running::RunningState;
@ -18,23 +18,10 @@ use workspace::{
use crate::debugger_panel::DebugPanel;
use crate::persistence::SerializedPaneLayout;
pub(crate) enum DebugSessionState {
Running(Entity<running::RunningState>),
}
impl DebugSessionState {
pub(crate) fn as_running(&self) -> Option<&Entity<running::RunningState>> {
match &self {
DebugSessionState::Running(entity) => Some(entity),
}
}
}
pub struct DebugSession {
remote_id: Option<workspace::ViewId>,
mode: DebugSessionState,
running_state: Entity<RunningState>,
label: OnceLock<SharedString>,
dap_store: WeakEntity<DapStore>,
_debug_panel: WeakEntity<DebugPanel>,
_worktree_store: WeakEntity<WorktreeStore>,
_workspace: WeakEntity<Workspace>,
@ -57,7 +44,7 @@ impl DebugSession {
window: &mut Window,
cx: &mut App,
) -> Entity<Self> {
let mode = cx.new(|cx| {
let running_state = cx.new(|cx| {
RunningState::new(
session.clone(),
project.clone(),
@ -69,13 +56,12 @@ impl DebugSession {
});
cx.new(|cx| Self {
_subscriptions: [cx.subscribe(&mode, |_, _, _, cx| {
_subscriptions: [cx.subscribe(&running_state, |_, _, _, cx| {
cx.notify();
})],
remote_id: None,
mode: DebugSessionState::Running(mode),
running_state,
label: OnceLock::new(),
dap_store: project.read(cx).dap_store().downgrade(),
_debug_panel,
_worktree_store: project.read(cx).worktree_store().downgrade(),
_workspace: workspace,
@ -83,25 +69,16 @@ impl DebugSession {
}
pub(crate) fn session_id(&self, cx: &App) -> SessionId {
match &self.mode {
DebugSessionState::Running(entity) => entity.read(cx).session_id(),
}
self.running_state.read(cx).session_id()
}
pub fn session(&self, cx: &App) -> Entity<Session> {
match &self.mode {
DebugSessionState::Running(entity) => entity.read(cx).session().clone(),
}
self.running_state.read(cx).session().clone()
}
pub(crate) fn shutdown(&mut self, cx: &mut Context<Self>) {
match &self.mode {
DebugSessionState::Running(state) => state.update(cx, |state, cx| state.shutdown(cx)),
}
}
pub(crate) fn mode(&self) -> &DebugSessionState {
&self.mode
self.running_state
.update(cx, |state, cx| state.shutdown(cx));
}
pub(crate) fn label(&self, cx: &App) -> SharedString {
@ -109,43 +86,40 @@ impl DebugSession {
return label.clone();
}
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 "".into();
};
let session = self.running_state.read(cx).session();
self.label
.get_or_init(|| session.read(cx).label())
.to_owned()
}
#[allow(unused)]
pub(crate) fn running_state(&self) -> Entity<RunningState> {
match &self.mode {
DebugSessionState::Running(running_state) => running_state.clone(),
}
pub(crate) fn running_state(&self) -> &Entity<RunningState> {
&self.running_state
}
pub(crate) fn label_element(&self, cx: &App) -> AnyElement {
let label = self.label(cx);
let icon = match &self.mode {
DebugSessionState::Running(state) => {
if state.read(cx).session().read(cx).is_terminated() {
Some(Indicator::dot().color(Color::Error))
} else {
match state.read(cx).thread_status(cx).unwrap_or_default() {
project::debugger::session::ThreadStatus::Stopped => {
Some(Indicator::dot().color(Color::Conflict))
}
_ => Some(Indicator::dot().color(Color::Success)),
let icon = {
if self
.running_state
.read(cx)
.session()
.read(cx)
.is_terminated()
{
Some(Indicator::dot().color(Color::Error))
} else {
match self
.running_state
.read(cx)
.thread_status(cx)
.unwrap_or_default()
{
project::debugger::session::ThreadStatus::Stopped => {
Some(Indicator::dot().color(Color::Conflict))
}
_ => Some(Indicator::dot().color(Color::Success)),
}
}
};
@ -163,9 +137,7 @@ impl EventEmitter<DebugPanelItemEvent> 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),
}
self.running_state.focus_handle(cx)
}
}
@ -244,10 +216,7 @@ impl FollowableItem for DebugSession {
impl Render for DebugSession {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
match &self.mode {
DebugSessionState::Running(running_state) => {
running_state.update(cx, |this, cx| this.render(window, cx).into_any_element())
}
}
self.running_state
.update(cx, |this, cx| this.render(window, cx).into_any_element())
}
}