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

@ -2,9 +2,10 @@ use anyhow::{anyhow, bail, Context, Result};
use async_compression::futures::bufread::GzipDecoder;
use async_trait::async_trait;
use futures::{io::BufReader, StreamExt};
use gpui::AsyncAppContext;
use gpui::{AppContext, AsyncAppContext};
use http::github::{latest_github_release, GitHubLspBinaryVersion};
pub use language::*;
use language_settings::all_language_settings;
use lazy_static::lazy_static;
use lsp::LanguageServerBinary;
use project::project_settings::{BinarySettings, ProjectSettings};
@ -407,7 +408,22 @@ impl ContextProvider for RustContextProvider {
Ok(TaskVariables::default())
}
fn associated_tasks(&self) -> Option<TaskTemplates> {
fn associated_tasks(
&self,
file: Option<Arc<dyn language::File>>,
cx: &AppContext,
) -> Option<TaskTemplates> {
const DEFAULT_RUN_NAME_STR: &'static str = "RUST_DEFAULT_PACKAGE_RUN";
let package_to_run = all_language_settings(file.as_ref(), cx)
.language(Some("Rust"))
.tasks
.variables
.get(DEFAULT_RUN_NAME_STR);
let run_task_args = if let Some(package_to_run) = package_to_run {
vec!["run".into(), "-p".into(), package_to_run.clone()]
} else {
vec!["run".into()]
};
Some(TaskTemplates(vec![
TaskTemplate {
label: format!(
@ -501,7 +517,7 @@ impl ContextProvider for RustContextProvider {
TaskTemplate {
label: "cargo run".into(),
command: "cargo".into(),
args: vec!["run".into()],
args: run_task_args,
cwd: Some("$ZED_DIRNAME".to_owned()),
..TaskTemplate::default()
},