Visual Studio Code, rebuilt in Rust
Rust
use std::path::Path;
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};
use config::registry::Registry;
use config::service::Settings;
use exthost::scan::scan_extension;
use gpui::{TestAppContext, VisualTestContext};
use super::*;
use crate::host_sync::{HostMessage, HostSync};
use crate::languages::Languages;
use crate::theme::load_theme;
#[test]
fn parameter_label_offsets_find_whole_words_or_take_offsets() {
let label = "foo(aa: number, a: string)";
assert_eq!(parameter_label_offsets(label, Some(&json!({ "label": "a" }))), (16, 17));
assert_eq!(parameter_label_offsets(label, Some(&json!({ "label": [4, 6] }))), (4, 6));
assert_eq!(parameter_label_offsets(label, Some(&json!({ "label": "b" }))), (0, 0));
assert_eq!(parameter_label_offsets(label, None), (0, 0));
}
/// A folder with `a.txt` holding `text`, open at its end, with one signature help provider triggered by `(` and `,`.
fn open(cx: &mut TestAppContext) -> (gpui::Entity<Workbench>, &mut VisualTestContext, Receiver<HostMessage>) {
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-parameter-hints-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("a.txt"), "foo").unwrap();
let workspace = config::workspace::Workspace::folder(dir.clone());
let settings = Arc::new(Mutex::new(Settings::load(Registry::with_extensions(&[]), dir.join("User"), Some(workspace.clone()))));
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(), Some(workspace), Languages::new(&[]), settings, HostSync::new(sender), cx);
workbench.watch_editor_focus(window, cx);
workbench.open_file(dir.join("a.txt"), true, window, cx);
window.focus(&workbench.editor_focus, cx);
workbench.language_features.register(REGISTER, vec![json!(5), json!("*"), json!({ "triggerCharacters": ["("], "retriggerCharacters": [","] })]);
workbench
});
cx.run_until_parked();
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("cursorBottom", vec![], window, cx));
cx.run_until_parked();
(workbench, cx, receiver)
}
fn type_text(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext, text: &str) {
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("type", vec![json!({ "text": text })], window, cx));
cx.executor().advance_clock(DELAY);
cx.run_until_parked();
}
/// Answers the signature help requests sent since the last answer; returns their contexts.
fn answer(receiver: &Receiver<HostMessage>, help: Value, cx: &mut VisualTestContext) -> Vec<Value> {
let mut contexts = Vec::new();
for message in receiver.try_iter().filter(|message| message.method == "$provideSignatureHelp") {
contexts.push(message.args[3].clone());
let _ = message.reply.expect("a call").send(Ok(help.clone()));
}
cx.run_until_parked();
contexts
}
fn help(active_signature: u64) -> Value {
let signature = |label: &str| json!({ "label": label, "parameters": [{ "label": "a" }] });
json!({ "id": 1, "signatures": [signature("foo(a)"), signature("foo(a, b)")], "activeSignature": active_signature, "activeParameter": 0 })
}
#[gpui::test]
fn a_trigger_character_shows_the_hints_and_a_retrigger_keeps_them(cx: &mut TestAppContext) {
let (workbench, cx, receiver) = open(cx);
type_text(&workbench, cx, "(");
let contexts = answer(&receiver, help(0), cx);
assert_eq!(contexts, [json!({ "triggerKind": TRIGGER_CHARACTER, "triggerCharacter": "(", "isRetrigger": false, "activeSignatureHelp": null })]);
workbench.read_with(cx, |workbench, _| assert!(workbench.active_parameter_hints().is_some()));
type_text(&workbench, cx, ",");
let contexts = answer(&receiver, help(1), cx);
assert_eq!(contexts.len(), 1);
assert_eq!(contexts[0]["isRetrigger"], json!(true));
assert_eq!(contexts[0]["triggerCharacter"], json!(","));
assert_eq!(contexts[0]["activeSignatureHelp"]["activeSignature"], json!(0));
// `,` both types a retrigger character and changes the content: the answer goes to the latest trigger.
workbench.read_with(cx, |workbench, _| assert_eq!(workbench.active_parameter_hints().unwrap()["activeSignature"], json!(1)));
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("closeParameterHints", vec![], window, cx));
workbench.read_with(cx, |workbench, _| assert!(workbench.active_parameter_hints().is_none()));
}
/// Text from the keyboard reaches the editor through the input handler, not the `type` command: it triggers the
/// hints too.
#[gpui::test]
fn keyboard_input_triggers_the_hints(cx: &mut TestAppContext) {
let (workbench, cx, receiver) = open(cx);
cx.simulate_input("(");
cx.executor().advance_clock(DELAY);
cx.run_until_parked();
let contexts = answer(&receiver, help(0), cx);
assert_eq!(contexts.len(), 1);
workbench.read_with(cx, |workbench, _| assert!(workbench.active_parameter_hints().is_some()));
}
#[gpui::test]
fn next_and_previous_wrap_with_cycle_and_an_empty_answer_hides(cx: &mut TestAppContext) {
let (workbench, cx, receiver) = open(cx);
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("editor.action.triggerParameterHints", vec![], window, cx));
cx.run_until_parked();
assert_eq!(answer(&receiver, help(1), cx)[0]["triggerKind"], json!(INVOKE));
let active = |workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.active_parameter_hints().map(|hints| hints["activeSignature"].clone()));
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("showNextParameterHint", vec![], window, cx));
assert_eq!(active(&workbench, cx), Some(json!(0)));
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("showPrevParameterHint", vec![], window, cx));
assert_eq!(active(&workbench, cx), Some(json!(1)));
type_text(&workbench, cx, "x");
answer(&receiver, json!({ "signatures": [] }), cx);
assert_eq!(active(&workbench, cx), None);
}
#[gpui::test]
fn a_new_active_parameter_is_read_out_once(cx: &mut TestAppContext) {
let (workbench, cx, receiver) = open(cx);
let alert = |cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.accessibility.alert.clone());
let documented = |parameter: u64| {
let parameters = json!([{ "label": [4, 5], "documentation": "first" }, { "label": "b", "documentation": { "value": "*second*" } }]);
json!({ "signatures": [{ "label": "foo(a, b)", "documentation": "", "parameters": parameters }], "activeSignature": 0, "activeParameter": parameter })
};
type_text(&workbench, cx, "(");
answer(&receiver, documented(0), cx);
let (message, count) = alert(cx).unwrap();
assert_eq!(message, "a, first, hint", "an offsets label reads its slice; empty docs are skipped");
type_text(&workbench, cx, "x");
answer(&receiver, documented(0), cx);
assert_eq!(alert(cx).unwrap().1, count, "the same parameter is not read again");
type_text(&workbench, cx, ",");
answer(&receiver, documented(1), cx);
assert_eq!(alert(cx).unwrap(), ("b, *second*, hint".to_string(), count + 1));
}
#[gpui::test]
fn docs_taller_than_the_widget_scroll(cx: &mut TestAppContext) {
let (workbench, cx, receiver) = open(cx);
type_text(&workbench, cx, "(");
let docs = "word ".repeat(2000);
answer(&receiver, json!({ "signatures": [{ "label": "foo(a)", "documentation": docs, "parameters": [{ "label": "a" }] }], "activeSignature": 0, "activeParameter": 0 }), cx);
cx.run_until_parked();
let bounds = workbench.read_with(cx, |workbench, _| workbench.parameter_hints.widget_bounds.get()).expect("the widget was measured");
// The first frame measures the widget hidden; the second draws it where the wheel can reach it.
cx.update(|window, _| window.refresh());
cx.run_until_parked();
let center = gpui::point(gpui::px(bounds.center().x), gpui::px(bounds.center().y));
cx.simulate_event(gpui::ScrollWheelEvent { position: center, delta: gpui::ScrollDelta::Pixels(gpui::point(gpui::px(0.), gpui::px(-100.))), ..Default::default() });
cx.run_until_parked();
assert!(workbench.read_with(cx, |workbench, _| workbench.parameter_hints.body_scroll.offset().y) < gpui::px(0.));
}
/// parameterHintsWidget.ts: the body's `DomScrollableElement` has the Modern UI default 8px scrollbar
/// (modernUI.contribution.ts `MODERN_UI_SCROLLBAR_SIZE`); a press on its track below the slider scrolls the docs.
#[gpui::test]
fn the_docs_scroll_with_the_8px_scrollbar(cx: &mut TestAppContext) {
let (workbench, cx, receiver) = open(cx);
type_text(&workbench, cx, "(");
let docs = "word ".repeat(2000);
answer(&receiver, json!({ "signatures": [{ "label": "foo(a)", "documentation": docs, "parameters": [{ "label": "a" }] }], "activeSignature": 0, "activeParameter": 0 }), cx);
cx.run_until_parked();
cx.update(|window, _| window.refresh());
cx.run_until_parked();
let bounds = workbench.read_with(cx, |workbench, _| workbench.parameter_hints.body_scroll.bounds());
assert!(bounds.size.height > gpui::px(0.), "the body is laid out");
let offset = |cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.parameter_hints.body_scroll.offset().y);
let near_bottom = bounds.bottom() - gpui::px(10.);
// scrollbars.css `.invisible`: the track takes presses once the pointer over the docs reveals it.
cx.simulate_mouse_move(bounds.center(), None, gpui::Modifiers::none());
cx.run_until_parked();
cx.simulate_click(gpui::point(bounds.right() - gpui::px(10.), near_bottom), gpui::Modifiers::none());
assert_eq!(offset(cx), gpui::px(0.), "a press left of the 8px track does not scroll");
cx.simulate_click(gpui::point(bounds.right() - gpui::px(4.), near_bottom), gpui::Modifiers::none());
assert!(offset(cx) < gpui::px(0.), "a press on the track scrolls the docs");
}