git: Improve diff performance (#54435)

Previously, passing an empty range to `range_to_buffer_range` would
return no ranges, which would trigger a fallback case in the git diff
that caused the entire multibuffer to be invalidated every frame.

This PR fixes `range_to_buffer_range`, so that it returns a sensible
range if the input range is empty, preventing the fallback behaviour and
edit expansion.

Self-Review Checklist:

- [ ] I've reviewed my own diff for quality, security, and reliability
- [ ] Unsafe blocks (if any) have justifying comments
- [ ] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [ ] Performance impact has been considered and is acceptable

Closes #ISSUE

Release Notes:

- N/A or Added/Fixed/Improved ...

---------

Co-authored-by: Cole Miller <cole@zed.dev>
Co-authored-by: Anthony Eid <hello@anthonyeid.me>
This commit is contained in:
authored and GitHub committed 2026-04-22 02:42:11 +00:00
1 parent 310e367448
commit 3cd0f4c419
6 files changed
+91 -41

No files matched your search

@@ -5864,6 +5864,48 @@ fn test_range_to_buffer_ranges(cx: &mut App) {
assert_eq!(ranges_half_open_max[1].1, BufferOffset(0)..BufferOffset(0));
}
#[gpui::test]
fn test_range_to_buffer_ranges_zero_length_at_excerpt_boundary(cx: &mut App) {
let buffer_1 = cx.new(|cx| Buffer::local("aaa\nbbb", cx));
let buffer_2 = cx.new(|cx| Buffer::local("ccc\nddd", cx));
let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
multibuffer.update(cx, |multibuffer, cx| {
multibuffer.set_excerpts_for_path(
PathKey::sorted(0),
buffer_1.clone(),
[Point::new(0, 0)..Point::new(1, 3)],
0,
cx,
);
multibuffer.set_excerpts_for_path(
PathKey::sorted(1),
buffer_2.clone(),
[Point::new(0, 0)..Point::new(1, 3)],
0,
cx,
);
});
let snapshot = multibuffer.read(cx).snapshot(cx);
assert_eq!(snapshot.text(), "aaa\nbbb\nccc\nddd");
// This point is right at the start of the very first excerpt, so if we get
// a buffer range, we should get `0..0`
let excerpt_2_start = Point::new(2, 0);
let expected_ranges = vec![BufferOffset(0)..BufferOffset(0)];
let ranges = snapshot
.range_to_buffer_ranges(excerpt_2_start..excerpt_2_start)
.into_iter()
.map(|tup| tup.1)
.collect_vec();
assert_eq!(
ranges, expected_ranges,
"Zero-length range at excerpt boundary should return the excerpt at that point"
);
}
#[gpui::test]
async fn test_buffer_range_to_excerpt_ranges(cx: &mut TestAppContext) {
let base_text = indoc!(