@@ -8737,6 +8737,36 @@ mod tests {
cx.executor().advance_clock(Duration::from_millis(250));
item.read_with(cx, |item, _| assert_eq!(item.save_count, 4));
+ // Autosave after delay, should save earlier than delay if tab is closed
+ item.update(cx, |item, cx| {
+ item.is_dirty = true;
+ cx.emit(ItemEvent::Edit);
+ });
+ cx.executor().advance_clock(Duration::from_millis(250));
+ item.read_with(cx, |item, _| assert_eq!(item.save_count, 4));
+
+ // // Ensure auto save with delay saves the item on close, even if the timer hasn't yet run out.
+ pane.update_in(cx, |pane, window, cx| {
+ pane.close_items(window, cx, SaveIntent::Close, move |id| id == item_id)
+ })
+ .await
+ .unwrap();
+ assert!(!cx.has_pending_prompt());
+ item.read_with(cx, |item, _| assert_eq!(item.save_count, 5));
+
+ // Add the item again, ensuring autosave is prevented if the underlying file has been deleted.
+ workspace.update_in(cx, |workspace, window, cx| {
+ workspace.add_item_to_active_pane(Box::new(item.clone()), None, true, window, cx);
+ });
+ item.update_in(cx, |item, _window, cx| {
+ item.is_dirty = true;
+ for project_item in &mut item.project_items {
+ project_item.update(cx, |project_item, _| project_item.is_dirty = true);
+ }
+ });
+ cx.run_until_parked();
+ item.read_with(cx, |item, _| assert_eq!(item.save_count, 5));
+
// Autosave on focus change, ensuring closing the tab counts as such.
item.update(cx, |item, cx| {
SettingsStore::update_global(cx, |settings, cx| {
@@ -8756,7 +8786,7 @@ mod tests {
.await
.unwrap();
assert!(!cx.has_pending_prompt());
- item.read_with(cx, |item, _| assert_eq!(item.save_count, 5));
+ item.read_with(cx, |item, _| assert_eq!(item.save_count, 6));
// Add the item again, ensuring autosave is prevented if the underlying file has been deleted.
workspace.update_in(cx, |workspace, window, cx| {
@@ -8770,7 +8800,7 @@ mod tests {
window.blur();
});
cx.run_until_parked();
- item.read_with(cx, |item, _| assert_eq!(item.save_count, 5));
+ item.read_with(cx, |item, _| assert_eq!(item.save_count, 6));
// Ensure autosave is prevented for deleted files also when closing the buffer.
let _close_items = pane.update_in(cx, |pane, window, cx| {
@@ -8778,7 +8808,7 @@ mod tests {
});
cx.run_until_parked();
assert!(cx.has_pending_prompt());
- item.read_with(cx, |item, _| assert_eq!(item.save_count, 5));
+ item.read_with(cx, |item, _| assert_eq!(item.save_count, 6));
}
#[gpui::test]
@@ -254,6 +254,17 @@ pub enum AutosaveSetting {
OnWindowChange,
}
+impl AutosaveSetting {
+ pub fn should_save_on_close(&self) -> bool {
+ matches!(
+ &self,
+ AutosaveSetting::OnFocusChange
+ | AutosaveSetting::OnWindowChange
+ | AutosaveSetting::AfterDelay { .. }
+ )
+ }
+}
+
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum PaneSplitDirectionHorizontal {