Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
pane: Fix apply formatting when new file is first created (#52692)
Bruna Fernandes219ea086f2b4
diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rsindex de707c8eb17..aa6e53ef666 100644--- a/crates/workspace/src/pane.rs+++ b/crates/workspace/src/pane.rs@@ -2486,13 +2486,29 @@ impl Pane {pane.remove_item(item.item_id(), false, false, window, cx);}- item.save_as(project, new_path, window, cx)+ item.save_as(project.clone(), new_path, window, cx)})?} else {return Ok(false);};save_task.await?;+ if should_format {+ pane.update_in(cx, |pane, window, cx| {+ pane.unpreview_item_if_preview(item.item_id());+ item.save(+ SaveOptions {+ format: true,+ autosave: false,+ force_format,+ },+ project,+ window,+ cx,+ )+ })?+ .await?;+ }return Ok(true);}}@@ -8025,6 +8041,74 @@ mod tests {});}+ #[gpui::test]+ async fn test_format_runs_on_first_save_of_new_file(cx: &mut TestAppContext) {+ init_test(cx);+ let fs = FakeFs::new(cx.executor());++ let project = Project::test(fs, None, cx).await;+ let (workspace, cx) =+ cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));+ let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());++ let item = add_labeled_item(&pane, "untitled", true, cx);+ item.update(cx, |item, cx| {+ item.project_items.push(TestProjectItem::new_untitled(cx));+ });+ assert_item_labels(&pane, ["untitled*^"], cx);++ let close_task = pane.update_in(cx, |pane, window, cx| {+ pane.close_item_by_id(item.item_id(), SaveIntent::Save, window, cx)+ });++ cx.executor().run_until_parked();+ cx.simulate_new_path_selection(|_| Some(Default::default()));+ close_task.await.unwrap();++ item.read_with(cx, |item, _| {+ assert_eq!(item.save_as_count, 1);+ assert_eq!(+ item.save_count, 1,+ "formatter should run after the file is given a path on first save"+ );+ });+ }++ #[gpui::test]+ async fn test_format_does_not_run_on_first_save_when_save_without_format(+ cx: &mut TestAppContext,+ ) {+ init_test(cx);+ let fs = FakeFs::new(cx.executor());++ let project = Project::test(fs, None, cx).await;+ let (workspace, cx) =+ cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));+ let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());++ let item = add_labeled_item(&pane, "untitled", true, cx);+ item.update(cx, |item, cx| {+ item.project_items.push(TestProjectItem::new_untitled(cx));+ });+ assert_item_labels(&pane, ["untitled*^"], cx);++ let close_task = pane.update_in(cx, |pane, window, cx| {+ pane.close_item_by_id(item.item_id(), SaveIntent::SaveWithoutFormat, window, cx)+ });++ cx.executor().run_until_parked();+ cx.simulate_new_path_selection(|_| Some(Default::default()));+ close_task.await.unwrap();++ item.read_with(cx, |item, _| {+ assert_eq!(item.save_as_count, 1);+ assert_eq!(+ item.save_count, 0,+ "formatter should not run when SaveWithoutFormat is used"+ );+ });+ }+#[gpui::test]async fn test_discard_does_not_reload_multibuffer(cx: &mut TestAppContext) {init_test(cx);diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex 8b54a995a87..b0c5d3cb97d 100644--- a/crates/workspace/src/workspace.rs+++ b/crates/workspace/src/workspace.rs@@ -11521,7 +11521,7 @@ mod tests {// The requested items are closed.pane.update(cx, |pane, cx| {- assert_eq!(item4.read(cx).save_count, 0);+ assert_eq!(item4.read(cx).save_count, 1);assert_eq!(item4.read(cx).save_as_count, 1);assert_eq!(item4.read(cx).reload_count, 0);assert_eq!(pane.items_len(), 1);