Visual Studio Code, rebuilt in Rust
Rust
use gpui::TestAppContext;
use serde_json::json;
use crate::editor::Position;
use crate::workbench::goto_symbol::tests::{answer, open};
/// breadcrumbsControl.ts: Focus Breadcrumbs focuses the last item, Left and Right move the focus and stop at the
/// ends, Space reveals the focused symbol in the editor, and Escape gives the editor the focus back.
#[gpui::test]
fn the_keyboard_focuses_moves_through_and_reveals_the_breadcrumbs(cx: &mut TestAppContext) {
let (workbench, cx, receiver, dir) = open(cx, "breadcrumbs-focus", "class A {\n run() {}\n}\n");
workbench.update(cx, |workbench, cx| {
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("User/keybindings.json"));
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = |start: usize, end: usize| json!({ "startLineNumber": start, "startColumn": 1, "endLineNumber": end, "endColumn": 20 });
let selection = |line: usize, column: usize| json!({ "startLineNumber": line, "startColumn": column, "endLineNumber": line, "endColumn": column + 1 });
answer(&receiver, "$provideDocumentSymbols", json!([
{ "name": "A", "detail": "", "kind": 4, "range": range(1, 3), "selectionRange": selection(1, 7), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": range(2, 2), "selectionRange": selection(2, 3), "children": [] },
] },
]), cx);
workbench.update_in(cx, |workbench, window, cx| {
workbench.refresh_outline_pane(cx);
workbench.reveal_outline_symbol("root/provider_0/A/run", false, window, cx);
});
let state = |cx: &mut gpui::VisualTestContext| {
workbench.update_in(cx, |workbench, window, _| {
let active = workbench.key_context(window)["breadcrumbsActive"] == json!(true);
(workbench.breadcrumbs_focused(), active, workbench.editor_focus.is_focused(window), workbench.editors.active().unwrap().selection().head)
})
};
assert_eq!(state(cx), (None, false, true, Position::new(1, 2)));
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-shift-;"));
assert_eq!(state(cx), (Some(2), true, false, Position::new(1, 2)), "a.txt, A, run: the last item");
cx.simulate_keystrokes("left left left");
assert_eq!(state(cx).0, Some(0), "the focus stops at the first item");
cx.simulate_keystrokes("right");
assert_eq!(state(cx).0, Some(1));
cx.simulate_keystrokes("space");
assert_eq!(state(cx), (None, false, true, Position::new(0, 6)), "Space reveals A at the start of its name");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-shift-; escape"));
assert_eq!(state(cx), (None, false, true, Position::new(0, 6)), "Escape focuses the editor");
}
/// breadcrumbsControl.ts `focusAndSelect` and breadcrumbsPicker.ts `BreadcrumbsOutlinePicker`: Shift+Cmd+. shows the
/// outline picker of the last item with its symbol focused and its parents expanded; Up and Down move the focus,
/// Left collapses, Enter reveals the symbol and closes the picker.
#[gpui::test]
fn focus_and_select_shows_the_outline_picker(cx: &mut TestAppContext) {
let (workbench, cx, receiver, dir) = open(cx, "breadcrumbs-outline-picker", "class A {\n run() {}\n stop() {}\n}\nclass B {}\n");
workbench.update(cx, |workbench, cx| {
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("User/keybindings.json"));
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = |start: usize, end: usize| json!({ "startLineNumber": start, "startColumn": 1, "endLineNumber": end, "endColumn": 20 });
let selection = |line: usize, column: usize| json!({ "startLineNumber": line, "startColumn": column, "endLineNumber": line, "endColumn": column + 1 });
answer(&receiver, "$provideDocumentSymbols", json!([
{ "name": "A", "detail": "", "kind": 4, "range": range(1, 4), "selectionRange": selection(1, 7), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": range(2, 2), "selectionRange": selection(2, 3), "children": [] },
{ "name": "stop", "detail": "", "kind": 5, "range": range(3, 3), "selectionRange": selection(3, 3), "children": [] },
] },
{ "name": "B", "detail": "", "kind": 4, "range": range(5, 5), "selectionRange": selection(5, 7), "children": [] },
]), cx);
workbench.update_in(cx, |workbench, window, cx| {
workbench.refresh_outline_pane(cx);
workbench.reveal_outline_symbol("root/provider_0/A/run", false, window, cx);
});
cx.run_until_parked();
let state = |cx: &mut gpui::VisualTestContext| {
workbench.update_in(cx, |workbench, window, _| {
let picker = workbench.breadcrumbs.picker.as_ref().map(|picker| {
let rows: Vec<String> = workbench.breadcrumbs_picker_rows(picker).into_iter().map(|row| row.key).collect();
(picker.item, rows, picker.focused.clone(), picker.focus.is_focused(window))
});
(picker, workbench.key_context(window)["breadcrumbsActive"] == json!(true), workbench.editors.active().unwrap().selection().head)
})
};
let ids = |names: &[&str]| names.iter().map(|name| format!("root/provider_0/{name}")).collect::<Vec<_>>();
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-shift-."));
let (picker, active, _) = state(cx);
assert_eq!(picker, Some((2, ids(&["A", "A/run", "A/stop", "B"]), Some(ids(&["A/run"])[0].clone()), true)), "run's picker, A expanded");
assert!(active, "a showing picker keeps breadcrumbsActive");
assert!(workbench.read_with(cx, |workbench, _| workbench.scrollable_drawn("breadcrumbs-picker-rows")), "breadcrumbs-picker-rows sits in a ScrollableElement");
let tree = cx.debug_bounds("breadcrumbs-picker-tree").unwrap();
let (scale, class) = workbench.update_in(cx, |workbench, window, _| (window.scale_factor(), workbench.color("symbolIcon.classForeground")));
let colors: Vec<gpui::Hsla> = cx.read_rendered_frame(|scene, _| scene.monochrome_sprites.iter().filter(|sprite| tree.contains(&sprite.bounds.center().map(|v| gpui::px(v.0 / scale)))).map(|sprite| sprite.color).collect());
assert!(!colors.is_empty() && !colors.contains(&class.into()), "the picker's symbol icons are uncolored: {colors:?}");
cx.simulate_keystrokes("alt-left");
assert_eq!(state(cx).0.map(|(item, _, focused, _)| (item, focused)), Some((1, Some(ids(&["A"])[0].clone()))), "focusPreviousWithPicker shows A's picker");
cx.simulate_keystrokes("alt-right");
assert_eq!(state(cx).0.map(|(item, _, focused, _)| (item, focused)), Some((2, Some(ids(&["A/run"])[0].clone()))), "and focusNextWithPicker run's again");
cx.simulate_keystrokes("down");
assert_eq!(state(cx).0.unwrap().2, Some(ids(&["A/stop"])[0].clone()));
cx.simulate_keystrokes("up up left");
assert_eq!(state(cx).0.unwrap().1, ids(&["A", "B"]), "Left collapses A");
cx.simulate_keystrokes("down enter");
assert_eq!(state(cx), (None, false, Position::new(4, 6)), "Enter reveals B and closes the picker");
}
/// breadcrumbsPicker.ts `BreadcrumbsFilePicker`: a click on the file item lists its folder, folders first, with the
/// file focused; Escape closes it, and Enter on another file opens it in the group.
#[gpui::test]
fn a_click_shows_the_file_picker(cx: &mut TestAppContext) {
let (workbench, cx, _receiver, dir) = open(cx, "breadcrumbs-file-picker", "text\n");
std::fs::write(dir.join("b.txt"), "b\n").unwrap();
std::fs::create_dir_all(dir.join("sub")).unwrap();
std::fs::create_dir_all(dir.join(".git")).unwrap();
workbench.update(cx, |workbench, _| {
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("User/keybindings.json"));
let seti = exthost::scan::scan_extension(&std::path::Path::new(env!("VSCODE_SRC")).join("extensions/theme-seti"), true).unwrap();
workbench.file_icons = crate::file_icons::FileIconTheme::load(&[seti], "vs-seti").ok();
});
cx.run_until_parked();
let file_item = cx.debug_bounds("breadcrumb-item-0").expect("the file item is drawn").center();
cx.simulate_click(file_item, gpui::Modifiers::none());
let rows = |cx: &mut gpui::VisualTestContext| {
workbench.update_in(cx, |workbench, window, _| {
workbench.breadcrumbs.picker.as_ref().map(|picker| {
let rows: Vec<String> = workbench.breadcrumbs_picker_rows(picker).into_iter().map(|row| row.key).collect();
(rows, picker.focused.clone(), picker.focus.is_focused(window))
})
})
};
let path = |name: &str| dir.join(name).to_string_lossy().into_owned();
assert_eq!(rows(cx), Some((vec![path("sub"), path("a.txt"), path("b.txt")], Some(path("a.txt")), true)), ".git is a files.exclude default");
// breadcrumbsPicker.ts `_layout`, contextview.ts and roundedCorners.css, measured against the reference with ui-pair:
// the last item's 8px padding, the picker at the item's left edge, the arrow's 8px and the 2px point anchor, the
// width plus the 1px border, the border, DefaultIndent and the twistie before a folder's contents, only DefaultIndent
// before a file's (Seti has no folder icons), and the focused row's rounded outline.
let item = cx.debug_bounds("breadcrumb-item-0").unwrap();
let label = cx.debug_bounds("breadcrumb-label-0").unwrap();
let tree = cx.debug_bounds("breadcrumbs-picker-tree").unwrap();
let contents = cx.debug_bounds("breadcrumbs-picker-row-contents-0").unwrap();
let file_contents = cx.debug_bounds("breadcrumbs-picker-row-contents-1").unwrap();
let viewport = cx.update(|window, _| f32::from(window.viewport_size().width)) - 8.;
let width = viewport.min((viewport / 4.17).max(240.)) + 2.;
let (scale, outline) = workbench.update_in(cx, |workbench, window, _| (window.scale_factor(), workbench.theme.color("list.focusOutline").or(workbench.theme.color("focusBorder")).unwrap()));
let radii: Vec<f32> = cx.read_rendered_frame(|scene, _| scene.quads.iter().filter(|quad| quad.border_color == outline.into() && quad.border_widths.top.0 > 0.).map(|quad| quad.corner_radii.top_left.0 / scale).collect());
let measured = (item.right() - label.right(), tree.left() - item.left(), tree.top() - item.bottom(), f32::from(tree.size.width).round() == width.round(), contents.left() - tree.left(), file_contents.left() - tree.left(), radii.contains(&4.));
assert_eq!(measured, (gpui::px(8.), gpui::px(0.), gpui::px(10.), true, gpui::px(1. + 8. + 16. + 6.), gpui::px(1. + 8.), true), "{:?} {width} {radii:?}", tree.size.width);
cx.simulate_keystrokes("escape");
assert_eq!(rows(cx), None, "Escape closes the picker");
let focused = workbench.update_in(cx, |workbench, window, _| workbench.editor_focus.is_focused(window));
assert!(focused, "and gives the editor the focus back");
cx.simulate_click(file_item, gpui::Modifiers::none());
cx.simulate_keystrokes("down enter");
let (picker, path) = workbench.update(cx, |workbench, _| (workbench.breadcrumbs.picker.is_some(), workbench.editors.active().unwrap().document().path.clone()));
assert_eq!((picker, path), (false, dir.join("b.txt")), "Enter opens b.txt and closes the picker");
cx.simulate_click(file_item, gpui::Modifiers::none());
cx.simulate_keystrokes(&crate::keybindings::primary_keys("up secondary-enter"));
workbench.read_with(cx, |workbench, _| {
let groups = workbench.groups_in_grid_order();
assert_eq!((groups.len(), workbench.editors.id), (2, groups[1]), "revealFocusedFromTreeAside opens to the side");
assert_eq!(workbench.editors.active().unwrap().document().path, dir.join("a.txt"));
assert!(workbench.breadcrumbs.picker.is_none());
});
}
/// breadcrumbsControl.ts `toggleToOn` turns the breadcrumbs on and selects the last item; with
/// `breadcrumbs.useQuickPick` a selection opens Quick Open, `@` for a symbol, instead of a picker.
#[gpui::test]
fn toggle_to_on_and_use_quick_pick(cx: &mut TestAppContext) {
let (workbench, cx, _receiver, dir) = open(cx, "breadcrumbs-toggle-to-on", "text\n");
workbench.update(cx, |workbench, cx| {
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("User/keybindings.json"));
// useQuickPick has no schema (breadcrumbs.ts `BreadcrumbsConfig`), so only settings.json sets it.
std::fs::create_dir_all(dir.join("User")).unwrap();
std::fs::write(dir.join("User/settings.json"), r#"{ "breadcrumbs.enabled": false, "breadcrumbs.useQuickPick": true }"#).unwrap();
workbench.reload_changed_settings_files(cx);
});
cx.run_until_parked();
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-shift-."));
cx.run_until_parked();
workbench.update(cx, |workbench, _| {
assert_eq!(workbench.setting("breadcrumbs.enabled"), json!(true), "toggleToOn turns the breadcrumbs on");
assert!(workbench.breadcrumbs.picker.is_none(), "useQuickPick shows no picker");
let value = workbench.quick_inputs.current().map(|input| input.input_box.value.clone());
assert_eq!(value, Some(String::new()), "the file item opens Quick Open");
});
}
/// breadcrumbsControl.ts takes any resource with a file system provider: View Binary Data's hex editor on
/// `vscode-debug-memory://<session>/3/memory.bin` shows `3 > memory.bin`, outside the workspace folder and the home.
#[gpui::test]
fn a_custom_editor_on_a_debug_memory_resource_has_breadcrumbs(cx: &mut TestAppContext) {
let (workbench, cx, _receiver, _dir) = open(cx, "breadcrumbs-debug-memory", "text\n");
workbench.update(cx, |workbench, cx| {
let custom = crate::workbench::custom_editors::CustomEditor {
resource: crate::workbench::debug_memory::memory_uri("session-1", "3"),
resolved: true,
backup_id: None,
navigation: crate::workbench::custom_editors::Navigation::None,
diff: None,
};
workbench.add_custom_document_editor("hexEditor.hexedit", "memory.bin".into(), &json!({}), false, Some(custom), false);
cx.notify();
});
cx.run_until_parked();
let elements = workbench.read_with(cx, |workbench, _| {
workbench.breadcrumb_elements().into_iter().map(|element| match element {
super::BreadcrumbElement::Folder(scheme, path, root) => format!("folder {scheme}:{} {root}", path.display()),
super::BreadcrumbElement::File(scheme, path) => format!("file {scheme}:{}", path.display()),
super::BreadcrumbElement::Symbol(_) => "symbol".into(),
}).collect::<Vec<_>>()
});
assert_eq!(elements, ["folder vscode-debug-memory:/3 false", "file vscode-debug-memory:/3/memory.bin"]);
assert!(cx.debug_bounds("breadcrumb-item-1").is_some(), "3 > memory.bin is drawn");
assert!(cx.debug_bounds("breadcrumb-item-2").is_none());
// FileDataSource resolves the folder `3` through the provider, whose `stat` answers a file: no rows, not the disk's `/`.
let folder_item = cx.debug_bounds("breadcrumb-item-0").unwrap().center();
cx.simulate_click(folder_item, gpui::Modifiers::none());
let rows = workbench.read_with(cx, |workbench, _| workbench.breadcrumbs.picker.as_ref().map(|picker| workbench.breadcrumbs_picker_rows(picker).len()));
assert_eq!(rows, Some(0));
}