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

@ -208,7 +208,7 @@ use workspace::{
CollaboratorId, Item as WorkspaceItem, ItemId, ItemNavHistory, OpenInTerminal, OpenTerminal,
RestoreOnStartupBehavior, SERIALIZATION_THROTTLE_TIME, SplitDirection, TabBarSettings, Toast,
ViewId, Workspace, WorkspaceId, WorkspaceSettings,
item::{ItemHandle, PreviewTabsSettings},
item::{ItemHandle, PreviewTabsSettings, SaveOptions},
notifications::{DetachAndPromptErr, NotificationId, NotifyTaskExt},
searchable::SearchEvent,
};
@ -1498,7 +1498,7 @@ impl InlayHintRefreshReason {
}
pub enum FormatTarget {
Buffers,
Buffers(HashSet<Entity<Buffer>>),
Ranges(Vec<Range<MultiBufferPoint>>),
}
@ -15601,7 +15601,7 @@ impl Editor {
Some(self.perform_format(
project,
FormatTrigger::Manual,
FormatTarget::Buffers,
FormatTarget::Buffers(self.buffer.read(cx).all_buffers()),
window,
cx,
))
@ -15636,10 +15636,6 @@ impl Editor {
))
}
fn save_non_dirty_buffers(&self, cx: &App) -> bool {
self.is_singleton(cx) && EditorSettings::get_global(cx).save_non_dirty_buffers
}
fn perform_format(
&mut self,
project: Entity<Project>,
@ -15650,13 +15646,7 @@ impl Editor {
) -> Task<Result<()>> {
let buffer = self.buffer.clone();
let (buffers, target) = match target {
FormatTarget::Buffers => {
let mut buffers = buffer.read(cx).all_buffers();
if trigger == FormatTrigger::Save && !self.save_non_dirty_buffers(cx) {
buffers.retain(|buffer| buffer.read(cx).is_dirty());
}
(buffers, LspFormatTarget::Buffers)
}
FormatTarget::Buffers(buffers) => (buffers, LspFormatTarget::Buffers),
FormatTarget::Ranges(selection_ranges) => {
let multi_buffer = buffer.read(cx);
let snapshot = multi_buffer.read(cx);
@ -17101,7 +17091,16 @@ impl Editor {
}
if let Some(project) = self.project.clone() {
self.save(true, project, window, cx).detach_and_log_err(cx);
self.save(
SaveOptions {
format: true,
autosave: false,
},
project,
window,
cx,
)
.detach_and_log_err(cx);
}
}
@ -17133,7 +17132,16 @@ impl Editor {
});
if let Some(project) = self.project.clone() {
self.save(true, project, window, cx).detach_and_log_err(cx);
self.save(
SaveOptions {
format: true,
autosave: false,
},
project,
window,
cx,
)
.detach_and_log_err(cx);
}
}