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/// Represents a short lived recipe of a task, whose main purpose
40/// is to get spawned.
41pub trait Task {
42    /// Unique identifier of the task to spawn.
43    fn id(&self) -> &TaskId;
44    /// Human readable name of the task to display in the UI.
45    fn name(&self) -> &str;
46    /// Task's current working directory. If `None`, current project's root will be used.
47    fn cwd(&self) -> Option<&Path>;
48    /// Sets up everything needed to spawn the task in the given directory (`cwd`).
49    /// If a task is intended to be spawned in the terminal, it should return the corresponding struct filled with the data necessary.
50    fn exec(&self, cwd: Option<PathBuf>) -> Option<SpawnInTerminal>;
51}
52
53/// [`Source`] produces tasks that can be scheduled.
54///
55/// Implementations of this trait could be e.g. [`StaticSource`] that parses tasks from a .json files and provides process templates to be spawned;
56/// another one could be a language server providing lenses with tests or build server listing all targets for a given project.
57pub trait TaskSource: Any {
58    /// A way to erase the type of the source, processing and storing them generically.
59    fn as_any(&mut self) -> &mut dyn Any;
60    /// Collects all tasks available for scheduling, for the path given.
61    fn tasks_for_path(
62        &mut self,
63        path: Option<&Path>,
64        cx: &mut ModelContext<Box<dyn TaskSource>>,
65    ) -> Vec<Arc<dyn Task>>;
66}