Visual Studio Code, rebuilt in Rust
Rust
use serde_json::{Value, json};
use super::{REFERENCED_SCHEMAS, RESOURCE_LANGUAGE_SETTINGS_SCHEMA_ID, compressed_content, inline_definitions, keybindings_schema, referenced_schema, settings_schemas};
use crate::registry::Registry;
/// keybindingService.ts `updateSchema`: an extension's command is a command name and a removal; the code action
/// commands suggest the provided kinds of their family.
#[test]
fn the_keybindings_schema_adds_extension_commands_and_code_action_kinds() {
let description = json!({ "contributes": { "commands": [{ "command": "port.hello", "title": "Hello" }] } });
let mut registry = Registry::with_extensions(&[("port.ext", &description)]);
registry.code_action_kinds = vec!["quickfix".into(), "refactor.extract".into(), "source.organizeImports".into()];
let schema = keybindings_schema(®istry);
let definitions = &schema["definitions"];
assert!(definitions["commandNames"]["enum"].as_array().unwrap().contains(&json!("port.hello")));
assert!(definitions["commandType"]["anyOf"][1]["enum"].as_array().unwrap().contains(&json!("-port.hello")));
let kinds = |command: &str| {
let all_of = definitions["commandsSchemas"]["allOf"].as_array().unwrap();
let addition = all_of.iter().rev().find(|entry| entry["if"]["properties"]["command"]["const"] == command).unwrap();
addition["then"]["properties"]["args"]["properties"]["kind"]["anyOf"][0]["enum"].clone()
};
assert_eq!(kinds("editor.action.codeAction"), json!(["quickfix", "refactor.extract", "source.organizeImports"]));
assert_eq!(kinds("editor.action.refactor"), json!(["refactor.extract"]));
assert_eq!(kinds("editor.action.sourceAction"), json!(["source.organizeImports"]));
}
/// The five `getCompressedContent` cases of base/test/common/jsonSchema.test.ts, as JSON.
const CASES: &str = include_str!("compressed-content-cases.json");
#[test]
fn compressed_content_matches_json_schema_test() {
let cases: Vec<Value> = serde_json::from_str(CASES).unwrap();
assert_eq!(cases.len(), 5);
for (index, case) in cases.iter().enumerate() {
assert_eq!(compressed_content(&case["schema"]), case["expected"].as_str().unwrap(), "getCompressedContent {}", index + 1);
}
}
#[test]
fn settings_schemas_split_the_registry_by_scope() {
let registry = Registry::with_extensions(&[]);
let schemas = settings_schemas(®istry, false);
let schema = |id: &str| schemas.iter().find(|(schema_id, _)| *schema_id == id).map(|(_, schema)| schema).unwrap();
let has = |id: &str, key: &str| schema(id)["properties"].get(key).is_some();
// window.restoreWindows is application, window.zoomLevel window, workbench.localHistory.enabled resource and
// editor.tabSize language-overridable.
assert!(has("vscode://schemas/settings/user", "window.restoreWindows"));
assert!(!has("vscode://schemas/settings/workspace", "window.restoreWindows"));
assert!(has("vscode://schemas/settings/workspace", "window.zoomLevel"));
assert!(has("vscode://schemas/settings/folder", "workbench.localHistory.enabled"));
assert!(has("vscode://schemas/settings/folder", "editor.tabSize"));
assert!(has(RESOURCE_LANGUAGE_SETTINGS_SCHEMA_ID, "editor.tabSize"));
assert!(!has(RESOURCE_LANGUAGE_SETTINGS_SCHEMA_ID, "workbench.localHistory.enabled"));
assert_eq!(schema("vscode://schemas/settings/user")["properties"]["editor.tabSize"]["default"], json!(4));
assert_eq!(schema("vscode://schemas/settings/user")["properties"]["[yaml]"]["$ref"], json!(RESOURCE_LANGUAGE_SETTINGS_SCHEMA_ID));
assert_eq!(schema("vscode://schemas/settings/user")["patternProperties"][r"^(\[([^\]]+)\])+$"]["$ref"], json!(RESOURCE_LANGUAGE_SETTINGS_SCHEMA_ID));
let multi_root = settings_schemas(®istry, true);
let folder = &multi_root.iter().find(|(id, _)| *id == "vscode://schemas/settings/folder").unwrap().1;
assert!(folder["properties"].get("window.zoomLevel").is_none(), "a multi-root folder takes resource settings only");
}
#[test]
fn each_registered_language_gets_an_override_property() {
let rust = json!({ "contributes": { "languages": [{ "id": "rust" }] } });
let registry = Registry::with_extensions(&[("vscode.rust", &rust)]);
let schemas = settings_schemas(®istry, false);
let user = &schemas.iter().find(|(id, _)| *id == "vscode://schemas/settings/user").unwrap().1;
assert_eq!(user["properties"]["[rust]"]["$ref"], json!(RESOURCE_LANGUAGE_SETTINGS_SCHEMA_ID));
assert_eq!(user["properties"]["[rust]"]["default"], json!({}));
assert_eq!(user["properties"]["[plaintext]"]["$ref"], json!(RESOURCE_LANGUAGE_SETTINGS_SCHEMA_ID));
}
/// Without contributions the dumped colors and token styling round-trip through expansion and compression.
#[test]
fn referenced_schemas_without_contributions_are_the_dump() {
let registry = Registry::with_extensions(&[]);
for uri in ["vscode://schemas/workbench-colors", "vscode://schemas/token-styling", "vscode://schemas/size-customizations"] {
let dump = REFERENCED_SCHEMAS.iter().find(|(id, _)| *id == uri).unwrap().1;
assert_eq!(referenced_schema(®istry, uri).unwrap(), dump, "{uri}");
}
}
/// colorExtensionPoint.ts, tokenClassificationExtensionPoint.ts and debugAdapterManager.ts `updateDebugAdapterSchema`:
/// an installed extension's colors, token types and modifiers and debuggers are in the schemas.
#[test]
fn referenced_schemas_list_an_installed_extension_s_contributions() {
let description = json!({ "contributes": {
"colors": [
{ "id": "port.highlight", "description": "Port highlight.", "defaults": { "dark": "#ABCF", "light": "#ffffff", "highContrast": null } },
{ "id": "bad id", "description": "Invalid.", "defaults": { "dark": "#000000" } },
],
"semanticTokenTypes": [{ "id": "portType", "description": "A port type." }],
"semanticTokenModifiers": [{ "id": "portModifier", "description": "A port modifier." }],
"debuggers": [{
"type": "port", "label": "Port Debug",
"configurationAttributes": { "launch": { "required": ["program"], "properties": { "program": { "type": "string" } } } },
"configurationSnippets": [{ "label": "Port: Launch", "body": { "type": "port", "request": "launch", "name": "Launch" } }],
}],
} });
let registry = Registry::with_extensions(&[("port.ext", &description)]);
let schema = |uri| -> Value {
let mut schema: Value = serde_json::from_str(&referenced_schema(®istry, uri).unwrap()).unwrap();
let definitions = schema["$defs"].take();
inline_definitions(&mut schema, &definitions);
schema
};
let colors = schema("vscode://schemas/workbench-colors");
let highlight = &colors["properties"]["port.highlight"];
assert_eq!(highlight["description"], "Port highlight.");
assert_eq!(highlight["oneOf"][0]["defaultSnippets"][0]["body"], "${1:#aabbcc}");
assert!(colors["properties"]["bad id"].is_null());
let tokens = schema("vscode://schemas/token-styling");
assert_eq!(tokens["properties"]["portType"]["description"], "A port type.");
assert_eq!(tokens["properties"]["*.portModifier"]["description"], "A port modifier.");
let launch = schema("vscode://schemas/launch");
let definition = &launch["definitions"]["port:launch"];
assert_eq!(definition["required"], json!(["name", "type", "request", "program"]));
assert_eq!(definition["properties"]["type"]["enumDescriptions"], json!(["Port Debug"]));
assert_eq!(definition["properties"]["program"]["pattern"], "^(?!.*\\$\\{(env|config|command)\\.)");
assert!(definition["properties"]["osx"].is_null());
assert_eq!(launch["definitions"]["port:launch:platform"]["properties"].as_object().unwrap().keys().any(|name| name == "type"), false);
let items = &launch["properties"]["configurations"]["items"];
assert_eq!(items["oneOf"].as_array().unwrap().len(), 1);
assert_eq!(items["oneOf"][0]["properties"]["osx"]["$ref"], "#/definitions/port:launch:platform");
assert_eq!(items["defaultSnippets"][0]["label"], "Port: Launch");
}