Skip to content

Visual Studio Code, rebuilt in Rust

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

use config::registry::Registry;
use config::service::Settings;
use exthost::scan::scan_extension;
use gpui::TestAppContext;
use serde_json::Value;

use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::{color_themes, load_theme};
use crate::workbench::Workbench;

#[gpui::test]
fn an_edit_of_settings_json_on_disk_reaches_the_host_the_theme_and_the_models(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let themes = ["theme-defaults", "theme-abyss"].map(|name| scan_extension(&app_root.join("extensions").join(name), true).unwrap());
    let theme = load_theme(&themes, "Dark 2026").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-configuration-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("User")).unwrap();
    let user_settings = dir.join("User/settings.json");
    std::fs::write(&user_settings, r#"{ "editor.detectIndentation": false }"#).unwrap();
    let file = dir.join("a.txt");
    std::fs::write(&file, "a\n").unwrap();
    let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), None)));
    let (sender, messages) = std::sync::mpsc::channel();
    let (workbench, cx) = cx.add_window_view(|window, cx| {
        let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(sender), cx);
        workbench.color_themes = color_themes(&themes).unwrap();
        workbench.open_file(file.clone(), true, window, cx);
        workbench
    });
    cx.run_until_parked();
    let tab_size = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().indentation.tab_size);
    assert_eq!(tab_size(cx), 4);
    _ = messages.try_iter().count();

    let edited = r#"{ "editor.detectIndentation": false, "editor.tabSize": 2, "workbench.colorTheme": "Abyss" }"#;
    std::fs::write(&user_settings, edited).unwrap();
    workbench.update(cx, |workbench, cx| workbench.reload_changed_settings_files(cx));

    assert_eq!(tab_size(cx), 2);
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.theme.settings_id.clone()), "Abyss");
    let sent: Vec<(&str, Vec<Value>)> = messages.try_iter().map(|message| (message.method, message.args)).collect();
    let methods: Vec<&str> = sent.iter().map(|(method, _)| *method).collect();
    assert_eq!(methods, ["$acceptConfigurationChanged", "$onColorThemeChange", "$acceptSyntaxHighlightingThemeChanged", "$acceptEditorPropertiesChanged"]);
    assert_eq!(sent[0].1[1]["keys"], serde_json::json!(["editor.tabSize", "workbench.colorTheme"]));
    assert_eq!(std::fs::read_to_string(&user_settings).unwrap(), edited, "applying the theme writes nothing back");

    workbench.update(cx, |workbench, cx| workbench.reload_changed_settings_files(cx));
    assert_eq!(messages.try_iter().count(), 0, "an unchanged file is no change");

    let looks = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| (workbench.file_icons.is_some(), workbench.theme.color("editor.background")));
    let seti = scan_extension(&app_root.join("extensions/theme-seti"), true).unwrap();
    workbench.update(cx, |workbench, _| workbench.extensions = vec![seti]);
    std::fs::write(&user_settings, r##"{ "workbench.colorTheme": "Abyss", "workbench.iconTheme": "vs-seti", "workbench.colorCustomizations": { "[Abyss]": { "editor.background": "#123456" } } }"##).unwrap();
    workbench.update(cx, |workbench, cx| workbench.reload_changed_settings_files(cx));
    assert_eq!(looks(cx), (true, crate::theme::parse_hex("#123456")));
    std::fs::write(&user_settings, r#"{ "workbench.colorTheme": "Abyss", "workbench.iconTheme": null }"#).unwrap();
    workbench.update(cx, |workbench, cx| workbench.reload_changed_settings_files(cx));
    assert_eq!(looks(cx), (false, crate::theme::parse_hex("#000c18")));
    std::fs::remove_dir_all(dir).unwrap();
}

/// workbenchThemeService.ts: the theme registries' `onDidChange` restores the themes the settings name, so themes
/// named before their extension is installed (an import) apply once it is.
#[gpui::test]
fn installed_extensions_bring_the_themes_the_settings_name(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let defaults = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
    let theme = load_theme(std::slice::from_ref(&defaults), "Dark 2026").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-configuration-install-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("User")).unwrap();
    std::fs::write(dir.join("User/settings.json"), r#"{ "workbench.colorTheme": "Abyss", "workbench.iconTheme": "vs-seti" }"#).unwrap();
    let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), None)));
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx));
    let looks = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| (workbench.theme.settings_id.clone(), workbench.file_icons.is_some()));
    assert_eq!(looks(cx), ("Dark 2026".to_owned(), false));

    let mut running = vec![defaults];
    running.extend(["theme-abyss", "theme-seti"].map(|name| scan_extension(&app_root.join("extensions").join(name), true).unwrap()));
    workbench.update(cx, |workbench, cx| workbench.set_running_extensions(running, cx));
    assert_eq!(looks(cx), ("Abyss".to_owned(), true));
    std::fs::remove_dir_all(dir).unwrap();
}

#[gpui::test]
#[cfg(target_os = "macos")]
fn the_app_icon_setting_swaps_the_dock_icon_and_follows_the_appearance(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 2026").unwrap();
    let dir = std::env::temp_dir().join(format!("code-gpui-app-icon-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("User")).unwrap();
    let user_settings = dir.join("User/settings.json");
    std::fs::write(&user_settings, "{}").unwrap();
    let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), None)));
    let (sender, _messages) = std::sync::mpsc::channel();
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(sender), cx));
    let icon = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.app_icon);
    let logo = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.app_logo.bytes().to_vec());
    workbench.update(cx, |workbench, _| {
        workbench.host_colors.dark = false;
        workbench.sync_app_icon();
    });
    let default_light = icon(cx).expect("a run outside the bundle shows the rendered default icon");

    std::fs::write(&user_settings, r#"{ "window.appIcon": "barsCream" }"#).unwrap();
    workbench.update(cx, |workbench, cx| workbench.reload_changed_settings_files(cx));
    assert_eq!(icon(cx), Some(crate::app_icon::BARS_CREAM_LIGHT));
    std::fs::write(&user_settings, r#"{ "window.appIcon": "bars" }"#).unwrap();
    workbench.update(cx, |workbench, cx| workbench.reload_changed_settings_files(cx));
    assert_eq!(icon(cx), Some(crate::app_icon::BARS_LIGHT));
    workbench.update(cx, |workbench, _| {
        workbench.host_colors.dark = true;
        workbench.sync_app_icon();
    });
    assert_eq!(icon(cx), Some(crate::app_icon::BARS_DARK));
    assert_eq!(logo(cx), crate::app_icon::BARS_DARK, "the update tooltip shows the Dock's icon");

    std::fs::write(&user_settings, r#"{ "window.appIcon": "default" }"#).unwrap();
    workbench.update(cx, |workbench, cx| workbench.reload_changed_settings_files(cx));
    assert!(icon(cx).is_some_and(|image| image != default_light && image != crate::app_icon::BARS_DARK), "the dark default");
    assert_eq!(crate::app_icon::image("default", true, true), None, "the bundle's own icon in VSRustium.app");
    assert_eq!(Some(logo(cx).as_slice()), icon(cx), "the update tooltip shows the default icon, not VS Code's code-icon.svg");
    std::fs::remove_dir_all(dir).unwrap();
}

/// configuration.ts's file watchers: an outside edit of the user's settings.json, and a folder's `.vscode/settings.json`
/// made after the window opened, apply without a reload call.
#[gpui::test]
fn settings_files_edited_outside_the_app_reach_the_watcher(cx: &mut TestAppContext) {
    let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
    let theme = load_theme(&[scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark 2026").unwrap();
    let dir = std::env::temp_dir().join(format!("vsrustium-settings-watch-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    let folder = dir.join("folder");
    std::fs::create_dir_all(dir.join("User")).unwrap();
    std::fs::create_dir_all(&folder).unwrap();
    std::fs::write(dir.join("User/settings.json"), "{}").unwrap();
    let workspace = config::workspace::Workspace::folder(folder.clone());
    let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), Some(workspace.clone()))));
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), Some(workspace), Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx));
    cx.executor().allow_parking();
    workbench.update_in(cx, |workbench, window, cx| workbench.watch_settings_files(window, cx));
    let tab_size = |cx: &mut gpui::VisualTestContext| {
        let overrides = config::configuration::Overrides { resource: Some(&folder), ..Default::default() };
        workbench.read_with(cx, |workbench, _| workbench.settings.lock().unwrap().get_value(Some("editor.tabSize"), &overrides))
    };
    let wait_for = |expected: Value, cx: &mut gpui::VisualTestContext| {
        for _ in 0..15 {
            std::thread::sleep(std::time::Duration::from_millis(200));
            cx.run_until_parked();
            if tab_size(cx) == Some(expected.clone()) {
                return;
            }
        }
        assert_eq!(tab_size(cx), Some(expected));
    };

    std::thread::sleep(std::time::Duration::from_millis(200));
    std::fs::write(dir.join("User/settings.json"), r#"{ "editor.tabSize": 7 }"#).unwrap();
    wait_for(7.into(), cx);
    std::fs::create_dir_all(folder.join(".vscode")).unwrap();
    std::fs::write(folder.join(".vscode/settings.json"), r#"{ "editor.tabSize": 3 }"#).unwrap();
    wait_for(3.into(), cx);
    std::thread::sleep(std::time::Duration::from_millis(200));
    std::fs::write(folder.join(".vscode/settings.json"), r#"{ "editor.tabSize": 5 }"#).unwrap();
    wait_for(5.into(), cx);
    std::fs::remove_dir_all(dir).unwrap();
}