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

@ -3,13 +3,17 @@ use crate::{LanguageRegistry, Location};
use anyhow::Result;
use gpui::{AppContext, Context, Model};
use std::sync::Arc;
use task::{static_source::tasks_for, static_source::TaskDefinitions, TaskSource, TaskVariables};
use task::{
static_source::{tasks_for, TaskDefinitions},
TaskSource, TaskVariables, VariableName,
};
/// Language Contexts are used by Zed tasks to extract information about source file.
pub trait ContextProvider: Send + Sync {
fn build_context(&self, _: Location, _: &mut AppContext) -> Result<TaskVariables> {
Ok(TaskVariables::default())
}
fn associated_tasks(&self) -> Option<TaskDefinitions> {
None
}
@ -29,18 +33,16 @@ impl ContextProvider for SymbolContextProvider {
.read(cx)
.snapshot()
.symbols_containing(location.range.start, None);
let symbol = symbols.and_then(|symbols| {
symbols.last().map(|symbol| {
let range = symbol
.name_ranges
.last()
.cloned()
.unwrap_or(0..symbol.text.len());
symbol.text[range].to_string()
})
let symbol = symbols.unwrap_or_default().last().map(|symbol| {
let range = symbol
.name_ranges
.last()
.cloned()
.unwrap_or(0..symbol.text.len());
symbol.text[range].to_string()
});
Ok(TaskVariables::from_iter(
symbol.map(|symbol| ("ZED_SYMBOL".to_string(), symbol)),
Some(VariableName::Symbol).zip(symbol),
))
}
}