Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(workbench): cover dirty files in inactive editor groups (#62)

Artur Rokf4d6aabdf09a
diff --git a/native/crates/app/src/workbench/actions.rs b/native/crates/app/src/workbench/actions.rs
index 01c9a5e93b6..a30e75bb953 100644
--- a/native/crates/app/src/workbench/actions.rs
+++ b/native/crates/app/src/workbench/actions.rs
@@ -29,8 +29,7 @@ impl Workbench {
// `saveAll` in fileCommands.ts (Alt+Cmd+S), the next in openEditorsView.ts, `saveFiles` in fileCommands.ts.
for id in ["saveAll", "workbench.action.files.saveAll", "workbench.action.files.saveFiles"] {
self.commands.register(id, |workbench, _, _, cx| {
- let mut saves: Vec<_> = workbench.dirty_editors().into_iter().map(|index| workbench.save_editor(index, cx)).collect();
- saves.extend(workbench.save_dirty_notebooks(cx));
+ let saves = workbench.save_all_editors(true, cx);
when_saved(saves, |_| Value::Null, cx)
});
}
@@ -91,8 +90,12 @@ impl Workbench {
});
self.commands.register("vscode.open", |workbench, args, window, cx| workbench.execute_command("_workbench.open", args.into_iter().take(1).collect(), window, cx));
self.commands.register("_workbench.revertAllDirty", |workbench, _, _, cx| {
- let dirty = workbench.dirty_editors();
- workbench.revert_and_close_untitled(dirty, cx);
+ for group in workbench.editors.group_ids_mru() {
+ workbench.with_group(group, |workbench| {
+ let dirty = workbench.dirty_editors();
+ workbench.revert_and_close_untitled(dirty, cx);
+ });
+ }
workbench.revert_dirty_notebooks(cx);
Outcome::done()
});
@@ -171,10 +174,10 @@ impl Workbench {
when_saved(saves, Value::Bool, cx)
}
- /// editorService.ts `saveAll`: the dirty editors, untitled ones only with `include_untitled`.
+ /// editorService.ts `saveAll`: the dirty editors of every group, untitled ones only with `include_untitled`.
pub(super) fn save_all_editors(&mut self, include_untitled: bool, cx: &mut Context<Self>) -> Vec<oneshot::Receiver<bool>> {
- let dirty: Vec<usize> = self.dirty_editors().into_iter().filter(|index| include_untitled || self.editors.editors[*index].document().scheme != "untitled").collect();
- let mut saves: Vec<_> = dirty.into_iter().map(|index| self.save_editor(index, cx)).collect();
+ let dirty: Vec<(u32, usize)> = self.dirty_editors_in_groups().into_iter().filter(|&(group, index)| include_untitled || self.group_editors(group)[index].document().scheme != "untitled").collect();
+ let mut saves: Vec<_> = dirty.into_iter().map(|(group, index)| self.with_group(group, |workbench| workbench.save_editor(index, cx))).collect();
saves.extend(self.save_dirty_notebooks(cx));
saves
}
@@ -183,6 +186,28 @@ impl Workbench {
(0..self.editors.editors.len()).filter(|index| self.editors.editors[*index].is_dirty()).collect()
}
+ /// The dirty editors of every group as (group, index in the group), each working copy once: a file open in two
+ /// groups is one model.
+ pub(super) fn dirty_editors_in_groups(&self) -> Vec<(u32, usize)> {
+ let mut seen = Vec::new();
+ let mut dirty = Vec::new();
+ for group in self.editors.group_ids_mru() {
+ for (index, editor) in self.group_editors(group).iter().enumerate().filter(|(_, editor)| editor.is_dirty()) {
+ let key = (editor.document().scheme.clone(), editor.document().path.clone());
+ if !seen.contains(&key) {
+ seen.push(key);
+ dirty.push((group, index));
+ }
+ }
+ }
+ dirty
+ }
+
+ /// The editors of `group` in tab order, the active group's included.
+ pub(super) fn group_editors(&self, group: u32) -> &[crate::editor::OpenEditor] {
+ self.editors.group(group).map_or(&self.editors.editors, |group| &group.editors)
+ }
+
/// dirtyFilesIndicator.ts: the explorer's badge counts the dirty working copies, each resource once (a split
/// editor's copy in another group is the same working copy), notebooks included.
pub(super) fn dirty_working_copy_count(&self) -> u64 {
diff --git a/native/crates/app/src/workbench/auto_save.rs b/native/crates/app/src/workbench/auto_save.rs
index e9aefd8b080..b57ee9cac6e 100644
--- a/native/crates/app/src/workbench/auto_save.rs
+++ b/native/crates/app/src/workbench/auto_save.rs
@@ -193,9 +193,7 @@ impl Workbench {
}
let dirty: Vec<(Key, u32)> = self
.editors
- .editors
- .iter()
- .chain(&self.editors.models)
+ .all_editors()
.filter(|model| model.is_dirty() && !self.untitled_working_copy(model))
.map(|model| ((model.document().scheme.clone(), model.document().path.clone()), model.text_model().version_id))
.collect();
@@ -267,14 +265,15 @@ impl Workbench {
/// `saveAllDirtyAutoSaveables`.
fn save_all_dirty_auto_saveables(&mut self, reason: SaveReason, cx: &mut Context<Self>) {
- let dirty: Vec<Key> = self
- .editors
+ let mut dirty: Vec<Key> = self
.editors
- .iter()
- .chain(&self.editors.models)
+ .all_editors()
.filter(|model| model.is_dirty() && !self.untitled_working_copy(model))
.map(|model| (model.document().scheme.clone(), model.document().path.clone()))
.collect();
+ // A file open in two groups is one model: save it once.
+ dirty.sort();
+ dirty.dedup();
for key in dirty {
self.auto_save_model(key, reason, cx);
}
diff --git a/native/crates/app/src/workbench/auto_save/tests.rs b/native/crates/app/src/workbench/auto_save/tests.rs
index 79e25b61189..58f27d16738 100644
--- a/native/crates/app/src/workbench/auto_save/tests.rs
+++ b/native/crates/app/src/workbench/auto_save/tests.rs
@@ -145,3 +145,37 @@ fn toggling_auto_save_on_saves_the_dirty_models(cx: &mut TestAppContext) {
assert!(!a_is_dirty(&workbench, cx, &dir));
std::fs::remove_dir_all(dir).unwrap();
}
+
+/// A dirty file whose only editor is in a group other than the active one still saves after the delay.
+#[gpui::test]
+fn an_edit_in_another_group_saves_after_the_delay(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, r#"{ "files.autoSave": "afterDelay", "files.autoSaveDelay": 50 }"#);
+ type_text(&workbench, cx, "x");
+ workbench.update_in(cx, |workbench, window, cx| {
+ workbench.open_file(dir.join("b.txt"), true, window, cx);
+ _ = workbench.execute_command("workbench.action.moveEditorToNextGroup", vec![], window, cx);
+ });
+ cx.run_until_parked();
+ assert!(workbench.read_with(cx, |workbench, _| workbench.editors.has_several_groups()));
+ assert!(a_is_dirty(&workbench, cx, &dir));
+ cx.executor().advance_clock(Duration::from_millis(60));
+ cx.run_until_parked();
+ assert!(!a_is_dirty(&workbench, cx, &dir));
+ assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "xa");
+ std::fs::remove_dir_all(dir).unwrap();
+}
+
+/// Turning auto save on saves a dirty file whose only editor is in a group other than the active one.
+#[gpui::test]
+fn toggling_auto_save_on_saves_a_dirty_file_in_another_group(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, "{}");
+ type_text(&workbench, cx, "x");
+ workbench.update_in(cx, |workbench, window, cx| {
+ workbench.open_file(dir.join("b.txt"), true, window, cx);
+ _ = workbench.execute_command("workbench.action.moveEditorToNextGroup", vec![], window, cx);
+ _ = workbench.execute_command("workbench.action.toggleAutoSave", vec![], window, cx);
+ });
+ cx.run_until_parked();
+ assert!(!a_is_dirty(&workbench, cx, &dir));
+ std::fs::remove_dir_all(dir).unwrap();
+}
diff --git a/native/crates/app/src/workbench/backup_tracker.rs b/native/crates/app/src/workbench/backup_tracker.rs
index 00001d8fbaa..50364268866 100644
--- a/native/crates/app/src/workbench/backup_tracker.rs
+++ b/native/crates/app/src/workbench/backup_tracker.rs
@@ -207,7 +207,7 @@ impl Workbench {
return;
}
let mut dirty = Vec::new();
- for model in self.editors.editors.iter().chain(&self.editors.models).filter(|model| tracked(model) && model.is_dirty()) {
+ for model in self.editors.all_editors().filter(|model| tracked(model) && model.is_dirty()) {
let document = model.document();
dirty.push(((document.scheme.clone(), document.path.clone()), model.alternative_version_id()));
}
@@ -371,7 +371,7 @@ impl Workbench {
if tracker.home.is_none() {
return true;
}
- let dirty: Vec<(String, PathBuf)> = self.editors.editors.iter().chain(&self.editors.models).filter(|model| tracked(model) && model.is_dirty()).map(|model| (model.document().scheme.clone(), model.document().path.clone())).collect();
+ let dirty: Vec<(String, PathBuf)> = self.editors.all_editors().filter(|model| tracked(model) && model.is_dirty()).map(|model| (model.document().scheme.clone(), model.document().path.clone())).collect();
let mut all = true;
for key in dirty {
if self.lifecycle.backups.backed_up.get(&key) != self.editors.model(&key.0, &key.1).map(OpenEditor::alternative_version_id).as_ref() {
@@ -384,7 +384,7 @@ impl Workbench {
/// workingCopyBackupTracker.ts `getUnattendedRestartBlocker`: a modified working copy that a shutdown would ask
/// about, save, or lose.
pub(super) fn working_copy_restart_blocker(&self) -> Option<&'static str> {
- let models: Vec<&OpenEditor> = self.editors.editors.iter().chain(&self.editors.models).filter(|model| tracked(model) && model.is_dirty()).collect();
+ let models: Vec<&OpenEditor> = self.editors.all_editors().filter(|model| tracked(model) && model.is_dirty()).collect();
let custom = self.dirty_documents(DocumentKind::Custom);
let notebooks = self.dirty_documents(DocumentKind::Notebook);
if models.is_empty() && custom.is_empty() && notebooks.is_empty() {
diff --git a/native/crates/app/src/workbench/backup_tracker/tests.rs b/native/crates/app/src/workbench/backup_tracker/tests.rs
index 85ddca18264..b9485b03996 100644
--- a/native/crates/app/src/workbench/backup_tracker/tests.rs
+++ b/native/crates/app/src/workbench/backup_tracker/tests.rs
@@ -348,3 +348,42 @@ fn an_interactive_window_backup_opens_an_interactive_window(cx: &mut TestAppCont
assert_eq!(backups, [("notebook/interactive/interactive".to_owned(), b"BACKUP".to_vec())]);
std::fs::remove_dir_all(&dir).unwrap();
}
+
+/// A dirty file whose only editor is in a group other than the active one keeps its backup.
+#[gpui::test]
+fn a_dirty_file_in_another_group_keeps_its_backup(cx: &mut TestAppContext) {
+ let dir = scratch();
+ std::fs::write(dir.join("folder/b.txt"), "b").unwrap();
+ let (workbench, cx) = open(cx, &dir, "{}");
+ type_x(&workbench, cx);
+ cx.executor().advance_clock(BACKUP_DELAY);
+ cx.run_until_parked();
+ assert_eq!(backups_of(&workbench, cx).len(), 1);
+ workbench.update_in(cx, |workbench, window, cx| {
+ workbench.open_file(dir.join("folder/b.txt"), true, window, cx);
+ _ = workbench.execute_command("workbench.action.moveEditorToNextGroup", vec![], window, cx);
+ });
+ cx.run_until_parked();
+ assert!(workbench.read_with(cx, |workbench, _| workbench.editors.has_several_groups()));
+ assert_eq!(backups_of(&workbench, cx).len(), 1);
+ std::fs::remove_dir_all(&dir).unwrap();
+}
+
+/// A quit backs up a dirty file whose only editor is in a group other than the active one.
+#[gpui::test]
+fn quit_backs_up_a_dirty_file_in_another_group(cx: &mut TestAppContext) {
+ let dir = scratch();
+ std::fs::write(dir.join("folder/b.txt"), "b").unwrap();
+ let (workbench, cx) = open(cx, &dir, "{}");
+ type_x(&workbench, cx);
+ workbench.update_in(cx, |workbench, window, cx| {
+ workbench.open_file(dir.join("folder/b.txt"), true, window, cx);
+ _ = workbench.execute_command("workbench.action.moveEditorToNextGroup", vec![], window, cx);
+ });
+ cx.run_until_parked();
+ assert_eq!(workbench.read_with(cx, |workbench, _| workbench.working_copy_restart_blocker()), Some("workingCopy.backupPending"));
+ workbench.update_in(cx, |workbench, window, cx| workbench.shutdown(ShutdownReason::Quit, window, cx, |_, _, _| {}));
+ cx.run_until_parked();
+ assert_eq!(backups_of(&workbench, cx).len(), 1);
+ std::fs::remove_dir_all(&dir).unwrap();
+}
diff --git a/native/crates/app/src/workbench/host.rs b/native/crates/app/src/workbench/host.rs
index 9238fdccdd7..caf4192e3a7 100644
--- a/native/crates/app/src/workbench/host.rs
+++ b/native/crates/app/src/workbench/host.rs
@@ -143,7 +143,7 @@ impl Workbench {
window.set_window_title(&title);
self.lifecycle.title = title;
}
- let edited = !self.dirty_editors().is_empty();
+ let edited = self.editors.all_editors().any(|editor| editor.is_dirty());
let represented = self.editors.active().filter(|editor| editor.document().scheme == "file").map(|editor| editor.document().path.clone());
if self.lifecycle.document_edited == edited && self.lifecycle.represented_filename == represented {
return;
diff --git a/native/crates/app/src/workbench/lifecycle.rs b/native/crates/app/src/workbench/lifecycle.rs
index f6535365dcc..ed4da88c47d 100644
--- a/native/crates/app/src/workbench/lifecycle.rs
+++ b/native/crates/app/src/workbench/lifecycle.rs
@@ -52,7 +52,7 @@ impl Workbench {
/// The names of the modified working copies (`workingCopyService.modifiedWorkingCopies`): dirty editors, then
/// dirty notebooks, whose tabs are not dirty editors.
fn modified_working_copy_names(&self) -> Vec<String> {
- let mut names: Vec<String> = self.dirty_editors().into_iter().map(|index| self.editor_name(&self.editors.editors[index])).collect();
+ let mut names: Vec<String> = self.dirty_editors_in_groups().into_iter().map(|(group, index)| self.editor_name(&self.group_editors(group)[index])).collect();
names.extend(self.host.notebooks.models.iter().filter(|model| model.dirty).map(|model| crate::notebook::label(&model.uri)));
names
}
@@ -292,8 +292,8 @@ fn asks_before_close(setting: &Value, keyboard: bool) -> bool {
/// up for hot exit or asks about it; without modified editors the window's backups go.
async fn backup_tracker_before_shutdown(workbench: &WeakEntity<Workbench>, reason: ShutdownReason, cx: &mut AsyncWindowContext) -> bool {
let Ok(auto_saves) = workbench.update(cx, |workbench, cx| {
- let indices: Vec<usize> = workbench.dirty_editors().into_iter().filter(|index| workbench.auto_saved_on_shutdown(&workbench.editors.editors[*index])).collect();
- indices.into_iter().map(|index| workbench.save_editor(index, cx)).collect::<Vec<_>>()
+ let dirty: Vec<(u32, usize)> = workbench.dirty_editors_in_groups().into_iter().filter(|&(group, index)| workbench.auto_saved_on_shutdown(&workbench.group_editors(group)[index])).collect();
+ dirty.into_iter().map(|(group, index)| workbench.with_group(group, |workbench| workbench.save_editor(index, cx))).collect::<Vec<_>>()
}) else {
return false;
};
@@ -355,7 +355,8 @@ async fn confirm_before_shutdown(workbench: &WeakEntity<Workbench>, cx: &mut Asy
match confirm.await.unwrap_or(ConfirmResult::Cancel) {
ConfirmResult::Save => {
let Ok(saves) = workbench.update(cx, |workbench, cx| {
- let mut saves: Vec<_> = workbench.dirty_editors().into_iter().map(|index| workbench.save_editor(index, cx)).collect();
+ let dirty = workbench.dirty_editors_in_groups();
+ let mut saves: Vec<_> = dirty.into_iter().map(|(group, index)| workbench.with_group(group, |workbench| workbench.save_editor(index, cx))).collect();
saves.extend(workbench.save_dirty_notebook_editors(cx));
saves
}) else {
@@ -366,8 +367,8 @@ async fn confirm_before_shutdown(workbench: &WeakEntity<Workbench>, cx: &mut Asy
}
ConfirmResult::DontSave => {
let _ = workbench.update(cx, |workbench, cx| {
- for index in workbench.dirty_editors() {
- workbench.revert_editor(index, cx);
+ for (group, index) in workbench.dirty_editors_in_groups() {
+ workbench.with_group(group, |workbench| workbench.revert_editor(index, cx));
}
workbench.revert_dirty_notebooks(cx);
});
diff --git a/native/crates/app/src/workbench/lifecycle/tests.rs b/native/crates/app/src/workbench/lifecycle/tests.rs
index b4ea1092d11..a4382650689 100644
--- a/native/crates/app/src/workbench/lifecycle/tests.rs
+++ b/native/crates/app/src/workbench/lifecycle/tests.rs
@@ -82,3 +82,81 @@ fn dont_save_reverts_and_shuts_down(cx: &mut TestAppContext) {
assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "a");
assert!(!a_is_dirty(&workbench, cx, &dir));
}
+
+/// Leaves dirty `a.txt` in its group and opens `b.txt` in a new active group.
+fn open_b_in_another_group(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext, dir: &Path) {
+ std::fs::write(dir.join("b.txt"), "b").unwrap();
+ workbench.update_in(cx, |workbench, window, cx| {
+ workbench.open_file(dir.join("b.txt"), true, window, cx);
+ _ = workbench.execute_command("workbench.action.moveEditorToNextGroup", vec![], window, cx);
+ });
+ cx.run_until_parked();
+ assert!(workbench.read_with(cx, |workbench, _| workbench.editors.has_several_groups()));
+ assert!(a_is_dirty(workbench, cx, dir));
+}
+
+/// The shutdown auto saves a dirty file whose only editor is in a group other than the active one.
+#[gpui::test]
+fn auto_save_saves_a_file_in_another_group_before_the_shutdown(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, r#"{ "files.autoSave": "onWindowChange", "files.hotExit": "off" }"#);
+ open_b_in_another_group(&workbench, cx, &dir);
+ assert!(quit(&workbench, cx));
+ assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "xa");
+ assert!(!a_is_dirty(&workbench, cx, &dir));
+}
+
+/// Don't Save reverts a dirty file whose only editor is in a group other than the active one.
+#[gpui::test]
+fn dont_save_reverts_a_file_in_another_group(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, r#"{ "files.hotExit": "off" }"#);
+ workbench.update(cx, |workbench, _| workbench.skip_dialogs = true);
+ open_b_in_another_group(&workbench, cx, &dir);
+ assert!(quit(&workbench, cx));
+ assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "a");
+ assert!(!a_is_dirty(&workbench, cx, &dir));
+}
+
+/// fileCommands.ts `saveAll`: `editorService.saveAll` saves the dirty editors of every group.
+#[gpui::test]
+fn save_all_saves_a_file_in_another_group(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, "{}");
+ open_b_in_another_group(&workbench, cx, &dir);
+ workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("workbench.action.files.saveAll", vec![], window, cx));
+ cx.run_until_parked();
+ assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "xa");
+ assert!(!a_is_dirty(&workbench, cx, &dir));
+}
+
+/// mainThreadWorkspace.ts `$saveAll`.
+#[gpui::test]
+fn the_hosts_save_all_saves_a_file_in_another_group(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, "{}");
+ open_b_in_another_group(&workbench, cx, &dir);
+ workbench.update(cx, |workbench, cx| _ = workbench.save_all(false, cx));
+ cx.run_until_parked();
+ assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "xa");
+ assert!(!a_is_dirty(&workbench, cx, &dir));
+}
+
+/// mainThreadEditors.ts `_workbench.revertAllDirty` reverts `workingCopyService.dirtyWorkingCopies`, whatever their group.
+#[gpui::test]
+fn revert_all_dirty_reverts_a_file_in_another_group(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, "{}");
+ open_b_in_another_group(&workbench, cx, &dir);
+ workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("_workbench.revertAllDirty", vec![], window, cx));
+ cx.run_until_parked();
+ assert!(!a_is_dirty(&workbench, cx, &dir));
+ assert_eq!(std::fs::read_to_string(dir.join("a.txt")).unwrap(), "a");
+}
+
+/// window.ts `updateDocumentEdited` follows `workingCopyService.hasDirty`, whatever the dirty file's group.
+#[gpui::test]
+fn the_window_is_edited_while_a_file_in_another_group_is_dirty(cx: &mut TestAppContext) {
+ let (workbench, cx, dir) = open(cx, "{}");
+ open_b_in_another_group(&workbench, cx, &dir);
+ let edited = workbench.update_in(cx, |workbench, window, _| {
+ workbench.sync_window_title(window);
+ workbench.lifecycle.document_edited
+ });
+ assert!(edited);
+}