git: Add word diff highlighting (#43269)
This PR adds word/character diff for expanded diff hunks that have both a deleted and added section, as well as a setting `word_diff_enabled` to enable/disable word diffs per language. - `word_diff_enabled`: Defaults to true. Whether or not expanded diff hunks will show word diff highlights when they're able to. ### Preview <img width="1502" height="430" alt="image" src="https://github.com/user-attachments/assets/1a8d5b71-449e-44cd-bc87-d6b65bfca545" /> ### Architecture I had three architecture goals I wanted to have when adding word diff support: - Caching: We should only calculate word diffs once and save the result. This is because calculating word diffs can be expensive, and Zed should always be responsive. - Don't block the main thread: Word diffs should be computed in the background to prevent hanging Zed. - Lazy calculation: We should calculate word diffs for buffers that are not visible to a user. To accomplish the three goals, word diffs are computed as a part of `BufferDiff` diff hunk processing because it happens on a background thread, is cached until the file is edited, and is only refreshed for open buffers. My original implementation calculated word diffs every frame in the Editor element. This had the benefit of lazy evaluation because it only calculated visible frames, but it didn't have caching for the calculations, and the code wasn't organized. Because the hunk calculations would happen in two separate places instead of just `BufferDiff`. Finally, it always happened on the main thread because it was during the `EditorElement` layout phase. I used Zed's [`diff_internal`](https://github.com/zed-industries/zed/blob/02b2aa6c50c03d3005bec2effbc9f87161fbb1e8/crates/language/src/text_diff.rs#L230-L267) as a starting place for word diff calculations because it uses `Imara_diff` behind the scenes and already has language-specific support. #### Future Improvements In the future, we could add `AST` based word diff highlights, e.g. https://github.com/zed-industries/zed/pull/43691. Release Notes: - git: Show word diff highlight in expanded diff hunks with less than 5 lines. - git: Add `word_diff_enabled` as a language setting that defaults to true. --------- Co-authored-by: David Kleingeld <davidsk@zed.dev> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: cameron <cameron.studdstreet@gmail.com> Co-authored-by: Lukas Wirth <lukas@zed.dev>
This commit is contained in:
co-authored by
David Kleingeld
Cole Miller
cameron
Lukas Wirth
parent
2df5993eb0
commit
464c0be2b7
@@ -152,6 +152,8 @@ pub struct MultiBufferDiffHunk {
|
||||
pub diff_base_byte_range: Range<BufferOffset>,
|
||||
/// Whether or not this hunk also appears in the 'secondary diff'.
|
||||
pub secondary_status: DiffHunkSecondaryStatus,
|
||||
/// The word diffs for this hunk.
|
||||
pub word_diffs: Vec<Range<MultiBufferOffset>>,
|
||||
}
|
||||
|
||||
impl MultiBufferDiffHunk {
|
||||
@@ -561,6 +563,7 @@ pub struct MultiBufferSnapshot {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// A piece of text in the multi-buffer
|
||||
enum DiffTransform {
|
||||
Unmodified {
|
||||
summary: MBTextSummary,
|
||||
@@ -961,6 +964,8 @@ struct MultiBufferCursor<'a, MBD, BD> {
|
||||
cached_region: Option<MultiBufferRegion<'a, MBD, BD>>,
|
||||
}
|
||||
|
||||
/// Matches transformations to an item
|
||||
/// This is essentially a more detailed version of DiffTransform
|
||||
#[derive(Clone)]
|
||||
struct MultiBufferRegion<'a, MBD, BD> {
|
||||
buffer: &'a BufferSnapshot,
|
||||
@@ -3870,11 +3875,31 @@ impl MultiBufferSnapshot {
|
||||
} else {
|
||||
range.end.row + 1
|
||||
};
|
||||
|
||||
let word_diffs = (!hunk.base_word_diffs.is_empty()
|
||||
|| !hunk.buffer_word_diffs.is_empty())
|
||||
.then(|| {
|
||||
let hunk_start_offset =
|
||||
Anchor::in_buffer(excerpt.id, hunk.buffer_range.start).to_offset(self);
|
||||
|
||||
hunk.base_word_diffs
|
||||
.iter()
|
||||
.map(|diff| hunk_start_offset + diff.start..hunk_start_offset + diff.end)
|
||||
.chain(
|
||||
hunk.buffer_word_diffs
|
||||
.into_iter()
|
||||
.map(|diff| Anchor::range_in_buffer(excerpt.id, diff).to_offset(self)),
|
||||
)
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
Some(MultiBufferDiffHunk {
|
||||
row_range: MultiBufferRow(range.start.row)..MultiBufferRow(end_row),
|
||||
buffer_id: excerpt.buffer_id,
|
||||
excerpt_id: excerpt.id,
|
||||
buffer_range: hunk.buffer_range.clone(),
|
||||
word_diffs,
|
||||
diff_base_byte_range: BufferOffset(hunk.diff_base_byte_range.start)
|
||||
..BufferOffset(hunk.diff_base_byte_range.end),
|
||||
secondary_status: hunk.secondary_status,
|
||||
@@ -6834,6 +6859,7 @@ where
|
||||
TextDimension::add_assign(&mut buffer_end, &buffer_range_len);
|
||||
let start = self.diff_transforms.start().output_dimension.0;
|
||||
let end = self.diff_transforms.end().output_dimension.0;
|
||||
|
||||
Some(MultiBufferRegion {
|
||||
buffer,
|
||||
excerpt,
|
||||
|
||||
Reference in New Issue
Block a user