From 0c939e5dfc1b297dd033ffd9c46a02b815275d54 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 21 Feb 2024 16:43:56 +0200 Subject: [PATCH] Add task docs and default keybindings (#8123) Also group task source modules together Release Notes: - N/A --------- Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> --- assets/keymaps/default-linux.json | 4 +- assets/keymaps/default-macos.json | 4 +- crates/task/src/lib.rs | 4 +- .../{tasks_ui => task}/src/oneshot_source.rs | 10 ++-- crates/task/src/static_source.rs | 46 ++++++++++++++++- crates/task/src/static_task.rs | 49 ------------------- crates/tasks_ui/src/lib.rs | 2 - crates/tasks_ui/src/modal.rs | 4 +- crates/zed/src/zed.rs | 3 +- docs/src/SUMMARY.md | 1 + docs/src/tasks.md | 21 ++++++++ 11 files changed, 84 insertions(+), 64 deletions(-) rename crates/{tasks_ui => task}/src/oneshot_source.rs (81%) delete mode 100644 crates/task/src/static_task.rs create mode 100644 docs/src/tasks.md diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.json index 221f24dc33e6ce1043e9818e3852ea8610e8aa0e..46e6f87df9ef9ada68e03b005b550fe00fa4af67 100644 --- a/assets/keymaps/default-linux.json +++ b/assets/keymaps/default-linux.json @@ -381,7 +381,9 @@ "ctrl-k shift-left": ["workspace::SwapPaneInDirection", "Left"], "ctrl-k shift-right": ["workspace::SwapPaneInDirection", "Right"], "ctrl-k shift-up": ["workspace::SwapPaneInDirection", "Up"], - "ctrl-k shift-down": ["workspace::SwapPaneInDirection", "Down"] + "ctrl-k shift-down": ["workspace::SwapPaneInDirection", "Down"], + "alt-t": "task::Rerun", + "alt-shift-t": "task::Spawn" } }, // Bindings from Sublime Text diff --git a/assets/keymaps/default-macos.json b/assets/keymaps/default-macos.json index 6e2b96b338767a78e83264e6e4e64e5b959bf9cc..6e3839eb407b3352ea3fd2bbfff531d56c2a9e42 100644 --- a/assets/keymaps/default-macos.json +++ b/assets/keymaps/default-macos.json @@ -423,7 +423,9 @@ "cmd-k shift-left": ["workspace::SwapPaneInDirection", "Left"], "cmd-k shift-right": ["workspace::SwapPaneInDirection", "Right"], "cmd-k shift-up": ["workspace::SwapPaneInDirection", "Up"], - "cmd-k shift-down": ["workspace::SwapPaneInDirection", "Down"] + "cmd-k shift-down": ["workspace::SwapPaneInDirection", "Down"], + "alt-t": "task::Rerun", + "alt-shift-t": "task::Spawn" } }, // Bindings from Sublime Text diff --git a/crates/task/src/lib.rs b/crates/task/src/lib.rs index 8b96c46c1242cddf91964275cd451714a510c806..b008c7e836c124e3a3c4a1f2a0cbd59be40f7aaa 100644 --- a/crates/task/src/lib.rs +++ b/crates/task/src/lib.rs @@ -1,10 +1,8 @@ //! Baseline interface of Tasks in Zed: all tasks in Zed are intended to use those for implementing their own logic. #![deny(missing_docs)] +pub mod oneshot_source; pub mod static_source; -mod static_task; - -pub use static_task::StaticTask; use collections::HashMap; use gpui::ModelContext; diff --git a/crates/tasks_ui/src/oneshot_source.rs b/crates/task/src/oneshot_source.rs similarity index 81% rename from crates/tasks_ui/src/oneshot_source.rs rename to crates/task/src/oneshot_source.rs index 7245adf5bb4d98f6efa1f299d8de178ca1efa8ae..0c874a4d3e128462c7741173ca1b40564384b69e 100644 --- a/crates/tasks_ui/src/oneshot_source.rs +++ b/crates/task/src/oneshot_source.rs @@ -1,9 +1,11 @@ +//! A source of tasks, based on ad-hoc user command prompt input. + use std::sync::Arc; -use gpui::{AppContext, Model}; -use task::{Source, SpawnInTerminal, Task, TaskId}; -use ui::Context; +use crate::{Source, SpawnInTerminal, Task, TaskId}; +use gpui::{AppContext, Context, Model}; +/// A storage and source of tasks generated out of user command prompt inputs. pub struct OneshotSource { tasks: Vec>, } @@ -51,10 +53,12 @@ impl Task for OneshotTask { } impl OneshotSource { + /// Initializes the oneshot source, preparing to store user prompts. pub fn new(cx: &mut AppContext) -> Model> { cx.new_model(|_| Box::new(Self { tasks: Vec::new() }) as Box) } + /// Spawns a certain task based on the user prompt. pub fn spawn(&mut self, prompt: String) -> Arc { let ret = Arc::new(OneshotTask::new(prompt)); self.tasks.push(ret.clone()); diff --git a/crates/task/src/static_source.rs b/crates/task/src/static_source.rs index e36f7d53ef0c7332fc5f21a36558ceef69b2b031..2f7f7b4d74422f70fd4d0c71417f19c1c96f17bc 100644 --- a/crates/task/src/static_source.rs +++ b/crates/task/src/static_source.rs @@ -12,9 +12,53 @@ use schemars::{gen::SchemaSettings, JsonSchema}; use serde::{Deserialize, Serialize}; use util::ResultExt; -use crate::{Source, StaticTask, Task}; +use crate::{Source, SpawnInTerminal, Task, TaskId}; use futures::channel::mpsc::UnboundedReceiver; +/// A single config file entry with the deserialized task definition. +#[derive(Clone, Debug, PartialEq)] +struct StaticTask { + id: TaskId, + definition: Definition, +} + +impl StaticTask { + pub(super) fn new(id: usize, task_definition: Definition) -> Self { + Self { + id: TaskId(format!("static_{}_{}", task_definition.label, id)), + definition: task_definition, + } + } +} + +impl Task for StaticTask { + fn exec(&self, cwd: Option) -> Option { + Some(SpawnInTerminal { + id: self.id.clone(), + cwd, + use_new_terminal: self.definition.use_new_terminal, + allow_concurrent_runs: self.definition.allow_concurrent_runs, + label: self.definition.label.clone(), + command: self.definition.command.clone(), + args: self.definition.args.clone(), + env: self.definition.env.clone(), + separate_shell: false, + }) + } + + fn name(&self) -> &str { + &self.definition.label + } + + fn id(&self) -> &TaskId { + &self.id + } + + fn cwd(&self) -> Option<&Path> { + self.definition.cwd.as_deref() + } +} + /// The source of tasks defined in a tasks config file. pub struct StaticSource { tasks: Vec, diff --git a/crates/task/src/static_task.rs b/crates/task/src/static_task.rs deleted file mode 100644 index 6976b3b2eec4494db7e23f5f8a1d89e9c3ff170a..0000000000000000000000000000000000000000 --- a/crates/task/src/static_task.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! Definitions of tasks with a static file config definition, not dependent on the application state. - -use std::path::{Path, PathBuf}; - -use crate::{static_source::Definition, SpawnInTerminal, Task, TaskId}; - -/// A single config file entry with the deserialized task definition. -#[derive(Clone, Debug, PartialEq)] -pub struct StaticTask { - id: TaskId, - definition: Definition, -} - -impl StaticTask { - pub(super) fn new(id: usize, task_definition: Definition) -> Self { - Self { - id: TaskId(format!("static_{}_{}", task_definition.label, id)), - definition: task_definition, - } - } -} - -impl Task for StaticTask { - fn exec(&self, cwd: Option) -> Option { - Some(SpawnInTerminal { - id: self.id.clone(), - cwd, - use_new_terminal: self.definition.use_new_terminal, - allow_concurrent_runs: self.definition.allow_concurrent_runs, - label: self.definition.label.clone(), - command: self.definition.command.clone(), - args: self.definition.args.clone(), - env: self.definition.env.clone(), - separate_shell: false, - }) - } - - fn name(&self) -> &str { - &self.definition.label - } - - fn id(&self) -> &TaskId { - &self.id - } - - fn cwd(&self) -> Option<&Path> { - self.definition.cwd.as_deref() - } -} diff --git a/crates/tasks_ui/src/lib.rs b/crates/tasks_ui/src/lib.rs index 5db7b254786a2aa78b0dbb8a2c1d567e31294ce9..7de495f2ae14ebdad26a982d780c0548378b9a7b 100644 --- a/crates/tasks_ui/src/lib.rs +++ b/crates/tasks_ui/src/lib.rs @@ -2,13 +2,11 @@ use std::path::PathBuf; use gpui::{AppContext, ViewContext, WindowContext}; use modal::TasksModal; -pub use oneshot_source::OneshotSource; use task::Task; use util::ResultExt; use workspace::Workspace; mod modal; -mod oneshot_source; pub fn init(cx: &mut AppContext) { cx.observe_new_views( diff --git a/crates/tasks_ui/src/modal.rs b/crates/tasks_ui/src/modal.rs index 36f188e2722aecf36210468862392e44b44ea354..af2b3813a1e48ac56a26078bb711033ae11d3678 100644 --- a/crates/tasks_ui/src/modal.rs +++ b/crates/tasks_ui/src/modal.rs @@ -8,12 +8,12 @@ use gpui::{ }; use picker::{Picker, PickerDelegate}; use project::Inventory; -use task::Task; +use task::{oneshot_source::OneshotSource, Task}; use ui::{v_flex, HighlightedLabel, ListItem, ListItemSpacing, Selectable}; use util::ResultExt; use workspace::{ModalView, Workspace}; -use crate::{schedule_task, OneshotSource}; +use crate::schedule_task; actions!(task, [Spawn, Rerun]); diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 57850834a4a38325f7a57093344c8ed00dce5368..5c2d8381ebac714cd5e9dfc3e4c55f9e1d299415 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -28,8 +28,7 @@ use settings::{ DEFAULT_KEYMAP_PATH, }; use std::{borrow::Cow, ops::Deref, path::Path, sync::Arc}; -use task::static_source::StaticSource; -use tasks_ui::OneshotSource; +use task::{oneshot_source::OneshotSource, static_source::StaticSource}; use terminal_view::terminal_panel::{self, TerminalPanel}; use util::{ asset_str, diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 06dc5015dd533fb5b3e62b87c089bf0b6e85c314..0935366be4fd008310d0a8de7e35886ae0d3823f 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -14,6 +14,7 @@ - [Workflows]() - [Collaboration]() - [Using AI]() +- [Tasks](./tasks.md) # Contributing to Zed diff --git a/docs/src/tasks.md b/docs/src/tasks.md new file mode 100644 index 0000000000000000000000000000000000000000..688551ec85ca9d5cfa2600180e99afeef8c2954a --- /dev/null +++ b/docs/src/tasks.md @@ -0,0 +1,21 @@ +# Tasks + +Zed supports ways to spawn (and rerun) commands using its integrated terminal to output the results. + +Currently, two kinds of tasks are supported, but more will be added in the future. + +## Static tasks + +Tasks, defined in a config file (`tasks.json` in the Zed config directory) that do not depend on the current editor or its content. + +Config file can be opened with `zed::OpenTasks` action ("zed: open tasks" in the command palette), it will have a configuration example with all options commented. + +Every task from that file can be spawned via the task modal, that is opened with `task::Spawn` action ("tasks: spawn" in the command pane). + +Last task spawned via that modal can be rerun with `task::Rerun` ("tasks: rerun" in the command palette) command. + +## Oneshot tasks + +Same task modal opened via `task::Spawn` supports arbitrary bash-like command execution: type a command inside the modal, and use `cmd-enter` to spawn it. + +Task modal will persist list of those command for current Zed session, `task::Rerun` will also rerun such tasks if they were the last ones spawned.