1use std::path::Path;
2
3use crate::Location;
4
5use anyhow::Result;
6use gpui::AppContext;
7use task::{TaskTemplates, TaskVariables, VariableName};
8use text::{Point, ToPoint};
9
10/// Language Contexts are used by Zed tasks to extract information about the source file where the tasks are supposed to be scheduled from.
11/// Multiple context providers may be used together: by default, Zed provides a base [`BasicContextProvider`] context that fills all non-custom [`VariableName`] variants.
12///
13/// The context will be used to fill data for the tasks, and filter out the ones that do not have the variables required.
14pub trait ContextProvider: Send + Sync {
15 /// Builds a specific context to be placed on top of the basic one (replacing all conflicting entries) and to be used for task resolving later.
16 fn build_context(
17 &self,
18 _worktree_abs_path: Option<&Path>,
19 _location: &Location,
20 _cx: &mut AppContext,
21 ) -> Result<TaskVariables> {
22 Ok(TaskVariables::default())
23 }
24
25 /// Provides all tasks, associated with the current language.
26 fn associated_tasks(&self) -> Option<TaskTemplates> {
27 None
28 }
29
30 // Determines whether the [`BasicContextProvider`] variables should be filled too (if `false`), or omitted (if `true`).
31 fn is_basic(&self) -> bool {
32 false
33 }
34}
35
36/// A context provided that tries to provide values for all non-custom [`VariableName`] variants for a currently opened file.
37/// Applied as a base for every custom [`ContextProvider`] unless explicitly oped out.
38pub struct BasicContextProvider;
39
40impl ContextProvider for BasicContextProvider {
41 fn is_basic(&self) -> bool {
42 true
43 }
44
45 fn build_context(
46 &self,
47 worktree_abs_path: Option<&Path>,
48 location: &Location,
49 cx: &mut AppContext,
50 ) -> Result<TaskVariables> {
51 let buffer = location.buffer.read(cx);
52 let buffer_snapshot = buffer.snapshot();
53 let symbols = buffer_snapshot.symbols_containing(location.range.start, None);
54 let symbol = symbols.unwrap_or_default().last().map(|symbol| {
55 let range = symbol
56 .name_ranges
57 .last()
58 .cloned()
59 .unwrap_or(0..symbol.text.len());
60 symbol.text[range].to_string()
61 });
62
63 let current_file = buffer
64 .file()
65 .and_then(|file| file.as_local())
66 .map(|file| file.abs_path(cx).to_string_lossy().to_string());
67 let Point { row, column } = location.range.start.to_point(&buffer_snapshot);
68 let row = row + 1;
69 let column = column + 1;
70 let selected_text = buffer
71 .chars_for_range(location.range.clone())
72 .collect::<String>();
73
74 let mut task_variables = TaskVariables::from_iter([
75 (VariableName::Row, row.to_string()),
76 (VariableName::Column, column.to_string()),
77 ]);
78
79 if let Some(symbol) = symbol {
80 task_variables.insert(VariableName::Symbol, symbol);
81 }
82 if !selected_text.trim().is_empty() {
83 task_variables.insert(VariableName::SelectedText, selected_text);
84 }
85 if let Some(path) = current_file {
86 task_variables.insert(VariableName::File, path);
87 }
88 if let Some(worktree_path) = worktree_abs_path {
89 task_variables.insert(
90 VariableName::WorktreeRoot,
91 worktree_path.to_string_lossy().to_string(),
92 );
93 }
94
95 Ok(task_variables)
96 }
97}
98
99/// A ContextProvider that doesn't provide any task variables on it's own, though it has some associated tasks.
100pub struct ContextProviderWithTasks {
101 templates: TaskTemplates,
102}
103
104impl ContextProviderWithTasks {
105 pub fn new(definitions: TaskTemplates) -> Self {
106 Self {
107 templates: definitions,
108 }
109 }
110}
111
112impl ContextProvider for ContextProviderWithTasks {
113 fn associated_tasks(&self) -> Option<TaskTemplates> {
114 Some(self.templates.clone())
115 }
116
117 fn build_context(
118 &self,
119 worktree_abs_path: Option<&Path>,
120 location: &Location,
121 cx: &mut AppContext,
122 ) -> Result<TaskVariables> {
123 BasicContextProvider.build_context(worktree_abs_path, location, cx)
124 }
125}