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