tasks: Add experimental support for user-defined task variables (#13699)

Context:
@bennetbo spotted a regression in handling of `cargo run` task in zed
repo following a merge of #13658. We've started invoking `cargo run`
from the folder of an active file whereas previously we did it from the
workspace root. We brainstormed few solutions that involved adding a
separate task that gets invoked at a workspace level, but I realized
that a cleaner solution may be to finally add user-configured task
variables. This way, we can choose which crate to run by default at a
workspace level.

This has been originally brought up in the context of javascript tasks
in
https://github.com/zed-industries/zed/pull/12118#issuecomment-2129232114

Note that this is intended for internal use only for the time being.
/cc @RemcoSmitsDev we should be unblocked on having runner-dependant
tasks now.

Release notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-07-01 15:59:19 +02:00 committed by GitHub
parent 065ab93ca7
commit bac6e2fee7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 108 additions and 25 deletions

View file

@ -15,7 +15,7 @@ use futures::{
};
use gpui::{AppContext, Context, Model, ModelContext, Task};
use itertools::Itertools;
use language::{ContextProvider, Language, Location};
use language::{ContextProvider, File, Language, Location};
use task::{
static_source::StaticSource, ResolvedTask, TaskContext, TaskId, TaskTemplate, TaskTemplates,
TaskVariables, VariableName,
@ -155,14 +155,16 @@ impl Inventory {
/// returns all task templates with their source kinds, in no specific order.
pub fn list_tasks(
&self,
file: Option<Arc<dyn File>>,
language: Option<Arc<Language>>,
worktree: Option<WorktreeId>,
cx: &AppContext,
) -> Vec<(TaskSourceKind, TaskTemplate)> {
let task_source_kind = language.as_ref().map(|language| TaskSourceKind::Language {
name: language.name(),
});
let language_tasks = language
.and_then(|language| language.context_provider()?.associated_tasks())
.and_then(|language| language.context_provider()?.associated_tasks(file, cx))
.into_iter()
.flat_map(|tasks| tasks.0.into_iter())
.flat_map(|task| Some((task_source_kind.as_ref()?, task)));
@ -207,8 +209,11 @@ impl Inventory {
let task_source_kind = language.as_ref().map(|language| TaskSourceKind::Language {
name: language.name(),
});
let file = location
.as_ref()
.and_then(|location| location.buffer.read(cx).file().cloned());
let language_tasks = language
.and_then(|language| language.context_provider()?.associated_tasks())
.and_then(|language| language.context_provider()?.associated_tasks(file, cx))
.into_iter()
.flat_map(|tasks| tasks.0.into_iter())
.flat_map(|task| Some((task_source_kind.as_ref()?, task)));
@ -471,9 +476,9 @@ mod test_inventory {
worktree: Option<WorktreeId>,
cx: &mut TestAppContext,
) -> Vec<String> {
inventory.update(cx, |inventory, _| {
inventory.update(cx, |inventory, cx| {
inventory
.list_tasks(None, worktree)
.list_tasks(None, None, worktree, cx)
.into_iter()
.map(|(_, task)| task.label)
.sorted()
@ -486,9 +491,9 @@ mod test_inventory {
task_name: &str,
cx: &mut TestAppContext,
) {
inventory.update(cx, |inventory, _| {
inventory.update(cx, |inventory, cx| {
let (task_source_kind, task) = inventory
.list_tasks(None, None)
.list_tasks(None, None, None, cx)
.into_iter()
.find(|(_, task)| task.label == task_name)
.unwrap_or_else(|| panic!("Failed to find task with name {task_name}"));
@ -639,7 +644,11 @@ impl ContextProviderWithTasks {
}
impl ContextProvider for ContextProviderWithTasks {
fn associated_tasks(&self) -> Option<TaskTemplates> {
fn associated_tasks(
&self,
_: Option<Arc<dyn language::File>>,
_: &AppContext,
) -> Option<TaskTemplates> {
Some(self.templates.clone())
}
}