Visual Studio Code, rebuilt in Rust
Rust
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, json};
use super::*;
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
fn validate<T>(option: &EditorOption<T>, value: Value) -> T {
(option.validate)(&value)
}
/// editorOptions.ts `EditorBooleanOption.boolean`: unset is the default, the string "false" is false, any other value is
/// `Boolean(value)`.
#[test]
fn a_boolean_option_reads_like_upstream() {
assert!(validate(&FOLDING, Value::Null));
assert!(!validate(&MOUSE_WHEEL_ZOOM, Value::Null));
assert!(!validate(&FOLDING, json!("false")));
assert!(validate(&MOUSE_WHEEL_ZOOM, json!("true")));
assert!(!validate(&FOLDING, json!(0)));
assert!(validate(&MOUSE_WHEEL_ZOOM, json!(1)));
assert!(!validate(&FOLDING, json!("")));
}
/// editorOptions.ts `EditorIntOption.clampedInt`: a string is `parseInt`, a number is clamped, anything else is the
/// default.
#[test]
fn an_int_option_is_clamped_and_parsed() {
assert_eq!(validate(&SELECTION_HIGHLIGHT_MAX_LENGTH, Value::Null), 200);
assert_eq!(validate(&SELECTION_HIGHLIGHT_MAX_LENGTH, json!(true)), 200);
assert_eq!(validate(&SELECTION_HIGHLIGHT_MAX_LENGTH, json!(-5)), 0);
assert_eq!(validate(&SELECTION_HIGHLIGHT_MAX_LENGTH, json!(" 42px")), 42);
assert_eq!(validate(&SELECTION_HIGHLIGHT_MAX_LENGTH, json!("px")), 200);
assert_eq!(validate(&STICKY_SCROLL_MAX_LINE_COUNT, json!(50)), 20);
assert_eq!(validate(&STICKY_SCROLL_MAX_LINE_COUNT, json!(0)), 1);
assert_eq!(validate(&STICKY_SCROLL_MAX_LINE_COUNT, json!(3.7)), 3);
assert_eq!(validate(&ACCESSIBILITY_PAGE_SIZE, json!(0)), 1);
assert_eq!(validate(&TAB_SIZE, Value::Null), 4);
}
/// editorOptions.ts `EditorFontSize.validate` and fontInfo.ts `EDITOR_FONT_DEFAULTS`: 0 is the platform default, else
/// clamped to 6 to 100; a string is `parseFloat`.
#[test]
fn the_font_size_is_clamped_with_the_platform_default() {
let default = if cfg!(target_os = "macos") { 12. } else { 14. };
assert_eq!(validate(&FONT_SIZE, Value::Null), default);
assert_eq!(validate(&FONT_SIZE, json!(0)), default);
assert_eq!(validate(&FONT_SIZE, json!(2)), 6.);
assert_eq!(validate(&FONT_SIZE, json!(500)), 100.);
assert_eq!(validate(&FONT_SIZE, json!("13.5px")), 13.5);
assert!(validate(&FONT_FAMILY, Value::Null).ends_with("monospace"));
}
/// editorOptions.ts `stringSet`: a value outside the set is the default.
#[test]
fn a_string_set_option_falls_back_to_its_default() {
assert_eq!(validate(&RENDER_WHITESPACE, Value::Null), "selection");
assert_eq!(validate(&RENDER_WHITESPACE, json!("everything")), "selection");
assert_eq!(validate(&RENDER_WHITESPACE, json!("all")), "all");
assert_eq!(validate(&CURSOR_STYLE, json!(true)), "line");
assert_eq!(validate(&OVERTYPE_CURSOR_STYLE, Value::Null), "block");
assert_eq!(validate(&CURSOR_BLINKING, json!("solid")), "solid");
assert_eq!(validate(&MULTI_CURSOR_MODIFIER, json!("ctrl")), "alt");
assert_eq!(validate(&ACCESSIBILITY_SUPPORT, json!("maybe")), "auto");
assert_eq!(validate(&SELECTED_TEXT_MATCH_MODE, Value::Null), "findOptions");
assert_eq!(validate(&MOUSE_MIDDLE_CLICK_ACTION, json!("openLink")), "openLink");
}
/// editorOptions.ts `EditorFind.validate`: `seedSearchStringFromSelection` and `autoFindInSelection` take a boolean
/// as `always` or `never`.
#[test]
fn the_find_options_take_a_boolean() {
assert_eq!(validate(&FIND_SEED_SEARCH_STRING_FROM_SELECTION, json!(false)), "never");
assert_eq!(validate(&FIND_SEED_SEARCH_STRING_FROM_SELECTION, json!("selection")), "selection");
assert_eq!(validate(&FIND_SEED_SEARCH_STRING_FROM_SELECTION, json!("other")), "always");
assert_eq!(validate(&FIND_AUTO_FIND_IN_SELECTION, json!(true)), "always");
assert_eq!(validate(&FIND_AUTO_FIND_IN_SELECTION, Value::Null), "never");
}
/// editorOptions.ts `GuideOptions.validate` (`primitiveSet`) and `EditorMinimap.validate` of `markSectionHeaderRegex`.
#[test]
fn guides_and_the_mark_regex_fall_back_to_their_defaults() {
assert_eq!(validate(&GUIDES_BRACKET_PAIRS, json!("active")), json!("active"));
assert_eq!(validate(&GUIDES_BRACKET_PAIRS, json!("sometimes")), json!(false));
assert_eq!(validate(&GUIDES_BRACKET_PAIRS_HORIZONTAL, Value::Null), json!("active"));
assert_eq!(validate(&GUIDES_HIGHLIGHT_ACTIVE_INDENTATION, json!(1)), json!(true));
let default = crate::editor::section_headers::MARK_SECTION_HEADER_REGEX;
assert_eq!(validate(&MINIMAP_MARK_SECTION_HEADER_REGEX, json!("(unclosed")), default);
assert_eq!(validate(&MINIMAP_MARK_SECTION_HEADER_REGEX, json!("MARK: (.*)")), "MARK: (.*)");
}
/// editorOptions.ts `UnicodeHighlight.validateBooleanMap`: only the characters set to `true` stay.
#[test]
fn the_allowed_characters_keep_the_true_entries() {
assert_eq!(validate(&UNICODE_HIGHLIGHT_ALLOWED_CHARACTERS, json!({ "a": true, "b": false, "c": 1 })), json!({ "a": true }));
assert_eq!(validate(&UNICODE_HIGHLIGHT_ALLOWED_CHARACTERS, json!("a")), json!({}));
}
/// A workbench showing `a.txt` (plaintext) with the user `settings`, in a temporary folder named after `test`.
fn open_plaintext<'a>(cx: &'a mut TestAppContext, test: &str, settings: &str) -> (gpui::Entity<Workbench>, &'a mut gpui::VisualTestContext, std::path::PathBuf) {
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-editor-options-{test}-{}", 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"), settings).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 (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
(workbench, cx, dir)
}
/// textResourceConfigurationService.ts `getValue(resource, position, section)`: a code editor reads its options with
/// its language's `[lang]` block; outside one the block does not apply.
#[gpui::test]
fn a_code_editor_reads_its_language_block(cx: &mut TestAppContext) {
let settings = r#"{ "editor.renderWhitespace": "none", "editor.guides.indentation": false, "[plaintext]": { "editor.renderWhitespace": "all", "editor.guides.indentation": true } }"#;
let (workbench, cx, dir) = open_plaintext(cx, "block", settings);
workbench.read_with(cx, |workbench, _| {
let editor = workbench.editors.active().unwrap();
assert_eq!(editor.document().language_id, "plaintext");
assert_eq!(workbench.editor_option(editor, &RENDER_WHITESPACE), "all");
assert!(workbench.editor_option(editor, &GUIDES_INDENTATION));
assert_eq!(workbench.global_editor_option(&RENDER_WHITESPACE), "none");
});
std::fs::remove_dir_all(dir).unwrap();
}
/// editorConfigurationSchema.ts registers every editor option under the `LANGUAGE_OVERRIDABLE` `editor` node, and
/// the readers ask the code editor (`editor.getOption`): minimap.ts, viewController.ts, middleScrollController.ts,
/// stickyScrollController.ts, findController.ts, findWidget.ts, multicursor.ts, contextmenu.ts, mouseHandler.ts,
/// viewLayout.ts, goToDefinitionAtPosition.ts, clickLinkGesture.ts. toggleMultiCursorModifier.ts's context key
/// reads `configurationService.getValue('editor')`, without the block.
#[gpui::test]
fn the_mouse_find_scroll_and_minimap_options_take_the_language_block(cx: &mut TestAppContext) {
let settings = r#"{ "[plaintext]": {
"editor.minimap.enabled": false, "editor.minimap.side": "left", "editor.minimap.autohide": "scroll", "editor.minimap.renderCharacters": false, "editor.minimap.scale": 2,
"editor.multiCursorModifier": "ctrlCmd", "editor.scrollOnMiddleClick": true, "editor.stickyScroll.maxLineCount": 2,
"editor.find.loop": false, "editor.find.findOnType": false, "editor.find.addExtraSpaceOnTop": false, "editor.selectedTextMatchMode": "caseSensitive",
"editor.mouseWheelZoom": true, "editor.columnSelection": true, "editor.doubleClickSelectsBlock": false, "editor.smoothScrolling": true,
"editor.contextmenu": false, "editor.definitionLinkOpensInPeek": true, "editor.mouseMiddleClickAction": "openLink"
} }"#;
let (workbench, cx, dir) = open_plaintext(cx, "sites", settings);
workbench.update_in(cx, |workbench, window, cx| {
let editor = workbench.editors.active().unwrap();
let minimap = workbench.minimap_settings(editor);
let checks = [
("minimap.enabled", !minimap.enabled),
("minimap.renderCharacters", !minimap.layout.render_characters),
("minimap.side", minimap.layout.side == crate::editor::minimap::MinimapSide::Left),
("minimap.scale", minimap.layout.scale == 2.),
("minimap.autohide", minimap.autohide == crate::workbench::minimap_view::Autohide::Scroll),
("stickyScroll.maxLineCount", workbench.sticky_scroll_lines(editor) == 2),
("multiCursorModifier", workbench.multi_cursor_modifier_is_cmd()),
("multiCursorModifier context key", workbench.key_context(window)["multiCursorModifier"] == json!("altKey")),
("contextmenu", !workbench.editor_option(editor, &CONTEXTMENU)),
("find.loop", !workbench.active_editor_option(&FIND_LOOP)),
("find.findOnType", !workbench.active_editor_option(&FIND_FIND_ON_TYPE)),
("find.addExtraSpaceOnTop", !workbench.active_editor_option(&FIND_ADD_EXTRA_SPACE_ON_TOP)),
("selectedTextMatchMode", workbench.active_editor_option(&SELECTED_TEXT_MATCH_MODE) == "caseSensitive"),
("mouseWheelZoom", workbench.active_editor_option(&MOUSE_WHEEL_ZOOM)),
("columnSelection", workbench.active_editor_option(&COLUMN_SELECTION)),
("doubleClickSelectsBlock", !workbench.active_editor_option(&DOUBLE_CLICK_SELECTS_BLOCK)),
("smoothScrolling", workbench.active_editor_option(&SMOOTH_SCROLLING)),
("definitionLinkOpensInPeek", workbench.active_editor_option(&DEFINITION_LINK_OPENS_IN_PEEK)),
("mouseMiddleClickAction", workbench.active_editor_option(&MOUSE_MIDDLE_CLICK_ACTION) == "openLink"),
("scrollOnMiddleClick", workbench.middle_scroll_mouse_down(true, gpui::point(gpui::px(10.), gpui::px(10.)), cx)),
];
let wrong: Vec<_> = checks.iter().filter(|(_, ok)| !ok).map(|(option, _)| *option).collect();
assert_eq!(wrong, Vec::<&str>::new());
});
std::fs::remove_dir_all(dir).unwrap();
}