Visual Studio Code, rebuilt in Rust
Rust
use std::path::Path;
use exthost::scan::scan_extension;
use gpui::TestAppContext;
use serde_json::json;
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
use crate::workbench::Workbench;
#[gpui::test]
fn one_undo_takes_back_a_workspace_edit_in_every_file(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!("code-gpui-bulk-undo-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (a, b) = (dir.join("a.txt"), dir.join("b.txt"));
std::fs::write(&a, "alpha").unwrap();
std::fs::write(&b, "beta").unwrap();
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.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(b.clone(), true, window, cx);
workbench.open_file(a.clone(), true, window, cx);
window.focus(&workbench.editor_focus, cx);
workbench.settings.lock().unwrap().update_value("window.dialogStyle", Some(&json!("custom")), config::service::Target::User, None, &[]).unwrap();
workbench
});
cx.run_until_parked();
let edit = |path: &Path, text: &str| json!({ "resource": { "scheme": "file", "path": path.to_string_lossy() }, "textEdit": { "range": { "startLineNumber": 1, "startColumn": 1, "endLineNumber": 1, "endColumn": 1 }, "text": text } });
let edits = json!([edit(&a, "1"), edit(&b, "2")]);
workbench.update(cx, |workbench, cx| workbench.apply_workspace_text_edits(&edits, &json!([]), files::operations::new_undo_group(), None, cx)).unwrap();
let texts = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| [&a, &b].map(|path| workbench.editors.model("file", path).unwrap().document().lines[0].clone()));
assert_eq!(texts(cx), ["1alpha", "2beta"]);
let run = |id: &str, cx: &mut gpui::VisualTestContext| {
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(id, Vec::new(), window, cx)));
cx.run_until_parked();
};
let answer = |button: usize, cx: &mut gpui::VisualTestContext| {
workbench.update(cx, |workbench, cx| {
assert_eq!(workbench.dialogs[0].dialog.message, "Would you like to undo 'Workspace Edit' across all files?");
workbench.close_custom_dialog(button, cx);
});
cx.run_until_parked();
};
run("undo", cx);
answer(0, cx);
assert_eq!(texts(cx), ["alpha", "beta"]);
run("redo", cx);
assert_eq!(texts(cx), ["1alpha", "2beta"]);
run("undo", cx);
answer(1, cx);
assert_eq!(texts(cx), ["alpha", "2beta"]);
std::fs::remove_dir_all(dir).unwrap();
}
/// bulkEditService.ts gives the file and text edits of one `workspace.applyEdit` one `UndoRedoGroup`: undo in the
/// edited file also undoes the file creation (`_continueUndoInGroup`), redo creates the file again first.
#[gpui::test]
fn undo_runs_the_file_edits_of_the_same_workspace_edit(cx: &mut TestAppContext) {
use files::operations::{self, FileEdit, FileEditOptions};
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!("code-gpui-group-undo-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let (a, c) = (dir.join("a.txt"), dir.join("c.txt"));
std::fs::write(&a, "alpha").unwrap();
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(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(a.clone(), true, window, cx);
window.focus(&workbench.editor_focus, cx);
workbench
});
cx.run_until_parked();
// `$tryApplyWorkspaceEdit` with a created c.txt, then an insert into a.txt.
let group = operations::new_undo_group();
let create = FileEdit::Create { new: files::Resource::file(c.to_string_lossy()), options: FileEditOptions::default(), contents: Some(b"c".to_vec()) };
let undo = operations::apply(&mut files::disk::Disk, vec![create], Some(group)).unwrap();
let edit = json!([{ "resource": { "scheme": "file", "path": a.to_string_lossy() }, "textEdit": { "range": { "startLineNumber": 1, "startColumn": 1, "endLineNumber": 1, "endColumn": 1 }, "text": "1" } }]);
workbench.update(cx, |workbench, cx| {
workbench.push_file_undo(undo);
workbench.apply_workspace_text_edits(&edit, &json!([]), group, None, cx).unwrap();
});
let text = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.model("file", &a).unwrap().document().lines[0].clone());
assert_eq!((text(cx).as_str(), c.exists()), ("1alpha", true));
let run = |id: &str, cx: &mut gpui::VisualTestContext| {
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(id, Vec::new(), window, cx)));
cx.run_until_parked();
};
run("undo", cx);
assert!(!cx.has_pending_prompt(), "an element of a group with others ready does not ask");
assert_eq!((text(cx).as_str(), c.exists()), ("alpha", false));
run("redo", cx);
assert_eq!((text(cx).as_str(), c.exists()), ("1alpha", true));
std::fs::remove_dir_all(dir).unwrap();
}
/// Rename preview's `apply_workspace_edit`: file, text and file edits in one group. Undo runs them last first, the
/// text element in between the two file elements that reverse on the host thread; redo runs them again.
#[gpui::test]
fn a_text_element_between_two_file_elements_of_one_group(cx: &mut TestAppContext) {
use files::operations::{self, FileEdit, FileEditOptions};
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!("code-gpui-mixed-group-undo-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let (a, c, d) = (dir.join("a.txt"), dir.join("c.txt"), dir.join("d.txt"));
std::fs::write(&a, "alpha").unwrap();
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(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(a.clone(), true, window, cx);
window.focus(&workbench.editor_focus, cx);
workbench
});
cx.run_until_parked();
let group = operations::new_undo_group();
let create = |path: &Path| FileEdit::Create { new: files::Resource::file(path.to_string_lossy()), options: FileEditOptions::default(), contents: Some(b"x".to_vec()) };
let edit = json!([{ "resource": { "scheme": "file", "path": a.to_string_lossy() }, "textEdit": { "range": { "startLineNumber": 1, "startColumn": 1, "endLineNumber": 1, "endColumn": 1 }, "text": "1" } }]);
workbench.update(cx, |workbench, cx| {
workbench.push_file_undo(operations::apply(&mut files::disk::Disk, vec![create(&c)], Some(group)).unwrap());
workbench.apply_workspace_text_edits(&edit, &json!([]), group, None, cx).unwrap();
workbench.push_file_undo(operations::apply(&mut files::disk::Disk, vec![create(&d)], Some(group)).unwrap());
});
let state = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| (workbench.editors.model("file", &a).unwrap().document().lines[0].clone(), c.exists(), d.exists()));
assert_eq!(state(cx), ("1alpha".into(), true, true));
let run = |id: &str, cx: &mut gpui::VisualTestContext| {
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(id, Vec::new(), window, cx)));
cx.run_until_parked();
};
run("undo", cx);
assert_eq!(state(cx), ("alpha".into(), false, false));
run("redo", cx);
assert_eq!(state(cx), ("1alpha".into(), true, true));
std::fs::remove_dir_all(dir).unwrap();
}
/// modelUndoRedoParticipant.ts `prepareUndoRedo`: a multi-model element whose file was closed (and saved) loads the
/// file's model again before it runs, so the undo reaches it.
#[gpui::test]
fn undo_across_files_loads_the_closed_files_model_again(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!("code-gpui-prepare-undo-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (a, b) = (dir.join("a.txt"), dir.join("b.txt"));
std::fs::write(&a, "alpha").unwrap();
std::fs::write(&b, "beta").unwrap();
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.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(b.clone(), true, window, cx);
workbench.open_file(a.clone(), true, window, cx);
window.focus(&workbench.editor_focus, cx);
workbench.settings.lock().unwrap().update_value("window.dialogStyle", Some(&json!("custom")), config::service::Target::User, None, &[]).unwrap();
workbench
});
cx.run_until_parked();
let edit = |path: &Path, text: &str| json!({ "resource": { "scheme": "file", "path": path.to_string_lossy() }, "textEdit": { "range": { "startLineNumber": 1, "startColumn": 1, "endLineNumber": 1, "endColumn": 1 }, "text": text } });
let edits = json!([edit(&a, "1"), edit(&b, "2")]);
workbench.update(cx, |workbench, cx| {
workbench.apply_workspace_text_edits(&edits, &json!([]), files::operations::new_undo_group(), None, cx).unwrap();
std::fs::write(&b, "2beta").unwrap();
workbench.editors.model_mut("file", &b).unwrap().mark_saved();
let index = workbench.editors.editors.iter().position(|editor| editor.document().is("file", &b)).unwrap();
workbench.editors.close(index);
assert!(workbench.editors.model("file", &b).is_none(), "b's model is disposed");
});
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("undo", Vec::new(), window, cx)));
cx.run_until_parked();
workbench.update(cx, |workbench, cx| workbench.close_custom_dialog(0, cx));
cx.run_until_parked();
let text = |path: &Path, cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.model("file", path).map(|model| model.document().lines[0].clone()));
assert_eq!([text(&a, cx), text(&b, cx)], [Some("alpha".into()), Some("beta".into())]);
std::fs::remove_dir_all(dir).unwrap();
}
/// modelService.ts `_schemaShouldMaintainUndoRedoElements` keeps `vscode-userdata` stacks too, and
/// modelUndoRedoParticipant.ts loads that model again for the undo.
#[gpui::test]
fn undo_across_files_loads_a_closed_user_data_model_again(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!("code-gpui-prepare-undo-user-data-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (a, b) = (dir.join("a.txt"), dir.join("User/b.txt"));
std::fs::write(&a, "alpha").unwrap();
std::fs::write(&b, "beta").unwrap();
let user_data = files::user_data::SCHEME;
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.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_disk_resource(user_data, b.clone(), true, window, cx);
workbench.open_file(a.clone(), true, window, cx);
window.focus(&workbench.editor_focus, cx);
workbench.settings.lock().unwrap().update_value("window.dialogStyle", Some(&json!("custom")), config::service::Target::User, None, &[]).unwrap();
workbench
});
cx.run_until_parked();
let edit = |scheme: &str, path: &Path, text: &str| json!({ "resource": { "scheme": scheme, "path": path.to_string_lossy() }, "textEdit": { "range": { "startLineNumber": 1, "startColumn": 1, "endLineNumber": 1, "endColumn": 1 }, "text": text } });
let edits = json!([edit("file", &a, "1"), edit(user_data, &b, "2")]);
workbench.update(cx, |workbench, cx| {
workbench.apply_workspace_text_edits(&edits, &json!([]), files::operations::new_undo_group(), None, cx).unwrap();
std::fs::write(&b, "2beta").unwrap();
workbench.editors.model_mut(user_data, &b).unwrap().mark_saved();
let index = workbench.editors.editors.iter().position(|editor| editor.document().is(user_data, &b)).unwrap();
workbench.editors.close(index);
assert!(workbench.editors.model(user_data, &b).is_none(), "b's model is disposed");
});
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("undo", Vec::new(), window, cx)));
cx.run_until_parked();
workbench.update(cx, |workbench, cx| workbench.close_custom_dialog(0, cx));
cx.run_until_parked();
let text = |scheme: &str, path: &Path, cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.model(scheme, path).map(|model| model.document().lines[0].clone()));
assert_eq!([text("file", &a, cx), text(user_data, &b, cx)], [Some("alpha".into()), Some("beta".into())]);
std::fs::remove_dir_all(dir).unwrap();
}