Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Autosave files on close, when setting is `afterDelay` (#36929)

itsaphelc54e294965df
diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs
index eaad5dad65b..33ccd095687 100644
--- a/crates/search/src/project_search.rs
+++ b/crates/search/src/project_search.rs
@@ -1071,18 +1071,12 @@ impl ProjectSearchView {
window: &mut Window,
cx: &mut Context<Self>,
) -> Task<anyhow::Result<()>> {
- use workspace::AutosaveSetting;
-
let project = self.entity.read(cx).project.clone();
let can_autosave = self.results_editor.can_autosave(cx);
let autosave_setting = self.results_editor.workspace_settings(cx).autosave;
- let will_autosave = can_autosave
- && matches!(
- autosave_setting,
- AutosaveSetting::OnFocusChange | AutosaveSetting::OnWindowChange
- );
+ let will_autosave = can_autosave && autosave_setting.should_save_on_close();
let is_dirty = self.is_dirty(cx);
diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs
index 4ce8a890237..0418be7a2fb 100644
--- a/crates/workspace/src/pane.rs
+++ b/crates/workspace/src/pane.rs
@@ -2082,10 +2082,8 @@ impl Pane {
} else if is_dirty && (can_save || can_save_as) {
if save_intent == SaveIntent::Close {
let will_autosave = cx.update(|_window, cx| {
- matches!(
- item.workspace_settings(cx).autosave,
- AutosaveSetting::OnFocusChange | AutosaveSetting::OnWindowChange
- ) && item.can_autosave(cx)
+ item.can_autosave(cx)
+ && item.workspace_settings(cx).autosave.should_save_on_close()
})?;
if !will_autosave {
let item_id = item.item_id();
diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs
index 23d611964c3..5772695310e 100644
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@ -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]
diff --git a/crates/workspace/src/workspace_settings.rs b/crates/workspace/src/workspace_settings.rs
index 8868f319057..a86f81f442b 100644
--- a/crates/workspace/src/workspace_settings.rs
+++ b/crates/workspace/src/workspace_settings.rs
@@ -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 {
diff --git a/docs/src/configuring-zed.md b/docs/src/configuring-zed.md
index a7b89dc5207..6c25d62e291 100644
--- a/docs/src/configuring-zed.md
+++ b/docs/src/configuring-zed.md
@@ -246,6 +246,8 @@ Define extensions which should be installed (`true`) or never installed (`false`
}
```
+Note that a save will be triggered when an unsaved tab is closed, even if this is earlier than the configured inactivity period.
+
## Autoscroll on Clicks
- Description: Whether to scroll when clicking near the edge of the visible text area.