Files
zed/crates/multi_buffer/src
Arth Srivastava 32558ab9d5 multi_buffer: Fix word diff ranges for collapsed hunks (#65829)
## Summary

Closes #65678.

Collapsed diff hunks can produce invalid word-diff ranges when offsets
calculated against deleted base text are applied to the modified buffer.
When the base and modified text differ in byte length, these offsets can
land inside a multibyte UTF-8 character, violating the boundary
invariant expected by the text rendering system.

The issue occurs because `self.show_deleted_hunks` indicates whether
deleted hunks can be displayed, not whether a particular hunk's deleted
text is currently present in the multibuffer.

This change checks for the corresponding `DiffTransform::DeletedHunk` in
`self.diff_transforms` before including base-side word diffs for a
specific hunk.

## Changes

- Check the per-hunk expansion state before projecting `base_word_diffs`
into multibuffer offsets.
- Preserve deleted-side and modified-side word highlighting for expanded
hunks.
- Avoid emitting base-side word-diff ranges for collapsed hunks, where
the deleted text is not present in the multibuffer.
- Add regression tests in
`crates/multi_buffer/src/multi_buffer_tests.rs` covering:
- `test_word_diff_byte_132_collapsed`: verifies that collapsed hunks do
not emit invalid UTF-8 ranges.
- `test_word_diff_byte_132_expanded`: verifies that expanded hunks
retain deleted-side and modified-side highlighting.
- `test_word_diff_mixed_expanded_and_collapsed`: verifies that expansion
state is handled independently for each hunk.
- `test_word_diff_expanded_cjk`: verifies word-diff highlighting with
multibyte CJK text.

## Testing

- `cargo test -p multi_buffer -j 1 --lib` — 72 passed.
- `cargo test -p multi_buffer -j 1 --lib test_word_diff` — 9 passed.
- `cargo clippy -p multi_buffer -j 1 --lib` — passed with no warnings.
- `cargo check -p multi_buffer -j 1 --tests` — passed.
- `cargo fmt --check -p multi_buffer` — passed.
- `git diff --check` — passed.

### Regression verification

The collapsed-hunk regression test fails against the original
implementation because a generated word-diff range ends at byte 132,
which is not a valid UTF-8 character boundary. It passes with this
change. The expanded and mixed-hunk regression tests also pass.

The complete macOS editor-rendering crash has not been independently
reproduced by an editor-level test; the regression tests verify the
invalid-range condition at the multibuffer layer.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI checklist not applicable; this PR contains no UI changes.
- [x] Tests cover the new or changed behavior.

Release Notes:

- Fixed an issue where collapsed inline diff hunks could produce invalid
word-diff highlights when rendering multibyte UTF-8 text.

---------

Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
2026-10-22 12:12:21 +00:00
..
…