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

+3 -1
View File
@@ -3636,6 +3636,7 @@ impl MultiBufferSnapshot {
result
}
/// Callers should not provide a range where `end < start`
pub fn range_to_buffer_ranges<T: ToOffset>(
&self,
range: Range<T>,
@@ -3647,6 +3648,7 @@ impl MultiBufferSnapshot {
let mut cursor = self.cursor::<MultiBufferOffset, BufferOffset>();
let start = range.start.to_offset(self);
let end = range.end.to_offset(self);
let range_non_empty = end > start;
cursor.seek(&start);
let mut result: Vec<(
@@ -3655,7 +3657,7 @@ impl MultiBufferSnapshot {
ExcerptRange<text::Anchor>,
)> = Vec::new();
while let Some(region) = cursor.region() {
if region.range.start >= end {
if region.range.start > end || (region.range.start == end && range_non_empty) {
break;
}
if region.is_main_buffer {