Move task centering code closer to user input (#22082)

Follow-up of https://github.com/zed-industries/zed/pull/22004 

* Reuse center terminals for tasks, when requested
* Extend task templates with `RevealTarget`, moving it from
`TaskSpawnTarget` into the core library
* Use `reveal_target` instead of `target` to avoid misinterpretations in
the task template context
* Do not expose `SpawnInTerminal` to user interface, avoid it
implementing `Serialize` and `Deserialize`
* Remove `NewCenterTask` action, extending `task::Spawn` interface
instead
* Do not require any extra unrelated parameters during task resolution,
instead, use task overrides on the resolved tasks on the modal side
* Add keybindings for opening the task modal in the
`RevealTarget::Center` mode

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2024-12-16 16:15:58 +02:00 committed by GitHub
parent ea012075fc
commit bc113e4b51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 356 additions and 285 deletions

View file

@ -1,9 +1,9 @@
use ::settings::Settings;
use editor::{tasks::task_context, Editor};
use gpui::{AppContext, Task as AsyncTask, ViewContext, WindowContext};
use modal::TasksModal;
use modal::{TaskOverrides, TasksModal};
use project::{Location, WorktreeId};
use task::TaskId;
use task::{RevealTarget, TaskId};
use workspace::tasks::schedule_task;
use workspace::{tasks::schedule_resolved_task, Workspace};
@ -11,7 +11,6 @@ mod modal;
mod settings;
pub use modal::{Rerun, Spawn};
use zed_actions::TaskSpawnTarget;
pub fn init(cx: &mut AppContext) {
settings::TaskSettings::register(cx);
@ -54,7 +53,6 @@ pub fn init(cx: &mut AppContext) {
task_source_kind,
&original_task,
&task_context,
Default::default(),
false,
cx,
)
@ -81,7 +79,7 @@ pub fn init(cx: &mut AppContext) {
);
}
} else {
toggle_modal(workspace, cx).detach();
toggle_modal(workspace, None, cx).detach();
};
});
},
@ -90,14 +88,25 @@ pub fn init(cx: &mut AppContext) {
}
fn spawn_task_or_modal(workspace: &mut Workspace, action: &Spawn, cx: &mut ViewContext<Workspace>) {
match &action.task_name {
Some(name) => spawn_task_with_name(name.clone(), action.target.unwrap_or_default(), cx)
.detach_and_log_err(cx),
None => toggle_modal(workspace, cx).detach(),
match action {
Spawn::ByName {
task_name,
reveal_target,
} => {
let overrides = reveal_target.map(|reveal_target| TaskOverrides {
reveal_target: Some(reveal_target),
});
spawn_task_with_name(task_name.clone(), overrides, cx).detach_and_log_err(cx)
}
Spawn::ViaModal { reveal_target } => toggle_modal(workspace, *reveal_target, cx).detach(),
}
}
fn toggle_modal(workspace: &mut Workspace, cx: &mut ViewContext<'_, Workspace>) -> AsyncTask<()> {
fn toggle_modal(
workspace: &mut Workspace,
reveal_target: Option<RevealTarget>,
cx: &mut ViewContext<'_, Workspace>,
) -> AsyncTask<()> {
let task_store = workspace.project().read(cx).task_store().clone();
let workspace_handle = workspace.weak_handle();
let can_open_modal = workspace.project().update(cx, |project, cx| {
@ -110,7 +119,15 @@ fn toggle_modal(workspace: &mut Workspace, cx: &mut ViewContext<'_, Workspace>)
workspace
.update(&mut cx, |workspace, cx| {
workspace.toggle_modal(cx, |cx| {
TasksModal::new(task_store.clone(), task_context, workspace_handle, cx)
TasksModal::new(
task_store.clone(),
task_context,
reveal_target.map(|target| TaskOverrides {
reveal_target: Some(target),
}),
workspace_handle,
cx,
)
})
})
.ok();
@ -122,7 +139,7 @@ fn toggle_modal(workspace: &mut Workspace, cx: &mut ViewContext<'_, Workspace>)
fn spawn_task_with_name(
name: String,
task_target: TaskSpawnTarget,
overrides: Option<TaskOverrides>,
cx: &mut ViewContext<Workspace>,
) -> AsyncTask<anyhow::Result<()>> {
cx.spawn(|workspace, mut cx| async move {
@ -157,14 +174,18 @@ fn spawn_task_with_name(
let did_spawn = workspace
.update(&mut cx, |workspace, cx| {
let (task_source_kind, target_task) =
let (task_source_kind, mut target_task) =
tasks.into_iter().find(|(_, task)| task.label == name)?;
if let Some(overrides) = &overrides {
if let Some(target_override) = overrides.reveal_target {
target_task.reveal_target = target_override;
}
}
schedule_task(
workspace,
task_source_kind,
&target_task,
&task_context,
task_target,
false,
cx,
);
@ -174,7 +195,13 @@ fn spawn_task_with_name(
if !did_spawn {
workspace
.update(&mut cx, |workspace, cx| {
spawn_task_or_modal(workspace, &Spawn::default(), cx);
spawn_task_or_modal(
workspace,
&Spawn::ViaModal {
reveal_target: overrides.and_then(|overrides| overrides.reveal_target),
},
cx,
);
})
.ok();
}