Visual Studio Code, rebuilt in Rust
Rust
use std::collections::HashMap;
use serde_json::{Value, json};
use super::*;
fn command(id: &str, title: &str, group: &str, order: f64) -> MenuItem {
let entry = Entry::Command { id: id.into(), precondition: None, toggled: None, alt: None };
MenuItem { menu: MenuId::Test, entry, title: title.into(), group: group.into(), order, when: None }
}
fn ids(actions: &[MenuAction]) -> Vec<&str> {
actions
.iter()
.map(|action| match action {
MenuAction::Command { id, .. } => id.as_str(),
MenuAction::Submenu { title, .. } => title.as_str(),
MenuAction::Separator => "-",
})
.collect()
}
fn no_context() -> HashMap<String, Value> {
HashMap::new()
}
#[test]
fn group_sorting() {
let registry = [command("one", "FOO", "0_hello", 0.), command("two", "FOO", "hello", 0.), command("three", "FOO", "Hello", 0.), command("four", "FOO", "", 0.), command("five", "FOO", "navigation", 0.)];
assert_eq!(ids(&menu_actions(®istry, MenuId::Test, &no_context())), ["five", "-", "one", "-", "two", "-", "three", "-", "four"]);
}
#[test]
fn in_group_sorting_by_title() {
let registry = [command("a", "aaa", "Hello", 0.), command("b", "fff", "Hello", 0.), command("c", "zzz", "Hello", 0.)];
assert_eq!(ids(&menu_actions(®istry, MenuId::Test, &no_context())), ["a", "b", "c"]);
}
#[test]
fn in_group_sorting_by_title_and_order() {
let registry = [command("a", "aaa", "Hello", 10.), command("b", "fff", "Hello", 0.), command("c", "zzz", "Hello", -1.), command("d", "yyy", "Hello", -1.)];
assert_eq!(ids(&menu_actions(®istry, MenuId::Test, &no_context())), ["d", "c", "b", "a"]);
}
#[test]
fn in_group_sorting_special_navigation() {
let registry = [command("a", "aaa", "navigation", 1.3), command("b", "fff", "navigation", 1.2), command("c", "zzz", "navigation", 1.1)];
assert_eq!(ids(&menu_actions(®istry, MenuId::Test, &no_context())), ["c", "b", "a"]);
}
#[test]
fn when_hides_precondition_disables_and_toggled_checks() {
let mut hidden = command("hidden", "Hidden", "1", 0.);
hidden.when = Expr::deserialize("shown");
let mut disabled = command("disabled", "Disabled", "1", 1.);
disabled.entry = Entry::Command { id: "disabled".into(), precondition: Expr::deserialize("enabled"), toggled: Toggled::when(Expr::deserialize("!enabled")), alt: None };
let context = HashMap::from([("shown".to_string(), json!(false)), ("enabled".to_string(), json!(false))]);
let actions = menu_actions(&[hidden, disabled], MenuId::Test, &context);
assert_eq!(actions, [MenuAction::Command { id: "disabled".into(), title: "Disabled".into(), enabled: false, checked: true, alt: None }]);
}
/// menuService.test.ts 'one-shot and persistent menus use the same group and item ordering': the localized title
/// sorts by its English original, which is the port's only title.
#[test]
fn group_and_item_ordering_with_ties() {
let mut hidden = command("hidden", "Hidden", "", 0.);
hidden.when = Some(Expr::has("hidden"));
let registry = [
command("z", "Z", "", 0.),
hidden,
command("a", "A", "", 0.),
command("first", "First", "", -1.),
command("navigation", "Navigation", "navigation", 0.),
command("localized", "AAA", "primary", 0.),
command("middle", "BBB", "primary", 0.),
command("tie", "BBB", "primary", 0.),
];
assert_eq!(ids(&menu_actions(®istry, MenuId::Test, &no_context())), ["navigation", "-", "localized", "middle", "tie", "-", "first", "a", "z"]);
}
/// menuService.test.ts 'getMenuActions omits empty submenus and observes nested registrations'.
#[test]
fn a_submenu_of_empty_submenus_is_left_out() {
let submenu = |menu, submenu, title: &str| MenuItem { menu, entry: Entry::Submenu { submenu, toggled: None }, title: title.into(), group: String::new(), order: 0., when: None };
let mut registry = vec![submenu(MenuId::Test, MenuId::ThemesSubMenu, "Submenu"), submenu(MenuId::ThemesSubMenu, MenuId::MenubarHelpMenu, "Nested submenu")];
assert_eq!(menu_actions(®istry, MenuId::Test, &no_context()), []);
let mut nested = command("nested", "Nested", "", 0.);
nested.menu = MenuId::MenubarHelpMenu;
registry.push(nested);
assert_eq!(ids(&menu_actions(®istry, MenuId::Test, &no_context())), ["Submenu"]);
}
/// menuService.test.ts 'getMenuActions reevaluates enablement, toggled state and alternate actions'.
#[test]
fn enablement_toggled_title_and_alt_precondition() {
let mut item = command("command", "Command", "", 0.);
let toggled = Some(Toggled { condition: Expr::has("toggled"), title: Some("Toggled".into()), icon: None });
let alt = Some(Alt { id: "alt".into(), title: "Alternate".into(), precondition: Some(Expr::has("enabled")) });
item.entry = Entry::Command { id: "command".into(), precondition: Some(Expr::has("enabled")), toggled, alt };
let registry = [item];
let snapshot = |enabled: bool, toggled: bool| {
let context = HashMap::from([("enabled".to_string(), json!(enabled)), ("toggled".to_string(), json!(toggled))]);
let actions = menu_actions(®istry, MenuId::Test, &context);
let [MenuAction::Command { enabled, checked, title, alt, .. }] = actions.as_slice() else { unreachable!() };
(*enabled, *checked, title.clone(), alt.is_some())
};
assert_eq!(snapshot(true, false), (true, false, "Command".to_string(), true));
assert_eq!(snapshot(false, true), (false, true, "Toggled".to_string(), false));
}
/// menuService.test.ts 'getMenuActions evaluates submenu toggle state without checking its children'.
#[test]
fn submenu_toggle_state_leaves_its_children_unchecked() {
let submenu = MenuItem { menu: MenuId::Test, entry: Entry::Submenu { submenu: MenuId::ThemesSubMenu, toggled: Some(Expr::has("submenuActive")) }, title: "Submenu".into(), group: String::new(), order: 0., when: None };
let mut child = command("child", "Child", "", 0.);
child.menu = MenuId::ThemesSubMenu;
let registry = [submenu, child];
let snapshot = |active: bool| {
let context = HashMap::from([("submenuActive".to_string(), json!(active))]);
let actions = menu_actions(®istry, MenuId::Test, &context);
let [MenuAction::Submenu { checked, actions: children, .. }] = actions.as_slice() else { unreachable!() };
let [MenuAction::Command { checked: child_checked, .. }] = children.as_slice() else { unreachable!() };
(*checked, *child_checked)
};
assert_eq!(snapshot(false), (false, false));
assert_eq!(snapshot(true), (true, false));
}
#[test]
fn submenus_without_actions_are_left_out() {
let submenu = |title: &str, group: &str| MenuItem { menu: MenuId::Test, entry: Entry::Submenu { submenu: MenuId::ThemesSubMenu, toggled: None }, title: title.into(), group: group.into(), order: 0., when: None };
let mut theme = command("theme", "Color Theme", "", 1.);
theme.menu = MenuId::ThemesSubMenu;
let empty = MenuItem { entry: Entry::Submenu { submenu: MenuId::MenubarHelpMenu, toggled: None }, ..submenu("Empty", "2") };
let actions = menu_actions(&[submenu("Themes", "1"), empty, theme], MenuId::Test, &no_context());
assert_eq!(actions, [MenuAction::Submenu { title: "Themes".into(), actions: vec![MenuAction::Command { id: "theme".into(), title: "Color Theme".into(), enabled: true, checked: false, alt: None }], checked: false }]);
}
fn extension(id: &str, contributes: Value) -> ScannedExtension {
ScannedExtension { id: id.into(), location: Default::default(), description: json!({ "name": id, "publisher": "test", "contributes": contributes }) }
}
/// menusExtensionPoint.ts: command items take the title and enablement of `contributes.commands`, `group@order`
/// splits, items of unknown commands and unknown menus are skipped.
#[test]
fn extension_menu_items() {
let extensions = [extension(
"a",
json!({
"commands": [
{ "command": "a.open", "title": "Open Preview", "enablement": "ready" },
{ "command": "a.alt", "title": "Open to the Side" },
{ "command": "a.lockedAlt", "title": "Locked", "enablement": "ready" }
],
"menus": {
"explorer/context": [
{ "command": "a.open", "alt": "a.alt", "group": "navigation@2", "when": "resourceLangId == markdown" },
{ "command": "a.missing", "group": "navigation" },
{ "command": "a.alt", "group": "4_search@x" },
{ "command": "a.alt", "alt": "a.lockedAlt", "group": "5_locked", "when": "resourceLangId == markdown" }
],
"notAMenu": [{ "command": "a.open" }]
}
}),
)];
let items = extension_menus(&extensions);
let summary: Vec<_> = items.iter().map(|item| (item.menu, item.title.as_str(), item.group.as_str(), item.order)).collect();
assert_eq!(
summary,
[(MenuId::ExplorerContext, "Open Preview", "navigation", 2.), (MenuId::ExplorerContext, "Open to the Side", "4_search", 0.), (MenuId::ExplorerContext, "Open to the Side", "5_locked", 0.)]
);
let context = HashMap::from([("resourceLangId".to_string(), json!("markdown"))]);
let actions = menu_actions(&items, MenuId::ExplorerContext, &context);
let open = MenuAction::Command { id: "a.open".into(), title: "Open Preview".into(), enabled: false, checked: false, alt: Some(("a.alt".into(), "Open to the Side".into())) };
assert_eq!(actions[0], open);
// An alt whose command's enablement does not hold is not offered (`getActionForAltMode`).
assert!(matches!(&actions[4], MenuAction::Command { id, alt: None, .. } if id == "a.alt"));
assert_eq!(ids(&menu_actions(&items, MenuId::ExplorerContext, &no_context())), ["a.alt"]);
}
/// menusExtensionPoint.ts `apiMenus`: the keys of the menus a view of the port shows.
#[test]
fn api_menu_keys_reach_their_menus() {
let keys = [
("terminal/context", MenuId::TerminalInstanceContext),
("terminal/title/context", MenuId::TerminalTabContext),
("scm/resourceFolder/context", MenuId::ScmResourceFolderContext),
("debug/callstack/context", MenuId::DebugCallStackContext),
("debug/variables/context", MenuId::DebugVariablesContext),
("debug/watch/context", MenuId::DebugWatchContext),
("scm/historyItemRef/context", MenuId::ScmHistoryItemRefContext),
("viewContainer/title", MenuId::ViewContainerTitle),
("extension/context", MenuId::ExtensionContext),
("editor/title/run", MenuId::EditorTitleRun),
("notebook/cell/execute", MenuId::NotebookCellExecute),
("notebook/kernelSource", MenuId::NotebookKernelSource),
("notebook/variables/context", MenuId::NotebookVariablesContext),
];
for (key, menu) in keys {
let item = json!({ "command": "k.run", "when": "viewContainer == workbench.view.debug" });
let mut contributor = extension("k", json!({ "commands": [{ "command": "k.run", "title": "Run" }], "menus": { key: [item] } }));
contributor.description["enabledApiProposals"] = json!(["contribSourceControlHistoryItemMenu", "contribViewContainerTitle", "notebookKernelSource"]);
let items = extension_menus(&[contributor]);
let reached: Vec<_> = items.iter().map(|item| item.menu).collect();
assert_eq!(reached, [menu], "{key}");
}
}
/// `isValidItems`: one invalid item drops the whole menu of that extension.
#[test]
fn an_invalid_item_drops_its_menu() {
let extensions = [extension(
"a",
json!({
"commands": [{ "command": "a.run", "title": "Run" }],
"menus": { "editor/context": [{ "command": "a.run" }, { "command": "a.run", "when": 1 }], "editor/title/context": [{ "command": "a.run" }] }
}),
)];
let menus: Vec<_> = extension_menus(&extensions).iter().map(|item| item.menu).collect();
assert_eq!(menus, [MenuId::EditorTitleContext]);
}
/// `editor/content` is a proposed menu: only an extension with `contribEditorContentMenu` enabled contributes to it.
#[test]
fn editor_content_needs_its_proposal() {
let contributes = json!({ "commands": [{ "command": "a.run", "title": "Run" }], "menus": { "editor/content": [{ "command": "a.run" }] } });
let mut proposed = extension("a", contributes.clone());
proposed.description["enabledApiProposals"] = json!(["contribEditorContentMenu"]);
let menus = |extension: ScannedExtension| extension_menus(&[extension]).iter().map(|item| item.menu).collect::<Vec<_>>();
assert_eq!(menus(extension("a", contributes)), Vec::<MenuId>::new());
assert_eq!(menus(proposed), [MenuId::EditorContent]);
}
/// `contributes.submenus`: a submenu item opens the submenu's own items; a submenu is added once per menu, the
/// first registration of an id wins, and one without a label is skipped.
#[test]
fn extension_submenus() {
let extensions = [
extension(
"a",
json!({
"commands": [{ "command": "a.one", "title": "One" }],
"submenus": [{ "id": "a.more", "label": "More", "icon": "$(plus)" }, { "id": "a.blank", "label": "" }],
"menus": {
"editor/context": [{ "submenu": "a.more", "group": "1_more" }, { "submenu": "a.more" }, { "submenu": "a.blank" }],
"a.more": [{ "command": "a.one" }]
}
}),
),
extension("b", json!({ "submenus": [{ "id": "a.more", "label": "Again", "icon": "$(gear)" }] })),
];
let items = extension_menus(&extensions);
let actions = menu_actions(&items, MenuId::EditorContext, &no_context());
let one = MenuAction::Command { id: "a.one".into(), title: "One".into(), enabled: true, checked: false, alt: None };
assert_eq!(actions, [MenuAction::Submenu { title: "More".into(), actions: vec![one], checked: false }]);
assert_eq!(submenu_icons(&extensions), [(MenuId::Api(0), CommandIcon::Codicon("plus".into()))].into());
}
/// menusExtensionPoint.ts, the `submenus` and `menus` handlers: what they skip is a message of the extension.
#[test]
fn skipped_menu_contributions_are_extension_messages() {
let contributes = json!({
"commands": [{ "command": "m.run", "title": "Run" }],
"submenus": [{ "id": "m.more", "label": "More" }, { "id": "m.more", "label": "Again" }, { "id": "m.blank", "label": "" }, { "id": 1 }, "m.text"],
"menus": {
"editor/context": [{ "submenu": "m.more" }, { "submenu": "m.more" }, { "submenu": "m.unknown" }, { "command": "m.run", "alt": "m.run" }],
"editor/title/context": [{ "command": "m.run", "group": 1 }],
"explorer/context": { "command": "m.run" },
"file/newFile": [{ "submenu": "m.more" }],
"editor/content": [{ "command": "m.run" }]
}
});
extension_menus(&[extension("menu-messages", contributes)]);
let messages: Vec<_> = crate::perf::messages("menu-messages").into_iter().map(|(severity, message)| format!("{severity:?}: {message}")).collect();
assert_eq!(
messages,
[
"Info: The `m.more` submenu was already previously registered.",
"Warning: `` is not a valid submenu label",
"Error: property `id` is mandatory and must be of type `string`",
"Error: submenu items must be an object",
"Warning: The `m.more` submenu was already contributed to the `editor/context` menu.",
"Error: Menu item references a submenu `m.unknown` which is not defined in the 'submenus' section.",
"Info: Menu item references the same command as default and alt-command",
"Error: property `group` can be omitted or must be of type `string`",
"Error: submenu items must be an array",
"Error: Menu item references a submenu for a menu which doesn't have submenu support.",
"Error: editor/content is a proposed menu identifier. It requires 'package.json#enabledApiProposals: [\"contribEditorContentMenu\"]' and is only available when running out of dev or with the following command line switch: --enable-proposed-api menu-messages",
]
);
}
/// menusExtensionPoint.ts: a `viewContainer/title` item whose `when` does not check the Run and Debug container is
/// left out with an error; the others stay.
#[test]
fn view_container_title_items_must_check_the_debug_container() {
let mut contributor = extension(
"container-title",
json!({
"commands": [{ "command": "c.run", "title": "Run" }],
"menus": { "viewContainer/title": [{ "command": "c.run", "when": "viewContainer == workbench.view.explorer" }, { "command": "c.run", "group": "navigation", "when": "viewContainer == workbench.view.debug" }] }
}),
);
contributor.description["enabledApiProposals"] = json!(["contribViewContainerTitle"]);
let items = extension_menus(&[contributor]);
let reached: Vec<_> = items.iter().map(|item| (item.menu, item.group.as_str())).collect();
assert_eq!(reached, [(MenuId::ViewContainerTitle, "navigation")]);
let messages: Vec<_> = crate::perf::messages("container-title").into_iter().map(|(severity, message)| format!("{severity:?}: {message}")).collect();
assert_eq!(messages, ["Error: The `viewContainer/title` menu contribution must check `viewContainer == workbench.view.debug` in its \"when\" clause."]);
}
/// clipboard.ts and menusExtensionPoint.ts: the github extension's share items fill the Share submenus of the
/// Explorer and editor context menus; Copy As stays hidden while nothing fills it.
#[test]
fn the_github_share_items_fill_the_share_submenus() {
let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
let github = exthost::scan::scan_extension(&app_root.join("extensions/github"), true).unwrap();
let mut registry = workbench_menus();
registry.extend(extension_menus(&[github]));
let context = HashMap::from([("github.hasGitHubRepo".to_string(), json!(true)), ("resourceScheme".to_string(), json!("file")), ("editorTextFocus".to_string(), json!(true))]);
let share = |menu| {
let actions = menu_actions(®istry, menu, &context);
let submenu = actions.iter().find(|action| matches!(action, MenuAction::Submenu { title, .. } if title == "Share")).cloned();
(ids(&actions).contains(&"Copy As"), submenu)
};
let link = |id: &str, title: &str| Some(MenuAction::Submenu { title: "Share".into(), actions: vec![MenuAction::Command { id: id.into(), title: title.into(), enabled: true, checked: false, alt: None }], checked: false });
assert_eq!(share(MenuId::ExplorerContext), (false, link("github.copyVscodeDevLinkWithoutRange", "Copy vscode.dev Link")));
assert_eq!(share(MenuId::EditorContext), (false, link("github.copyVscodeDevLink", "Copy vscode.dev Link")));
assert_eq!(share(MenuId::EditorTitleContext), (false, link("github.copyVscodeDevLinkWithoutRange", "Copy vscode.dev Link")));
// coreActions.ts: a notebook cell's toolbar menu shows the editor context Share submenu.
let cell_context = HashMap::from([("github.hasGitHubRepo".to_string(), json!(true)), ("resourceScheme".to_string(), json!("vscode-notebook-cell")), ("activeEditor".to_string(), json!("workbench.editor.notebook"))]);
assert!(menu_actions(®istry, MenuId::NotebookCellTitle, &cell_context).contains(&link("github.copyVscodeDevLink", "Copy vscode.dev Link").unwrap()));
assert!(!ids(&menu_actions(®istry, MenuId::ExplorerContext, &no_context())).contains(&"Share"), "no repository, no Share");
}
/// The activity bar's Manage menu: the reference's items in its groups; Tasks once the host can run tasks.
#[test]
fn the_manage_menu_lists_the_global_activity_items() {
let registry = workbench_menus();
let actions = menu_actions(®istry, MenuId::GlobalActivity, &no_context());
let without_tasks = ["workbench.action.showCommands", "-", "workbench.action.openSettings", "workbench.view.extensions", "workbench.action.openGlobalKeybindings", "workbench.action.openSnippets", "Themes"];
assert_eq!(ids(&actions), without_tasks);
let context = HashMap::from([("shellExecutionSupported".to_owned(), json!(true)), ("processExecutionSupported".to_owned(), json!(true))]);
let actions = menu_actions(®istry, MenuId::GlobalActivity, &context);
assert_eq!(ids(&actions)[5..7], ["workbench.action.openSnippets", "workbench.action.tasks.openUserTasks"]);
// update.ts `appendUpdateMenuItems`: one item of the update state, in its own group; none while disabled.
for (state, id) in [("idle", "update.check"), ("checking for updates", "update.checking"), ("available for download", "update.downloadNow"), ("downloading", "update.downloading"), ("downloaded", "update.install"), ("ready", "update.restart")] {
let context = HashMap::from([("updateState".to_owned(), json!(state))]);
assert_eq!(ids(&menu_actions(®istry, MenuId::GlobalActivity, &context))[without_tasks.len()..], ["-", id]);
}
}
/// modernUI.contribution.test.ts: Layout Density follows Themes in the Manage menu while the modern UI is on, and
/// its options are checked by `window.density.layout`.
#[test]
fn layout_density_is_in_the_manage_menu_with_the_current_density_checked() {
let registry = workbench_menus();
let context = HashMap::from([("config.workbench.experimental.modernUI".to_owned(), json!(true)), ("config.window.density.layout".to_owned(), json!("compact"))]);
assert_eq!(ids(&menu_actions(®istry, MenuId::GlobalActivity, &context)).last(), Some(&"Layout Density"));
let options = menu_actions(®istry, MenuId::LayoutDensityMenu, &context);
let checked: Vec<_> = options.iter().map(|action| match action {
MenuAction::Command { id, checked, .. } => (id.as_str(), *checked),
_ => unreachable!(),
}).collect();
assert_eq!(checked, [("workbench.action.setLayoutDensity.default", false), ("workbench.action.setLayoutDensity.compact", true)]);
}
#[test]
fn mnemonic_menu_label_drops_mnemonics() {
assert_eq!(mnemonic_menu_label("Save A&&ll"), "Save All");
assert_eq!(mnemonic_menu_label("Open (&&O)"), "Open ");
assert_eq!(mnemonic_menu_label("Fish & Chips"), "Fish & Chips");
}
#[test]
fn refactor_and_source_action_show_for_the_kinds_the_providers_offer() {
let registry = workbench_menus();
let shown = |supported: &str| {
let context: HashMap<String, Value> = [("supportedCodeAction".to_owned(), json!(supported)), ("editorHasCodeActionsProvider".to_owned(), json!(true))].into();
let actions = menu_actions(®istry, MenuId::EditorContext, &context);
ids(&actions).into_iter().filter(|id| id.starts_with("editor.action.refactor") || id.starts_with("editor.action.sourceAction")).map(str::to_owned).collect::<Vec<_>>()
};
assert_eq!(shown(""), Vec::<String>::new());
assert_eq!(shown("quickfix refactor.extract source.organizeImports"), ["editor.action.refactor", "editor.action.sourceAction"]);
assert_eq!(shown("refactoring"), Vec::<String>::new(), "`\\b` ends the kind");
}
/// debugToolBar.ts: the toolbar's own items in their order with their `when` and preconditions, and an
/// extension's `debug/toolBar` item among them; `debug/createConfiguration` needs its API proposal.
#[test]
fn the_debug_toolbar_and_create_configuration_menus_take_contributed_items() {
let contributes = json!({
"commands": [{ "command": "x.hotReload", "title": "Hot Reload" }, { "command": "x.create", "title": "Create a Configuration" }],
"menus": { "debug/toolBar": [{ "command": "x.hotReload", "group": "navigation@65" }], "debug/createConfiguration": [{ "command": "x.create" }] }
});
let mut proposed = extension("debug-menus", contributes.clone());
proposed.description["enabledApiProposals"] = json!(["contribDebugCreateConfiguration"]);
let mut registry = workbench_menus();
registry.extend(extension_menus(&[proposed]));
let stopped = HashMap::from([("debugState".to_string(), json!("stopped")), ("terminateDebuggeeSupported".to_string(), json!(true))]);
let actions = menu_actions(®istry, MenuId::DebugToolBar, &stopped);
assert_eq!(
ids(&actions),
["workbench.action.debug.continue", "workbench.action.debug.stepOver", "workbench.action.debug.stepInto", "workbench.action.debug.stepOut", "workbench.action.debug.restart", "x.hotReload", "workbench.action.debug.stop"]
);
assert!(matches!(&actions[3], MenuAction::Command { enabled: false, .. }), "one frame, no Step Out: {:?}", actions[3]);
assert!(matches!(&actions[6], MenuAction::Command { alt: Some((alt, _)), .. } if alt == "workbench.action.debug.disconnect"), "{:?}", actions[6]);
assert_eq!(ids(&menu_actions(®istry, MenuId::DebugCreateConfiguration, &no_context())), ["x.create"]);
assert!(extension_menus(&[extension("debug-menus-unproposed", contributes)]).iter().all(|item| item.menu != MenuId::DebugCreateConfiguration));
}