Don't autosave unmodified buffers (#32626)

Closes https://github.com/zed-industries/zed/issues/12091

Proper redo of https://github.com/zed-industries/zed/pull/32603

Release Notes:

- Fixed formatting effects not triggered when saving unmodified
singleton buffers

---------

Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-authored-by: Cole Miller <m@cole-miller.net>
This commit is contained in:
Kirill Bulatov 2025-06-13 01:12:14 +03:00 committed by GitHub
parent cd018da1ad
commit cef0c415f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 453 additions and 171 deletions

View file

@ -32,6 +32,21 @@ use util::ResultExt;
pub const LEADER_UPDATE_THROTTLE: Duration = Duration::from_millis(200);
#[derive(Clone, Copy, Debug)]
pub struct SaveOptions {
pub format: bool,
pub autosave: bool,
}
impl Default for SaveOptions {
fn default() -> Self {
Self {
format: true,
autosave: false,
}
}
}
#[derive(Deserialize)]
pub struct ItemSettings {
pub git_status: bool,
@ -326,7 +341,7 @@ pub trait Item: Focusable + EventEmitter<Self::Event> + Render + Sized {
}
fn save(
&mut self,
_format: bool,
_options: SaveOptions,
_project: Entity<Project>,
_window: &mut Window,
_cx: &mut Context<Self>,
@ -528,7 +543,7 @@ pub trait ItemHandle: 'static + Send {
fn can_save_as(&self, cx: &App) -> bool;
fn save(
&self,
format: bool,
options: SaveOptions,
project: Entity<Project>,
window: &mut Window,
cx: &mut App,
@ -991,12 +1006,12 @@ impl<T: Item> ItemHandle for Entity<T> {
fn save(
&self,
format: bool,
options: SaveOptions,
project: Entity<Project>,
window: &mut Window,
cx: &mut App,
) -> Task<Result<()>> {
self.update(cx, |item, cx| item.save(format, project, window, cx))
self.update(cx, |item, cx| item.save(options, project, window, cx))
}
fn save_as(
@ -1305,7 +1320,7 @@ impl<T: FollowableItem> WeakFollowableItemHandle for WeakEntity<T> {
#[cfg(any(test, feature = "test-support"))]
pub mod test {
use super::{Item, ItemEvent, SerializableItem, TabContentParams};
use crate::{ItemId, ItemNavHistory, Workspace, WorkspaceId};
use crate::{ItemId, ItemNavHistory, Workspace, WorkspaceId, item::SaveOptions};
use gpui::{
AnyElement, App, AppContext as _, Context, Entity, EntityId, EventEmitter, Focusable,
InteractiveElement, IntoElement, Render, SharedString, Task, WeakEntity, Window,
@ -1615,7 +1630,7 @@ pub mod test {
fn save(
&mut self,
_: bool,
_: SaveOptions,
_: Entity<Project>,
_window: &mut Window,
cx: &mut Context<Self>,

View file

@ -4,8 +4,8 @@ use crate::{
WorkspaceItemBuilder,
item::{
ActivateOnClose, ClosePosition, Item, ItemHandle, ItemSettings, PreviewTabsSettings,
ProjectItemKind, ShowCloseButton, ShowDiagnostics, TabContentParams, TabTooltipContent,
WeakItemHandle,
ProjectItemKind, SaveOptions, ShowCloseButton, ShowDiagnostics, TabContentParams,
TabTooltipContent, WeakItemHandle,
},
move_item,
notifications::NotifyResultExt,
@ -1870,7 +1870,15 @@ impl Pane {
match answer.await {
Ok(0) => {
pane.update_in(cx, |_, window, cx| {
item.save(should_format, project, window, cx)
item.save(
SaveOptions {
format: should_format,
autosave: false,
},
project,
window,
cx,
)
})?
.await?
}
@ -1896,7 +1904,15 @@ impl Pane {
match answer.await {
Ok(0) => {
pane.update_in(cx, |_, window, cx| {
item.save(should_format, project, window, cx)
item.save(
SaveOptions {
format: should_format,
autosave: false,
},
project,
window,
cx,
)
})?
.await?
}
@ -1967,7 +1983,15 @@ impl Pane {
if pane.is_active_preview_item(item.item_id()) {
pane.set_preview_item_id(None, cx);
}
item.save(should_format, project, window, cx)
item.save(
SaveOptions {
format: should_format,
autosave: false,
},
project,
window,
cx,
)
})?
.await?;
} else if can_save_as && is_singleton {
@ -2044,7 +2068,15 @@ impl Pane {
AutosaveSetting::AfterDelay { .. }
);
if item.can_autosave(cx) {
item.save(format, project, window, cx)
item.save(
SaveOptions {
format,
autosave: true,
},
project,
window,
cx,
)
} else {
Task::ready(Ok(()))
}