Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
action_log: Diff overwritten files against their prior content (#62267)
Bobob20c4694791f
diff --git a/crates/action_log/src/action_log.rs b/crates/action_log/src/action_log.rsindex f9588c155f1..db4cdef4e37 100644--- a/crates/action_log/src/action_log.rs+++ b/crates/action_log/src/action_log.rs@@ -172,11 +172,22 @@ impl ActionLog {let diff_base;let unreviewed_edits;if is_created {- diff_base = Rope::default();- unreviewed_edits = Patch::new(vec![Edit {- old: 0..1,- new: 0..text_snapshot.max_point().row + 1,- }])+ if let TrackedBufferStatus::Created {+ existing_file_content: Some(existing_content),+ } = &status+ {+ diff_base = existing_content.clone();+ unreviewed_edits = Patch::new(vec![Edit {+ old: 0..existing_content.max_point().row + 1,+ new: 0..text_snapshot.max_point().row + 1,+ }]);+ } else {+ diff_base = Rope::default();+ unreviewed_edits = Patch::new(vec![Edit {+ old: 0..1,+ new: 0..text_snapshot.max_point().row + 1,+ }]);+ }} else {diff_base = buffer.read(cx).as_rope().clone();unreviewed_edits = Patch::default();@@ -1047,7 +1058,7 @@ impl ActionLog {}}-#[derive(Default, Debug, Clone, Copy)]+#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]pub struct DiffStats {pub lines_added: u32,pub lines_removed: u32,@@ -1687,8 +1698,8 @@ mod tests {buffer.clone(),vec![HunkStatus {range: Point::new(0, 0)..Point::new(0, 19),- diff_status: DiffHunkStatusKind::Added,- old_text: "".into(),+ diff_status: DiffHunkStatusKind::Modified,+ old_text: "Lorem ipsum dolor".into(),}],)]);@@ -1708,6 +1719,50 @@ mod tests {);}+ #[gpui::test(iterations = 10)]+ async fn test_overwriting_file_counts_removed_lines(cx: &mut TestAppContext) {+ init_test(cx);++ let fs = FakeFs::new(cx.executor());+ fs.insert_tree(+ path!("/dir"),+ json!({+ "file1": "line1\nline2\nline3\n"+ }),+ )+ .await;+ let project = Project::test(fs.clone(), [path!("/dir").as_ref()], cx).await;+ let action_log = cx.new(|_| ActionLog::new(project.clone()));+ let file_path = project+ .read_with(cx, |project, cx| project.find_project_path("dir/file1", cx))+ .unwrap();++ let buffer = project+ .update(cx, |project, cx| project.open_buffer(file_path, cx))+ .await+ .unwrap();+ cx.update(|cx| {+ action_log.update(cx, |log, cx| log.buffer_created(buffer.clone(), cx));+ buffer.update(cx, |buffer, cx| buffer.set_text("new1\nnew2\n", cx));+ action_log.update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));+ });+ project+ .update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))+ .await+ .unwrap();+ cx.run_until_parked();++ // Overwriting an existing file must count the replaced lines as removed,+ // not treat the whole file as added (+2 -0 would be the bug).+ assert_eq!(+ action_log.read_with(cx, |log, cx| log.diff_stats(cx)),+ DiffStats {+ lines_added: 2,+ lines_removed: 3,+ }+ );+ }+#[gpui::test(iterations = 10)]async fn test_overwriting_previously_edited_files(cx: &mut TestAppContext) {init_test(cx);@@ -1768,8 +1823,8 @@ mod tests {buffer.clone(),vec![HunkStatus {range: Point::new(0, 0)..Point::new(0, 9),- diff_status: DiffHunkStatusKind::Added,- old_text: "".into(),+ diff_status: DiffHunkStatusKind::Modified,+ old_text: "Lorem ipsum dolor".into(),}],)]);@@ -1885,8 +1940,8 @@ mod tests {buffer2.clone(),vec![HunkStatus {range: Point::new(0, 0)..Point::new(0, 5),- diff_status: DiffHunkStatusKind::Added,- old_text: "".into(),+ diff_status: DiffHunkStatusKind::Modified,+ old_text: "ipsum\n".into(),}],)]);