Stricten Zed Task variable API (#10163)

Introduce `VariableName` enum to simplify Zed task templating
management: now all the variables can be looked up statically and can be
checked/modified in a centralized way: e.g. `ZED_` prefix is now added
for all such custom vars.

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2024-04-04 15:02:24 +02:00 committed by GitHub
parent ee1b1779f1
commit 1085642c88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 188 additions and 91 deletions

View file

@ -20,7 +20,10 @@ use std::{
Arc,
},
};
use task::static_source::{Definition, TaskDefinitions};
use task::{
static_source::{Definition, TaskDefinitions},
VariableName,
};
use util::{
fs::remove_matching,
github::{latest_github_release, GitHubLspBinaryVersion},
@ -557,25 +560,32 @@ pub(super) fn elixir_task_context() -> ContextProviderWithTasks {
label: "Elixir: test suite".to_owned(),
command: "mix".to_owned(),
args: vec!["test".to_owned()],
..Default::default()
..Definition::default()
},
Definition {
label: "Elixir: failed tests suite".to_owned(),
command: "mix".to_owned(),
args: vec!["test".to_owned(), "--failed".to_owned()],
..Default::default()
..Definition::default()
},
Definition {
label: "Elixir: test file".to_owned(),
command: "mix".to_owned(),
args: vec!["test".to_owned(), "$ZED_FILE".to_owned()],
..Default::default()
args: vec!["test".to_owned(), VariableName::Symbol.template_value()],
..Definition::default()
},
Definition {
label: "Elixir: test at current line".to_owned(),
command: "mix".to_owned(),
args: vec!["test".to_owned(), "$ZED_FILE:$ZED_ROW".to_owned()],
..Default::default()
args: vec![
"test".to_owned(),
format!(
"{}:{}",
VariableName::File.template_value(),
VariableName::Row.template_value()
),
],
..Definition::default()
},
Definition {
label: "Elixir: break line".to_owned(),
@ -585,9 +595,13 @@ pub(super) fn elixir_task_context() -> ContextProviderWithTasks {
"mix".to_owned(),
"test".to_owned(),
"-b".to_owned(),
"$ZED_FILE:$ZED_ROW".to_owned(),
format!(
"{}:{}",
VariableName::File.template_value(),
VariableName::Row.template_value()
),
],
..Default::default()
..Definition::default()
},
]))
}

View file

@ -13,7 +13,7 @@ use smol::fs::{self, File};
use std::{any::Any, borrow::Cow, env::consts, path::PathBuf, sync::Arc};
use task::{
static_source::{Definition, TaskDefinitions},
TaskVariables,
TaskVariables, VariableName,
};
use util::{
fs::remove_matching,
@ -322,6 +322,9 @@ impl LspAdapter for RustLspAdapter {
pub(crate) struct RustContextProvider;
const RUST_PACKAGE_TASK_VARIABLE: VariableName =
VariableName::Custom(Cow::Borrowed("RUST_PACKAGE"));
impl ContextProvider for RustContextProvider {
fn build_context(
&self,
@ -347,19 +350,24 @@ impl ContextProvider for RustContextProvider {
.ok();
if let Some(package_name) = package_name {
context.0.insert("ZED_PACKAGE".to_owned(), package_name);
context.insert(RUST_PACKAGE_TASK_VARIABLE.clone(), package_name);
}
}
Ok(context)
}
fn associated_tasks(&self) -> Option<TaskDefinitions> {
Some(TaskDefinitions(vec![
Definition {
label: "Rust: Test current crate".to_owned(),
command: "cargo".into(),
args: vec!["test".into(), "-p".into(), "$ZED_PACKAGE".into()],
..Default::default()
args: vec![
"test".into(),
"-p".into(),
RUST_PACKAGE_TASK_VARIABLE.template_value(),
],
..Definition::default()
},
Definition {
label: "Rust: Test current function".to_owned(),
@ -367,29 +375,33 @@ impl ContextProvider for RustContextProvider {
args: vec![
"test".into(),
"-p".into(),
"$ZED_PACKAGE".into(),
RUST_PACKAGE_TASK_VARIABLE.template_value(),
"--".into(),
"$ZED_SYMBOL".into(),
VariableName::Symbol.template_value(),
],
..Default::default()
..Definition::default()
},
Definition {
label: "Rust: cargo run".into(),
command: "cargo".into(),
args: vec!["run".into()],
..Default::default()
..Definition::default()
},
Definition {
label: "Rust: cargo check current crate".into(),
command: "cargo".into(),
args: vec!["check".into(), "-p".into(), "$ZED_PACKAGE".into()],
..Default::default()
args: vec![
"check".into(),
"-p".into(),
RUST_PACKAGE_TASK_VARIABLE.template_value(),
],
..Definition::default()
},
Definition {
label: "Rust: cargo check workspace".into(),
command: "cargo".into(),
args: vec!["check".into(), "--workspace".into()],
..Default::default()
..Definition::default()
},
]))
}