tasks: Expose captured variables to ContextProvider (#12134)

This PR changes the interface of ContextProvider, allowing it to inspect
*all* variables set so far during the process of building
`TaskVariables`. This makes it possible to capture e.g. an identifier in
tree-sitter query, process it and then export it as a task variable.

Notably, the list of variables includes captures prefixed with leading
underscore; they are removed after all calls to `build_context`, but it
makes it possible to capture something and then conditionally preserve
it (and perhaps modify it).

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-05-22 19:45:43 +02:00 committed by GitHub
parent ba9449692e
commit 58796a8480
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 212 additions and 201 deletions

View file

@ -1,4 +1,4 @@
use language::ContextProviderWithTasks;
use project::ContextProviderWithTasks;
use task::{TaskTemplate, TaskTemplates, VariableName};
pub(super) fn bash_task_context() -> ContextProviderWithTasks {

View file

@ -16,7 +16,7 @@ use std::{
borrow::Cow,
ffi::{OsStr, OsString},
ops::Range,
path::{Path, PathBuf},
path::PathBuf,
str,
sync::{
atomic::{AtomicBool, Ordering::SeqCst},
@ -447,7 +447,7 @@ const GO_PACKAGE_TASK_VARIABLE: VariableName = VariableName::Custom(Cow::Borrowe
impl ContextProvider for GoContextProvider {
fn build_context(
&self,
worktree_abs_path: Option<&Path>,
variables: &TaskVariables,
location: &Location,
cx: &mut gpui::AppContext,
) -> Result<TaskVariables> {
@ -465,7 +465,8 @@ impl ContextProvider for GoContextProvider {
// Prefer the relative form `./my-nested-package/is-here` over
// absolute path, because it's more readable in the modal, but
// the absolute path also works.
let package_name = worktree_abs_path
let package_name = variables
.get(&VariableName::WorktreeRoot)
.and_then(|worktree_abs_path| buffer_dir.strip_prefix(worktree_abs_path).ok())
.map(|relative_pkg_dir| {
if relative_pkg_dir.as_os_str().is_empty() {

View file

@ -1,8 +1,9 @@
use anyhow::Result;
use async_trait::async_trait;
use language::{ContextProviderWithTasks, LanguageServerName, LspAdapter, LspAdapterDelegate};
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
use lsp::LanguageServerBinary;
use node_runtime::NodeRuntime;
use project::ContextProviderWithTasks;
use std::{
any::Any,
ffi::OsString,

View file

@ -328,7 +328,7 @@ const RUST_PACKAGE_TASK_VARIABLE: VariableName =
impl ContextProvider for RustContextProvider {
fn build_context(
&self,
_: Option<&Path>,
_: &TaskVariables,
location: &Location,
cx: &mut gpui::AppContext,
) -> Result<TaskVariables> {