Visual Studio Code, rebuilt in Rust
Rust
//! taskQuickPick.ts and the pickers of abstractTaskService.ts: run (two levels: recent and configured tasks, then
//! the tasks of a contributed type), build and test groups, terminate and restart, and Configure Task.
use std::path::{Path, PathBuf};
use gpui::{Context, PromptLevel, Window};
use serde_json::{Value, json};
use super::Workbench;
use super::quick_pick_pin::{self, PIN_BUTTON, PINNED_BUTTON};
use super::task_configure::tabs_to_spaces;
use super::task_matcher_prompt::MatcherChoice;
use crate::commands::Outcome;
use crate::dialog;
use crate::main_thread_quick_open::Owner;
use crate::notifications::{Notification, Severity};
use crate::quick_input::{Button, Entry, Event, Item, Kind, QuickInput};
use crate::tasks::configuration::{CUSTOMIZED_TASK_TYPE, Task, USER_TASKS_GROUP_KEY};
use crate::workbench::editor_options::{INSERT_SPACES, TAB_SIZE};
/// `AbstractTaskService.RecentlyUsedTasks_KeyV2`, workspace storage.
const RECENT_TASKS_KEY: &str = "workbench.tasks.recentlyUsedTasks2";
const SHOW_ALL: &str = "Show All Tasks...";
const CONFIGURE_TOOLTIP: &str = "Configure Task";
const REMOVE_RECENT_TOOLTIP: &str = "Remove Recently Used Task";
/// taskQuickPick.ts `runTaskStorageKey`, workspace storage: the pinned items of the run picker.
const PINNED_KEY: &str = "runTaskStorageKey";
/// The item buttons by handle, beside quick_pick_pin's.
const CONFIGURE_BUTTON: i64 = 0;
const REMOVE_BUTTON: i64 = 1;
#[derive(Clone)]
pub enum TaskPick {
Task(Task),
/// A contributed type: its tasks on the second level.
Type(String),
ShowAll,
/// The default entry (`task: null`): configure tasks, or go back on the second level.
Configure,
GoBack,
/// `settingType`: `{type}.autoDetect` is off.
EnableDetection(String),
TerminateAll,
/// One running instance of a task, by its terminal.
Instance(Task, u32),
/// Configure Task's Create or Open tasks.json entry of a folder.
OpenConfig(PathBuf),
/// The same entry in a window without a folder: the profile's tasks.json.
OpenUserTasks,
/// "No build task to run found. Configure Build Task...": Configure Default Build or Test Task.
ConfigureDefault(&'static str),
/// An entry of the problem matcher prompt for the task about to run.
Matcher(Task, MatcherChoice),
Nothing,
}
#[derive(Clone, Copy, PartialEq)]
pub enum PickAction {
Run,
Terminate,
Restart,
/// Configure Task: the picked task is customized or opened.
Configure,
/// Configure Default Build or Test Task.
ConfigureDefault(&'static str),
/// `_attachProblemMatcher`: dismissing it runs the task too.
AttachMatcher,
}
#[derive(Clone)]
pub struct TaskPicker {
pub picks: Vec<TaskPick>,
pub action: PickAction,
pub pins: Option<RunTaskPins>,
}
/// quickPickPin.ts `showWithPinnedItems` on the run picker's first level: its items before the pin buttons, and
/// with them without the pinned group (shown while the filter has text) and with it.
#[derive(Clone)]
pub struct RunTaskPins {
base: Vec<Entry>,
without: Vec<Entry>,
with_pinned: Vec<Entry>,
}
impl RunTaskPins {
fn shown(&self, value: &str) -> &[Entry] {
if value.is_empty() { &self.with_pinned } else { &self.without }
}
}
impl Workbench {
pub(super) fn register_task_pick_actions(&mut self) {
// abstractTaskService.ts `_registerCommands`: each command asks for workspace trust first (`_trust`).
self.commands.register("workbench.action.tasks.build", |workbench, _, window, cx| workbench.with_task_trust(window, cx, |workbench, window, cx| workbench.run_group_command("build", window, cx)));
self.commands.register("workbench.action.tasks.test", |workbench, _, window, cx| workbench.with_task_trust(window, cx, |workbench, window, cx| workbench.run_group_command("test", window, cx)));
self.commands.register("workbench.action.tasks.terminate", |workbench, args, window, cx| {
let argument = args.into_iter().next().unwrap_or(Value::Null);
workbench.with_task_trust(window, cx, move |workbench, window, cx| {
workbench.terminate_task_command(argument, window, cx);
Outcome::done()
})
});
self.commands.register("workbench.action.tasks.restartTask", |workbench, args, window, cx| {
let argument = args.into_iter().next().unwrap_or(Value::Null);
workbench.with_task_trust(window, cx, move |workbench, window, cx| {
workbench.restart_task_command(argument, window, cx);
Outcome::done()
})
});
self.commands.register("workbench.action.tasks.reRunTask", |workbench, _, window, cx| {
workbench.with_task_trust(window, cx, |workbench, window, cx| {
match workbench.tasks.last.clone() {
Some(task) => drop(workbench.run_task(task, window, cx)),
None => workbench.show_run_task_picker(None, window, cx),
}
Outcome::done()
})
});
self.commands.register("workbench.action.tasks.rerunAllRunningTasks", |workbench, _, window, cx| {
workbench.with_task_trust(window, cx, |workbench, window, cx| {
// `_runRerunAllRunningTasksCommand`.
let running = workbench.running_tasks();
if running.is_empty() {
workbench.notify(Notification::new(Severity::Info, "No running tasks to restart"));
}
for task in &running {
workbench.restart_task(task, window, cx);
}
Outcome::done()
})
});
self.commands.register("workbench.action.tasks.showTasks", |workbench, _, window, cx| {
workbench.with_task_trust(window, cx, |workbench, window, cx| {
workbench.terminate_or_show(PickAction::Restart, "Select the task to show its output", window, cx);
Outcome::done()
})
});
self.commands.register("workbench.action.tasks.configureTaskRunner", |workbench, _, window, cx| {
workbench.with_task_trust(window, cx, |workbench, window, cx| {
workbench.run_configure_tasks(window, cx);
Outcome::done()
})
});
self.commands.register("workbench.action.tasks.openUserTasks", |workbench, _, window, cx| {
workbench.open_user_tasks(window, cx);
Outcome::done()
});
self.commands.register("workbench.action.tasks.configureDefaultBuildTask", |workbench, _, window, cx| {
workbench.with_task_trust(window, cx, |workbench, window, cx| {
workbench.configure_default_task("build", window, cx);
Outcome::done()
})
});
self.commands.register("workbench.action.tasks.configureDefaultTestTask", |workbench, _, window, cx| {
workbench.with_task_trust(window, cx, |workbench, window, cx| {
workbench.configure_default_task("test", window, cx);
Outcome::done()
})
});
}
/// `_doRunTaskCommand` with the two-level picker; `name` fills the filter.
pub(super) fn show_run_task_picker(&mut self, name: Option<String>, window: &mut Window, cx: &mut Context<Self>) {
let configured: Vec<Task> = self.configured_tasks().into_iter().filter(|task| !task.hide).collect();
// `QUICKOPEN_SKIP_CONFIG`: a single configured task runs without the picker.
if configured.len() == 1 && self.setting("task.quickOpen.skip") == true {
self.run_picked_task(configured[0].clone(), window, cx);
return;
}
let mut input = QuickInput::new(0, Kind::QuickPick);
input.placeholder = Some("Select the task to run".into());
input.match_on_description = true;
let picker = self.top_level_entries(&mut input, configured, window);
let id = self.quick_inputs.add(input, Owner::Tasks(picker));
if let Some(name) = name {
self.quick_inputs.get_mut(id).expect("just added").set_value(name);
}
self.show_quick_input(id, window, cx);
self.flush_quick_input_events(window, cx);
}
/// `getTopLevelEntries`: recently used, configured (or Configure a Task), then the contributed types.
fn top_level_entries(&mut self, input: &mut QuickInput, configured: Vec<Task>, window: &Window) -> TaskPicker {
let mut entries = Vec::new();
let mut picks = Vec::new();
let recent = self.recent_tasks(&configured);
let remove = Button { handle: REMOVE_BUTTON, codicon: Some("close".into()), tooltip: Some(REMOVE_RECENT_TOOLTIP.into()), ..Button::default() };
if !recent.is_empty() {
self.task_group_entries(&mut entries, &mut picks, "recently used", recent.clone(), Some(remove));
}
let mut deduped: Vec<Task> = configured.iter().filter(|task| !recent.iter().any(|seen| recent_key(seen) == recent_key(task))).cloned().collect();
deduped.sort_by(|a, b| a.label.to_lowercase().cmp(&b.label.to_lowercase()));
if !deduped.is_empty() {
self.task_group_entries(&mut entries, &mut picks, "configured", deduped, None);
}
if configured.is_empty() {
entries.push(Entry::Separator(Some("configured".into())));
push_entry(&mut entries, &mut picks, Item { label: "$(plus) Configure a Task".into(), ..Item::default() }, TaskPick::Configure);
}
let types = self.task_types(window);
if !types.is_empty() {
entries.push(Entry::Separator(Some("contributed".into())));
for task_type in types {
push_entry(&mut entries, &mut picks, Item { label: format!("$(folder) {task_type}"), ..Item::default() }, TaskPick::Type(task_type));
}
push_entry(&mut entries, &mut picks, Item { label: SHOW_ALL.into(), always_show: true, ..Item::default() }, TaskPick::ShowAll);
}
let pins = self.run_task_pins(entries, &mut picks);
input.set_items(pins.shown(&input.input_box.value).to_vec());
TaskPicker { picks, action: PickAction::Run, pins: Some(pins) }
}
/// `showWithPinnedItems` with `runTaskStorageKey`.
fn run_task_pins(&self, base: Vec<Entry>, picks: &mut Vec<TaskPick>) -> RunTaskPins {
let (without, with_pinned) = quick_pick_pin::format_pinned(base.clone(), picks, &self.pinned_items(PINNED_KEY));
RunTaskPins { base, without, with_pinned }
}
/// `_createEntriesForGroup`: a separator, then each task with its Configure Task button.
fn task_group_entries(&self, entries: &mut Vec<Entry>, picks: &mut Vec<TaskPick>, label: &str, tasks: Vec<Task>, extra: Option<Button>) {
entries.push(Entry::Separator(Some(label.into())));
for task in tasks {
let mut item = self.task_item(&task);
item.buttons.extend(extra.clone());
push_entry(entries, picks, item, TaskPick::Task(task));
}
}
/// `_createTaskEntry`: the label with the task's icon, `getTaskDescription` (User for a user task, the folder
/// in a multi-root workspace), the detail.
pub(super) fn task_item(&self, task: &Task) -> Item {
let configure = Button { handle: 0, codicon: Some("gear".into()), tooltip: Some(CONFIGURE_TOOLTIP.into()), ..Button::default() };
let multi_root = self.workspace().is_some_and(|workspace| workspace.folders.len() > 1);
let description = match task.is_user() {
true => Some("User".to_owned()),
false => multi_root.then(|| task.workspace_folder.as_deref().and_then(Path::file_name).map(|name| name.to_string_lossy().into_owned())).flatten(),
};
let detail = (self.setting("task.quickOpen.detail") != false).then(|| task.detail.clone()).flatten();
Item { label: task.label.clone(), description, detail, buttons: vec![configure], ..Item::default() }
}
pub fn task_pick_event(&mut self, id: i64, event: Event, window: &mut Window, cx: &mut Context<Self>) {
let Some(Owner::Tasks(picker)) = self.quick_inputs.owner(id) else { return };
let picker = picker.clone();
match event {
Event::Accept(_) => {
let Some(handle) = self.quick_inputs.get(id).and_then(|input| input.selected_handles().first().copied()) else { return };
let pick = picker.picks[handle as usize].clone();
if let (PickAction::ConfigureDefault(group), TaskPick::Task(task)) = (picker.action, &pick) {
let all: Vec<Task> = picker.picks.iter().filter_map(|pick| if let TaskPick::Task(task) = pick { Some(task.clone()) } else { None }).collect();
self.hide_quick_input_by_id(id, window, cx);
return self.set_default_task(group, task.clone(), all, window, cx);
}
if let Some(Owner::Tasks(picker)) = self.quick_inputs.owner_mut(id) {
// Accepted: the Hide that follows must not run the task again.
picker.action = PickAction::Run;
}
self.accept_task_pick(id, pick, picker.action, window, cx);
}
Event::TriggerItemButton(handle, index, _) => {
let Some(item) = self.quick_inputs.get(id).and_then(|input| input.items().iter().find_map(|entry| match entry {
Entry::Item(item) if item.handle == handle => Some(item.clone()),
_ => None,
})) else {
return;
};
let button = item.buttons[index as usize].handle;
if button == PIN_BUTTON || button == PINNED_BUTTON {
let Some(base) = picker.pins.map(|pins| pins.base) else { return };
self.toggle_pinned_item(PINNED_KEY, &item, button == PINNED_BUTTON);
let mut picks = picker.picks;
let pins = self.run_task_pins(base, &mut picks);
let input = self.quick_inputs.get_mut(id).expect("the picker is open");
input.set_items(pins.shown(&input.input_box.value).to_vec());
*self.quick_inputs.owner_mut(id).expect("the picker is open") = Owner::Tasks(TaskPicker { picks, action: picker.action, pins: Some(pins) });
cx.notify();
return;
}
let TaskPick::Task(task) = picker.picks[handle as usize].clone() else { return };
if button == REMOVE_BUTTON {
self.remove_recent_task(&task);
let Some(input) = self.quick_inputs.get_mut(id) else { return };
input.remove_item(handle);
cx.notify();
return;
}
debug_assert_eq!(button, CONFIGURE_BUTTON);
self.hide_quick_input_by_id(id, window, cx);
self.configure_task(task, window, cx);
}
// `showWithPinnedItems`: the pinned group shows only while the filter is empty.
Event::ChangeValue(value) => {
let Some(pins) = &picker.pins else { return };
let Some(input) = self.quick_inputs.get_mut(id) else { return };
if input.items() != pins.shown(&value) {
input.set_items(pins.shown(&value).to_vec());
cx.notify();
}
}
Event::Hide => {
self.quick_inputs.remove(id);
if picker.action == PickAction::AttachMatcher
&& let Some(TaskPick::Matcher(task, _)) = picker.picks.first()
{
drop(self.run_task(task.clone(), window, cx));
}
}
_ => {}
}
}
fn accept_task_pick(&mut self, id: i64, pick: TaskPick, action: PickAction, window: &mut Window, cx: &mut Context<Self>) {
match pick {
TaskPick::Type(task_type) => return self.show_second_level(id, Some(task_type), window, cx),
TaskPick::ShowAll => return self.show_second_level(id, None, window, cx),
TaskPick::GoBack => {
let configured = self.configured_tasks().into_iter().filter(|task| !task.hide).collect();
let Some(input) = self.quick_inputs.get_mut(id) else { return };
input.set_value(String::new());
let mut input = std::mem::replace(input, QuickInput::new(id, Kind::QuickPick));
let picker = self.top_level_entries(&mut input, configured, window);
*self.quick_inputs.get_mut(id).expect("placeholder keeps the id") = input;
*self.quick_inputs.owner_mut(id).expect("placeholder keeps the id") = Owner::Tasks(picker);
cx.notify();
return;
}
_ => {}
}
self.hide_quick_input_by_id(id, window, cx);
match (pick, action) {
(TaskPick::Task(task), PickAction::Configure) => self.configure_task(task, window, cx),
(TaskPick::OpenConfig(folder), _) => self.open_task_config(folder, window, cx),
(TaskPick::OpenUserTasks, _) => self.open_user_tasks(window, cx),
(TaskPick::ConfigureDefault(group), _) => self.configure_default_task(group, window, cx),
(TaskPick::Task(task), PickAction::Run) => self.run_picked_task(task, window, cx),
(TaskPick::Task(task), PickAction::Terminate) => self.terminate_task(&task, window, cx),
(TaskPick::Task(task), PickAction::Restart) => self.restart_task(&task, window, cx),
(TaskPick::Instance(task, terminal), _) => self.restart_task_instance(&task, terminal, window, cx),
(TaskPick::TerminateAll, _) => self.terminate_all_tasks(window, cx),
(TaskPick::Configure, _) => self.run_configure_tasks(window, cx),
(TaskPick::EnableDetection(task_type), _) => self.enable_task_detection(task_type, window, cx),
(TaskPick::Matcher(task, choice), _) => self.accept_problem_matcher(task, choice, window, cx),
_ => {}
}
}
/// `doPickerSecondLevel`: the tasks of one type (or all of them), Go back, and the detection setting entries.
fn show_second_level(&mut self, id: i64, task_type: Option<String>, window: &mut Window, cx: &mut Context<Self>) {
let Some(input) = self.quick_inputs.get_mut(id) else { return };
input.busy = true;
if task_type.is_some() {
input.set_value(String::new());
}
cx.notify();
let fetched = self.fetch_tasks(task_type.clone(), cx);
cx.spawn_in(window, async move |workbench, cx| {
let tasks = fetched.await.unwrap_or_default();
let _ = workbench.update_in(cx, |workbench, _, cx| {
let mut tasks: Vec<Task> = tasks.into_iter().filter(|task| !task.hide && task_type.as_ref().is_none_or(|wanted| task.definition["type"] == *wanted)).collect();
tasks.sort_by(|a, b| a.label.to_lowercase().cmp(&b.label.to_lowercase()));
let mut entries = Vec::new();
let mut picks = Vec::new();
for task in &tasks {
push_entry(&mut entries, &mut picks, workbench.task_item(task), TaskPick::Task(task.clone()));
}
let detection_types: Vec<String> = match &task_type {
Some(task_type) => {
if tasks.is_empty() {
push_entry(&mut entries, &mut picks, Item { label: format!("No {task_type} tasks found. Go back \u{21a9}"), always_show: true, ..Item::default() }, TaskPick::GoBack);
} else {
entries.push(Entry::Separator(None));
push_entry(&mut entries, &mut picks, Item { label: "Go back \u{21a9}".into(), always_show: true, ..Item::default() }, TaskPick::GoBack);
}
vec![task_type.clone()]
}
None => vec!["grunt".into(), "gulp".into(), "jake".into()],
};
for detection_type in detection_types {
if workbench.setting(&format!("{detection_type}.autoDetect")) == "off" {
let label = format!("$(gear) {} task detection is turned off. Enable {detection_type} task detection...", capitalize(&detection_type));
push_entry(&mut entries, &mut picks, Item { label, always_show: true, ..Item::default() }, TaskPick::EnableDetection(detection_type));
}
}
let Some(input) = workbench.quick_inputs.get_mut(id) else { return };
input.set_items(entries);
input.busy = false;
let Some(owner) = workbench.quick_inputs.owner_mut(id) else { return };
*owner = Owner::Tasks(TaskPicker { picks, action: PickAction::Run, pins: None });
cx.notify();
});
})
.detach();
}
/// `handleSettingOption`: confirm, turn the detection on, and pick again among that type's tasks.
fn enable_task_detection(&mut self, task_type: String, window: &mut Window, cx: &mut Context<Self>) {
let message = format!("Task detection for {task_type} tasks causes files in any workspace you open to be run as code. Enabling {task_type} task detection is a user setting and will apply to any workspace you open. \n\n Do you want to enable {task_type} task detection for all workspaces?");
let answer = dialog::show_message_box(window, crate::dialog::InTests::Confirm, PromptLevel::Warning, &message, None, &["Yes", "No"], None, cx);
cx.spawn_in(window, async move |workbench, cx| {
if !matches!(answer.await, Ok((0, _))) {
return;
}
let _ = workbench.update_in(cx, |workbench, window, cx| {
workbench.update_setting(&format!("{task_type}.autoDetect"), json!("on"));
let mut input = QuickInput::new(0, Kind::QuickPick);
input.placeholder = Some("Select the task to run".into());
let id = workbench.quick_inputs.add(input, Owner::Tasks(TaskPicker { picks: Vec::new(), action: PickAction::Run, pins: None }));
workbench.show_quick_input(id, window, cx);
workbench.show_second_level(id, Some(task_type), window, cx);
});
})
.detach();
}
/// A picked task: a tasks.json configuration of a contributed type first gets the provided task it configures.
fn run_picked_task(&mut self, task: Task, window: &mut Window, cx: &mut Context<Self>) {
self.add_recent_task(&task);
if !task.configures {
self.run_attaching_problem_matcher(task, window, cx);
return;
}
let fetched = self.fetch_tasks(task.definition["type"].as_str().map(str::to_owned), cx);
cx.spawn_in(window, async move |workbench, cx| {
let tasks = fetched.await.unwrap_or_default();
let resolved = tasks.into_iter().find(|candidate| candidate.definition["_key"] == task.definition["_key"]);
let _ = workbench.update_in(cx, |workbench, window, cx| match resolved {
Some(resolved) => workbench.run_attaching_problem_matcher(resolved, window, cx),
None => workbench.notify_error(format!("There is no task provider registered for tasks of type \"{}\".", task.definition["type"].as_str().unwrap_or_default())),
});
})
.detach();
}
/// `_runTaskGroupCommand` for build or test: the single default task runs, else a picker of the group.
fn run_group_command(&mut self, group: &'static str, window: &mut Window, cx: &mut Context<Self>) -> Outcome {
let defaults: Vec<Task> = self.configured_tasks().into_iter().filter(|task| in_group(task, group) && task_is_default(task)).collect();
if defaults.len() == 1 {
self.run_picked_task(defaults[0].clone(), window, cx);
return Outcome::done();
}
let fetched = self.fetch_tasks(None, cx);
cx.spawn_in(window, async move |workbench, cx| {
let tasks: Vec<Task> = fetched.await.unwrap_or_default().into_iter().filter(|task| in_group(task, group)).collect();
let defaults: Vec<Task> = tasks.iter().filter(|task| task_is_default(task)).cloned().collect();
let _ = workbench.update_in(cx, |workbench, window, cx| {
if defaults.len() == 1 {
workbench.run_picked_task(defaults[0].clone(), window, cx);
return;
}
let tasks = if defaults.is_empty() { tasks } else { defaults };
let (select, not_found) = match group {
"build" => ("Select the build task to run", "No build task to run found. Configure Build Task..."),
_ => ("Select the test task to run", "No test task to run found. Configure Tasks..."),
};
workbench.show_task_list(tasks, select, (not_found, TaskPick::ConfigureDefault(group)), None, PickAction::Run, window, cx);
});
})
.detach();
Outcome::done()
}
/// `_showQuickPick`: the tasks sorted by label, the default entry when there is none, extra entries after.
#[allow(clippy::too_many_arguments)]
fn show_task_list(&mut self, mut tasks: Vec<Task>, placeholder: &str, default: (&str, TaskPick), extra: Option<(&str, TaskPick)>, action: PickAction, window: &mut Window, cx: &mut Context<Self>) {
tasks.retain(|task| !task.hide);
tasks.sort_by(|a, b| a.label.to_lowercase().cmp(&b.label.to_lowercase()));
let mut entries = Vec::new();
let mut picks = Vec::new();
if tasks.is_empty() {
push_entry(&mut entries, &mut picks, Item { label: default.0.into(), ..Item::default() }, default.1);
}
for task in tasks {
let mut item = self.task_item(&task);
if action != PickAction::Run {
item.buttons.clear();
}
push_entry(&mut entries, &mut picks, item, TaskPick::Task(task));
}
if let Some((label, pick)) = extra.filter(|_| !picks.iter().all(|pick| matches!(pick, TaskPick::Nothing))) {
entries.push(Entry::Separator(None));
push_entry(&mut entries, &mut picks, Item { label: label.into(), ..Item::default() }, pick);
}
let mut input = QuickInput::new(0, Kind::QuickPick);
input.placeholder = Some(placeholder.into());
input.match_on_description = true;
input.set_items(entries);
let id = self.quick_inputs.add(input, Owner::Tasks(TaskPicker { picks, action, pins: None }));
self.show_quick_input(id, window, cx);
self.flush_quick_input_events(window, cx);
}
/// `_handleInstancePolicy` prompt: the running instances of the task; the picked one restarts.
pub(super) fn show_instance_picker(&mut self, task: &Task, terminals: Vec<u32>, window: &mut Window, cx: &mut Context<Self>) {
let mut entries = Vec::new();
let mut picks = Vec::new();
for terminal in terminals {
let mut item = self.task_item(task);
item.buttons.clear();
push_entry(&mut entries, &mut picks, item, TaskPick::Instance(task.clone(), terminal));
}
let mut input = QuickInput::new(0, Kind::QuickPick);
input.placeholder = Some("Select an instance to terminate".into());
input.set_items(entries);
let id = self.quick_inputs.add(input, Owner::Tasks(TaskPicker { picks, action: PickAction::Restart, pins: None }));
self.show_quick_input(id, window, cx);
self.flush_quick_input_events(window, cx);
}
/// `_runTerminateCommand`: `terminateAll`, a task identifier, or a picker of the running tasks.
fn terminate_task_command(&mut self, argument: Value, window: &mut Window, cx: &mut Context<Self>) {
if argument == "terminateAll" {
return self.terminate_all_tasks(window, cx);
}
if let Some(task) = self.active_task_matching(&argument) {
return self.terminate_task(&task, window, cx);
}
self.terminate_or_show(PickAction::Terminate, "Select a task to terminate", window, cx);
}
/// `_runRestartTaskCommand`: the only running task, a matching one, or a picker of the running tasks.
fn restart_task_command(&mut self, argument: Value, window: &mut Window, cx: &mut Context<Self>) {
let running = self.running_tasks();
if running.len() == 1 {
return self.restart_task(&running[0], window, cx);
}
if let Some(task) = self.active_task_matching(&argument) {
return self.restart_task(&task, window, cx);
}
self.terminate_or_show(PickAction::Restart, "Select the task to restart", window, cx);
}
fn terminate_or_show(&mut self, action: PickAction, placeholder: &str, window: &mut Window, cx: &mut Context<Self>) {
let running = self.running_tasks();
match action {
PickAction::Terminate => self.show_task_list(running, placeholder, ("No task is currently running", TaskPick::Nothing), Some(("All Running Tasks", TaskPick::TerminateAll)), action, window, cx),
_ if placeholder.starts_with("Select the task to show") => {
// `_showTasks`: one running task shows its terminal, more get a picker.
match running.as_slice() {
[] => drop(self.notify(Notification::new(Severity::Info, "There are no task results to show"))),
[only] => self.reveal_task_terminal(only, window, cx),
_ => self.show_task_list(running, placeholder, ("No task is currently running", TaskPick::Nothing), None, PickAction::Run, window, cx),
}
}
_ => self.show_task_list(running, placeholder, ("No task to restart", TaskPick::Nothing), None, action, window, cx),
}
}
fn active_task_matching(&self, argument: &Value) -> Option<Task> {
match argument {
Value::Null => None,
reference => {
let running = self.running_tasks();
running.iter().find(|task| match reference {
Value::String(label) => task.label == *label || task.name == *label,
Value::Object(identifier) => identifier.iter().all(|(key, value)| key == "_key" || task.definition.get(key) == Some(value)),
_ => false,
}).cloned()
}
}
}
pub(super) fn terminate_task(&mut self, task: &Task, window: &mut Window, cx: &mut Context<Self>) {
if let Some(terminal) = self.task_terminal(task) {
self.kill_terminal(terminal, window, cx);
}
}
fn terminate_all_tasks(&mut self, window: &mut Window, cx: &mut Context<Self>) {
for task in self.running_tasks() {
self.terminate_task(&task, window, cx);
}
}
/// `_restart`: terminate, then run the task again.
fn restart_task(&mut self, task: &Task, window: &mut Window, cx: &mut Context<Self>) {
self.terminate_task(task, window, cx);
drop(self.run_task(task.clone(), window, cx));
}
fn reveal_task_terminal(&mut self, task: &Task, window: &mut Window, cx: &mut Context<Self>) {
if let Some(terminal) = self.task_terminal(task) {
self.terminal_view.set_active(terminal);
self.show_terminal_tab(window, cx);
}
}
/// `_openTaskFile`: an existing tasks.json opens; without one, a template is picked and written.
pub(super) fn open_task_config(&mut self, folder: PathBuf, window: &mut Window, cx: &mut Context<Self>) {
let existing = config::service::FOLDER_CONFIG_DIRS.iter().map(|dir| folder.join(dir).join("tasks.json")).find(|path| path.exists());
if let Some(path) = existing {
return self.open_tasks_file(path, true, window, cx);
}
self.pick_task_template(folder.join(".vscode").join("tasks.json"), window, cx);
}
/// `workbench.action.tasks.openUserTasks` (`_openTaskFile` of `TaskSourceKind.User`): the profile's tasks.json
/// opens when it has tasks, else a template is picked first.
fn open_user_tasks(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let (path, user_tasks) = {
let settings = self.settings.lock().unwrap();
let inspected = settings.configuration.inspect("tasks", &config::configuration::Overrides::default());
(settings.user_files().tasks.clone(), inspected.user_local.value)
};
if user_tasks.is_some_and(|config| config["tasks"].as_array().is_some_and(|tasks| !tasks.is_empty())) {
return self.open_tasks_file(path, false, window, cx);
}
self.pick_task_template(path, window, cx);
}
/// The profile's tasks.json is a `vscode-userdata` resource (`tasksResource`), a folder's a `file` one.
pub(super) fn open_tasks_file(&mut self, path: PathBuf, pinned: bool, window: &mut Window, cx: &mut Context<Self>) {
let user_tasks = self.settings.lock().unwrap().user_files().tasks == path;
let scheme = if user_tasks { files::user_data::SCHEME } else { "file" };
self.open_disk_resource(scheme, path, pinned, window, cx);
}
fn pick_task_template(&mut self, path: PathBuf, window: &mut Window, cx: &mut Context<Self>) {
let mut input = QuickInput::new(0, Kind::QuickPick);
input.placeholder = Some("Select a Task Template".into());
let mut entries = Vec::new();
let mut picks = Vec::new();
for template in crate::tasks::templates::templates() {
let item = Item { label: template.label.into(), detail: Some(template.description.into()), ..Item::default() };
push_entry(&mut entries, &mut picks, item, TaskPick::Nothing);
}
input.set_items(entries);
let id = self.quick_inputs.add(input, Owner::TaskTemplates(path));
self.show_quick_input(id, window, cx);
self.flush_quick_input_events(window, cx);
}
pub fn task_template_event(&mut self, id: i64, event: Event, window: &mut Window, cx: &mut Context<Self>) {
match event {
Event::Accept(_) => {
let Some(Owner::TaskTemplates(path)) = self.quick_inputs.owner(id) else { return };
let path = path.clone();
let Some(handle) = self.quick_inputs.get(id).and_then(|input| input.selected_handles().first().copied()) else { return };
self.hide_quick_input_by_id(id, window, cx);
let mut content = crate::tasks::templates::templates()[handle as usize].content.to_owned();
if self.setting(INSERT_SPACES.key) == true {
content = tabs_to_spaces(&content, self.setting(TAB_SIZE.key).as_u64().unwrap_or(4) as usize);
}
match self.write_task_template(&path, &content) {
Ok(()) => self.open_tasks_file(path, true, window, cx),
Err(error) => self.notify_error(format!("Unable to write {}: {error}", path.display())),
}
}
Event::Hide => self.quick_inputs.remove(id),
_ => {}
}
}
/// A new file gets the template's text; only the user tasks.json can exist here (a folder's opens without a
/// template), and gets the template's value as its `tasks` through the configuration service.
fn write_task_template(&mut self, path: &Path, content: &str) -> Result<(), String> {
if !path.exists() {
let parent = path.parent().expect("tasks.json is in a folder");
return std::fs::create_dir_all(parent).and_then(|()| std::fs::write(path, content)).map_err(|error| error.to_string());
}
let value = config::json::parse(content, &mut Vec::new(), Default::default());
let mut settings = self.settings.lock().unwrap();
let change = settings.update_value("tasks", value.as_ref(), config::service::Target::User, None, &[])?;
self.host.send_configuration_change(&settings, &change);
Ok(())
}
/// `_getRecentTasks`: the stored entries still configured or of a contributed type, most recent first.
fn recent_tasks(&self, configured: &[Task]) -> Vec<Task> {
let mut recent = Vec::new();
for entry in self.stored_recent_tasks() {
let key = entry["key"].as_str().unwrap_or_default();
if let Some(task) = configured.iter().find(|task| recent_key(task) == key) {
recent.push(task.clone());
continue;
}
let custom = entry["definition"]["type"] == CUSTOMIZED_TASK_TYPE;
if custom || entry["label"].is_null() {
continue;
}
let label = entry["label"].as_str().unwrap_or_default().to_owned();
recent.push(Task {
id: key.to_owned(),
name: label.clone(),
label,
definition: entry["definition"].clone(),
workspace_folder: entry["folder"].as_str().map(PathBuf::from),
configures: true,
..Task::default()
});
}
recent
}
fn stored_recent_tasks(&self) -> Vec<Value> {
let storage = self.workspace_storage.lock().unwrap();
let stored = storage.get(RECENT_TASKS_KEY).and_then(|raw| serde_json::from_str::<Vec<Value>>(raw).ok());
stored.unwrap_or_default()
}
fn store_recent_tasks(&self, entries: Vec<Value>) {
self.workspace_storage.lock().unwrap().set(RECENT_TASKS_KEY, Value::Array(entries).to_string());
}
/// `_setRecentlyUsedTask`: most recent first, at most `task.quickOpen.history` entries.
pub(super) fn add_recent_task(&mut self, task: &Task) {
self.tasks.last = Some(task.clone());
let limit = self.setting("task.quickOpen.history").as_u64().unwrap_or(30) as usize;
let key = recent_key(task);
let mut entries = self.stored_recent_tasks();
entries.retain(|entry| entry["key"] != key.as_str());
entries.insert(0, json!({ "key": key, "label": task.label, "definition": task.definition, "folder": task.workspace_folder }));
entries.truncate(limit);
self.store_recent_tasks(entries);
}
fn remove_recent_task(&mut self, task: &Task) {
let key = recent_key(task);
let mut entries = self.stored_recent_tasks();
entries.retain(|entry| entry["key"] != key.as_str());
self.store_recent_tasks(entries);
}
}
pub(super) fn push_entry(entries: &mut Vec<Entry>, picks: &mut Vec<TaskPick>, item: Item, pick: TaskPick) {
entries.push(Entry::Item(Item { handle: picks.len() as i64, ..item }));
picks.push(pick);
}
/// `Task.getKey`: the folder (`USER_TASKS_GROUP_KEY` for a user task) and label of a tasks.json task, the
/// definition key of a contributed one.
fn recent_key(task: &Task) -> String {
let folder = match task.is_user() {
true => USER_TASKS_GROUP_KEY.to_owned(),
false => task.workspace_folder.as_deref().map(|folder| folder.to_string_lossy().into_owned()).unwrap_or_default(),
};
match task.definition["type"] == CUSTOMIZED_TASK_TYPE {
true => format!("{folder}|{}", task.label),
false => format!("{folder}|{}", task.definition["_key"].as_str().unwrap_or_default()),
}
}
fn in_group(task: &Task, group: &str) -> bool {
task.group.as_ref().is_some_and(|value| value["_id"] == group)
}
fn task_is_default(task: &Task) -> bool {
task.group.as_ref().is_some_and(|value| value["isDefault"] == true)
}
fn capitalize(text: &str) -> String {
let mut chars = text.chars();
chars.next().map(|first| first.to_uppercase().chain(chars).collect()).unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recent_key_separates_custom_and_contributed_tasks() {
let custom = Task { label: "echo".into(), definition: json!({ "type": CUSTOMIZED_TASK_TYPE, "_key": "$customized,echo" }), workspace_folder: Some("/w".into()), ..Task::default() };
let npm = Task { label: "npm: build".into(), definition: json!({ "type": "npm", "_key": "npm,build," }), workspace_folder: Some("/w".into()), ..Task::default() };
assert_eq!(recent_key(&custom), "/w|echo");
assert_eq!(recent_key(&npm), "/w|npm,build,");
assert_eq!(capitalize("grunt"), "Grunt");
}
#[test]
fn group_defaults() {
let task = Task { group: Some(json!({ "_id": "build", "isDefault": true })), ..Task::default() };
assert!(in_group(&task, "build") && task_is_default(&task));
assert!(!in_group(&task, "test"));
}
#[gpui::test]
fn go_back_rebuilds_the_top_level_in_the_same_picker(cx: &mut gpui::TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = crate::theme::load_theme(&[themes], "Dark Modern").unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
workbench.update_in(cx, |workbench, window, cx| {
workbench.show_run_task_picker(None, window, cx);
let id = workbench.quick_inputs.find_id(|owner| matches!(owner, Owner::Tasks(_))).unwrap();
workbench.quick_inputs.get_mut(id).unwrap().set_value("gulp".into());
workbench.accept_task_pick(id, TaskPick::GoBack, PickAction::Run, window, cx);
assert_eq!(workbench.quick_inputs.get(id).unwrap().input_box.value, "");
let Some(Owner::Tasks(picker)) = workbench.quick_inputs.owner(id) else { panic!("the task picker is gone") };
assert!(matches!(picker.picks[0], TaskPick::Configure));
});
}
/// task.contribution.ts `openUserTasks`: without user tasks a template is picked and written to the profile's
/// tasks.json with `editor.insertSpaces`; once it has tasks, the file opens directly.
#[gpui::test]
fn open_user_tasks_writes_a_template_then_opens_the_file(cx: &mut gpui::TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = crate::theme::load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-user-tasks-{}", std::process::id()));
std::fs::create_dir_all(dir.join("User")).unwrap();
std::fs::write(dir.join("User/settings.json"), r#"{ "editor.insertSpaces": true, "editor.tabSize": 4 }"#).unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
let settings = config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None);
let tasks_file = settings.user_files().tasks.clone();
workbench.update(cx, |workbench, _| {
*workbench.settings.lock().unwrap() = settings;
workbench.start_tasks();
});
workbench.update_in(cx, |workbench, window, cx| {
drop(workbench.execute_command("workbench.action.tasks.openUserTasks", Vec::new(), window, cx));
let id = workbench.quick_inputs.find_id(|owner| matches!(owner, Owner::TaskTemplates(_))).expect("the template picker");
assert_eq!(workbench.quick_inputs.get(id).unwrap().placeholder.as_deref(), Some("Select a Task Template"));
workbench.quick_inputs.get_mut(id).unwrap().set_selected_items(&[3]);
workbench.task_template_event(id, Event::Accept(false), window, cx);
workbench.flush_quick_input_events(window, cx);
});
let written = std::fs::read_to_string(&tasks_file).unwrap();
assert!(written.starts_with("{\n \"version\": \"2.0.0\",\n \"tasks\": [\n {"), "{written}");
workbench.update_in(cx, |workbench, window, cx| {
let opened = workbench.editors.active().map(|editor| (editor.document().scheme.clone(), editor.document().path.clone()));
assert_eq!(opened, Some(("vscode-userdata".to_string(), tasks_file.clone())), "tasksResource is a user data resource");
*workbench.settings.lock().unwrap() = config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None);
drop(workbench.execute_command("workbench.action.tasks.openUserTasks", Vec::new(), window, cx));
assert!(workbench.quick_inputs.find_id(|owner| matches!(owner, Owner::TaskTemplates(_))).is_none(), "user tasks open without a template");
});
std::fs::remove_dir_all(&dir).unwrap();
}
/// abstractTaskService.ts `_computeUserTasks`: the profile's tasks.json tasks join the folder's, in the first
/// folder, with the "User" source and description and the `settings` recent key; Configure Task opens the
/// profile's file at the entry and Configure Default Build Task writes there.
#[gpui::test]
fn user_tasks_are_listed_with_the_folder_tasks_and_configured_in_the_profile(cx: &mut gpui::TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = crate::theme::load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-user-task-list-{}", std::process::id()));
let folder = dir.join("folder");
std::fs::create_dir_all(folder.join(".vscode")).unwrap();
std::fs::create_dir_all(dir.join("User")).unwrap();
std::fs::write(folder.join(".vscode/tasks.json"), r#"{ "version": "2.0.0", "tasks": [{ "label": "lint", "type": "shell", "command": "echo lint" }] }"#).unwrap();
std::fs::write(dir.join("User/tasks.json"), r#"{ "version": "2.0.0", "tasks": [{ "label": "hello", "type": "shell", "command": "echo ${workspaceFolder}", "problemMatcher": [] }] }"#).unwrap();
let workspace = config::workspace::Workspace::folder(folder.clone());
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), Some(workspace.clone()), crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
let settings = config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), Some(workspace));
workbench.update(cx, |workbench, _| {
*workbench.settings.lock().unwrap() = settings;
workbench.start_tasks();
});
workbench.update_in(cx, |workbench, window, cx| {
let tasks = workbench.configured_tasks();
let labels: Vec<_> = tasks.iter().map(|task| (task.label.as_str(), task.is_user())).collect();
assert_eq!(labels, [("lint", false), ("hello", true)]);
let user = tasks[1].clone();
assert_eq!((user.source.clone(), user.workspace_folder.clone()), (json!({ "label": "User" }), Some(folder.clone())));
assert_eq!(workbench.task_item(&user).description.as_deref(), Some("User"));
assert_eq!(recent_key(&user), "settings|hello");
workbench.configure_task(user.clone(), window, cx);
assert_eq!(workbench.editors.active().map(|editor| editor.document().path.clone()), Some(dir.join("User/tasks.json")));
let properties = serde_json::Map::from_iter([("group".to_owned(), json!({ "kind": "build", "isDefault": true }))]);
workbench.customize_task(&user, properties, false, window, cx);
});
let written: Value = config::json::parse(&std::fs::read_to_string(dir.join("User/tasks.json")).unwrap(), &mut Vec::new(), Default::default()).unwrap();
assert_eq!(written["tasks"][0]["group"], json!({ "kind": "build", "isDefault": true }));
assert!(!std::fs::read_to_string(folder.join(".vscode/tasks.json")).unwrap().contains("isDefault"));
std::fs::remove_dir_all(&dir).unwrap();
}
/// quickPickPin.ts on the run picker: every item has the pin button; a pin stores the item in workspace storage
/// and shows its copy under "pinned" while the filter is empty; the copy's pinned button removes it.
#[gpui::test]
fn the_run_task_picker_pins_items(cx: &mut gpui::TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = crate::theme::load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-task-pins-{}", std::process::id()));
let folder = dir.join("folder");
std::fs::create_dir_all(folder.join(".vscode")).unwrap();
std::fs::create_dir_all(dir.join("User")).unwrap();
std::fs::write(folder.join(".vscode/tasks.json"), r#"{ "version": "2.0.0", "tasks": [{ "label": "lint", "type": "shell", "command": "echo lint" }, { "label": "test", "type": "shell", "command": "echo test" }] }"#).unwrap();
let workspace = config::workspace::Workspace::folder(folder.clone());
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), Some(workspace.clone()), crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
let settings = config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), Some(workspace));
workbench.update(cx, |workbench, _| *workbench.settings.lock().unwrap() = settings);
let rows = |workbench: &Workbench, id: i64| -> Vec<String> {
workbench.quick_inputs.get(id).unwrap().items().iter().map(|entry| match entry {
Entry::Separator(label) => format!("-{}", label.clone().unwrap_or_default()),
Entry::Item(item) => format!("{} {}", item.label, item.buttons.iter().map(|button| button.codicon.clone().unwrap_or_default()).collect::<Vec<_>>().join(",")),
Entry::Header(_) => unreachable!(),
}).collect()
};
workbench.update_in(cx, |workbench, window, cx| {
workbench.show_run_task_picker(None, window, cx);
let id = workbench.quick_inputs.find_id(|owner| matches!(owner, Owner::Tasks(_))).unwrap();
assert_eq!(rows(workbench, id)[..3], ["-configured", "lint pin,gear", "test pin,gear"]);
workbench.task_pick_event(id, Event::TriggerItemButton(1, 0, None), window, cx);
assert_eq!(workbench.workspace_storage.lock().unwrap().get(PINNED_KEY), Some(r#"[{"label":"test"}]"#));
assert_eq!(rows(workbench, id)[..4], ["-pinned", "test pinned,gear", "-configured", "lint pin,gear"]);
workbench.quick_inputs.get_mut(id).unwrap().set_value("te".into());
workbench.flush_quick_input_events(window, cx);
assert_eq!(rows(workbench, id)[0], "-configured");
workbench.quick_inputs.get_mut(id).unwrap().set_value(String::new());
workbench.flush_quick_input_events(window, cx);
let Entry::Item(pinned) = &workbench.quick_inputs.get(id).unwrap().items()[1] else { panic!("the pinned copy") };
let pinned = pinned.handle;
workbench.task_pick_event(id, Event::TriggerItemButton(pinned, 0, None), window, cx);
assert_eq!(rows(workbench, id)[0], "-configured");
assert_eq!(workbench.workspace_storage.lock().unwrap().get(PINNED_KEY), Some("[]"));
});
std::fs::remove_dir_all(&dir).unwrap();
}
}