Visual Studio Code, rebuilt in Rust
Rust
use std::path::Path;
use std::sync::mpsc::Receiver;
use std::time::Duration;
use exthost::scan::scan_extension;
use exthost::session::ForwardedRequest;
use gpui::{TestAppContext, VisualTestContext};
use serde_json::{Value, json};
use crate::host_sync::{HostMessage, HostSync};
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;
fn workbench_with<'a>(name: &str, cx: &'a mut TestAppContext) -> (gpui::Entity<Workbench>, &'a mut VisualTestContext, Receiver<HostMessage>, 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!("vsrustium-folding-providers-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("a.txt"), "import a\nimport b\nimport c\nx\n y\n z").unwrap();
let (sender, receiver) = std::sync::mpsc::channel();
let file = dir.join("a.txt");
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
});
(workbench, cx, receiver, dir)
}
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)));
}
/// Waits out the update delay (`min * 1.5` before an update took a measured time) and answers the `$provideFoldingRanges` calls it sends, by provider handle.
fn answer(receiver: &Receiver<HostMessage>, cx: &mut VisualTestContext, answers: &[(u64, Value)]) -> Vec<u64> {
cx.executor().advance_clock(Duration::from_millis(300));
cx.run_until_parked();
let mut asked = Vec::new();
for message in receiver.try_iter() {
if message.method != "$provideFoldingRanges" {
continue;
}
assert!(message.cancellable, "the host gets a CancellationToken");
let handle = message.args[0].as_u64().unwrap();
asked.push(handle);
let answer = answers.iter().find(|(provider, _)| *provider == handle).map_or(Value::Null, |(_, answer)| answer.clone());
let _ = message.reply.unwrap().send(Ok(answer));
}
cx.run_until_parked();
asked
}
fn regions(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext) -> Vec<(usize, usize, bool)> {
workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().folding.regions.ranges().iter().map(|range| (range.start, range.end, range.collapsed)).collect())
}
#[gpui::test]
fn extension_providers_fold_the_model_and_indentation_is_the_fallback(cx: &mut TestAppContext) {
let (workbench, cx, receiver, dir) = workbench_with("fold", cx);
assert_eq!(regions(&workbench, cx), [(4, 6, false)], "indentation folds first");
request(&workbench, cx, "$registerFoldingRangeProvider", json!([1, [{ "language": "plaintext" }], { "value": "ext.one" }, 7]));
request(&workbench, cx, "$registerFoldingRangeProvider", json!([2, [{ "language": "markdown" }], { "value": "ext.two" }, null]));
let ranges = json!([{ "start": 1, "end": 3, "kind": { "value": "imports" } }, { "start": 2, "end": 9 }]);
assert_eq!(answer(&receiver, cx, &[(1, ranges.clone())]), [1], "only the provider whose selector matches");
assert_eq!(regions(&workbench, cx), [(1, 3, false)], "a range past the last line is dropped");
// `$emitFoldingRangeEvent` with the provider's event handle asks again.
request(&workbench, cx, "$emitFoldingRangeEvent", json!([7]));
assert_eq!(answer(&receiver, cx, &[(1, json!([{ "start": 4, "end": 6 }]))]), [1]);
assert_eq!(regions(&workbench, cx), [(4, 6, false)]);
// An answer without an array falls back to the indentation ranges.
request(&workbench, cx, "$emitFoldingRangeEvent", json!([7]));
assert_eq!(answer(&receiver, cx, &[]), [1]);
assert!(!workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().syntax_folding));
request(&workbench, cx, "$unregister", json!([1]));
assert_eq!(answer(&receiver, cx, &[]), Vec::<u64>::new());
assert_eq!(regions(&workbench, cx), [(4, 6, false)]);
std::fs::remove_dir_all(dir).unwrap();
}
/// folding.ts `updateScheduler`: typing leaves the indentation regions as they were until the update delay, 300 ms
/// (`min * 1.5`) first, then the 200 ms `min` its learned average is clamped to.
#[gpui::test]
fn an_edit_folds_by_indentation_again_after_the_update_delay(cx: &mut TestAppContext) {
let (workbench, cx, _receiver, dir) = workbench_with("delay", cx);
let type_indent = |workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext, line: usize| {
workbench.update(cx, |workbench, cx| {
let editor = workbench.editors.active_mut().unwrap();
editor.move_to(crate::editor::Position::new(line, 0), false);
editor.type_text(" ").unwrap();
cx.notify();
});
cx.run_until_parked();
};
type_indent(&workbench, cx, 1);
cx.executor().advance_clock(Duration::from_millis(299));
cx.run_until_parked();
assert_eq!(regions(&workbench, cx), [(4, 6, false)], "not before the first delay");
cx.executor().advance_clock(Duration::from_millis(1));
cx.run_until_parked();
assert_eq!(regions(&workbench, cx), [(1, 2, false), (4, 6, false)]);
type_indent(&workbench, cx, 3);
cx.executor().advance_clock(Duration::from_millis(199));
cx.run_until_parked();
assert_eq!(regions(&workbench, cx), [(1, 2, false), (4, 6, false)]);
cx.executor().advance_clock(Duration::from_millis(1));
cx.run_until_parked();
assert_eq!(regions(&workbench, cx), [(1, 2, false), (3, 6, false)]);
std::fs::remove_dir_all(dir).unwrap();
}
/// folding.ts `onDidChangeModelContent` in the other editor of a split: its regions move with the edit and are computed
/// again by its own update Delayer, not on the frame that shows the edit.
#[gpui::test]
fn the_other_editor_of_a_split_folds_again_after_its_update_delay(cx: &mut TestAppContext) {
let (workbench, cx, _receiver, dir) = workbench_with("split", cx);
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.splitEditor", vec![], window, cx)));
cx.run_until_parked();
let left_regions = |workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext| {
workbench.read_with(cx, |workbench, _| {
let left = workbench.groups_in_grid_order()[0];
workbench.editors.group_tabs(left).0[0].folding.regions.ranges().iter().map(|range| (range.start, range.end)).collect::<Vec<_>>()
})
};
assert_eq!(left_regions(&workbench, cx), [(4, 6)]);
workbench.update(cx, |workbench, cx| {
let editor = workbench.editors.active_mut().unwrap();
editor.move_to(crate::editor::Position::new(1, 0), false);
editor.type_text(" ").unwrap();
editor.move_to(crate::editor::Position::new(0, 0), false);
editor.type_text("\n").unwrap();
cx.notify();
});
cx.run_until_parked();
assert_eq!(left_regions(&workbench, cx), [(5, 7)], "the regions move with the edits, the new indentation waits");
cx.executor().advance_clock(Duration::from_millis(299));
cx.run_until_parked();
assert_eq!(left_regions(&workbench, cx), [(5, 7)], "not before the update delay");
cx.executor().advance_clock(Duration::from_millis(1));
cx.run_until_parked();
assert_eq!(left_regions(&workbench, cx), [(2, 3), (5, 7)]);
std::fs::remove_dir_all(dir).unwrap();
}
/// folding.ts `_executeFoldingRangeProvider`: a provider's ranges with their kind, the indentation ranges without one.
#[gpui::test]
fn execute_folding_range_provider_answers_the_ranges(cx: &mut TestAppContext) {
let (workbench, cx, receiver, dir) = workbench_with("execute", cx);
let uri = json!({ "scheme": "file", "path": dir.join("a.txt").to_string_lossy() });
let execute = |cx: &mut VisualTestContext| {
let outcome = workbench.update_in(cx, |workbench, window, cx| workbench.execute_command("_executeFoldingRangeProvider", vec![uri.clone()], window, cx));
let crate::commands::Outcome::Pending(mut receiver) = outcome else { panic!("the ranges come later") };
cx.run_until_parked();
receiver.try_recv().unwrap().unwrap().unwrap()
};
assert_eq!(execute(cx), json!([{ "start": 4, "end": 6 }]));
request(&workbench, cx, "$registerFoldingRangeProvider", json!([1, [{ "language": "plaintext" }], { "value": "ext.one" }, 7]));
answer(&receiver, cx, &[]);
let outcome = workbench.update_in(cx, |workbench, window, cx| workbench.execute_command("_executeFoldingRangeProvider", vec![uri.clone()], window, cx));
answer(&receiver, cx, &[(1, json!([{ "start": 1, "end": 3, "kind": { "value": "imports" } }]))]);
let crate::commands::Outcome::Pending(mut ranges) = outcome else { panic!("the provider answers later") };
assert_eq!(ranges.try_recv().unwrap().unwrap().unwrap(), json!([{ "start": 1, "end": 3, "kind": { "value": "imports" } }]));
std::fs::remove_dir_all(dir).unwrap();
}