debugger/tasks: Remove TaskType enum (#29208)

Closes #ISSUE

Release Notes:

- N/A

---------

Co-authored-by: Cole Miller <m@cole-miller.net>
Co-authored-by: Anthony Eid <hello@anthonyeid.me>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: Anthony <anthony@zed.dev>
Co-authored-by: Conrad <conrad@zed.dev>
This commit is contained in:
Piotr Osiewicz 2025-04-26 01:44:56 +02:00 committed by GitHub
parent 053fafa90e
commit 67615b968b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
53 changed files with 1272 additions and 1114 deletions

View file

@ -1,4 +1,5 @@
use dap::DebugRequest;
use dap::adapters::DebugTaskDefinition;
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{DismissEvent, Entity, EventEmitter, Focusable, Render};
use gpui::{Subscription, WeakEntity};
@ -24,20 +25,20 @@ pub(crate) struct AttachModalDelegate {
selected_index: usize,
matches: Vec<StringMatch>,
placeholder_text: Arc<str>,
pub(crate) definition: DebugTaskDefinition,
workspace: WeakEntity<Workspace>,
pub(crate) debug_config: task::DebugTaskDefinition,
candidates: Arc<[Candidate]>,
}
impl AttachModalDelegate {
fn new(
workspace: Entity<Workspace>,
debug_config: task::DebugTaskDefinition,
definition: DebugTaskDefinition,
candidates: Arc<[Candidate]>,
) -> Self {
Self {
workspace: workspace.downgrade(),
debug_config,
definition,
candidates,
selected_index: 0,
matches: Vec::default(),
@ -53,8 +54,8 @@ pub struct AttachModal {
impl AttachModal {
pub fn new(
definition: DebugTaskDefinition,
workspace: Entity<Workspace>,
debug_config: task::DebugTaskDefinition,
modal: bool,
window: &mut Window,
cx: &mut Context<Self>,
@ -77,12 +78,12 @@ impl AttachModal {
.collect();
processes.sort_by_key(|k| k.name.clone());
let processes = processes.into_iter().collect();
Self::with_processes(workspace, debug_config, processes, modal, window, cx)
Self::with_processes(workspace, definition, processes, modal, window, cx)
}
pub(super) fn with_processes(
workspace: Entity<Workspace>,
debug_config: task::DebugTaskDefinition,
definition: DebugTaskDefinition,
processes: Arc<[Candidate]>,
modal: bool,
window: &mut Window,
@ -90,7 +91,7 @@ impl AttachModal {
) -> Self {
let picker = cx.new(|cx| {
Picker::uniform_list(
AttachModalDelegate::new(workspace, debug_config, processes),
AttachModalDelegate::new(workspace, definition, processes),
window,
cx,
)
@ -217,7 +218,7 @@ impl PickerDelegate for AttachModalDelegate {
return cx.emit(DismissEvent);
};
match &mut self.debug_config.request {
match &mut self.definition.request {
DebugRequest::Attach(config) => {
config.process_id = Some(candidate.pid);
}
@ -227,7 +228,8 @@ impl PickerDelegate for AttachModalDelegate {
}
}
let definition = self.debug_config.clone();
let scenario = self.definition.to_scenario();
let panel = self
.workspace
.update(cx, |workspace, cx| workspace.panel::<DebugPanel>(cx))
@ -235,9 +237,10 @@ impl PickerDelegate for AttachModalDelegate {
.flatten();
if let Some(panel) = panel {
panel.update(cx, |panel, cx| {
panel.start_session(definition, window, cx);
panel.start_session(scenario, Default::default(), None, window, cx);
});
}
cx.emit(DismissEvent);
}

View file

@ -9,11 +9,12 @@ use crate::{new_session_modal::NewSessionModal, session::DebugSession};
use anyhow::{Result, anyhow};
use collections::HashMap;
use command_palette_hooks::CommandPaletteFilter;
use dap::StartDebuggingRequestArguments;
use dap::DebugRequest;
use dap::{
ContinuedEvent, LoadedSourceEvent, ModuleEvent, OutputEvent, StoppedEvent, ThreadEvent,
client::SessionId, debugger_settings::DebuggerSettings,
};
use dap::{StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
use futures::{SinkExt as _, channel::mpsc};
use gpui::{
Action, App, AsyncWindowContext, Context, DismissEvent, Entity, EntityId, EventEmitter,
@ -21,6 +22,7 @@ use gpui::{
actions, anchored, deferred,
};
use language::Buffer;
use project::debugger::session::{Session, SessionStateEvent};
use project::{
Project,
@ -35,9 +37,7 @@ use settings::Settings;
use std::any::TypeId;
use std::path::Path;
use std::sync::Arc;
use task::{
DebugTaskDefinition, DebugTaskTemplate, HideStrategy, RevealStrategy, RevealTarget, TaskId,
};
use task::{DebugScenario, HideStrategy, RevealStrategy, RevealTarget, TaskContext, TaskId};
use terminal_view::TerminalView;
use ui::{ContextMenu, Divider, DropdownMenu, Tooltip, prelude::*};
use workspace::SplitDirection;
@ -87,45 +87,8 @@ impl DebugPanel {
let project = workspace.project().clone();
let dap_store = project.read(cx).dap_store();
let weak = cx.weak_entity();
let modal_subscription =
cx.observe_new::<tasks_ui::TasksModal>(move |_, window, cx| {
let modal_entity = cx.entity();
weak.update(cx, |_: &mut DebugPanel, cx| {
let Some(window) = window else {
log::error!("Debug panel couldn't subscribe to tasks modal because there was no window");
return;
};
cx.subscribe_in(
&modal_entity,
window,
|panel, _, event: &tasks_ui::ShowAttachModal, window, cx| {
panel.workspace.update(cx, |workspace, cx| {
let workspace_handle = cx.entity().clone();
workspace.toggle_modal(window, cx, |window, cx| {
crate::attach_modal::AttachModal::new(
workspace_handle,
event.debug_config.clone(),
true,
window,
cx,
)
});
}).ok();
},
)
.detach();
})
.ok();
});
let _subscriptions = vec![
cx.subscribe_in(&dap_store, window, Self::handle_dap_store_event),
modal_subscription,
];
let _subscriptions =
vec![cx.subscribe_in(&dap_store, window, Self::handle_dap_store_event)];
let debug_panel = Self {
size: px(300.),
@ -259,43 +222,16 @@ impl DebugPanel {
})
}
pub fn start_session(
fn start_from_definition(
&mut self,
definition: DebugTaskDefinition,
window: &mut Window,
cx: &mut Context<Self>,
) {
let task_contexts = self
.workspace
.update(cx, |workspace, cx| {
tasks_ui::task_contexts(workspace, window, cx)
})
.ok();
let dap_store = self.project.read(cx).dap_store().clone();
) -> Task<Result<()>> {
cx.spawn_in(window, async move |this, cx| {
let task_context = if let Some(task) = task_contexts {
task.await
.active_worktree_context
.map_or(task::TaskContext::default(), |context| context.1)
} else {
task::TaskContext::default()
};
let dap_store = this.update(cx, |this, cx| this.project.read(cx).dap_store())?;
let (session, task) = dap_store.update(cx, |dap_store, cx| {
let template = DebugTaskTemplate {
locator: None,
definition: definition.clone(),
};
let session = if let Some(debug_config) = template
.to_zed_format()
.resolve_task("debug_task", &task_context)
.and_then(|resolved_task| resolved_task.resolved_debug_adapter_config())
{
dap_store.new_session(debug_config.definition, None, cx)
} else {
dap_store.new_session(definition.clone(), None, cx)
};
let session = dap_store.new_session(definition, None, cx);
(session.clone(), dap_store.boot_session(session, cx))
})?;
@ -318,6 +254,27 @@ impl DebugPanel {
anyhow::Ok(())
})
}
pub fn start_session(
&mut self,
scenario: DebugScenario,
task_context: TaskContext,
active_buffer: Option<Entity<Buffer>>,
window: &mut Window,
cx: &mut Context<Self>,
) {
cx.spawn_in(window, async move |this, cx| {
let definition = this
.update_in(cx, |this, window, cx| {
this.resolve_scenario(scenario, task_context, active_buffer, window, cx)
})?
.await?;
this.update_in(cx, |this, window, cx| {
this.start_from_definition(definition, window, cx)
})?
.await
})
.detach_and_log_err(cx);
}
@ -343,13 +300,13 @@ impl DebugPanel {
let definition = curr_session.update(cx, |session, _| session.definition());
let task = curr_session.update(cx, |session, cx| session.shutdown(cx));
let definition = definition.clone();
cx.spawn_in(window, async move |this, cx| {
task.await;
this.update_in(cx, |this, window, cx| {
this.start_session(definition, window, cx)
})
this.start_from_definition(definition, window, cx)
})?
.await
})
.detach_and_log_err(cx);
}
@ -503,6 +460,75 @@ impl DebugPanel {
}
}
pub fn resolve_scenario(
&self,
scenario: DebugScenario,
task_context: TaskContext,
buffer: Option<Entity<Buffer>>,
window: &Window,
cx: &mut Context<Self>,
) -> Task<Result<DebugTaskDefinition>> {
let project = self.project.read(cx);
let dap_store = project.dap_store().downgrade();
let task_store = project.task_store().downgrade();
let workspace = self.workspace.clone();
cx.spawn_in(window, async move |_, cx| {
let DebugScenario {
adapter,
label,
build,
request,
initialize_args,
tcp_connection,
stop_on_entry,
} = scenario;
let request = if let Some(mut request) = request {
// Resolve task variables within the request.
if let DebugRequest::Launch(_) = &mut request {}
request
} else if let Some(build) = build {
let Some(task) = task_store.update(cx, |this, cx| {
this.task_inventory().and_then(|inventory| {
inventory
.read(cx)
.task_template_by_label(buffer, &build, cx)
})
})?
else {
anyhow::bail!("Couldn't find task template for {:?}", build)
};
let Some(task) = task.resolve_task("debug-build-task", &task_context) else {
anyhow::bail!("Could not resolve task variables within a debug scenario");
};
let run_build = workspace.update_in(cx, |workspace, window, cx| {
workspace.spawn_in_terminal(task.resolved.clone(), window, cx)
})?;
let exit_status = run_build.await?;
if !exit_status.success() {
anyhow::bail!("Build failed");
}
dap_store
.update(cx, |this, cx| this.run_debug_locator(task.resolved, cx))?
.await?
} else {
return Err(anyhow!("No request or build provided"));
};
Ok(DebugTaskDefinition {
label,
adapter,
request,
initialize_args,
stop_on_entry,
tcp_connection,
})
})
}
fn handle_run_in_terminal_request(
&self,
session_id: SessionId,
@ -1409,10 +1435,17 @@ impl Render for DebugPanel {
struct DebuggerProvider(Entity<DebugPanel>);
impl workspace::DebuggerProvider for DebuggerProvider {
fn start_session(&self, definition: DebugTaskDefinition, window: &mut Window, cx: &mut App) {
fn start_session(
&self,
definition: DebugScenario,
context: TaskContext,
buffer: Option<Entity<Buffer>>,
window: &mut Window,
cx: &mut App,
) {
self.0.update(cx, |_, cx| {
cx.defer_in(window, |this, window, cx| {
this.start_session(definition, window, cx);
this.start_session(definition, context, buffer, window, cx);
})
})
}

View file

@ -4,14 +4,14 @@ use std::{
path::{Path, PathBuf},
};
use dap::{DapRegistry, DebugRequest};
use dap::{DapRegistry, DebugRequest, adapters::DebugTaskDefinition};
use editor::{Editor, EditorElement, EditorStyle};
use gpui::{
App, AppContext, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Render, TextStyle,
WeakEntity,
};
use settings::Settings;
use task::{DebugTaskDefinition, DebugTaskTemplate, LaunchRequest};
use task::{DebugScenario, LaunchRequest, TaskContext};
use theme::ThemeSettings;
use ui::{
ActiveTheme, Button, ButtonCommon, ButtonSize, CheckboxWithLabel, Clickable, Color, Context,
@ -34,7 +34,7 @@ pub(super) struct NewSessionModal {
last_selected_profile_name: Option<SharedString>,
}
fn suggested_label(request: &DebugRequest, debugger: &str) -> String {
fn suggested_label(request: &DebugRequest, debugger: &str) -> SharedString {
match request {
DebugRequest::Launch(config) => {
let last_path_component = Path::new(&config.program)
@ -42,12 +42,13 @@ fn suggested_label(request: &DebugRequest, debugger: &str) -> String {
.map(|name| name.to_string_lossy())
.unwrap_or_else(|| Cow::Borrowed(&config.program));
format!("{} ({debugger})", last_path_component)
format!("{} ({debugger})", last_path_component).into()
}
DebugRequest::Attach(config) => format!(
"pid: {} ({debugger})",
config.process_id.unwrap_or(u32::MAX)
),
)
.into(),
}
}
@ -61,7 +62,7 @@ impl NewSessionModal {
) -> Self {
let debugger = past_debug_definition
.as_ref()
.map(|def| def.adapter.clone().into());
.map(|def| def.adapter.clone());
let stop_on_entry = past_debug_definition
.as_ref()
@ -85,18 +86,20 @@ impl NewSessionModal {
}
}
fn debug_config(&self, cx: &App, debugger: &str) -> DebugTaskDefinition {
fn debug_config(&self, cx: &App, debugger: &str) -> DebugScenario {
let request = self.mode.debug_task(cx);
DebugTaskDefinition {
adapter: debugger.to_owned(),
label: suggested_label(&request, debugger),
request,
let label = suggested_label(&request, debugger);
DebugScenario {
adapter: debugger.to_owned().into(),
label,
request: Some(request),
initialize_args: self.initialize_args.clone(),
tcp_connection: None,
stop_on_entry: match self.stop_on_entry {
ToggleState::Selected => Some(true),
_ => None,
},
build: None,
}
}
@ -109,36 +112,9 @@ impl NewSessionModal {
let config = self.debug_config(cx, debugger);
let debug_panel = self.debug_panel.clone();
let task_contexts = self
.workspace
.update(cx, |workspace, cx| {
tasks_ui::task_contexts(workspace, window, cx)
})
.ok();
cx.spawn_in(window, async move |this, cx| {
let task_context = if let Some(task) = task_contexts {
task.await
.active_worktree_context
.map_or(task::TaskContext::default(), |context| context.1)
} else {
task::TaskContext::default()
};
debug_panel.update_in(cx, |debug_panel, window, cx| {
let template = DebugTaskTemplate {
locator: None,
definition: config.clone(),
};
if let Some(debug_config) = template
.to_zed_format()
.resolve_task("debug_task", &task_context)
.and_then(|resolved_task| resolved_task.resolved_debug_adapter_config())
{
debug_panel.start_session(debug_config.definition, window, cx)
} else {
debug_panel.start_session(config, window, cx)
}
debug_panel.start_session(config, TaskContext::default(), None, window, cx)
})?;
this.update(cx, |_, cx| {
cx.emit(DismissEvent);
@ -156,12 +132,13 @@ impl NewSessionModal {
cx: &mut App,
) {
attach.update(cx, |this, cx| {
if selected_debugger != this.debug_definition.adapter {
this.debug_definition.adapter = selected_debugger.into();
if selected_debugger != this.definition.adapter.as_ref() {
let adapter: SharedString = selected_debugger.to_owned().into();
this.definition.adapter = adapter.clone();
this.attach_picker.update(cx, |this, cx| {
this.picker.update(cx, |this, cx| {
this.delegate.debug_config.adapter = selected_debugger.into();
this.delegate.definition.adapter = adapter;
this.focus(window, cx);
})
});
@ -224,22 +201,22 @@ impl NewSessionModal {
"debug-config-menu",
last_profile.unwrap_or_else(|| SELECT_SCENARIO_LABEL.clone()),
ContextMenu::build(window, cx, move |mut menu, _, cx| {
let setter_for_name = |task: DebugTaskDefinition| {
let setter_for_name = |task: DebugScenario| {
let weak = weak.clone();
move |window: &mut Window, cx: &mut App| {
weak.update(cx, |this, cx| {
this.last_selected_profile_name = Some(SharedString::from(&task.label));
this.debugger = Some(task.adapter.clone().into());
this.debugger = Some(task.adapter.clone());
this.initialize_args = task.initialize_args.clone();
match &task.request {
DebugRequest::Launch(launch_config) => {
Some(DebugRequest::Launch(launch_config)) => {
this.mode = NewSessionMode::launch(
Some(launch_config.clone()),
window,
cx,
);
}
DebugRequest::Attach(_) => {
Some(DebugRequest::Attach(_)) => {
let Some(workspace) = this.workspace.upgrade() else {
return;
};
@ -256,6 +233,7 @@ impl NewSessionModal {
Self::update_attach_picker(&attach, &debugger, window, cx);
}
}
_ => log::warn!("Selected debug scenario without either attach or launch request specified"),
}
cx.notify();
})
@ -263,7 +241,7 @@ impl NewSessionModal {
}
};
let available_adapters: Vec<DebugTaskTemplate> = workspace
let available_tasks: Vec<DebugScenario> = workspace
.update(cx, |this, cx| {
this.project()
.read(cx)
@ -271,19 +249,19 @@ impl NewSessionModal {
.read(cx)
.task_inventory()
.iter()
.flat_map(|task_inventory| task_inventory.read(cx).list_debug_tasks())
.cloned()
.filter_map(|task| task.try_into().ok())
.flat_map(|task_inventory| {
task_inventory.read(cx).list_debug_scenarios(None)
})
.collect()
})
.ok()
.unwrap_or_default();
for debug_definition in available_adapters {
for debug_definition in available_tasks {
menu = menu.entry(
debug_definition.definition.label.clone(),
debug_definition.label.clone(),
None,
setter_for_name(debug_definition.definition),
setter_for_name(debug_definition),
);
}
menu
@ -332,13 +310,14 @@ impl LaunchMode {
program: self.program.read(cx).text(cx),
cwd: path.is_empty().not().then(|| PathBuf::from(path)),
args: Default::default(),
env: Default::default(),
}
}
}
#[derive(Clone)]
struct AttachMode {
debug_definition: DebugTaskDefinition,
definition: DebugTaskDefinition,
attach_picker: Entity<AttachModal>,
}
@ -349,22 +328,22 @@ impl AttachMode {
window: &mut Window,
cx: &mut Context<NewSessionModal>,
) -> Entity<Self> {
let debug_definition = DebugTaskDefinition {
let definition = DebugTaskDefinition {
adapter: debugger.clone().unwrap_or_default(),
label: "Attach New Session Setup".into(),
request: dap::DebugRequest::Attach(task::AttachRequest { process_id: None }),
tcp_connection: None,
adapter: debugger.clone().unwrap_or_default().into(),
initialize_args: None,
tcp_connection: None,
stop_on_entry: Some(false),
};
let attach_picker = cx.new(|cx| {
let modal = AttachModal::new(workspace, debug_definition.clone(), false, window, cx);
let modal = AttachModal::new(definition.clone(), workspace, false, window, cx);
window.focus(&modal.focus_handle(cx));
modal
});
cx.new(|_| Self {
debug_definition,
definition,
attach_picker,
})
}

View file

@ -33,7 +33,7 @@ impl DebugSessionState {
pub struct DebugSession {
remote_id: Option<workspace::ViewId>,
mode: DebugSessionState,
label: OnceLock<String>,
label: OnceLock<SharedString>,
dap_store: WeakEntity<DapStore>,
_debug_panel: WeakEntity<DebugPanel>,
_worktree_store: WeakEntity<WorktreeStore>,
@ -110,9 +110,9 @@ impl DebugSession {
}
}
pub(crate) fn label(&self, cx: &App) -> String {
pub(crate) fn label(&self, cx: &App) -> SharedString {
if let Some(label) = self.label.get() {
return label.to_owned();
return label.clone();
}
let session_id = match &self.mode {
@ -123,7 +123,7 @@ impl DebugSession {
.dap_store
.read_with(cx, |store, _| store.session_by_id(session_id))
else {
return "".to_owned();
return "".into();
};
self.label

View file

@ -1,11 +1,12 @@
use std::sync::Arc;
use anyhow::{Result, anyhow};
use dap::adapters::DebugTaskDefinition;
use dap::{DebugRequest, client::DebugAdapterClient};
use gpui::{Entity, TestAppContext, WindowHandle};
use project::{Project, debugger::session::Session};
use settings::SettingsStore;
use task::DebugTaskDefinition;
use task::TaskContext;
use terminal_view::terminal_panel::TerminalPanel;
use workspace::Workspace;
@ -104,7 +105,13 @@ pub fn start_debug_session_with<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
) -> Result<Entity<Session>> {
let _subscription = project::debugger::test::intercept_debug_sessions(cx, configure);
workspace.update(cx, |workspace, window, cx| {
workspace.start_debug_session(config, window, cx)
workspace.start_debug_session(
config.to_scenario(),
TaskContext::default(),
None,
window,
cx,
)
})?;
cx.run_until_parked();
let session = workspace.read_with(cx, |workspace, cx| {
@ -128,9 +135,9 @@ pub fn start_debug_session<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
workspace,
cx,
DebugTaskDefinition {
adapter: "fake-adapter".to_string(),
adapter: "fake-adapter".into(),
request: DebugRequest::Launch(Default::default()),
label: "test".to_string(),
label: "test".into(),
initialize_args: None,
tcp_connection: None,
stop_on_entry: None,

View file

@ -1,11 +1,11 @@
use crate::{attach_modal::Candidate, tests::start_debug_session_with, *};
use attach_modal::AttachModal;
use dap::FakeAdapter;
use dap::{FakeAdapter, adapters::DebugTaskDefinition};
use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
use menu::Confirm;
use project::{FakeFs, Project};
use serde_json::json;
use task::{AttachRequest, DebugTaskDefinition, TcpArgumentsTemplate};
use task::{AttachRequest, TcpArgumentsTemplate};
use tests::{init_test, init_test_workspace};
#[gpui::test]
@ -30,11 +30,11 @@ async fn test_direct_attach_to_process(executor: BackgroundExecutor, cx: &mut Te
&workspace,
cx,
DebugTaskDefinition {
adapter: "fake-adapter".to_string(),
adapter: "fake-adapter".into(),
request: dap::DebugRequest::Attach(AttachRequest {
process_id: Some(10),
}),
label: "label".to_string(),
label: "label".into(),
initialize_args: None,
tcp_connection: None,
stop_on_entry: None,
@ -104,6 +104,7 @@ async fn test_show_attach_modal_and_select_process(
workspace_handle,
DebugTaskDefinition {
adapter: FakeAdapter::ADAPTER_NAME.into(),
request: dap::DebugRequest::Attach(AttachRequest::default()),
label: "attach example".into(),
initialize_args: None,