task: Add task contexts (#8675)

This PR supplements tasks with additional environment variables; ideally
we'll be able to write a task like:
`cargo test -p $ZED_CURRENT_PACKAGE -- $ZED_CURRENT_FUNCTION`
- [x] Flesh out multibuffer interactions
- [x] Add ZED_SYMBOL detection based on tree-sitter queries
- [ ] Add release note and demo
- [x] Figure out a solution for rerun dilemma - should `task: rerun`
reevaluate contexts for tasks?

This PR introduced the following variables:
- ZED_COLUMN - current line column
- ZED_ROW - current line row
and the following, which are available for buffers with associated
files:
- ZED_WORKTREE_ROOT - absolute path to the root of the current worktree.
- ZED_FILE - absolute path to the file
- ZED_SYMBOL - currently selected symbol; should match the last symbol
shown in a symbol breadcrumb (e.g. `mod tests > fn test_task_contexts`
should be equal to ZED_SYMBOL of `test_task_contexts`). Note that this
isn't necessarily a test function or a function at all.

Also, you can use them in `cwd` field of definitions (note though that
we're using https://docs.rs/subst/latest/subst/#features for that, so
don't expect a full shell functionality to work); the syntax should
match up with your typical Unix shell.


Release Notes:

- Added task contexts, which are additional environment variables set by
Zed for task execution; task content is dependent on the state of the
editor at the time the task is spawned.

---------

Co-authored-by: Anthony <anthonyeid7@protonmail.com>
This commit is contained in:
Piotr Osiewicz 2024-03-04 21:04:53 +01:00 committed by GitHub
parent b2f18cfe71
commit 2201b9b116
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 623 additions and 190 deletions

View file

@ -2,23 +2,36 @@ use std::{path::PathBuf, sync::Arc};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
actions, rems, AppContext, DismissEvent, EventEmitter, FocusableView, InteractiveElement,
Model, ParentElement, Render, SharedString, Styled, Subscription, View, ViewContext,
VisualContext, WeakView,
actions, impl_actions, rems, AppContext, DismissEvent, EventEmitter, FocusableView,
InteractiveElement, Model, ParentElement, Render, SharedString, Styled, Subscription, View,
ViewContext, VisualContext, WeakView,
};
use picker::{
highlighted_match_with_paths::{HighlightedMatchWithPaths, HighlightedText},
Picker, PickerDelegate,
};
use project::{Inventory, ProjectPath, TaskSourceKind};
use task::{oneshot_source::OneshotSource, Task};
use task::{oneshot_source::OneshotSource, Task, TaskContext};
use ui::{v_flex, ListItem, ListItemSpacing, RenderOnce, Selectable, WindowContext};
use util::{paths::PathExt, ResultExt};
use workspace::{ModalView, Workspace};
use crate::schedule_task;
use serde::Deserialize;
actions!(task, [Spawn]);
actions!(task, [Spawn, Rerun]);
/// Rerun last task
#[derive(PartialEq, Clone, Deserialize, Default)]
pub struct Rerun {
#[serde(default)]
/// Controls whether the task context is reevaluated prior to execution of a task.
/// If it is not, environment variables such as ZED_COLUMN, ZED_FILE are gonna be the same as in the last execution of a task
/// If it is, these variables will be updated to reflect current state of editor at the time task::Rerun is executed.
/// default: false
pub reevaluate_context: bool,
}
impl_actions!(task, [Rerun]);
/// A modal used to spawn new tasks.
pub(crate) struct TasksModalDelegate {
@ -28,10 +41,15 @@ pub(crate) struct TasksModalDelegate {
selected_index: usize,
workspace: WeakView<Workspace>,
prompt: String,
task_context: TaskContext,
}
impl TasksModalDelegate {
fn new(inventory: Model<Inventory>, workspace: WeakView<Workspace>) -> Self {
fn new(
inventory: Model<Inventory>,
task_context: TaskContext,
workspace: WeakView<Workspace>,
) -> Self {
Self {
inventory,
workspace,
@ -39,6 +57,7 @@ impl TasksModalDelegate {
matches: Vec::new(),
selected_index: 0,
prompt: String::default(),
task_context,
}
}
@ -79,11 +98,16 @@ pub(crate) struct TasksModal {
impl TasksModal {
pub(crate) fn new(
inventory: Model<Inventory>,
task_context: TaskContext,
workspace: WeakView<Workspace>,
cx: &mut ViewContext<Self>,
) -> Self {
let picker = cx
.new_view(|cx| Picker::uniform_list(TasksModalDelegate::new(inventory, workspace), cx));
let picker = cx.new_view(|cx| {
Picker::uniform_list(
TasksModalDelegate::new(inventory, task_context, workspace),
cx,
)
});
let _subscription = cx.subscribe(&picker, |_, _, _, cx| {
cx.emit(DismissEvent);
});
@ -223,7 +247,7 @@ impl PickerDelegate for TasksModalDelegate {
self.workspace
.update(cx, |workspace, cx| {
schedule_task(workspace, task.as_ref(), cx);
schedule_task(workspace, task.as_ref(), self.task_context.clone(), cx);
})
.ok();
cx.emit(DismissEvent);
@ -279,13 +303,12 @@ mod tests {
use gpui::{TestAppContext, VisualTestContext};
use project::{FakeFs, Project};
use serde_json::json;
use workspace::AppState;
use super::*;
#[gpui::test]
async fn test_spawn_tasks_modal_query_reuse(cx: &mut TestAppContext) {
init_test(cx);
crate::tests::init_test(cx);
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
"/dir",
@ -431,16 +454,4 @@ mod tests {
.collect::<Vec<_>>()
})
}
fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
cx.update(|cx| {
let state = AppState::test(cx);
language::init(cx);
crate::init(cx);
editor::init(cx);
workspace::init_settings(cx);
Project::init_settings(cx);
state
})
}
}