Use active worktree's task sources (#25784)

Follow-up of https://github.com/zed-industries/zed/pull/25605

Previous PR made global tasks with `ZED_WORKTREE_ROOT` available for
"nothing open" scenario, this PR also gets all related worktree task
templates, using the centralized `TextContexts`' active worktree
detection.

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2025-02-28 00:57:59 +02:00 committed by GitHub
parent 6f30d5da71
commit df7beb4217
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 125 additions and 124 deletions

View file

@ -1,6 +1,6 @@
use std::sync::Arc;
use crate::{active_item_selection_properties, TaskContexts};
use crate::TaskContexts;
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
rems, Action, AnyElement, App, AppContext as _, Context, DismissEvent, Entity, EventEmitter,
@ -209,13 +209,6 @@ impl PickerDelegate for TasksModalDelegate {
match &mut picker.delegate.candidates {
Some(candidates) => string_match_candidates(candidates.iter()),
None => {
let Ok((worktree, location)) =
picker.delegate.workspace.update(cx, |workspace, cx| {
active_item_selection_properties(workspace, cx)
})
else {
return Vec::new();
};
let Some(task_inventory) = picker
.delegate
.task_store
@ -228,8 +221,6 @@ impl PickerDelegate for TasksModalDelegate {
let (used, current) =
task_inventory.read(cx).used_and_current_resolved_tasks(
worktree,
location,
&picker.delegate.task_contexts,
cx,
);
@ -655,8 +646,8 @@ mod tests {
);
assert_eq!(
task_names(&tasks_picker, cx),
Vec::<String>::new(),
"With no global tasks and no open item, no tasks should be listed"
vec!["another one", "example task"],
"With no global tasks and no open item, a single worktree should be used and its tasks listed"
);
drop(tasks_picker);
@ -815,8 +806,8 @@ mod tests {
let tasks_picker = open_spawn_tasks(&workspace, cx);
assert_eq!(
task_names(&tasks_picker, cx),
Vec::<String>::new(),
"Should list no file or worktree context-dependent when no file is open"
vec![concat!("opened now: ", path!("/dir")).to_string()],
"When no file is open for a single worktree, should autodetect all worktree-related tasks"
);
tasks_picker.update(cx, |_, cx| {
cx.emit(DismissEvent);

View file

@ -5,7 +5,7 @@ use ::settings::Settings;
use editor::Editor;
use gpui::{App, AppContext as _, Context, Entity, Task, Window};
use modal::{TaskOverrides, TasksModal};
use project::{Location, TaskContexts, Worktree, WorktreeId};
use project::{Location, TaskContexts, Worktree};
use task::{RevealTarget, TaskContext, TaskId, TaskVariables, VariableName};
use workspace::tasks::schedule_task;
use workspace::{tasks::schedule_resolved_task, Workspace};
@ -49,20 +49,19 @@ pub fn init(cx: &mut App) {
let task_contexts = task_contexts(workspace, window, cx);
cx.spawn_in(window, |workspace, mut cx| async move {
let task_contexts = task_contexts.await;
let default_context = TaskContext::default();
workspace
.update_in(&mut cx, |workspace, window, cx| {
if let Some(task_context) = task_contexts.active_context() {
schedule_task(
workspace,
task_source_kind,
&original_task,
task_context,
false,
cx,
)
} else {
toggle_modal(workspace, None, window, cx).detach();
}
.update_in(&mut cx, |workspace, _, cx| {
schedule_task(
workspace,
task_source_kind,
&original_task,
task_contexts
.active_context()
.unwrap_or(&default_context),
false,
cx,
)
})
.ok()
})
@ -175,8 +174,8 @@ fn spawn_task_with_name(
else {
return Vec::new();
};
let (worktree, location) = active_item_selection_properties(workspace, cx);
let (file, language) = location
let (file, language) = task_contexts
.location()
.map(|location| {
let buffer = location.buffer.read(cx);
(
@ -187,7 +186,7 @@ fn spawn_task_with_name(
.unwrap_or_default();
task_inventory
.read(cx)
.list_tasks(file, language, worktree, cx)
.list_tasks(file, language, task_contexts.worktree(), cx)
})?;
let did_spawn = workspace
@ -231,42 +230,6 @@ fn spawn_task_with_name(
})
}
fn active_item_selection_properties(
workspace: &Workspace,
cx: &mut App,
) -> (Option<WorktreeId>, Option<Location>) {
let active_item = workspace.active_item(cx);
let worktree_id = active_item
.as_ref()
.and_then(|item| item.project_path(cx))
.map(|path| path.worktree_id)
.filter(|worktree_id| {
workspace
.project()
.read(cx)
.worktree_for_id(*worktree_id, cx)
.map_or(false, |worktree| is_visible_directory(&worktree, cx))
});
let location = active_item
.and_then(|active_item| active_item.act_as::<Editor>(cx))
.and_then(|editor| {
editor.update(cx, |editor, cx| {
let selection = editor.selections.newest_anchor();
let multi_buffer = editor.buffer().clone();
let multi_buffer_snapshot = multi_buffer.read(cx).snapshot(cx);
let (buffer_snapshot, buffer_offset) =
multi_buffer_snapshot.point_to_buffer_offset(selection.head())?;
let buffer_anchor = buffer_snapshot.anchor_before(buffer_offset);
let buffer = multi_buffer.read(cx).buffer(buffer_snapshot.remote_id())?;
Some(Location {
buffer,
range: buffer_anchor..buffer_anchor,
})
})
});
(worktree_id, location)
}
fn task_contexts(workspace: &Workspace, window: &mut Window, cx: &mut App) -> Task<TaskContexts> {
let active_item = workspace.active_item(cx);
let active_worktree = active_item
@ -281,12 +244,27 @@ fn task_contexts(workspace: &Workspace, window: &mut Window, cx: &mut App) -> Ta
.map_or(false, |worktree| is_visible_directory(&worktree, cx))
});
let editor_context_task =
active_item
.and_then(|item| item.act_as::<Editor>(cx))
.map(|active_editor| {
active_editor.update(cx, |editor, cx| editor.task_context(window, cx))
});
let active_editor = active_item.and_then(|item| item.act_as::<Editor>(cx));
let editor_context_task = active_editor.as_ref().map(|active_editor| {
active_editor.update(cx, |editor, cx| editor.task_context(window, cx))
});
let location = active_editor.as_ref().and_then(|editor| {
editor.update(cx, |editor, cx| {
let selection = editor.selections.newest_anchor();
let multi_buffer = editor.buffer().clone();
let multi_buffer_snapshot = multi_buffer.read(cx).snapshot(cx);
let (buffer_snapshot, buffer_offset) =
multi_buffer_snapshot.point_to_buffer_offset(selection.head())?;
let buffer_anchor = buffer_snapshot.anchor_before(buffer_offset);
let buffer = multi_buffer.read(cx).buffer(buffer_snapshot.remote_id())?;
Some(Location {
buffer,
range: buffer_anchor..buffer_anchor,
})
})
});
let mut worktree_abs_paths = workspace
.worktrees(cx)
@ -302,7 +280,8 @@ fn task_contexts(workspace: &Workspace, window: &mut Window, cx: &mut App) -> Ta
if let Some(editor_context_task) = editor_context_task {
if let Some(editor_context) = editor_context_task.await {
task_contexts.active_item_context = Some((active_worktree, editor_context));
task_contexts.active_item_context =
Some((active_worktree, location, editor_context));
}
}