Skip to content

Visual Studio Code, rebuilt in Rust

Rust
5.9 KB
Raw Blame
use std::path::Path;
use std::sync::{Arc, Mutex};

use exthost::scan::scan_extension;
use gpui::TestAppContext;

use super::FocusPart;
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;
use config::workspace::Workspace;
use storage::Storage;

/// navigationActions.ts: F6 goes editor, panel, auxiliary bar, status bar, activity bar, side bar, skipping the hidden
/// parts, and Shift+F6 goes back.
#[gpui::test]
fn f6_walks_the_visible_parts(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-f6-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("a.txt"), "one\n").unwrap();
    let workspace = Workspace::open(dir.clone());
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), Some(workspace), Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
        let storage = Arc::new(Mutex::new(Storage::in_memory()));
        workbench.restore_profile_state(storage.clone(), storage);
        workbench
    });
    workbench.update_in(cx, |workbench, window, cx| workbench.open_file(dir.join("a.txt"), true, window, cx));
    let walk = |keys: &str, cx: &mut gpui::VisualTestContext| {
        let mut parts = Vec::new();
        for key in keys.split(' ') {
            cx.simulate_keystrokes(key);
            cx.run_until_parked();
            parts.push(workbench.update_in(cx, |workbench, window, cx| workbench.focused_part(window, cx)));
        }
        parts
    };
    use FocusPart::*;

    assert_eq!(walk("f6 f6 f6 f6", cx), [Some(StatusBar), Some(ActivityBar), Some(SideBar), Some(Editor)]);
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.togglePanel", Vec::new(), window, cx)));
    workbench.update_in(cx, |workbench, window, cx| workbench.focus_group(workbench.editors.id, window, cx));
    assert_eq!(walk("f6 f6 shift-f6 shift-f6", cx), [Some(Panel), Some(StatusBar), Some(Panel), Some(Editor)]);
    assert_eq!(walk("shift-f6 shift-f6", cx), [Some(SideBar), Some(ActivityBar)]);
    std::fs::remove_dir_all(dir).unwrap();
}

/// The browser's Tab order is the DOM order of the parts (workbench.ts `renderWorkbench`): title bar, activity bar,
/// side bar, editor, panel, status bar; Shift+Tab walks it back. Each part counts once however many stops it has.
#[gpui::test]
fn tab_walks_the_parts_in_the_dom_order(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-tab-order-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("a.txt"), "one\n").unwrap();
    let workspace = Workspace::open(dir.clone());
    let (workbench, cx) = cx.add_window_view(|_, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), Some(workspace), Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
        workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
        let storage = Arc::new(Mutex::new(Storage::in_memory()));
        workbench.restore_profile_state(storage.clone(), storage);
        workbench
    });
    workbench.update_in(cx, |workbench, window, cx| {
        workbench.open_file(dir.join("a.txt"), true, window, cx);
        // The terminal keeps Tab for its shell.
        drop(workbench.execute_command("workbench.actions.view.problems", Vec::new(), window, cx));
        drop(workbench.execute_command("editor.action.toggleTabFocusMode", Vec::new(), window, cx));
    });
    cx.run_until_parked();
    workbench.update_in(cx, |workbench, window, cx| workbench.focus_group(workbench.editors.id, window, cx));
    let walk = |key: &str, cx: &mut gpui::VisualTestContext| {
        let mut parts = vec!["editor".to_string()];
        for _ in 0..60 {
            cx.simulate_keystrokes(key);
            cx.run_until_parked();
            let part = workbench.update_in(cx, |workbench, window, cx| match workbench.focused_action_bar(window) {
                Some(bar) if bar.starts_with("title-") => "title bar".to_string(),
                // `focused_part` knows the parts' own bars only: the editor group's and the side bar container's.
                Some("editor-tab-actions" | "editor-actions") => "Some(Editor)".to_string(),
                Some("explorer") => "Some(SideBar)".to_string(),
                _ => format!("{:?}", workbench.focused_part(window, cx)),
            });
            if parts.last() != Some(&part) {
                parts.push(part.clone());
            }
            if parts.len() > 1 && part == "Some(Editor)" && workbench.update_in(cx, |workbench, window, _| workbench.editor_focus.is_focused(window)) {
                break;
            }
        }
        parts[1..].to_vec()
    };
    let order = ["Some(Panel)", "Some(StatusBar)", "title bar", "Some(ActivityBar)", "Some(SideBar)", "Some(Editor)"];
    assert_eq!(walk("tab", cx), order, "Tab from the editor");
    // The editor group's title stops come first: its tab and actions sit before the text.
    let back = ["Some(Editor)", "Some(SideBar)", "Some(ActivityBar)", "title bar", "Some(StatusBar)", "Some(Panel)", "Some(Editor)"];
    assert_eq!(walk("shift-tab", cx), back, "Shift+Tab from the editor");
    std::fs::remove_dir_all(dir).unwrap();
}