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