Visual Studio Code, rebuilt in Rust
Rust
use std::path::Path;
use std::sync::{Arc, Mutex};
use exthost::scan::scan_extension;
use gpui::{Modifiers, MouseButton, MouseDownEvent, TestAppContext, point, px};
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;
use storage::Storage;
#[gpui::test]
fn dragging_the_header_moves_the_quick_input_and_a_double_click_puts_it_back(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-drag-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let storage = Arc::new(Mutex::new(Storage::in_memory()));
let profile = storage.clone();
let (workbench, cx) = cx.add_window_view(|_, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.restore_profile_state(profile.clone(), profile);
workbench
});
cx.run_until_parked();
cx.simulate_keystrokes(&crate::keybindings::primary_keys("shift-secondary-p"));
let bounds = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.quick_inputs.bounds.get());
let start = bounds(cx);
// The input box, here its top border, is not a drag area.
let in_input = start.origin + point(start.size.width / 2., px(6.5));
cx.simulate_mouse_down(in_input, MouseButton::Left, Modifiers::none());
assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.drag.is_none()));
cx.simulate_mouse_up(in_input, MouseButton::Left, Modifiers::none());
// The header's 6px padding is.
let grab = start.origin + point(px(3.), px(3.));
cx.simulate_mouse_down(grab, MouseButton::Left, Modifiers::none());
cx.simulate_mouse_move(grab + point(px(-100.), px(100.)), MouseButton::Left, Modifiers::none());
cx.simulate_mouse_up(grab + point(px(-100.), px(100.)), MouseButton::Left, Modifiers::none());
let moved = bounds(cx);
assert_eq!(moved.origin - start.origin, point(px(-100.), px(100.)));
let view_state = workbench.read_with(cx, |workbench, _| workbench.quick_inputs.view_state);
assert!(view_state.is_some());
let stored = storage.lock().unwrap().get("workbench.quickInput.viewState").map(str::to_owned).expect("the drag end stores the position");
assert!(stored.starts_with(r#"{"top":"#) && stored.contains(r#","left":"#), "{stored}");
// Released: moving the pointer leaves it in place.
cx.simulate_mouse_move(grab, None, Modifiers::none());
assert_eq!(bounds(cx).origin, moved.origin);
let header = moved.origin + point(px(3.), px(3.));
cx.simulate_event(MouseDownEvent { position: header, modifiers: Modifiers::none(), button: MouseButton::Left, click_count: 2, first_mouse: false });
cx.simulate_mouse_up(header, MouseButton::Left, Modifiers::none());
assert_eq!(bounds(cx).origin, start.origin);
assert_eq!(storage.lock().unwrap().get("workbench.quickInput.viewState"), None, "the reset removes the key");
// The next window reads the stored position.
storage.lock().unwrap().set("workbench.quickInput.viewState", stored);
workbench.update(cx, |workbench, _| workbench.load_quick_input_view_state());
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.view_state), view_state);
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn the_quick_input_stays_open_while_the_window_is_inactive(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-deactivate-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench
});
cx.update(|window, _| window.activate_window());
cx.run_until_parked();
cx.simulate_keystrokes(&crate::keybindings::primary_keys("shift-secondary-p"));
let shown = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.quick_inputs.visible.is_some());
assert!(shown(cx));
cx.deactivate_window();
assert!(shown(cx), "a window deactivation is no blur");
cx.update(|window, _| window.activate_window());
cx.run_until_parked();
cx.simulate_keystrokes("a");
let value = workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().map(|input| input.input_box.value.clone()));
assert_eq!(value.as_deref(), Some(">a"));
cx.update(|window, cx| window.blur(cx));
cx.run_until_parked();
assert!(!shown(cx), "a real focus out hides it");
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn the_word_highlight_stops_while_the_quick_input_has_the_focus(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-word-blur-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "the cat\nthe dog").unwrap();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.watch_editor_focus(window, cx);
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.update(|window, _| window.activate_window());
cx.run_until_parked();
let highlights = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().highlights.len());
assert_eq!(highlights(cx), 2);
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-p"));
assert_eq!(highlights(cx), 0, "onDidBlurEditorWidget");
cx.deactivate_window();
cx.update(|window, _| window.activate_window());
cx.run_until_parked();
assert_eq!(highlights(cx), 0);
cx.simulate_keystrokes("escape");
assert_eq!(highlights(cx), 2, "onDidFocusEditorText");
// A picked command that shows another quick input (Compare Active File With...): the editor had the focus back
// for a moment, so upstream's query, 50 ms later, still highlights.
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-p"));
workbench.update_in(cx, |workbench, window, cx| {
let id = workbench.quick_inputs.visible.unwrap();
workbench.hide_quick_input_by_id(id, window, cx);
drop(workbench.execute_command("workbench.files.action.compareFileWith", Vec::new(), window, cx));
});
cx.run_until_parked();
assert_eq!(highlights(cx), 0);
cx.executor().advance_clock(std::time::Duration::from_millis(50));
cx.run_until_parked();
assert_eq!(highlights(cx), 2, "runDelayer");
assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.visible.is_some()));
cx.simulate_keystrokes("escape");
// A picked command edits before GPUI reports the restored focus; the edit stops upstream's pending query.
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-p"));
workbench.update_in(cx, |workbench, window, cx| {
workbench.editors.active_mut().unwrap().type_text("x");
window.focus(&workbench.editor_focus.clone(), cx);
});
cx.run_until_parked();
assert_eq!(highlights(cx), 0, "onDidChangeModelContent");
// A window deactivation keeps the DOM focus: the highlights stay, and the reactivation runs no query, so an
// edit made meanwhile (a dialog's action) leaves them off.
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-z"));
workbench.update(cx, |workbench, _| workbench.editors.active_mut().unwrap().refresh_highlights());
assert_eq!(highlights(cx), 2);
cx.deactivate_window();
cx.run_until_parked();
assert_eq!(highlights(cx), 2);
workbench.update(cx, |workbench, _| _ = workbench.editors.active_mut().unwrap().type_text("x"));
cx.update(|window, _| window.activate_window());
cx.run_until_parked();
assert_eq!(highlights(cx), 0);
std::fs::remove_dir_all(dir).unwrap();
}
/// quickinput.test.ts "keepScrollPosition - works with activeItems" and "keepScrollPosition - works with items".
#[gpui::test]
fn keep_scroll_position_keeps_the_list_where_it_is(cx: &mut TestAppContext) {
use crate::main_thread_quick_open::Owner;
use crate::quick_input::{Entry, Item, Kind, QuickInput};
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx));
let items: Vec<Entry> = (0..1000).map(|handle| Entry::Item(Item { handle, label: format!("item {handle}"), pickable: true, ..Item::default() })).collect();
let id = workbench.update_in(cx, |workbench, window, cx| {
let id = workbench.quick_inputs.add(QuickInput::new(0, Kind::QuickPick), Owner::Session(1));
workbench.show_quick_input(id, window, cx);
let input = workbench.quick_inputs.get_mut(id).unwrap();
input.set_items(items.clone());
input.set_active_items(&[999]);
id
});
cx.run_until_parked();
let scroll_top = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.quick_inputs.list.logical_scroll_top().item_ix);
let update = |cx: &mut gpui::VisualTestContext, keep: bool, set: &dyn Fn(&mut QuickInput)| {
workbench.update(cx, |workbench, cx| {
let input = workbench.quick_inputs.get_mut(id).unwrap();
input.keep_scroll_position = keep;
set(input);
cx.notify();
});
cx.run_until_parked();
};
let bottom = scroll_top(cx);
assert_ne!(bottom, 0);
update(cx, true, &|input| input.set_active_items(&[0]));
assert_eq!(scroll_top(cx), bottom);
update(cx, false, &|input| input.set_active_items(&[0]));
assert_eq!(scroll_top(cx), 0);
update(cx, false, &|input| input.set_active_items(&[999]));
assert_eq!(scroll_top(cx), bottom);
update(cx, true, &|input| input.set_items(items.clone()));
assert_eq!(scroll_top(cx), bottom);
update(cx, false, &|input| input.set_items(items.clone()));
assert_eq!(scroll_top(cx), 0);
// quickInputList.ts's list sits in a ScrollableElement: the pointer shows its scrollbar, the wheel scrolls it.
use crate::workbench::list_scroll::ListId;
let bounds = workbench.read_with(cx, |workbench, _| workbench.list_scroller(ListId::QuickInput).bounds());
cx.simulate_mouse_move(bounds.center(), None, Modifiers::none());
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.list_hover(ListId::QuickInput).map(|(over, _)| over)), Some(true));
cx.simulate_event(gpui::ScrollWheelEvent { position: bounds.center(), delta: gpui::ScrollDelta::Lines(point(0., -1.)), ..Default::default() });
cx.run_until_parked();
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.list_scroller(ListId::QuickInput).position().y), 50.);
}
/// quickinput.test.ts "close motion requires modern UI with motion enabled", "finishes closing when the close
/// animation finishes" and "reopening discards the previous close animation callbacks and timeout".
#[gpui::test]
fn the_close_animation_ends_after_150ms_and_reopening_discards_it(cx: &mut TestAppContext) {
use crate::main_thread_quick_open::Owner;
use crate::quick_input::{Kind, QuickInput};
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Default High Contrast").unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx));
let id = workbench.update_in(cx, |workbench, window, cx| {
let id = workbench.quick_inputs.add(QuickInput::new(0, Kind::QuickPick), Owner::Session(1));
workbench.show_quick_input(id, window, cx);
workbench.hide_quick_input_by_id(id, window, cx);
id
});
let state = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| (workbench.quick_inputs.closing.is_some(), workbench.quick_inputs.current().is_some()));
assert_eq!(state(cx), (true, false), "closing, not visible");
cx.executor().advance_clock(std::time::Duration::from_millis(149));
cx.run_until_parked();
assert_eq!(state(cx), (true, false));
cx.executor().advance_clock(std::time::Duration::from_millis(1));
cx.run_until_parked();
assert_eq!(state(cx), (false, false), "the animation ended");
workbench.update_in(cx, |workbench, window, cx| {
workbench.show_quick_input(id, window, cx);
workbench.hide_quick_input_by_id(id, window, cx);
workbench.show_quick_input(id, window, cx);
});
assert_eq!(state(cx), (false, true), "reopened");
cx.executor().advance_clock(std::time::Duration::from_millis(150));
cx.run_until_parked();
assert_eq!(state(cx), (false, true), "the old close timer changes nothing");
}
/// quickInputList.ts `_registerSeparatorActionShowingListeners`: a separator's buttons show while an item of its
/// section is active, as the Open File button of Run Recent's `{shell} history` separator.
#[gpui::test]
fn a_separator_s_buttons_show_while_an_item_of_its_section_is_active(cx: &mut TestAppContext) {
use crate::main_thread_quick_open::Owner;
use crate::quick_input::{Button, Entry, Header, Item, Kind, QuickInput};
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx));
let open_file = Button { handle: 0, codicon: Some("go-to-file".into()), tooltip: Some("Open File".into()), ..Button::default() };
let items = vec![
Entry::Header(Header { handle: -1, label: "current session".into(), ..Header::default() }),
Entry::Item(Item { handle: 0, label: "ls".into(), pickable: true, ..Item::default() }),
Entry::Header(Header { handle: -2, label: "zsh history".into(), buttons: vec![open_file], ..Header::default() }),
Entry::Item(Item { handle: 1, label: "pwd".into(), pickable: true, ..Item::default() }),
];
let id = workbench.update_in(cx, |workbench, window, cx| {
let id = workbench.quick_inputs.add(QuickInput::new(0, Kind::QuickPick), Owner::Session(1));
workbench.show_quick_input(id, window, cx);
let input = workbench.quick_inputs.get_mut(id).unwrap();
input.set_items(items);
input.set_active_items(&[0]);
id
});
cx.run_until_parked();
assert!(cx.debug_bounds("quick-input-header-button").is_none(), "another section's item is active");
workbench.update(cx, |workbench, cx| {
workbench.quick_inputs.get_mut(id).unwrap().set_active_items(&[1]);
cx.notify();
});
cx.run_until_parked();
assert!(cx.debug_bounds("quick-input-header-button").is_some(), "an item of the separator's section is active");
}
/// A row hovered before the user types keeps its index while the list narrows under the pointer.
#[gpui::test]
fn a_hovered_row_index_past_a_narrowed_list_draws(cx: &mut TestAppContext) {
use crate::main_thread_quick_open::Owner;
use crate::quick_input::{Entry, Item, Kind, QuickInput};
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx));
let item = |handle: i64| Entry::Item(Item { handle, label: format!("item {handle}"), pickable: true, ..Item::default() });
workbench.update_in(cx, |workbench, window, cx| {
let id = workbench.quick_inputs.add(QuickInput::new(0, Kind::QuickPick), Owner::Session(1));
workbench.show_quick_input(id, window, cx);
workbench.quick_inputs.get_mut(id).unwrap().set_items((0..8).map(item).collect());
workbench.quick_inputs.hovered = Some(7);
let input = workbench.quick_inputs.get_mut(id).unwrap();
input.set_items(vec![item(0)]);
input.focused = None;
cx.notify();
});
cx.run_until_parked();
assert!(cx.debug_bounds("quick-input-label").is_some(), "the narrowed list draws");
}
/// quickInputList.ts `showHover` and `QuickInputHoverDelegate`: an item's tooltip shows right of its row after the
/// hover delay and stays while the user types; an item without one has its description's hover.
#[gpui::test]
fn an_item_s_tooltip_is_its_row_s_hover_until_the_value_changes(cx: &mut TestAppContext) {
use crate::main_thread_quick_open::Owner;
use crate::quick_input::{Entry, Item, Kind, QuickInput};
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-item-hover-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (workbench, cx) = cx.add_window_view(|_, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench
});
let items = vec![Entry::Item(Item { handle: 0, label: "Toggle Tab Key Moves Focus".into(), description: Some("ignored".into()), tooltip: Some("Moves focus around the workbench.".into()), pickable: true, ..Item::default() })];
workbench.update_in(cx, |workbench, window, cx| {
let id = workbench.quick_inputs.add(QuickInput::new(0, Kind::QuickPick), Owner::Session(1));
workbench.show_quick_input(id, window, cx);
workbench.quick_inputs.get_mut(id).unwrap().set_items(items);
});
cx.run_until_parked();
let shown = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.hovers.shown_text().map(str::to_owned));
let rest_on = |cx: &mut gpui::VisualTestContext, at: gpui::Point<gpui::Pixels>| {
cx.simulate_mouse_move(at, None, Modifiers::none());
cx.executor().advance_clock(std::time::Duration::from_millis(1500));
cx.run_until_parked();
};
let row = cx.debug_bounds("quick-input-label").unwrap();
rest_on(cx, row.center());
assert_eq!(shown(cx).as_deref(), Some("Moves focus around the workbench."));
let item_hover = cx.debug_bounds("workbench-hover").unwrap();
cx.simulate_keystrokes("left");
assert_eq!(shown(cx).as_deref(), Some("Moves focus around the workbench."), "hideOnKeyDown is off");
cx.simulate_keystrokes("escape");
assert_eq!(shown(cx), None, "the hover goes with the quick input");
workbench.update_in(cx, |workbench, window, cx| {
let id = workbench.quick_inputs.add(QuickInput::new(0, Kind::QuickPick), Owner::Session(1));
workbench.show_quick_input(id, window, cx);
workbench.quick_inputs.get_mut(id).unwrap().set_items(vec![Entry::Item(Item { handle: 1, label: "main.rs".into(), description: Some("Moves focus around the workbench.".into()), pickable: true, ..Item::default() })]);
});
cx.run_until_parked();
let description = cx.debug_bounds("quick-input-description").unwrap();
rest_on(cx, description.center());
assert_eq!(shown(cx).as_deref(), Some("Moves focus around the workbench."));
assert_eq!(item_hover.size.width - cx.debug_bounds("workbench-hover").unwrap().size.width, gpui::px(12.), "the list's 6px scrollable padding");
cx.simulate_keystrokes("m");
assert_eq!(shown(cx), None, "hideHoverOnValueChange");
}