Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
workspace: Add separate "format and save" action (#53710)
Cameron Mcloughlinda6ac6c55127
diff --git a/crates/debugger_ui/src/tests/debugger_panel.rs b/crates/debugger_ui/src/tests/debugger_panel.rsindex 223ed13142d..4a6c8816a2c 100644--- a/crates/debugger_ui/src/tests/debugger_panel.rs+++ b/crates/debugger_ui/src/tests/debugger_panel.rs@@ -1229,6 +1229,7 @@ async fn test_send_breakpoints_when_editor_has_been_saved(editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),diff --git a/crates/editor/src/document_colors.rs b/crates/editor/src/document_colors.rsindex 8f8b70128ff..d62bb87404d 100644--- a/crates/editor/src/document_colors.rs+++ b/crates/editor/src/document_colors.rs@@ -602,6 +602,7 @@ mod tests {editor.save(SaveOptions {format: true,+ force_format: false,autosave: true,},project.clone(),@@ -692,6 +693,7 @@ mod tests {editor.save(SaveOptions {format: false,+ force_format: false,autosave: true,},project.clone(),diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex bca55b5d175..2878c8f3b88 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -21349,6 +21349,7 @@ impl Editor {self.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project,@@ -21395,6 +21396,7 @@ impl Editor {self.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project,diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex c29df272d35..b94e9f79b0c 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -13471,6 +13471,7 @@ async fn test_document_format_during_save(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -13510,6 +13511,7 @@ async fn test_document_format_during_save(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -13556,6 +13558,7 @@ async fn test_document_format_during_save(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -13642,6 +13645,7 @@ async fn test_auto_formatter_skips_server_without_formatting(cx: &mut TestAppConeditor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -13712,6 +13716,7 @@ async fn test_redo_after_noop_format(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -13931,6 +13936,7 @@ async fn test_multibuffer_format_during_save(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -14127,6 +14133,7 @@ async fn test_autosave_with_dirty_buffers(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: true,},project.clone(),@@ -14163,6 +14170,7 @@ async fn test_autosave_with_dirty_buffers(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -14250,6 +14258,7 @@ async fn test_range_format_on_save_success(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -14305,6 +14314,7 @@ async fn test_range_format_on_save_timeout(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -14332,6 +14342,7 @@ async fn test_range_format_not_called_for_clean_buffer(cx: &mut TestAppContext)editor.save(SaveOptions {format: false,+ force_format: false,autosave: false,},project.clone(),@@ -14373,6 +14384,7 @@ async fn test_range_format_respects_language_tab_size_override(cx: &mut TestAppCeditor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),@@ -31442,6 +31454,7 @@ async fn test_race_in_multibuffer_save(cx: &mut TestAppContext) {editor.save(SaveOptions {format: true,+ force_format: false,autosave: true,},project,diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rsindex 55fbf1c8ff1..9d062391a5c 100644--- a/crates/editor/src/items.rs+++ b/crates/editor/src/items.rs@@ -889,12 +889,18 @@ impl Item for Editor {.collect()};+ let format_trigger = if options.force_format {+ FormatTrigger::Manual+ } else {+ FormatTrigger::Save+ };+cx.spawn_in(window, async move |this, cx| {if options.format {this.update_in(cx, |editor, window, cx| {editor.perform_format(project.clone(),- FormatTrigger::Save,+ format_trigger,FormatTarget::Buffers(buffers_to_save.clone()),window,cx,diff --git a/crates/git_ui/src/project_diff.rs b/crates/git_ui/src/project_diff.rsindex a0708cae36c..c4aad77396f 100644--- a/crates/git_ui/src/project_diff.rs+++ b/crates/git_ui/src/project_diff.rs@@ -1973,6 +1973,7 @@ mod tests {buffer_editor.save(SaveOptions {format: false,+ force_format: false,autosave: false,},project.clone(),diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rsindex 7e7903674e3..441c1cf47d1 100644--- a/crates/search/src/project_search.rs+++ b/crates/search/src/project_search.rs@@ -1298,6 +1298,7 @@ impl ProjectSearchView {this.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project,diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rsindex 64647419e30..c4b664e2480 100644--- a/crates/workspace/src/item.rs+++ b/crates/workspace/src/item.rs@@ -39,6 +39,7 @@ pub const LEADER_UPDATE_THROTTLE: Duration = Duration::from_millis(200);#[derive(Clone, Copy, Debug)]pub struct SaveOptions {pub format: bool,+ pub force_format: bool,pub autosave: bool,}@@ -46,6 +47,7 @@ impl Default for SaveOptions {fn default() -> Self {Self {format: true,+ force_format: false,autosave: false,}}diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rsindex 6e38fb7d63d..f8906455a85 100644--- a/crates/workspace/src/pane.rs+++ b/crates/workspace/src/pane.rs@@ -83,6 +83,8 @@ pub enum SaveIntent {/// write all files (even if unchanged)/// prompt before overwriting on-disk changesSave,+ /// same as Save, but always formats regardless of the format_on_save setting+ FormatAndSave,/// same as Save, but without auto formattingSaveWithoutFormat,/// write any files that have local changes@@ -2267,7 +2269,10 @@ impl Pane {})?;// when saving a single buffer, we ignore whether or not it's dirty.- if save_intent == SaveIntent::Save || save_intent == SaveIntent::SaveWithoutFormat {+ if save_intent == SaveIntent::Save+ || save_intent == SaveIntent::FormatAndSave+ || save_intent == SaveIntent::SaveWithoutFormat+ {is_dirty = true;}@@ -2282,6 +2287,7 @@ impl Pane {}let should_format = save_intent != SaveIntent::SaveWithoutFormat;+ let force_format = save_intent == SaveIntent::FormatAndSave;if has_conflict && can_save {if has_deleted_file && is_singleton {@@ -2301,6 +2307,7 @@ impl Pane {item.save(SaveOptions {format: should_format,+ force_format,autosave: false,},project,@@ -2335,6 +2342,7 @@ impl Pane {item.save(SaveOptions {format: should_format,+ force_format,autosave: false,},project,@@ -2416,6 +2424,7 @@ impl Pane {item.save(SaveOptions {format: should_format,+ force_format,autosave: false,},project,@@ -2500,6 +2509,7 @@ impl Pane {item.save(SaveOptions {format,+ force_format: false,autosave: true,},project,diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex 7aa9b45c52d..b84ec0f176f 100644--- a/crates/workspace/src/workspace.rs+++ b/crates/workspace/src/workspace.rs@@ -303,6 +303,8 @@ actions!(ResetOpenDocksSize,/// Reloads the applicationReload,+ /// Formats and saves the current file, regardless of the format_on_save setting.+ FormatAndSave,/// Saves the current file with a new name.SaveAs,/// Saves without formatting.@@ -6942,6 +6944,11 @@ impl Workspace {.save_active_item(action.save_intent.unwrap_or(SaveIntent::Save), window, cx).detach_and_prompt_err("Failed to save", window, cx, |_, _, _| None);}))+ .on_action(cx.listener(|workspace, _: &FormatAndSave, window, cx| {+ workspace+ .save_active_item(SaveIntent::FormatAndSave, window, cx)+ .detach_and_prompt_err("Failed to save", window, cx, |_, _, _| None);+ })).on_action(cx.listener(|workspace, _: &SaveWithoutFormat, window, cx| {workspace.save_active_item(SaveIntent::SaveWithoutFormat, window, cx)diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex bb4a8dc2950..848efaffbb8 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -4300,6 +4300,7 @@ mod tests {editor.save(SaveOptions {format: true,+ force_format: false,autosave: false,},project.clone(),