lib.rs

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