Skip to content

Visual Studio Code, rebuilt in Rust

Rust
4.9 KB
Raw Blame
use std::path::Path;

use exthost::scan::scan_extension;
use exthost::session::ForwardedRequest;
use gpui::{TestAppContext, VisualTestContext};
use serde_json::{Value, json};

use crate::editor::{Position, Selection};
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;

fn request(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext, method: &str, args: Value) {
    let request = ForwardedRequest { req: 1, proxy: "MainThreadLanguageFeatures", method: method.into(), args: args.as_array().unwrap().clone() };
    drop(workbench.update_in(cx, |workbench, window, cx| workbench.handle_host_request(request, window, cx)));
}

/// typescript-language-features' JSDoc continuation as `languages.setLanguageConfiguration` sends it: an
/// `IOnEnterRuleDto` with `indentAction` `IndentAction.None` and the regexes as `{ pattern, flags }`.
fn jsdoc_configuration() -> Value {
    json!({
        "comments": { "lineComment": "//", "blockComment": ["/*", "*/"] },
        "wordPattern": { "pattern": "[a-z]+", "flags": "" },
        "onEnterRules": [{ "beforeText": { "pattern": "^\\s*/\\*\\*(?!/)([^\\*]|\\*(?!/))*$", "flags": "" }, "action": { "indentAction": 0, "appendText": " * " } }],
    })
}

/// mainThreadLanguageFeatures.ts `$setLanguageConfiguration`: the configuration applies to the language's open models
/// and the typing rules until the registration's `$unregister`.
#[gpui::test]
fn an_extension_language_configuration_applies_until_unregistered(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 Modern").unwrap();
    let dir = std::env::temp_dir().join(format!("vsrustium-language-configuration-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let file = dir.join("a.txt");
    std::fs::write(&file, "/**").unwrap();
    let (sender, _receiver) = 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(&[]), crate::default_settings(), HostSync::new(sender), cx);
        workbench.open_file(file, true, window, cx);
        workbench
    });
    let block_comment = |workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().block_comment.clone());
    let enter = |workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext| {
        workbench.update_in(cx, |workbench, window, cx| {
            workbench.editors.active_mut().unwrap().set_selections(vec![Selection::caret(Position::new(0, 3))]);
            workbench.execute_command("type", vec![json!({ "text": "\n" })], window, cx);
            let lines = workbench.editors.active().unwrap().document().lines.clone();
            workbench.execute_command("undo", Vec::new(), window, cx);
            lines
        })
    };

    request(&workbench, cx, "$setLanguageConfiguration", json!([7, "no-such-language", jsdoc_configuration()]));
    assert_eq!(block_comment(&workbench, cx), None, "an unregistered language takes no configuration");

    request(&workbench, cx, "$setLanguageConfiguration", json!([7, "plaintext", jsdoc_configuration()]));
    assert_eq!(block_comment(&workbench, cx), Some(("/*".into(), "*/".into())));
    assert!(workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().word_pattern.is_some()));
    assert_eq!(workbench.read_with(cx, |workbench, _| workbench.languages.comments("plaintext").line.map(str::to_owned)), Some("//".into()));
    assert_eq!(enter(&workbench, cx), ["/**", " * "]);

    request(&workbench, cx, "$unregister", json!([7]));
    assert_eq!(block_comment(&workbench, cx), None);
    assert!(workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().word_pattern.is_none()));
    assert_eq!(enter(&workbench, cx), ["/**", ""]);
    let _ = std::fs::remove_dir_all(dir);
}

/// rpcProtocol.ts `_doInvokeHandler`: a method the actor does not have is an error, not `undefined`.
#[gpui::test]
fn an_unknown_method_of_a_routed_proxy_answers_an_error(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 Modern").unwrap();
    let (sender, _receiver) = std::sync::mpsc::channel();
    let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root, None, Languages::new(&[]), crate::default_settings(), HostSync::new(sender), cx));
    let answer = workbench.update_in(cx, |workbench, window, cx| workbench.answer_host_request("MainThreadStatusBar", "$bar", &[], window, cx));
    assert_eq!(answer, Err(exthost::main_thread::serialized_error("Unknown method $bar on actor MainThreadStatusBar")));
}