tasks_ui.rs

  1use std::path::Path;
  2
  3use collections::HashMap;
  4use editor::Editor;
  5use gpui::{App, AppContext as _, Context, Entity, Task, Window};
  6use modal::TaskOverrides;
  7use project::{Location, TaskContexts, TaskSourceKind, Worktree};
  8use task::{RevealTarget, TaskContext, TaskId, TaskTemplate, TaskVariables, VariableName};
  9use workspace::Workspace;
 10
 11mod modal;
 12
 13pub use modal::{Rerun, ShowAttachModal, Spawn, TasksModal};
 14
 15pub fn init(cx: &mut App) {
 16    cx.observe_new(
 17        |workspace: &mut Workspace, _: Option<&mut Window>, _: &mut Context<Workspace>| {
 18            workspace
 19                .register_action(spawn_task_or_modal)
 20                .register_action(move |workspace, action: &modal::Rerun, window, cx| {
 21                    if let Some((task_source_kind, mut last_scheduled_task)) = workspace
 22                        .project()
 23                        .read(cx)
 24                        .task_store()
 25                        .read(cx)
 26                        .task_inventory()
 27                        .and_then(|inventory| {
 28                            inventory.read(cx).last_scheduled_task(
 29                                action
 30                                    .task_id
 31                                    .as_ref()
 32                                    .map(|id| TaskId(id.clone()))
 33                                    .as_ref(),
 34                            )
 35                        })
 36                    {
 37                        if action.reevaluate_context {
 38                            let mut original_task = last_scheduled_task.original_task().clone();
 39                            if let Some(allow_concurrent_runs) = action.allow_concurrent_runs {
 40                                original_task.allow_concurrent_runs = allow_concurrent_runs;
 41                            }
 42                            if let Some(use_new_terminal) = action.use_new_terminal {
 43                                original_task.use_new_terminal = use_new_terminal;
 44                            }
 45                            let task_contexts = task_contexts(workspace, window, cx);
 46                            cx.spawn_in(window, async move |workspace, cx| {
 47                                let task_contexts = task_contexts.await;
 48                                let default_context = TaskContext::default();
 49                                workspace
 50                                    .update_in(cx, |workspace, window, cx| {
 51                                        workspace.schedule_task(
 52                                            task_source_kind,
 53                                            &original_task,
 54                                            task_contexts
 55                                                .active_context()
 56                                                .unwrap_or(&default_context),
 57                                            false,
 58                                            window,
 59                                            cx,
 60                                        )
 61                                    })
 62                                    .ok()
 63                            })
 64                            .detach()
 65                        } else {
 66                            let resolved = &mut last_scheduled_task.resolved;
 67
 68                            if let Some(allow_concurrent_runs) = action.allow_concurrent_runs {
 69                                resolved.allow_concurrent_runs = allow_concurrent_runs;
 70                            }
 71                            if let Some(use_new_terminal) = action.use_new_terminal {
 72                                resolved.use_new_terminal = use_new_terminal;
 73                            }
 74
 75                            workspace.schedule_resolved_task(
 76                                task_source_kind,
 77                                last_scheduled_task,
 78                                false,
 79                                window,
 80                                cx,
 81                            );
 82                        }
 83                    } else {
 84                        toggle_modal(workspace, None, window, cx).detach();
 85                    };
 86                });
 87        },
 88    )
 89    .detach();
 90}
 91
 92fn spawn_task_or_modal(
 93    workspace: &mut Workspace,
 94    action: &Spawn,
 95    window: &mut Window,
 96    cx: &mut Context<Workspace>,
 97) {
 98    match action {
 99        Spawn::ByName {
100            task_name,
101            reveal_target,
102        } => {
103            let overrides = reveal_target.map(|reveal_target| TaskOverrides {
104                reveal_target: Some(reveal_target),
105            });
106            let name = task_name.clone();
107            spawn_tasks_filtered(move |(_, task)| task.label.eq(&name), overrides, window, cx)
108                .detach_and_log_err(cx)
109        }
110        Spawn::ByTag {
111            task_tag,
112            reveal_target,
113        } => {
114            let overrides = reveal_target.map(|reveal_target| TaskOverrides {
115                reveal_target: Some(reveal_target),
116            });
117            let tag = task_tag.clone();
118            spawn_tasks_filtered(
119                move |(_, task)| task.tags.contains(&tag),
120                overrides,
121                window,
122                cx,
123            )
124            .detach_and_log_err(cx)
125        }
126        Spawn::ViaModal { reveal_target } => {
127            toggle_modal(workspace, *reveal_target, window, cx).detach()
128        }
129    }
130}
131
132pub fn toggle_modal(
133    workspace: &mut Workspace,
134    reveal_target: Option<RevealTarget>,
135    window: &mut Window,
136    cx: &mut Context<Workspace>,
137) -> Task<()> {
138    let task_store = workspace.project().read(cx).task_store().clone();
139    let workspace_handle = workspace.weak_handle();
140    let can_open_modal = workspace.project().update(cx, |project, cx| {
141        project.is_local() || project.ssh_connection_string(cx).is_some() || project.is_via_ssh()
142    });
143    if can_open_modal {
144        let task_contexts = task_contexts(workspace, window, cx);
145        cx.spawn_in(window, async move |workspace, cx| {
146            let task_contexts = task_contexts.await;
147            workspace
148                .update_in(cx, |workspace, window, cx| {
149                    workspace.toggle_modal(window, cx, |window, cx| {
150                        TasksModal::new(
151                            task_store.clone(),
152                            task_contexts,
153                            reveal_target.map(|target| TaskOverrides {
154                                reveal_target: Some(target),
155                            }),
156                            workspace_handle,
157                            window,
158                            cx,
159                        )
160                    })
161                })
162                .ok();
163        })
164    } else {
165        Task::ready(())
166    }
167}
168
169fn spawn_tasks_filtered<F>(
170    mut predicate: F,
171    overrides: Option<TaskOverrides>,
172    window: &mut Window,
173    cx: &mut Context<Workspace>,
174) -> Task<anyhow::Result<()>>
175where
176    F: FnMut((&TaskSourceKind, &TaskTemplate)) -> bool + 'static,
177{
178    cx.spawn_in(window, async move |workspace, cx| {
179        let task_contexts = workspace.update_in(cx, |workspace, window, cx| {
180            task_contexts(workspace, window, cx)
181        })?;
182        let task_contexts = task_contexts.await;
183        let mut tasks = workspace.update(cx, |workspace, cx| {
184            let Some(task_inventory) = workspace
185                .project()
186                .read(cx)
187                .task_store()
188                .read(cx)
189                .task_inventory()
190                .cloned()
191            else {
192                return Vec::new();
193            };
194            let (file, language) = task_contexts
195                .location()
196                .map(|location| {
197                    let buffer = location.buffer.read(cx);
198                    (
199                        buffer.file().cloned(),
200                        buffer.language_at(location.range.start),
201                    )
202                })
203                .unwrap_or_default();
204            task_inventory
205                .read(cx)
206                .list_tasks(file, language, task_contexts.worktree(), cx)
207        })?;
208
209        let did_spawn = workspace
210            .update_in(cx, |workspace, window, cx| {
211                let default_context = TaskContext::default();
212                let active_context = task_contexts.active_context().unwrap_or(&default_context);
213
214                tasks.retain_mut(|(task_source_kind, target_task)| {
215                    if predicate((task_source_kind, target_task)) {
216                        if let Some(overrides) = &overrides {
217                            if let Some(target_override) = overrides.reveal_target {
218                                target_task.reveal_target = target_override;
219                            }
220                        }
221                        workspace.schedule_task(
222                            task_source_kind.clone(),
223                            target_task,
224                            active_context,
225                            false,
226                            window,
227                            cx,
228                        );
229                        true
230                    } else {
231                        false
232                    }
233                });
234
235                if tasks.is_empty() { None } else { Some(()) }
236            })?
237            .is_some();
238        if !did_spawn {
239            workspace
240                .update_in(cx, |workspace, window, cx| {
241                    spawn_task_or_modal(
242                        workspace,
243                        &Spawn::ViaModal {
244                            reveal_target: overrides.and_then(|overrides| overrides.reveal_target),
245                        },
246                        window,
247                        cx,
248                    );
249                })
250                .ok();
251        }
252
253        Ok(())
254    })
255}
256
257pub fn task_contexts(
258    workspace: &Workspace,
259    window: &mut Window,
260    cx: &mut App,
261) -> Task<TaskContexts> {
262    let active_item = workspace.active_item(cx);
263    let active_worktree = active_item
264        .as_ref()
265        .and_then(|item| item.project_path(cx))
266        .map(|project_path| project_path.worktree_id)
267        .filter(|worktree_id| {
268            workspace
269                .project()
270                .read(cx)
271                .worktree_for_id(*worktree_id, cx)
272                .map_or(false, |worktree| is_visible_directory(&worktree, cx))
273        })
274        .or(workspace
275            .visible_worktrees(cx)
276            .next()
277            .map(|tree| tree.read(cx).id()));
278
279    let active_editor = active_item.and_then(|item| item.act_as::<Editor>(cx));
280
281    let editor_context_task = active_editor.as_ref().map(|active_editor| {
282        active_editor.update(cx, |editor, cx| editor.task_context(window, cx))
283    });
284
285    let location = active_editor.as_ref().and_then(|editor| {
286        editor.update(cx, |editor, cx| {
287            let selection = editor.selections.newest_anchor();
288            let multi_buffer = editor.buffer().clone();
289            let multi_buffer_snapshot = multi_buffer.read(cx).snapshot(cx);
290            let (buffer_snapshot, buffer_offset) =
291                multi_buffer_snapshot.point_to_buffer_offset(selection.head())?;
292            let buffer_anchor = buffer_snapshot.anchor_before(buffer_offset);
293            let buffer = multi_buffer.read(cx).buffer(buffer_snapshot.remote_id())?;
294            Some(Location {
295                buffer,
296                range: buffer_anchor..buffer_anchor,
297            })
298        })
299    });
300
301    let lsp_task_sources = active_editor
302        .as_ref()
303        .map(|active_editor| active_editor.update(cx, |editor, cx| editor.lsp_task_sources(cx)))
304        .unwrap_or_default();
305
306    let latest_selection = active_editor.as_ref().map(|active_editor| {
307        active_editor
308            .read(cx)
309            .selections
310            .newest_anchor()
311            .head()
312            .text_anchor
313    });
314
315    let mut worktree_abs_paths = workspace
316        .worktrees(cx)
317        .filter(|worktree| is_visible_directory(worktree, cx))
318        .map(|worktree| {
319            let worktree = worktree.read(cx);
320            (worktree.id(), worktree.abs_path())
321        })
322        .collect::<HashMap<_, _>>();
323
324    cx.background_spawn(async move {
325        let mut task_contexts = TaskContexts::default();
326
327        task_contexts.lsp_task_sources = lsp_task_sources;
328        task_contexts.latest_selection = latest_selection;
329
330        if let Some(editor_context_task) = editor_context_task {
331            if let Some(editor_context) = editor_context_task.await {
332                task_contexts.active_item_context =
333                    Some((active_worktree, location, editor_context));
334            }
335        }
336
337        if let Some(active_worktree) = active_worktree {
338            if let Some(active_worktree_abs_path) = worktree_abs_paths.remove(&active_worktree) {
339                task_contexts.active_worktree_context =
340                    Some((active_worktree, worktree_context(&active_worktree_abs_path)));
341            }
342        } else if worktree_abs_paths.len() == 1 {
343            task_contexts.active_worktree_context = worktree_abs_paths
344                .drain()
345                .next()
346                .map(|(id, abs_path)| (id, worktree_context(&abs_path)));
347        }
348
349        task_contexts.other_worktree_contexts.extend(
350            worktree_abs_paths
351                .into_iter()
352                .map(|(id, abs_path)| (id, worktree_context(&abs_path))),
353        );
354        task_contexts
355    })
356}
357
358fn is_visible_directory(worktree: &Entity<Worktree>, cx: &App) -> bool {
359    let worktree = worktree.read(cx);
360    worktree.is_visible() && worktree.root_entry().map_or(false, |entry| entry.is_dir())
361}
362
363fn worktree_context(worktree_abs_path: &Path) -> TaskContext {
364    let mut task_variables = TaskVariables::default();
365    task_variables.insert(
366        VariableName::WorktreeRoot,
367        worktree_abs_path.to_string_lossy().to_string(),
368    );
369    TaskContext {
370        cwd: Some(worktree_abs_path.to_path_buf()),
371        task_variables,
372        project_env: HashMap::default(),
373    }
374}
375
376#[cfg(test)]
377mod tests {
378    use std::{collections::HashMap, sync::Arc};
379
380    use editor::Editor;
381    use gpui::TestAppContext;
382    use language::{Language, LanguageConfig};
383    use project::{BasicContextProvider, FakeFs, Project, task_store::TaskStore};
384    use serde_json::json;
385    use task::{TaskContext, TaskVariables, VariableName};
386    use ui::VisualContext;
387    use util::{path, separator};
388    use workspace::{AppState, Workspace};
389
390    use crate::task_contexts;
391
392    #[gpui::test]
393    async fn test_default_language_context(cx: &mut TestAppContext) {
394        init_test(cx);
395        let fs = FakeFs::new(cx.executor());
396        fs.insert_tree(
397            path!("/dir"),
398            json!({
399                ".zed": {
400                    "tasks.json": r#"[
401                            {
402                                "label": "example task",
403                                "command": "echo",
404                                "args": ["4"]
405                            },
406                            {
407                                "label": "another one",
408                                "command": "echo",
409                                "args": ["55"]
410                            },
411                        ]"#,
412                },
413                "a.ts": "function this_is_a_test() { }",
414                "rust": {
415                                    "b.rs": "use std; fn this_is_a_rust_file() { }",
416                }
417
418            }),
419        )
420        .await;
421        let project = Project::test(fs, [path!("/dir").as_ref()], cx).await;
422        let worktree_store = project.read_with(cx, |project, _| project.worktree_store().clone());
423        let rust_language = Arc::new(
424            Language::new(
425                LanguageConfig::default(),
426                Some(tree_sitter_rust::LANGUAGE.into()),
427            )
428            .with_outline_query(
429                r#"(function_item
430            "fn" @context
431            name: (_) @name) @item"#,
432            )
433            .unwrap()
434            .with_context_provider(Some(Arc::new(BasicContextProvider::new(
435                worktree_store.clone(),
436            )))),
437        );
438
439        let typescript_language = Arc::new(
440            Language::new(
441                LanguageConfig::default(),
442                Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
443            )
444            .with_outline_query(
445                r#"(function_declaration
446                    "async"? @context
447                    "function" @context
448                    name: (_) @name
449                    parameters: (formal_parameters
450                        "(" @context
451                        ")" @context)) @item"#,
452            )
453            .unwrap()
454            .with_context_provider(Some(Arc::new(BasicContextProvider::new(
455                worktree_store.clone(),
456            )))),
457        );
458
459        let worktree_id = project.update(cx, |project, cx| {
460            project.worktrees(cx).next().unwrap().read(cx).id()
461        });
462        let (workspace, cx) =
463            cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
464
465        let buffer1 = workspace
466            .update(cx, |this, cx| {
467                this.project()
468                    .update(cx, |this, cx| this.open_buffer((worktree_id, "a.ts"), cx))
469            })
470            .await
471            .unwrap();
472        buffer1.update(cx, |this, cx| {
473            this.set_language(Some(typescript_language), cx)
474        });
475        let editor1 = cx.new_window_entity(|window, cx| {
476            Editor::for_buffer(buffer1, Some(project.clone()), window, cx)
477        });
478        let buffer2 = workspace
479            .update(cx, |this, cx| {
480                this.project().update(cx, |this, cx| {
481                    this.open_buffer((worktree_id, "rust/b.rs"), cx)
482                })
483            })
484            .await
485            .unwrap();
486        buffer2.update(cx, |this, cx| this.set_language(Some(rust_language), cx));
487        let editor2 = cx
488            .new_window_entity(|window, cx| Editor::for_buffer(buffer2, Some(project), window, cx));
489
490        let first_context = workspace
491            .update_in(cx, |workspace, window, cx| {
492                workspace.add_item_to_center(Box::new(editor1.clone()), window, cx);
493                workspace.add_item_to_center(Box::new(editor2.clone()), window, cx);
494                assert_eq!(
495                    workspace.active_item(cx).unwrap().item_id(),
496                    editor2.entity_id()
497                );
498                task_contexts(workspace, window, cx)
499            })
500            .await;
501
502        assert_eq!(
503            first_context
504                .active_context()
505                .expect("Should have an active context"),
506            &TaskContext {
507                cwd: Some(path!("/dir").into()),
508                task_variables: TaskVariables::from_iter([
509                    (VariableName::File, path!("/dir/rust/b.rs").into()),
510                    (VariableName::Filename, "b.rs".into()),
511                    (VariableName::RelativeFile, separator!("rust/b.rs").into()),
512                    (VariableName::RelativeDir, "rust".into()),
513                    (VariableName::Dirname, path!("/dir/rust").into()),
514                    (VariableName::Stem, "b".into()),
515                    (VariableName::WorktreeRoot, path!("/dir").into()),
516                    (VariableName::Row, "1".into()),
517                    (VariableName::Column, "1".into()),
518                ]),
519                project_env: HashMap::default(),
520            }
521        );
522
523        // And now, let's select an identifier.
524        editor2.update_in(cx, |editor, window, cx| {
525            editor.change_selections(None, window, cx, |selections| {
526                selections.select_ranges([14..18])
527            })
528        });
529
530        assert_eq!(
531            workspace
532                .update_in(cx, |workspace, window, cx| {
533                    task_contexts(workspace, window, cx)
534                })
535                .await
536                .active_context()
537                .expect("Should have an active context"),
538            &TaskContext {
539                cwd: Some(path!("/dir").into()),
540                task_variables: TaskVariables::from_iter([
541                    (VariableName::File, path!("/dir/rust/b.rs").into()),
542                    (VariableName::Filename, "b.rs".into()),
543                    (VariableName::RelativeFile, separator!("rust/b.rs").into()),
544                    (VariableName::RelativeDir, "rust".into()),
545                    (VariableName::Dirname, path!("/dir/rust").into()),
546                    (VariableName::Stem, "b".into()),
547                    (VariableName::WorktreeRoot, path!("/dir").into()),
548                    (VariableName::Row, "1".into()),
549                    (VariableName::Column, "15".into()),
550                    (VariableName::SelectedText, "is_i".into()),
551                    (VariableName::Symbol, "this_is_a_rust_file".into()),
552                ]),
553                project_env: HashMap::default(),
554            }
555        );
556
557        assert_eq!(
558            workspace
559                .update_in(cx, |workspace, window, cx| {
560                    // Now, let's switch the active item to .ts file.
561                    workspace.activate_item(&editor1, true, true, window, cx);
562                    task_contexts(workspace, window, cx)
563                })
564                .await
565                .active_context()
566                .expect("Should have an active context"),
567            &TaskContext {
568                cwd: Some(path!("/dir").into()),
569                task_variables: TaskVariables::from_iter([
570                    (VariableName::File, path!("/dir/a.ts").into()),
571                    (VariableName::Filename, "a.ts".into()),
572                    (VariableName::RelativeFile, "a.ts".into()),
573                    (VariableName::RelativeDir, ".".into()),
574                    (VariableName::Dirname, path!("/dir").into()),
575                    (VariableName::Stem, "a".into()),
576                    (VariableName::WorktreeRoot, path!("/dir").into()),
577                    (VariableName::Row, "1".into()),
578                    (VariableName::Column, "1".into()),
579                    (VariableName::Symbol, "this_is_a_test".into()),
580                ]),
581                project_env: HashMap::default(),
582            }
583        );
584    }
585
586    pub(crate) fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
587        cx.update(|cx| {
588            let state = AppState::test(cx);
589            language::init(cx);
590            crate::init(cx);
591            editor::init(cx);
592            workspace::init_settings(cx);
593            Project::init_settings(cx);
594            TaskStore::init(None);
595            state
596        })
597    }
598}