Add task docs and default keybindings (#8123)

Also group task source modules together

Release Notes:

- N/A

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
This commit is contained in:
Kirill Bulatov 2024-02-21 16:43:56 +02:00 committed by GitHub
parent b9151b9506
commit 0c939e5dfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 84 additions and 64 deletions

View file

@ -1,10 +1,8 @@
//! Baseline interface of Tasks in Zed: all tasks in Zed are intended to use those for implementing their own logic.
#![deny(missing_docs)]
pub mod oneshot_source;
pub mod static_source;
mod static_task;
pub use static_task::StaticTask;
use collections::HashMap;
use gpui::ModelContext;

View file

@ -0,0 +1,81 @@
//! A source of tasks, based on ad-hoc user command prompt input.
use std::sync::Arc;
use crate::{Source, SpawnInTerminal, Task, TaskId};
use gpui::{AppContext, Context, Model};
/// A storage and source of tasks generated out of user command prompt inputs.
pub struct OneshotSource {
tasks: Vec<Arc<dyn Task>>,
}
#[derive(Clone)]
struct OneshotTask {
id: TaskId,
}
impl OneshotTask {
fn new(prompt: String) -> Self {
Self { id: TaskId(prompt) }
}
}
impl Task for OneshotTask {
fn id(&self) -> &TaskId {
&self.id
}
fn name(&self) -> &str {
&self.id.0
}
fn cwd(&self) -> Option<&std::path::Path> {
None
}
fn exec(&self, cwd: Option<std::path::PathBuf>) -> Option<SpawnInTerminal> {
if self.id().0.is_empty() {
return None;
}
Some(SpawnInTerminal {
id: self.id().clone(),
label: self.name().to_owned(),
command: self.id().0.clone(),
args: vec![],
cwd,
env: Default::default(),
use_new_terminal: Default::default(),
allow_concurrent_runs: Default::default(),
separate_shell: true,
})
}
}
impl OneshotSource {
/// Initializes the oneshot source, preparing to store user prompts.
pub fn new(cx: &mut AppContext) -> Model<Box<dyn Source>> {
cx.new_model(|_| Box::new(Self { tasks: Vec::new() }) as Box<dyn Source>)
}
/// Spawns a certain task based on the user prompt.
pub fn spawn(&mut self, prompt: String) -> Arc<dyn Task> {
let ret = Arc::new(OneshotTask::new(prompt));
self.tasks.push(ret.clone());
ret
}
}
impl Source for OneshotSource {
fn as_any(&mut self) -> &mut dyn std::any::Any {
self
}
fn tasks_for_path(
&mut self,
_path: Option<&std::path::Path>,
_cx: &mut gpui::ModelContext<Box<dyn Source>>,
) -> Vec<Arc<dyn Task>> {
self.tasks.clone()
}
}

View file

@ -12,9 +12,53 @@ use schemars::{gen::SchemaSettings, JsonSchema};
use serde::{Deserialize, Serialize};
use util::ResultExt;
use crate::{Source, StaticTask, Task};
use crate::{Source, SpawnInTerminal, Task, TaskId};
use futures::channel::mpsc::UnboundedReceiver;
/// A single config file entry with the deserialized task definition.
#[derive(Clone, Debug, PartialEq)]
struct StaticTask {
id: TaskId,
definition: Definition,
}
impl StaticTask {
pub(super) fn new(id: usize, task_definition: Definition) -> Self {
Self {
id: TaskId(format!("static_{}_{}", task_definition.label, id)),
definition: task_definition,
}
}
}
impl Task for StaticTask {
fn exec(&self, cwd: Option<PathBuf>) -> Option<SpawnInTerminal> {
Some(SpawnInTerminal {
id: self.id.clone(),
cwd,
use_new_terminal: self.definition.use_new_terminal,
allow_concurrent_runs: self.definition.allow_concurrent_runs,
label: self.definition.label.clone(),
command: self.definition.command.clone(),
args: self.definition.args.clone(),
env: self.definition.env.clone(),
separate_shell: false,
})
}
fn name(&self) -> &str {
&self.definition.label
}
fn id(&self) -> &TaskId {
&self.id
}
fn cwd(&self) -> Option<&Path> {
self.definition.cwd.as_deref()
}
}
/// The source of tasks defined in a tasks config file.
pub struct StaticSource {
tasks: Vec<StaticTask>,

View file

@ -1,49 +0,0 @@
//! Definitions of tasks with a static file config definition, not dependent on the application state.
use std::path::{Path, PathBuf};
use crate::{static_source::Definition, SpawnInTerminal, Task, TaskId};
/// A single config file entry with the deserialized task definition.
#[derive(Clone, Debug, PartialEq)]
pub struct StaticTask {
id: TaskId,
definition: Definition,
}
impl StaticTask {
pub(super) fn new(id: usize, task_definition: Definition) -> Self {
Self {
id: TaskId(format!("static_{}_{}", task_definition.label, id)),
definition: task_definition,
}
}
}
impl Task for StaticTask {
fn exec(&self, cwd: Option<PathBuf>) -> Option<SpawnInTerminal> {
Some(SpawnInTerminal {
id: self.id.clone(),
cwd,
use_new_terminal: self.definition.use_new_terminal,
allow_concurrent_runs: self.definition.allow_concurrent_runs,
label: self.definition.label.clone(),
command: self.definition.command.clone(),
args: self.definition.args.clone(),
env: self.definition.env.clone(),
separate_shell: false,
})
}
fn name(&self) -> &str {
&self.definition.label
}
fn id(&self) -> &TaskId {
&self.id
}
fn cwd(&self) -> Option<&Path> {
self.definition.cwd.as_deref()
}
}