task_inventory.rs

   1//! Project-wide storage of the tasks available, capable of updating itself from the sources set.
   2
   3use std::{
   4    borrow::Cow,
   5    cmp::{self, Reverse},
   6    collections::hash_map,
   7    path::{Path, PathBuf},
   8    sync::Arc,
   9};
  10
  11use anyhow::Result;
  12use collections::{HashMap, HashSet, VecDeque};
  13use gpui::{App, AppContext as _, Entity, SharedString, Task};
  14use itertools::Itertools;
  15use language::{
  16    Buffer, ContextProvider, File, Language, LanguageToolchainStore, Location,
  17    language_settings::language_settings,
  18};
  19use lsp::{LanguageServerId, LanguageServerName};
  20use paths::{debug_task_file_name, task_file_name};
  21use settings::{InvalidSettingsError, parse_json_with_comments};
  22use task::{
  23    DebugScenario, ResolvedTask, TaskContext, TaskId, TaskTemplate, TaskTemplates, TaskVariables,
  24    VariableName,
  25};
  26use text::{BufferId, Point, ToPoint};
  27use util::{NumericPrefixWithSuffix, ResultExt as _, paths::PathExt as _, post_inc};
  28use worktree::WorktreeId;
  29
  30use crate::{task_store::TaskSettingsLocation, worktree_store::WorktreeStore};
  31
  32/// Inventory tracks available tasks for a given project.
  33#[derive(Debug, Default)]
  34pub struct Inventory {
  35    last_scheduled_tasks: VecDeque<(TaskSourceKind, ResolvedTask)>,
  36    templates_from_settings: InventoryFor<TaskTemplate>,
  37    scenarios_from_settings: InventoryFor<DebugScenario>,
  38}
  39
  40// Helper trait for better error messages in [InventoryFor]
  41trait InventoryContents: Clone {
  42    const GLOBAL_SOURCE_FILE: &'static str;
  43    const LABEL: &'static str;
  44}
  45
  46impl InventoryContents for TaskTemplate {
  47    const GLOBAL_SOURCE_FILE: &'static str = "tasks.json";
  48    const LABEL: &'static str = "tasks";
  49}
  50
  51impl InventoryContents for DebugScenario {
  52    const GLOBAL_SOURCE_FILE: &'static str = "debug.json";
  53
  54    const LABEL: &'static str = "debug scenarios";
  55}
  56
  57#[derive(Debug)]
  58struct InventoryFor<T> {
  59    global: HashMap<PathBuf, Vec<T>>,
  60    worktree: HashMap<WorktreeId, HashMap<Arc<Path>, Vec<T>>>,
  61}
  62
  63impl<T: InventoryContents> InventoryFor<T> {
  64    fn worktree_scenarios(
  65        &self,
  66        worktree: Option<WorktreeId>,
  67    ) -> impl '_ + Iterator<Item = (TaskSourceKind, T)> {
  68        worktree.into_iter().flat_map(|worktree| {
  69            self.worktree
  70                .get(&worktree)
  71                .into_iter()
  72                .flatten()
  73                .flat_map(|(directory, templates)| {
  74                    templates.iter().map(move |template| (directory, template))
  75                })
  76                .map(move |(directory, template)| {
  77                    (
  78                        TaskSourceKind::Worktree {
  79                            id: worktree,
  80                            directory_in_worktree: directory.to_path_buf(),
  81                            id_base: Cow::Owned(format!(
  82                                "local worktree {} from directory {directory:?}",
  83                                T::LABEL
  84                            )),
  85                        },
  86                        template.clone(),
  87                    )
  88                })
  89        })
  90    }
  91
  92    fn global_scenarios(&self) -> impl '_ + Iterator<Item = (TaskSourceKind, T)> {
  93        self.global.iter().flat_map(|(file_path, templates)| {
  94            templates.into_iter().map(|template| {
  95                (
  96                    TaskSourceKind::AbsPath {
  97                        id_base: Cow::Owned(format!("global {}", T::GLOBAL_SOURCE_FILE)),
  98                        abs_path: file_path.clone(),
  99                    },
 100                    template.clone(),
 101                )
 102            })
 103        })
 104    }
 105}
 106
 107impl<T> Default for InventoryFor<T> {
 108    fn default() -> Self {
 109        Self {
 110            global: HashMap::default(),
 111            worktree: HashMap::default(),
 112        }
 113    }
 114}
 115
 116/// Kind of a source the tasks are fetched from, used to display more source information in the UI.
 117#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
 118pub enum TaskSourceKind {
 119    /// bash-like commands spawned by users, not associated with any path
 120    UserInput,
 121    /// Tasks from the worktree's .zed/task.json
 122    Worktree {
 123        id: WorktreeId,
 124        directory_in_worktree: PathBuf,
 125        id_base: Cow<'static, str>,
 126    },
 127    /// ~/.config/zed/task.json - like global files with task definitions, applicable to any path
 128    AbsPath {
 129        id_base: Cow<'static, str>,
 130        abs_path: PathBuf,
 131    },
 132    /// Languages-specific tasks coming from extensions.
 133    Language { name: SharedString },
 134    /// Language-specific tasks coming from LSP servers.
 135    Lsp(LanguageServerId),
 136}
 137
 138/// A collection of task contexts, derived from the current state of the workspace.
 139/// Only contains worktrees that are visible and with their root being a directory.
 140#[derive(Debug, Default)]
 141pub struct TaskContexts {
 142    /// A context, related to the currently opened item.
 143    /// Item can be opened from an invisible worktree, or any other, not necessarily active worktree.
 144    pub active_item_context: Option<(Option<WorktreeId>, Option<Location>, TaskContext)>,
 145    /// A worktree that corresponds to the active item, or the only worktree in the workspace.
 146    pub active_worktree_context: Option<(WorktreeId, TaskContext)>,
 147    /// If there are multiple worktrees in the workspace, all non-active ones are included here.
 148    pub other_worktree_contexts: Vec<(WorktreeId, TaskContext)>,
 149    pub lsp_task_sources: HashMap<LanguageServerName, Vec<BufferId>>,
 150    pub latest_selection: Option<text::Anchor>,
 151}
 152
 153impl TaskContexts {
 154    pub fn active_context(&self) -> Option<&TaskContext> {
 155        self.active_item_context
 156            .as_ref()
 157            .map(|(_, _, context)| context)
 158            .or_else(|| {
 159                self.active_worktree_context
 160                    .as_ref()
 161                    .map(|(_, context)| context)
 162            })
 163    }
 164
 165    pub fn location(&self) -> Option<&Location> {
 166        self.active_item_context
 167            .as_ref()
 168            .and_then(|(_, location, _)| location.as_ref())
 169    }
 170
 171    pub fn worktree(&self) -> Option<WorktreeId> {
 172        self.active_item_context
 173            .as_ref()
 174            .and_then(|(worktree_id, _, _)| worktree_id.as_ref())
 175            .or_else(|| {
 176                self.active_worktree_context
 177                    .as_ref()
 178                    .map(|(worktree_id, _)| worktree_id)
 179            })
 180            .copied()
 181    }
 182}
 183
 184impl TaskSourceKind {
 185    pub fn to_id_base(&self) -> String {
 186        match self {
 187            Self::UserInput => "oneshot".to_string(),
 188            Self::AbsPath { id_base, abs_path } => {
 189                format!("{id_base}_{}", abs_path.display())
 190            }
 191            Self::Worktree {
 192                id,
 193                id_base,
 194                directory_in_worktree,
 195            } => {
 196                format!("{id_base}_{id}_{}", directory_in_worktree.display())
 197            }
 198            Self::Language { name } => format!("language_{name}"),
 199            Self::Lsp(server_id) => format!("lsp_{server_id}"),
 200        }
 201    }
 202}
 203
 204impl Inventory {
 205    pub fn new(cx: &mut App) -> Entity<Self> {
 206        cx.new(|_| Self::default())
 207    }
 208
 209    pub fn list_debug_scenarios(&self, worktree: Option<WorktreeId>) -> Vec<DebugScenario> {
 210        let global_scenarios = self.global_debug_scenarios_from_settings();
 211        let worktree_scenarios = self.worktree_scenarios_from_settings(worktree);
 212
 213        worktree_scenarios
 214            .chain(global_scenarios)
 215            .map(|(_, scenario)| scenario)
 216            .collect()
 217    }
 218
 219    pub fn task_template_by_label(
 220        &self,
 221        buffer: Option<Entity<Buffer>>,
 222        label: &str,
 223        cx: &App,
 224    ) -> Option<TaskTemplate> {
 225        let (worktree_id, file, language) = buffer
 226            .map(|buffer| {
 227                let buffer = buffer.read(cx);
 228                let file = buffer.file().cloned();
 229                (
 230                    file.as_ref().map(|file| file.worktree_id(cx)),
 231                    file,
 232                    buffer.language().cloned(),
 233                )
 234            })
 235            .unwrap_or((None, None, None));
 236
 237        self.list_tasks(file, language, worktree_id, cx)
 238            .iter()
 239            .find(|(_, template)| template.label == label)
 240            .map(|val| val.1.clone())
 241    }
 242
 243    /// Pulls its task sources relevant to the worktree and the language given,
 244    /// returns all task templates with their source kinds, worktree tasks first, language tasks second
 245    /// and global tasks last. No specific order inside source kinds groups.
 246    pub fn list_tasks(
 247        &self,
 248        file: Option<Arc<dyn File>>,
 249        language: Option<Arc<Language>>,
 250        worktree: Option<WorktreeId>,
 251        cx: &App,
 252    ) -> Vec<(TaskSourceKind, TaskTemplate)> {
 253        let global_tasks = self.global_templates_from_settings();
 254        let worktree_tasks = self.worktree_templates_from_settings(worktree);
 255        let task_source_kind = language.as_ref().map(|language| TaskSourceKind::Language {
 256            name: language.name().into(),
 257        });
 258        let language_tasks = language
 259            .filter(|language| {
 260                language_settings(Some(language.name()), file.as_ref(), cx)
 261                    .tasks
 262                    .enabled
 263            })
 264            .and_then(|language| language.context_provider()?.associated_tasks(file, cx))
 265            .into_iter()
 266            .flat_map(|tasks| tasks.0.into_iter())
 267            .flat_map(|task| Some((task_source_kind.clone()?, task)));
 268
 269        worktree_tasks
 270            .chain(language_tasks)
 271            .chain(global_tasks)
 272            .collect()
 273    }
 274
 275    /// Pulls its task sources relevant to the worktree and the language given and resolves them with the [`TaskContexts`] given.
 276    /// Joins the new resolutions with the resolved tasks that were used (spawned) before,
 277    /// orders them so that the most recently used come first, all equally used ones are ordered so that the most specific tasks come first.
 278    /// Deduplicates the tasks by their labels and context and splits the ordered list into two: used tasks and the rest, newly resolved tasks.
 279    pub fn used_and_current_resolved_tasks<'a>(
 280        &'a self,
 281        task_contexts: &'a TaskContexts,
 282        cx: &'a App,
 283    ) -> (
 284        Vec<(TaskSourceKind, ResolvedTask)>,
 285        Vec<(TaskSourceKind, ResolvedTask)>,
 286    ) {
 287        let worktree = task_contexts.worktree();
 288        let location = task_contexts.location();
 289        let language = location
 290            .and_then(|location| location.buffer.read(cx).language_at(location.range.start));
 291        let task_source_kind = language.as_ref().map(|language| TaskSourceKind::Language {
 292            name: language.name().into(),
 293        });
 294        let file = location.and_then(|location| location.buffer.read(cx).file().cloned());
 295
 296        let mut task_labels_to_ids = HashMap::<String, HashSet<TaskId>>::default();
 297        let mut lru_score = 0_u32;
 298        let previously_spawned_tasks = self
 299            .last_scheduled_tasks
 300            .iter()
 301            .rev()
 302            .filter(|(task_kind, _)| {
 303                if matches!(task_kind, TaskSourceKind::Language { .. }) {
 304                    Some(task_kind) == task_source_kind.as_ref()
 305                } else {
 306                    true
 307                }
 308            })
 309            .filter(|(_, resolved_task)| {
 310                match task_labels_to_ids.entry(resolved_task.resolved_label.clone()) {
 311                    hash_map::Entry::Occupied(mut o) => {
 312                        o.get_mut().insert(resolved_task.id.clone());
 313                        // Neber allow duplicate reused tasks with the same labels
 314                        false
 315                    }
 316                    hash_map::Entry::Vacant(v) => {
 317                        v.insert(HashSet::from_iter(Some(resolved_task.id.clone())));
 318                        true
 319                    }
 320                }
 321            })
 322            .map(|(task_source_kind, resolved_task)| {
 323                (
 324                    task_source_kind.clone(),
 325                    resolved_task.clone(),
 326                    post_inc(&mut lru_score),
 327                )
 328            })
 329            .sorted_unstable_by(task_lru_comparator)
 330            .map(|(kind, task, _)| (kind, task))
 331            .collect::<Vec<_>>();
 332
 333        let not_used_score = post_inc(&mut lru_score);
 334        let global_tasks = self.global_templates_from_settings();
 335
 336        let language_tasks = language
 337            .filter(|language| {
 338                language_settings(Some(language.name()), file.as_ref(), cx)
 339                    .tasks
 340                    .enabled
 341            })
 342            .and_then(|language| language.context_provider()?.associated_tasks(file, cx))
 343            .into_iter()
 344            .flat_map(|tasks| tasks.0.into_iter())
 345            .flat_map(|task| Some((task_source_kind.clone()?, task)));
 346        let worktree_tasks = self
 347            .worktree_templates_from_settings(worktree)
 348            .chain(language_tasks)
 349            .chain(global_tasks);
 350
 351        let new_resolved_tasks = worktree_tasks
 352            .flat_map(|(kind, task)| {
 353                let id_base = kind.to_id_base();
 354                if let TaskSourceKind::Worktree { id, .. } = &kind {
 355                    None.or_else(|| {
 356                        let (_, _, item_context) = task_contexts
 357                            .active_item_context
 358                            .as_ref()
 359                            .filter(|(worktree_id, _, _)| Some(id) == worktree_id.as_ref())?;
 360                        task.resolve_task(&id_base, item_context)
 361                    })
 362                    .or_else(|| {
 363                        let (_, worktree_context) = task_contexts
 364                            .active_worktree_context
 365                            .as_ref()
 366                            .filter(|(worktree_id, _)| id == worktree_id)?;
 367                        task.resolve_task(&id_base, worktree_context)
 368                    })
 369                    .or_else(|| {
 370                        if let TaskSourceKind::Worktree { id, .. } = &kind {
 371                            let worktree_context = task_contexts
 372                                .other_worktree_contexts
 373                                .iter()
 374                                .find(|(worktree_id, _)| worktree_id == id)
 375                                .map(|(_, context)| context)?;
 376                            task.resolve_task(&id_base, worktree_context)
 377                        } else {
 378                            None
 379                        }
 380                    })
 381                } else {
 382                    None.or_else(|| {
 383                        let (_, _, item_context) = task_contexts.active_item_context.as_ref()?;
 384                        task.resolve_task(&id_base, item_context)
 385                    })
 386                    .or_else(|| {
 387                        let (_, worktree_context) =
 388                            task_contexts.active_worktree_context.as_ref()?;
 389                        task.resolve_task(&id_base, worktree_context)
 390                    })
 391                }
 392                .or_else(|| task.resolve_task(&id_base, &TaskContext::default()))
 393                .map(move |resolved_task| (kind.clone(), resolved_task, not_used_score))
 394            })
 395            .filter(|(_, resolved_task, _)| {
 396                match task_labels_to_ids.entry(resolved_task.resolved_label.clone()) {
 397                    hash_map::Entry::Occupied(mut o) => {
 398                        // Allow new tasks with the same label, if their context is different
 399                        o.get_mut().insert(resolved_task.id.clone())
 400                    }
 401                    hash_map::Entry::Vacant(v) => {
 402                        v.insert(HashSet::from_iter(Some(resolved_task.id.clone())));
 403                        true
 404                    }
 405                }
 406            })
 407            .sorted_unstable_by(task_lru_comparator)
 408            .map(|(kind, task, _)| (kind, task))
 409            .collect::<Vec<_>>();
 410
 411        (previously_spawned_tasks, new_resolved_tasks)
 412    }
 413
 414    /// Returns the last scheduled task by task_id if provided.
 415    /// Otherwise, returns the last scheduled task.
 416    pub fn last_scheduled_task(
 417        &self,
 418        task_id: Option<&TaskId>,
 419    ) -> Option<(TaskSourceKind, ResolvedTask)> {
 420        if let Some(task_id) = task_id {
 421            self.last_scheduled_tasks
 422                .iter()
 423                .find(|(_, task)| &task.id == task_id)
 424                .cloned()
 425        } else {
 426            self.last_scheduled_tasks.back().cloned()
 427        }
 428    }
 429
 430    /// Registers task "usage" as being scheduled – to be used for LRU sorting when listing all tasks.
 431    pub fn task_scheduled(
 432        &mut self,
 433        task_source_kind: TaskSourceKind,
 434        resolved_task: ResolvedTask,
 435    ) {
 436        self.last_scheduled_tasks
 437            .push_back((task_source_kind, resolved_task));
 438        if self.last_scheduled_tasks.len() > 5_000 {
 439            self.last_scheduled_tasks.pop_front();
 440        }
 441    }
 442
 443    /// Deletes a resolved task from history, using its id.
 444    /// A similar may still resurface in `used_and_current_resolved_tasks` when its [`TaskTemplate`] is resolved again.
 445    pub fn delete_previously_used(&mut self, id: &TaskId) {
 446        self.last_scheduled_tasks.retain(|(_, task)| &task.id != id);
 447    }
 448
 449    fn global_templates_from_settings(
 450        &self,
 451    ) -> impl '_ + Iterator<Item = (TaskSourceKind, TaskTemplate)> {
 452        self.templates_from_settings.global_scenarios()
 453    }
 454
 455    fn global_debug_scenarios_from_settings(
 456        &self,
 457    ) -> impl '_ + Iterator<Item = (TaskSourceKind, DebugScenario)> {
 458        self.scenarios_from_settings.global_scenarios()
 459    }
 460
 461    fn worktree_scenarios_from_settings(
 462        &self,
 463        worktree: Option<WorktreeId>,
 464    ) -> impl '_ + Iterator<Item = (TaskSourceKind, DebugScenario)> {
 465        self.scenarios_from_settings.worktree_scenarios(worktree)
 466    }
 467
 468    fn worktree_templates_from_settings(
 469        &self,
 470        worktree: Option<WorktreeId>,
 471    ) -> impl '_ + Iterator<Item = (TaskSourceKind, TaskTemplate)> {
 472        self.templates_from_settings.worktree_scenarios(worktree)
 473    }
 474
 475    /// Updates in-memory task metadata from the JSON string given.
 476    /// Will fail if the JSON is not a valid array of objects, but will continue if any object will not parse into a [`TaskTemplate`].
 477    ///
 478    /// Global tasks are updated for no worktree provided, otherwise the worktree metadata for a given path will be updated.
 479    pub(crate) fn update_file_based_tasks(
 480        &mut self,
 481        location: TaskSettingsLocation<'_>,
 482        raw_tasks_json: Option<&str>,
 483    ) -> Result<(), InvalidSettingsError> {
 484        let raw_tasks = match parse_json_with_comments::<Vec<serde_json::Value>>(
 485            raw_tasks_json.unwrap_or("[]"),
 486        ) {
 487            Ok(tasks) => tasks,
 488            Err(e) => {
 489                return Err(InvalidSettingsError::Tasks {
 490                    path: match location {
 491                        TaskSettingsLocation::Global(path) => path.to_owned(),
 492                        TaskSettingsLocation::Worktree(settings_location) => {
 493                            settings_location.path.join(task_file_name())
 494                        }
 495                    },
 496                    message: format!("Failed to parse tasks file content as a JSON array: {e}"),
 497                });
 498            }
 499        };
 500        let new_templates = raw_tasks.into_iter().filter_map(|raw_template| {
 501            serde_json::from_value::<TaskTemplate>(raw_template).log_err()
 502        });
 503
 504        let parsed_templates = &mut self.templates_from_settings;
 505        match location {
 506            TaskSettingsLocation::Global(path) => {
 507                parsed_templates
 508                    .global
 509                    .entry(path.to_owned())
 510                    .insert_entry(new_templates.collect());
 511            }
 512            TaskSettingsLocation::Worktree(location) => {
 513                let new_templates = new_templates.collect::<Vec<_>>();
 514                if new_templates.is_empty() {
 515                    if let Some(worktree_tasks) =
 516                        parsed_templates.worktree.get_mut(&location.worktree_id)
 517                    {
 518                        worktree_tasks.remove(location.path);
 519                    }
 520                } else {
 521                    parsed_templates
 522                        .worktree
 523                        .entry(location.worktree_id)
 524                        .or_default()
 525                        .insert(Arc::from(location.path), new_templates);
 526                }
 527            }
 528        }
 529
 530        Ok(())
 531    }
 532
 533    /// Updates in-memory task metadata from the JSON string given.
 534    /// Will fail if the JSON is not a valid array of objects, but will continue if any object will not parse into a [`TaskTemplate`].
 535    ///
 536    /// Global tasks are updated for no worktree provided, otherwise the worktree metadata for a given path will be updated.
 537    pub(crate) fn update_file_based_scenarios(
 538        &mut self,
 539        location: TaskSettingsLocation<'_>,
 540        raw_tasks_json: Option<&str>,
 541    ) -> Result<(), InvalidSettingsError> {
 542        let raw_tasks = match parse_json_with_comments::<Vec<serde_json::Value>>(
 543            raw_tasks_json.unwrap_or("[]"),
 544        ) {
 545            Ok(tasks) => tasks,
 546            Err(e) => {
 547                return Err(InvalidSettingsError::Debug {
 548                    path: match location {
 549                        TaskSettingsLocation::Global(path) => path.to_owned(),
 550                        TaskSettingsLocation::Worktree(settings_location) => {
 551                            settings_location.path.join(debug_task_file_name())
 552                        }
 553                    },
 554                    message: format!("Failed to parse tasks file content as a JSON array: {e}"),
 555                });
 556            }
 557        };
 558        let new_templates = raw_tasks.into_iter().filter_map(|raw_template| {
 559            serde_json::from_value::<DebugScenario>(raw_template).log_err()
 560        });
 561
 562        let parsed_scenarios = &mut self.scenarios_from_settings;
 563        match location {
 564            TaskSettingsLocation::Global(path) => {
 565                parsed_scenarios
 566                    .global
 567                    .entry(path.to_owned())
 568                    .insert_entry(new_templates.collect());
 569            }
 570            TaskSettingsLocation::Worktree(location) => {
 571                let new_templates = new_templates.collect::<Vec<_>>();
 572                if new_templates.is_empty() {
 573                    if let Some(worktree_tasks) =
 574                        parsed_scenarios.worktree.get_mut(&location.worktree_id)
 575                    {
 576                        worktree_tasks.remove(location.path);
 577                    }
 578                } else {
 579                    parsed_scenarios
 580                        .worktree
 581                        .entry(location.worktree_id)
 582                        .or_default()
 583                        .insert(Arc::from(location.path), new_templates);
 584                }
 585            }
 586        }
 587
 588        Ok(())
 589    }
 590}
 591
 592fn task_lru_comparator(
 593    (kind_a, task_a, lru_score_a): &(TaskSourceKind, ResolvedTask, u32),
 594    (kind_b, task_b, lru_score_b): &(TaskSourceKind, ResolvedTask, u32),
 595) -> cmp::Ordering {
 596    lru_score_a
 597        // First, display recently used templates above all.
 598        .cmp(lru_score_b)
 599        // Then, ensure more specific sources are displayed first.
 600        .then(task_source_kind_preference(kind_a).cmp(&task_source_kind_preference(kind_b)))
 601        // After that, display first more specific tasks, using more template variables.
 602        // Bonus points for tasks with symbol variables.
 603        .then(task_variables_preference(task_a).cmp(&task_variables_preference(task_b)))
 604        // Finally, sort by the resolved label, but a bit more specifically, to avoid mixing letters and digits.
 605        .then({
 606            NumericPrefixWithSuffix::from_numeric_prefixed_str(&task_a.resolved_label)
 607                .cmp(&NumericPrefixWithSuffix::from_numeric_prefixed_str(
 608                    &task_b.resolved_label,
 609                ))
 610                .then(task_a.resolved_label.cmp(&task_b.resolved_label))
 611                .then(kind_a.cmp(kind_b))
 612        })
 613}
 614
 615fn task_source_kind_preference(kind: &TaskSourceKind) -> u32 {
 616    match kind {
 617        TaskSourceKind::Lsp(..) => 0,
 618        TaskSourceKind::Language { .. } => 1,
 619        TaskSourceKind::UserInput => 2,
 620        TaskSourceKind::Worktree { .. } => 3,
 621        TaskSourceKind::AbsPath { .. } => 4,
 622    }
 623}
 624
 625fn task_variables_preference(task: &ResolvedTask) -> Reverse<usize> {
 626    let task_variables = task.substituted_variables();
 627    Reverse(if task_variables.contains(&VariableName::Symbol) {
 628        task_variables.len() + 1
 629    } else {
 630        task_variables.len()
 631    })
 632}
 633
 634#[cfg(test)]
 635mod test_inventory {
 636    use gpui::{Entity, TestAppContext};
 637    use itertools::Itertools;
 638    use task::TaskContext;
 639    use worktree::WorktreeId;
 640
 641    use crate::Inventory;
 642
 643    use super::TaskSourceKind;
 644
 645    pub(super) fn task_template_names(
 646        inventory: &Entity<Inventory>,
 647        worktree: Option<WorktreeId>,
 648        cx: &mut TestAppContext,
 649    ) -> Vec<String> {
 650        inventory.update(cx, |inventory, cx| {
 651            inventory
 652                .list_tasks(None, None, worktree, cx)
 653                .into_iter()
 654                .map(|(_, task)| task.label)
 655                .sorted()
 656                .collect()
 657        })
 658    }
 659
 660    pub(super) fn register_task_used(
 661        inventory: &Entity<Inventory>,
 662        task_name: &str,
 663        cx: &mut TestAppContext,
 664    ) {
 665        inventory.update(cx, |inventory, cx| {
 666            let (task_source_kind, task) = inventory
 667                .list_tasks(None, None, None, cx)
 668                .into_iter()
 669                .find(|(_, task)| task.label == task_name)
 670                .unwrap_or_else(|| panic!("Failed to find task with name {task_name}"));
 671            let id_base = task_source_kind.to_id_base();
 672            inventory.task_scheduled(
 673                task_source_kind.clone(),
 674                task.resolve_task(&id_base, &TaskContext::default())
 675                    .unwrap_or_else(|| panic!("Failed to resolve task with name {task_name}")),
 676            );
 677        });
 678    }
 679
 680    pub(super) async fn list_tasks(
 681        inventory: &Entity<Inventory>,
 682        worktree: Option<WorktreeId>,
 683        cx: &mut TestAppContext,
 684    ) -> Vec<(TaskSourceKind, String)> {
 685        inventory.update(cx, |inventory, cx| {
 686            let task_context = &TaskContext::default();
 687            inventory
 688                .list_tasks(None, None, worktree, cx)
 689                .into_iter()
 690                .filter_map(|(source_kind, task)| {
 691                    let id_base = source_kind.to_id_base();
 692                    Some((source_kind, task.resolve_task(&id_base, task_context)?))
 693                })
 694                .map(|(source_kind, resolved_task)| (source_kind, resolved_task.resolved_label))
 695                .collect()
 696        })
 697    }
 698}
 699
 700/// A context provided that tries to provide values for all non-custom [`VariableName`] variants for a currently opened file.
 701/// Applied as a base for every custom [`ContextProvider`] unless explicitly oped out.
 702pub struct BasicContextProvider {
 703    worktree_store: Entity<WorktreeStore>,
 704}
 705
 706impl BasicContextProvider {
 707    pub fn new(worktree_store: Entity<WorktreeStore>) -> Self {
 708        Self { worktree_store }
 709    }
 710}
 711impl ContextProvider for BasicContextProvider {
 712    fn build_context(
 713        &self,
 714        _: &TaskVariables,
 715        location: &Location,
 716        _: Option<HashMap<String, String>>,
 717        _: Arc<dyn LanguageToolchainStore>,
 718        cx: &mut App,
 719    ) -> Task<Result<TaskVariables>> {
 720        let buffer = location.buffer.read(cx);
 721        let buffer_snapshot = buffer.snapshot();
 722        let symbols = buffer_snapshot.symbols_containing(location.range.start, None);
 723        let symbol = symbols.unwrap_or_default().last().map(|symbol| {
 724            let range = symbol
 725                .name_ranges
 726                .last()
 727                .cloned()
 728                .unwrap_or(0..symbol.text.len());
 729            symbol.text[range].to_string()
 730        });
 731
 732        let current_file = buffer
 733            .file()
 734            .and_then(|file| file.as_local())
 735            .map(|file| file.abs_path(cx).to_sanitized_string());
 736        let Point { row, column } = location.range.start.to_point(&buffer_snapshot);
 737        let row = row + 1;
 738        let column = column + 1;
 739        let selected_text = buffer
 740            .chars_for_range(location.range.clone())
 741            .collect::<String>();
 742
 743        let mut task_variables = TaskVariables::from_iter([
 744            (VariableName::Row, row.to_string()),
 745            (VariableName::Column, column.to_string()),
 746        ]);
 747
 748        if let Some(symbol) = symbol {
 749            task_variables.insert(VariableName::Symbol, symbol);
 750        }
 751        if !selected_text.trim().is_empty() {
 752            task_variables.insert(VariableName::SelectedText, selected_text);
 753        }
 754        let worktree_root_dir =
 755            buffer
 756                .file()
 757                .map(|file| file.worktree_id(cx))
 758                .and_then(|worktree_id| {
 759                    self.worktree_store
 760                        .read(cx)
 761                        .worktree_for_id(worktree_id, cx)
 762                        .and_then(|worktree| worktree.read(cx).root_dir())
 763                });
 764        if let Some(worktree_path) = worktree_root_dir {
 765            task_variables.insert(
 766                VariableName::WorktreeRoot,
 767                worktree_path.to_sanitized_string(),
 768            );
 769            if let Some(full_path) = current_file.as_ref() {
 770                let relative_path = pathdiff::diff_paths(full_path, worktree_path);
 771                if let Some(relative_path) = relative_path {
 772                    task_variables.insert(
 773                        VariableName::RelativeFile,
 774                        relative_path.to_sanitized_string(),
 775                    );
 776                }
 777            }
 778        }
 779
 780        if let Some(path_as_string) = current_file {
 781            let path = Path::new(&path_as_string);
 782            if let Some(filename) = path.file_name().and_then(|f| f.to_str()) {
 783                task_variables.insert(VariableName::Filename, String::from(filename));
 784            }
 785
 786            if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
 787                task_variables.insert(VariableName::Stem, stem.into());
 788            }
 789
 790            if let Some(dirname) = path.parent().and_then(|s| s.to_str()) {
 791                task_variables.insert(VariableName::Dirname, dirname.into());
 792            }
 793
 794            task_variables.insert(VariableName::File, path_as_string);
 795        }
 796
 797        Task::ready(Ok(task_variables))
 798    }
 799
 800    fn debug_adapter(&self) -> Option<String> {
 801        None
 802    }
 803}
 804
 805/// A ContextProvider that doesn't provide any task variables on it's own, though it has some associated tasks.
 806pub struct ContextProviderWithTasks {
 807    templates: TaskTemplates,
 808}
 809
 810impl ContextProviderWithTasks {
 811    pub fn new(definitions: TaskTemplates) -> Self {
 812        Self {
 813            templates: definitions,
 814        }
 815    }
 816}
 817
 818impl ContextProvider for ContextProviderWithTasks {
 819    fn associated_tasks(
 820        &self,
 821        _: Option<Arc<dyn language::File>>,
 822        _: &App,
 823    ) -> Option<TaskTemplates> {
 824        Some(self.templates.clone())
 825    }
 826
 827    fn debug_adapter(&self) -> Option<String> {
 828        None
 829    }
 830}
 831
 832#[cfg(test)]
 833mod tests {
 834    use gpui::TestAppContext;
 835    use paths::tasks_file;
 836    use pretty_assertions::assert_eq;
 837    use serde_json::json;
 838    use settings::SettingsLocation;
 839
 840    use crate::task_store::TaskStore;
 841
 842    use super::test_inventory::*;
 843    use super::*;
 844
 845    #[gpui::test]
 846    async fn test_task_list_sorting(cx: &mut TestAppContext) {
 847        init_test(cx);
 848        let inventory = cx.update(Inventory::new);
 849        let initial_tasks = resolved_task_names(&inventory, None, cx);
 850        assert!(
 851            initial_tasks.is_empty(),
 852            "No tasks expected for empty inventory, but got {initial_tasks:?}"
 853        );
 854        let initial_tasks = task_template_names(&inventory, None, cx);
 855        assert!(
 856            initial_tasks.is_empty(),
 857            "No tasks expected for empty inventory, but got {initial_tasks:?}"
 858        );
 859        cx.run_until_parked();
 860        let expected_initial_state = [
 861            "1_a_task".to_string(),
 862            "1_task".to_string(),
 863            "2_task".to_string(),
 864            "3_task".to_string(),
 865        ];
 866
 867        inventory.update(cx, |inventory, _| {
 868            inventory
 869                .update_file_based_tasks(
 870                    TaskSettingsLocation::Global(tasks_file()),
 871                    Some(&mock_tasks_from_names(
 872                        expected_initial_state.iter().map(|name| name.as_str()),
 873                    )),
 874                )
 875                .unwrap();
 876        });
 877        assert_eq!(
 878            task_template_names(&inventory, None, cx),
 879            &expected_initial_state,
 880        );
 881        assert_eq!(
 882            resolved_task_names(&inventory, None, cx),
 883            &expected_initial_state,
 884            "Tasks with equal amount of usages should be sorted alphanumerically"
 885        );
 886
 887        register_task_used(&inventory, "2_task", cx);
 888        assert_eq!(
 889            task_template_names(&inventory, None, cx),
 890            &expected_initial_state,
 891        );
 892        assert_eq!(
 893            resolved_task_names(&inventory, None, cx),
 894            vec![
 895                "2_task".to_string(),
 896                "1_a_task".to_string(),
 897                "1_task".to_string(),
 898                "3_task".to_string()
 899            ],
 900        );
 901
 902        register_task_used(&inventory, "1_task", cx);
 903        register_task_used(&inventory, "1_task", cx);
 904        register_task_used(&inventory, "1_task", cx);
 905        register_task_used(&inventory, "3_task", cx);
 906        assert_eq!(
 907            task_template_names(&inventory, None, cx),
 908            &expected_initial_state,
 909        );
 910        assert_eq!(
 911            resolved_task_names(&inventory, None, cx),
 912            vec![
 913                "3_task".to_string(),
 914                "1_task".to_string(),
 915                "2_task".to_string(),
 916                "1_a_task".to_string(),
 917            ],
 918        );
 919
 920        inventory.update(cx, |inventory, _| {
 921            inventory
 922                .update_file_based_tasks(
 923                    TaskSettingsLocation::Global(tasks_file()),
 924                    Some(&mock_tasks_from_names(
 925                        ["10_hello", "11_hello"]
 926                            .into_iter()
 927                            .chain(expected_initial_state.iter().map(|name| name.as_str())),
 928                    )),
 929                )
 930                .unwrap();
 931        });
 932        cx.run_until_parked();
 933        let expected_updated_state = [
 934            "10_hello".to_string(),
 935            "11_hello".to_string(),
 936            "1_a_task".to_string(),
 937            "1_task".to_string(),
 938            "2_task".to_string(),
 939            "3_task".to_string(),
 940        ];
 941        assert_eq!(
 942            task_template_names(&inventory, None, cx),
 943            &expected_updated_state,
 944        );
 945        assert_eq!(
 946            resolved_task_names(&inventory, None, cx),
 947            vec![
 948                "3_task".to_string(),
 949                "1_task".to_string(),
 950                "2_task".to_string(),
 951                "1_a_task".to_string(),
 952                "10_hello".to_string(),
 953                "11_hello".to_string(),
 954            ],
 955        );
 956
 957        register_task_used(&inventory, "11_hello", cx);
 958        assert_eq!(
 959            task_template_names(&inventory, None, cx),
 960            &expected_updated_state,
 961        );
 962        assert_eq!(
 963            resolved_task_names(&inventory, None, cx),
 964            vec![
 965                "11_hello".to_string(),
 966                "3_task".to_string(),
 967                "1_task".to_string(),
 968                "2_task".to_string(),
 969                "1_a_task".to_string(),
 970                "10_hello".to_string(),
 971            ],
 972        );
 973    }
 974
 975    #[gpui::test]
 976    async fn test_inventory_static_task_filters(cx: &mut TestAppContext) {
 977        init_test(cx);
 978        let inventory = cx.update(Inventory::new);
 979        let common_name = "common_task_name";
 980        let worktree_1 = WorktreeId::from_usize(1);
 981        let worktree_2 = WorktreeId::from_usize(2);
 982
 983        cx.run_until_parked();
 984        let worktree_independent_tasks = vec![
 985            (
 986                TaskSourceKind::AbsPath {
 987                    id_base: "global tasks.json".into(),
 988                    abs_path: paths::tasks_file().clone(),
 989                },
 990                common_name.to_string(),
 991            ),
 992            (
 993                TaskSourceKind::AbsPath {
 994                    id_base: "global tasks.json".into(),
 995                    abs_path: paths::tasks_file().clone(),
 996                },
 997                "static_source_1".to_string(),
 998            ),
 999            (
1000                TaskSourceKind::AbsPath {
1001                    id_base: "global tasks.json".into(),
1002                    abs_path: paths::tasks_file().clone(),
1003                },
1004                "static_source_2".to_string(),
1005            ),
1006        ];
1007        let worktree_1_tasks = [
1008            (
1009                TaskSourceKind::Worktree {
1010                    id: worktree_1,
1011                    directory_in_worktree: PathBuf::from(".zed"),
1012                    id_base: "local worktree tasks from directory \".zed\"".into(),
1013                },
1014                common_name.to_string(),
1015            ),
1016            (
1017                TaskSourceKind::Worktree {
1018                    id: worktree_1,
1019                    directory_in_worktree: PathBuf::from(".zed"),
1020                    id_base: "local worktree tasks from directory \".zed\"".into(),
1021                },
1022                "worktree_1".to_string(),
1023            ),
1024        ];
1025        let worktree_2_tasks = [
1026            (
1027                TaskSourceKind::Worktree {
1028                    id: worktree_2,
1029                    directory_in_worktree: PathBuf::from(".zed"),
1030                    id_base: "local worktree tasks from directory \".zed\"".into(),
1031                },
1032                common_name.to_string(),
1033            ),
1034            (
1035                TaskSourceKind::Worktree {
1036                    id: worktree_2,
1037                    directory_in_worktree: PathBuf::from(".zed"),
1038                    id_base: "local worktree tasks from directory \".zed\"".into(),
1039                },
1040                "worktree_2".to_string(),
1041            ),
1042        ];
1043
1044        inventory.update(cx, |inventory, _| {
1045            inventory
1046                .update_file_based_tasks(
1047                    TaskSettingsLocation::Global(tasks_file()),
1048                    Some(&mock_tasks_from_names(
1049                        worktree_independent_tasks
1050                            .iter()
1051                            .map(|(_, name)| name.as_str()),
1052                    )),
1053                )
1054                .unwrap();
1055            inventory
1056                .update_file_based_tasks(
1057                    TaskSettingsLocation::Worktree(SettingsLocation {
1058                        worktree_id: worktree_1,
1059                        path: Path::new(".zed"),
1060                    }),
1061                    Some(&mock_tasks_from_names(
1062                        worktree_1_tasks.iter().map(|(_, name)| name.as_str()),
1063                    )),
1064                )
1065                .unwrap();
1066            inventory
1067                .update_file_based_tasks(
1068                    TaskSettingsLocation::Worktree(SettingsLocation {
1069                        worktree_id: worktree_2,
1070                        path: Path::new(".zed"),
1071                    }),
1072                    Some(&mock_tasks_from_names(
1073                        worktree_2_tasks.iter().map(|(_, name)| name.as_str()),
1074                    )),
1075                )
1076                .unwrap();
1077        });
1078
1079        assert_eq!(
1080            list_tasks_sorted_by_last_used(&inventory, None, cx).await,
1081            worktree_independent_tasks,
1082            "Without a worktree, only worktree-independent tasks should be listed"
1083        );
1084        assert_eq!(
1085            list_tasks_sorted_by_last_used(&inventory, Some(worktree_1), cx).await,
1086            worktree_1_tasks
1087                .iter()
1088                .chain(worktree_independent_tasks.iter())
1089                .cloned()
1090                .sorted_by_key(|(kind, label)| (task_source_kind_preference(kind), label.clone()))
1091                .collect::<Vec<_>>(),
1092        );
1093        assert_eq!(
1094            list_tasks_sorted_by_last_used(&inventory, Some(worktree_2), cx).await,
1095            worktree_2_tasks
1096                .iter()
1097                .chain(worktree_independent_tasks.iter())
1098                .cloned()
1099                .sorted_by_key(|(kind, label)| (task_source_kind_preference(kind), label.clone()))
1100                .collect::<Vec<_>>(),
1101        );
1102
1103        assert_eq!(
1104            list_tasks(&inventory, None, cx).await,
1105            worktree_independent_tasks,
1106            "Without a worktree, only worktree-independent tasks should be listed"
1107        );
1108        assert_eq!(
1109            list_tasks(&inventory, Some(worktree_1), cx).await,
1110            worktree_1_tasks
1111                .iter()
1112                .chain(worktree_independent_tasks.iter())
1113                .cloned()
1114                .collect::<Vec<_>>(),
1115        );
1116        assert_eq!(
1117            list_tasks(&inventory, Some(worktree_2), cx).await,
1118            worktree_2_tasks
1119                .iter()
1120                .chain(worktree_independent_tasks.iter())
1121                .cloned()
1122                .collect::<Vec<_>>(),
1123        );
1124    }
1125
1126    fn init_test(_cx: &mut TestAppContext) {
1127        if std::env::var("RUST_LOG").is_ok() {
1128            env_logger::try_init().ok();
1129        }
1130        TaskStore::init(None);
1131    }
1132
1133    fn resolved_task_names(
1134        inventory: &Entity<Inventory>,
1135        worktree: Option<WorktreeId>,
1136        cx: &mut TestAppContext,
1137    ) -> Vec<String> {
1138        inventory.update(cx, |inventory, cx| {
1139            let mut task_contexts = TaskContexts::default();
1140            task_contexts.active_worktree_context =
1141                worktree.map(|worktree| (worktree, TaskContext::default()));
1142            let (used, current) = inventory.used_and_current_resolved_tasks(&task_contexts, cx);
1143            used.into_iter()
1144                .chain(current)
1145                .map(|(_, task)| task.original_task().label.clone())
1146                .collect()
1147        })
1148    }
1149
1150    fn mock_tasks_from_names<'a>(task_names: impl Iterator<Item = &'a str> + 'a) -> String {
1151        serde_json::to_string(&serde_json::Value::Array(
1152            task_names
1153                .map(|task_name| {
1154                    json!({
1155                        "label": task_name,
1156                        "command": "echo",
1157                        "args": vec![task_name],
1158                    })
1159                })
1160                .collect::<Vec<_>>(),
1161        ))
1162        .unwrap()
1163    }
1164
1165    async fn list_tasks_sorted_by_last_used(
1166        inventory: &Entity<Inventory>,
1167        worktree: Option<WorktreeId>,
1168        cx: &mut TestAppContext,
1169    ) -> Vec<(TaskSourceKind, String)> {
1170        inventory.update(cx, |inventory, cx| {
1171            let mut task_contexts = TaskContexts::default();
1172            task_contexts.active_worktree_context =
1173                worktree.map(|worktree| (worktree, TaskContext::default()));
1174            let (used, current) = inventory.used_and_current_resolved_tasks(&task_contexts, cx);
1175            let mut all = used;
1176            all.extend(current);
1177            all.into_iter()
1178                .map(|(source_kind, task)| (source_kind, task.resolved_label))
1179                .sorted_by_key(|(kind, label)| (task_source_kind_preference(kind), label.clone()))
1180                .collect()
1181        })
1182    }
1183}