Rework diff rendering to allow putting the cursor into deleted text, soft-wrapping and scrolling deleted text correctly (#22994)

Closes #12553

* [x] Fix `diff_hunk_before`
* [x] Fix failure to show deleted text when expanding hunk w/ cursor on
second line of the hunk
* [x] Failure to expand diff hunk below the cursor.
* [x] Delete the whole file, and expand the diff. Backspace over the
deleted hunk, panic!
* [x] Go-to-line now counts the diff hunks, but it should not
* [x] backspace at the beginning of a deleted hunk deletes too much text
* [x] Indent guides are rendered incorrectly 
* [ ] Fix randomized multi buffer tests

Maybe:
* [ ] Buffer search should include deleted text (in vim mode it turns
out I use `/x` all the time to jump to the next x I can see).
* [ ] vim: should refuse to switch into insert mode if selection is
fully within a diff.
* [ ] vim `o` command when cursor is on last line of deleted hunk.
* [ ] vim `shift-o` on first line of deleted hunk moves cursor but
doesn't insert line
* [x] `enter` at end of diff hunk inserts a new line but doesn't move
cursor
* [x] (`shift-enter` at start of diff hunk does nothing)
* [ ] Inserting a line just before an expanded hunk collapses it

Release Notes:


- Improved diff rendering, allowing you to navigate with your cursor
inside of deleted text in diff hunks.

---------

Co-authored-by: Conrad <conrad@zed.dev>
Co-authored-by: Cole <cole@zed.dev>
Co-authored-by: Mikayla <mikayla@zed.dev>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: Michael <michael@zed.dev>
Co-authored-by: Agus <agus@zed.dev>
Co-authored-by: João <joao@zed.dev>
This commit is contained in:
Max Brunsfeld 2025-01-24 13:18:22 -08:00 committed by GitHub
parent 1fdae4bae0
commit d2c55cbe3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
64 changed files with 7653 additions and 5495 deletions

View file

@ -42,6 +42,7 @@ where
self.0
}
#[must_use]
pub fn compose(&self, new_edits_iter: impl IntoIterator<Item = Edit<T>>) -> Self {
let mut old_edits_iter = self.0.iter().cloned().peekable();
let mut new_edits_iter = new_edits_iter.into_iter().peekable();

View file

@ -1507,9 +1507,9 @@ impl Buffer {
let mut rope_cursor = self.visible_text.cursor(0);
disjoint_ranges.map(move |range| {
position.add_assign(&rope_cursor.summary(range.start));
let start = position.clone();
let start = position;
position.add_assign(&rope_cursor.summary(range.end));
let end = position.clone();
let end = position;
start..end
})
}
@ -2029,11 +2029,11 @@ impl BufferSnapshot {
row_range: Range<u32>,
) -> impl Iterator<Item = (u32, LineIndent)> + '_ {
let start = Point::new(row_range.start, 0).to_offset(self);
let end = Point::new(row_range.end - 1, self.line_len(row_range.end - 1)).to_offset(self);
let end = Point::new(row_range.end, self.line_len(row_range.end)).to_offset(self);
let mut chunks = self.as_rope().chunks_in_range(start..end);
let mut row = row_range.start;
let mut done = start == end;
let mut done = false;
std::iter::from_fn(move || {
if done {
None
@ -2071,7 +2071,7 @@ impl BufferSnapshot {
}
let mut row = end_point.row;
let mut done = start == end;
let mut done = false;
std::iter::from_fn(move || {
if done {
None
@ -2168,7 +2168,7 @@ impl BufferSnapshot {
}
position.add_assign(&text_cursor.summary(fragment_offset));
(position.clone(), payload)
(position, payload)
})
}
@ -2176,10 +2176,14 @@ impl BufferSnapshot {
where
D: TextDimension,
{
self.text_summary_for_range(0..self.offset_for_anchor(anchor))
}
pub fn offset_for_anchor(&self, anchor: &Anchor) -> usize {
if *anchor == Anchor::MIN {
D::zero(&())
0
} else if *anchor == Anchor::MAX {
D::from_text_summary(&self.visible_text.summary())
self.visible_text.len()
} else {
let anchor_key = InsertionFragmentKey {
timestamp: anchor.timestamp,
@ -2217,7 +2221,7 @@ impl BufferSnapshot {
if fragment.visible {
fragment_offset += anchor.offset - insertion.split_offset;
}
self.text_summary_for_range(0..fragment_offset)
fragment_offset
}
}
@ -2580,16 +2584,16 @@ impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator fo
}
let fragment_summary = self.visible_cursor.summary(visible_end);
let mut new_end = self.new_end.clone();
let mut new_end = self.new_end;
new_end.add_assign(&fragment_summary);
if let Some((edit, range)) = pending_edit.as_mut() {
edit.new.end = new_end.clone();
edit.new.end = new_end;
range.end = end_anchor;
} else {
pending_edit = Some((
Edit {
old: self.old_end.clone()..self.old_end.clone(),
new: self.new_end.clone()..new_end.clone(),
old: self.old_end..self.old_end,
new: self.new_end..new_end,
},
start_anchor..end_anchor,
));
@ -2609,16 +2613,16 @@ impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator fo
self.deleted_cursor.seek_forward(cursor.start().deleted);
}
let fragment_summary = self.deleted_cursor.summary(deleted_end);
let mut old_end = self.old_end.clone();
let mut old_end = self.old_end;
old_end.add_assign(&fragment_summary);
if let Some((edit, range)) = pending_edit.as_mut() {
edit.old.end = old_end.clone();
edit.old.end = old_end;
range.end = end_anchor;
} else {
pending_edit = Some((
Edit {
old: self.old_end.clone()..old_end.clone(),
new: self.new_end.clone()..self.new_end.clone(),
old: self.old_end..old_end,
new: self.new_end..self.new_end,
},
start_anchor..end_anchor,
));