Merge pull request #1444 from zed-industries/smaller-diffs

Compute diffs based on characters rather than lines
This commit is contained in:
Antonio Scandurra 2022-08-02 19:17:54 +02:00 committed by GitHub
commit 0851524889
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 4 deletions

View File

@ -1006,7 +1006,7 @@ impl Buffer {
let old_text = old_text.to_string(); let old_text = old_text.to_string();
let line_ending = LineEnding::detect(&new_text); let line_ending = LineEnding::detect(&new_text);
LineEnding::normalize(&mut new_text); LineEnding::normalize(&mut new_text);
let changes = TextDiff::from_lines(old_text.as_str(), new_text.as_str()) let changes = TextDiff::from_chars(old_text.as_str(), new_text.as_str())
.iter_all_changes() .iter_all_changes()
.map(|c| (c.tag(), c.value().len())) .map(|c| (c.tag(), c.value().len()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View File

@ -183,20 +183,23 @@ fn test_edit_events(cx: &mut gpui::MutableAppContext) {
async fn test_apply_diff(cx: &mut gpui::TestAppContext) { async fn test_apply_diff(cx: &mut gpui::TestAppContext) {
let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n"; let text = "a\nbb\nccc\ndddd\neeeee\nffffff\n";
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx)); let buffer = cx.add_model(|cx| Buffer::new(0, text, cx));
let anchor = buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(3, 3)));
let text = "a\nccc\ndddd\nffffff\n"; let text = "a\nccc\ndddd\nffffff\n";
let diff = buffer.read_with(cx, |b, cx| b.diff(text.into(), cx)).await; let diff = buffer.read_with(cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx).unwrap(); buffer.apply_diff(diff, cx).unwrap();
assert_eq!(buffer.text(), text);
assert_eq!(anchor.to_point(&buffer), Point::new(2, 3));
}); });
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
let text = "a\n1\n\nccc\ndd2dd\nffffff\n"; let text = "a\n1\n\nccc\ndd2dd\nffffff\n";
let diff = buffer.read_with(cx, |b, cx| b.diff(text.into(), cx)).await; let diff = buffer.read_with(cx, |b, cx| b.diff(text.into(), cx)).await;
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx).unwrap(); buffer.apply_diff(diff, cx).unwrap();
assert_eq!(buffer.text(), text);
assert_eq!(anchor.to_point(&buffer), Point::new(4, 4));
}); });
cx.read(|cx| assert_eq!(buffer.read(cx).text(), text));
} }
#[gpui::test] #[gpui::test]

View File

@ -2498,7 +2498,7 @@ async fn test_buffer_file_changes_on_disk(cx: &mut gpui::TestAppContext) {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!( assert_eq!(
anchor_positions, anchor_positions,
[Point::new(1, 1), Point::new(3, 1), Point::new(4, 0)] [Point::new(1, 1), Point::new(3, 1), Point::new(3, 5)]
); );
}); });