1//! Baseline interface of Tasks in Zed: all tasks in Zed are intended to use those for implementing their own logic.
2#![deny(missing_docs)]
3
4pub mod oneshot_source;
5pub mod static_source;
6
7use collections::HashMap;
8use gpui::ModelContext;
9use static_source::RevealStrategy;
10use std::any::Any;
11use std::path::{Path, PathBuf};
12use std::sync::Arc;
13
14/// Task identifier, unique within the application.
15/// Based on it, task reruns and terminal tabs are managed.
16#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17pub struct TaskId(pub String);
18
19/// Contains all information needed by Zed to spawn a new terminal tab for the given task.
20#[derive(Debug, Clone)]
21pub struct SpawnInTerminal {
22 /// Id of the task to use when determining task tab affinity.
23 pub id: TaskId,
24 /// Human readable name of the terminal tab.
25 pub label: String,
26 /// Executable command to spawn.
27 pub command: String,
28 /// Arguments to the command.
29 pub args: Vec<String>,
30 /// Current working directory to spawn the command into.
31 pub cwd: Option<PathBuf>,
32 /// Env overrides for the command, will be appended to the terminal's environment from the settings.
33 pub env: HashMap<String, String>,
34 /// Whether to use a new terminal tab or reuse the existing one to spawn the process.
35 pub use_new_terminal: bool,
36 /// Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish.
37 pub allow_concurrent_runs: bool,
38 /// What to do with the terminal pane and tab, after the command was started.
39 pub reveal: RevealStrategy,
40}
41
42/// Keeps track of the file associated with a task and context of tasks execution (i.e. current file or current function)
43#[derive(Clone, Debug, Default, PartialEq, Eq)]
44pub struct TaskContext {
45 /// A path to a directory in which the task should be executed.
46 pub cwd: Option<PathBuf>,
47 /// Additional environment variables associated with a given task.
48 pub env: HashMap<String, String>,
49}
50
51/// Represents a short lived recipe of a task, whose main purpose
52/// is to get spawned.
53pub trait Task {
54 /// Unique identifier of the task to spawn.
55 fn id(&self) -> &TaskId;
56 /// Human readable name of the task to display in the UI.
57 fn name(&self) -> &str;
58 /// Task's current working directory. If `None`, current project's root will be used.
59 fn cwd(&self) -> Option<&str>;
60 /// Sets up everything needed to spawn the task in the given directory (`cwd`).
61 /// If a task is intended to be spawned in the terminal, it should return the corresponding struct filled with the data necessary.
62 fn exec(&self, cx: TaskContext) -> Option<SpawnInTerminal>;
63}
64
65/// [`Source`] produces tasks that can be scheduled.
66///
67/// Implementations of this trait could be e.g. [`StaticSource`] that parses tasks from a .json files and provides process templates to be spawned;
68/// another one could be a language server providing lenses with tests or build server listing all targets for a given project.
69pub trait TaskSource: Any {
70 /// A way to erase the type of the source, processing and storing them generically.
71 fn as_any(&mut self) -> &mut dyn Any;
72 /// Collects all tasks available for scheduling, for the path given.
73 fn tasks_for_path(
74 &mut self,
75 path: Option<&Path>,
76 cx: &mut ModelContext<Box<dyn TaskSource>>,
77 ) -> Vec<Arc<dyn Task>>;
78}