Skip to content

Commit

Permalink
Use proper Workspace references when querying for tasks by name (#10383)
Browse files Browse the repository at this point in the history
Closes #10380

Release Notes:

- Fixed Zed panicking when running tasks via a keybinding
([10380](#10380))
  • Loading branch information
SomeoneToIgnore committed Apr 10, 2024
1 parent 04e6d5d commit 25b8e46
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
21 changes: 9 additions & 12 deletions crates/tasks_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{path::PathBuf, sync::Arc};

use ::settings::Settings;
use editor::Editor;
use gpui::{AppContext, ViewContext, WeakView, WindowContext};
use gpui::{AppContext, ViewContext, WindowContext};
use language::{Language, Point};
use modal::{Spawn, TasksModal};
use project::{Location, WorktreeId};
Expand Down Expand Up @@ -60,17 +60,17 @@ fn spawn_task_or_modal(workspace: &mut Workspace, action: &Spawn, cx: &mut ViewC
fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
cx.spawn(|workspace, mut cx| async move {
let did_spawn = workspace
.update(&mut cx, |this, cx| {
let (worktree, language) = active_item_selection_properties(&workspace, cx);
let tasks = this.project().update(cx, |project, cx| {
.update(&mut cx, |workspace, cx| {
let (worktree, language) = active_item_selection_properties(workspace, cx);
let tasks = workspace.project().update(cx, |project, cx| {
project.task_inventory().update(cx, |inventory, cx| {
inventory.list_tasks(language, worktree, false, cx)
})
});
let (_, target_task) = tasks.into_iter().find(|(_, task)| task.name() == name)?;
let cwd = task_cwd(this, cx).log_err().flatten();
let task_context = task_context(this, cwd, cx);
schedule_task(this, &target_task, task_context, false, cx);
let cwd = task_cwd(workspace, cx).log_err().flatten();
let task_context = task_context(workspace, cwd, cx);
schedule_task(workspace, &target_task, task_context, false, cx);
Some(())
})
.ok()
Expand All @@ -88,13 +88,10 @@ fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
}

fn active_item_selection_properties(
workspace: &WeakView<Workspace>,
workspace: &Workspace,
cx: &mut WindowContext,
) -> (Option<WorktreeId>, Option<Arc<Language>>) {
let active_item = workspace
.update(cx, |workspace, cx| workspace.active_item(cx))
.ok()
.flatten();
let active_item = workspace.active_item(cx);
let worktree_id = active_item
.as_ref()
.and_then(|item| item.project_path(cx))
Expand Down
29 changes: 26 additions & 3 deletions crates/tasks_ui/src/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,13 @@ impl PickerDelegate for TasksModalDelegate {
let Some(candidates) = picker
.update(&mut cx, |picker, cx| {
let candidates = picker.delegate.candidates.get_or_insert_with(|| {
let (worktree, language) =
active_item_selection_properties(&picker.delegate.workspace, cx);
let Ok((worktree, language)) =
picker.delegate.workspace.update(cx, |workspace, cx| {
active_item_selection_properties(workspace, cx)
})
else {
return Vec::new();
};
picker.delegate.inventory.update(cx, |inventory, cx| {
inventory.list_tasks(language, worktree, true, cx)
})
Expand Down Expand Up @@ -376,6 +381,8 @@ mod tests {
use project::{FakeFs, Project};
use serde_json::json;

use crate::modal::Spawn;

use super::*;

#[gpui::test]
Expand Down Expand Up @@ -514,13 +521,29 @@ mod tests {
vec!["echo 4", "another one", "example task", "echo 40"],
"Last recently used one show task should be listed last, as it is a fire-and-forget task"
);

cx.dispatch_action(Spawn {
task_name: Some("example task".to_string()),
});
let tasks_picker = workspace.update(cx, |workspace, cx| {
workspace
.active_modal::<TasksModal>(cx)
.unwrap()
.read(cx)
.picker
.clone()
});
assert_eq!(
task_names(&tasks_picker, cx),
vec!["echo 4", "another one", "example task", "echo 40"],
);
}

fn open_spawn_tasks(
workspace: &View<Workspace>,
cx: &mut VisualTestContext,
) -> View<Picker<TasksModalDelegate>> {
cx.dispatch_action(crate::modal::Spawn::default());
cx.dispatch_action(Spawn::default());
workspace.update(cx, |workspace, cx| {
workspace
.active_modal::<TasksModal>(cx)
Expand Down

0 comments on commit 25b8e46

Please sign in to comment.