buffer_diff.rs

   1use futures::channel::oneshot;
   2use git2::{DiffLineType as GitDiffLineType, DiffOptions as GitOptions, Patch as GitPatch};
   3use gpui::{App, AppContext as _, Context, Entity, EventEmitter, Task};
   4use language::{
   5    Capability, Diff, DiffOptions, File, Language, LanguageName, LanguageRegistry,
   6    language_settings::language_settings, word_diff_ranges,
   7};
   8use rope::Rope;
   9use std::{
  10    cmp::Ordering,
  11    future::Future,
  12    iter,
  13    ops::{Range, RangeInclusive},
  14    sync::Arc,
  15};
  16use sum_tree::SumTree;
  17use text::{
  18    Anchor, Bias, BufferId, Edit, OffsetRangeExt, Patch, Point, ToOffset as _, ToPoint as _,
  19};
  20use util::ResultExt;
  21
  22pub const MAX_WORD_DIFF_LINE_COUNT: usize = 5;
  23
  24pub struct BufferDiff {
  25    pub buffer_id: BufferId,
  26    inner: BufferDiffInner<Entity<language::Buffer>>,
  27    secondary_diff: Option<Entity<BufferDiff>>,
  28}
  29
  30#[derive(Clone)]
  31pub struct BufferDiffSnapshot {
  32    inner: BufferDiffInner<language::BufferSnapshot>,
  33    secondary_diff: Option<Arc<BufferDiffSnapshot>>,
  34}
  35
  36impl std::fmt::Debug for BufferDiffSnapshot {
  37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  38        f.debug_struct("BufferDiffSnapshot")
  39            .field("inner", &self.inner)
  40            .field("secondary_diff", &self.secondary_diff)
  41            .finish()
  42    }
  43}
  44
  45#[derive(Clone)]
  46pub struct BufferDiffUpdate {
  47    inner: BufferDiffInner<Arc<str>>,
  48    buffer_snapshot: text::BufferSnapshot,
  49    base_text_edits: Option<Diff>,
  50    base_text_changed: bool,
  51}
  52
  53impl BufferDiffUpdate {
  54    pub fn from_hunks(
  55        base_text: Arc<str>,
  56        base_snapshot: &Rope,
  57        buffer_snapshot: text::BufferSnapshot,
  58        edits: Patch<usize>,
  59        diff_options: Option<DiffOptions>,
  60    ) -> Self {
  61        let hunks = edits.into_iter().map(|edit| {
  62            let buffer_range = buffer_snapshot.anchor_after(edit.new.start)
  63                ..buffer_snapshot.anchor_after(edit.new.end);
  64
  65            let (base_word_diffs, buffer_word_diffs) = if let Some(options) = &diff_options {
  66                word_diffs(
  67                    base_snapshot,
  68                    edit.old.clone(),
  69                    &buffer_snapshot,
  70                    buffer_range,
  71                    options,
  72                )
  73            } else {
  74                (Vec::new(), Vec::new())
  75            };
  76            InternalDiffHunk {
  77                buffer_range: buffer_snapshot.anchor_before(edit.new.start)
  78                    ..buffer_snapshot.anchor_before(edit.new.end),
  79                diff_base_byte_range: edit.old,
  80                base_word_diffs,
  81                buffer_word_diffs,
  82            }
  83        });
  84
  85        Self {
  86            inner: BufferDiffInner {
  87                hunks: SumTree::from_iter(hunks, &buffer_snapshot),
  88                pending_hunks: SumTree::new(&buffer_snapshot),
  89                base_text,
  90                base_text_exists: true,
  91                buffer_snapshot: buffer_snapshot.clone(),
  92            },
  93            buffer_snapshot,
  94            base_text_edits: None,
  95            base_text_changed: false,
  96        }
  97    }
  98}
  99
 100#[derive(Clone)]
 101struct BufferDiffInner<BaseText> {
 102    hunks: SumTree<InternalDiffHunk>,
 103    pending_hunks: SumTree<PendingHunk>,
 104    base_text: BaseText,
 105    base_text_exists: bool,
 106    buffer_snapshot: text::BufferSnapshot,
 107}
 108
 109impl<BaseText> BufferDiffInner<BaseText> {
 110    fn buffer_version(&self) -> &clock::Global {
 111        self.buffer_snapshot.version()
 112    }
 113}
 114
 115#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 116pub struct DiffHunkStatus {
 117    pub kind: DiffHunkStatusKind,
 118    pub secondary: DiffHunkSecondaryStatus,
 119}
 120
 121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 122pub enum DiffHunkStatusKind {
 123    Added,
 124    Modified,
 125    Deleted,
 126}
 127
 128#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 129/// Diff of Working Copy vs Index
 130/// aka 'is this hunk staged or not'
 131pub enum DiffHunkSecondaryStatus {
 132    /// Unstaged
 133    HasSecondaryHunk,
 134    /// Partially staged
 135    OverlapsWithSecondaryHunk,
 136    /// Staged
 137    NoSecondaryHunk,
 138    /// We are unstaging
 139    SecondaryHunkAdditionPending,
 140    /// We are stagind
 141    SecondaryHunkRemovalPending,
 142}
 143
 144/// A diff hunk resolved to rows in the buffer.
 145#[derive(Debug, Clone, PartialEq, Eq)]
 146pub struct DiffHunk {
 147    /// The buffer range as points.
 148    pub range: Range<Point>,
 149    /// The range in the buffer to which this hunk corresponds.
 150    pub buffer_range: Range<Anchor>,
 151    /// The range in the buffer's diff base text to which this hunk corresponds.
 152    pub diff_base_byte_range: Range<usize>,
 153    pub secondary_status: DiffHunkSecondaryStatus,
 154    // Anchors representing the word diff locations in the active buffer
 155    pub buffer_word_diffs: Vec<Range<Anchor>>,
 156    // Offsets relative to the start of the deleted diff that represent word diff locations
 157    pub base_word_diffs: Vec<Range<usize>>,
 158}
 159
 160/// We store [`InternalDiffHunk`]s internally so we don't need to store the additional row range.
 161#[derive(Debug, Clone, PartialEq, Eq)]
 162struct InternalDiffHunk {
 163    buffer_range: Range<Anchor>,
 164    diff_base_byte_range: Range<usize>,
 165    base_word_diffs: Vec<Range<usize>>,
 166    buffer_word_diffs: Vec<Range<Anchor>>,
 167}
 168
 169#[derive(Debug, Clone, PartialEq, Eq)]
 170struct PendingHunk {
 171    buffer_range: Range<Anchor>,
 172    diff_base_byte_range: Range<usize>,
 173    buffer_version: clock::Global,
 174    new_status: DiffHunkSecondaryStatus,
 175}
 176
 177#[derive(Debug, Clone)]
 178pub struct DiffHunkSummary {
 179    buffer_range: Range<Anchor>,
 180    diff_base_byte_range: Range<usize>,
 181}
 182
 183impl sum_tree::Item for InternalDiffHunk {
 184    type Summary = DiffHunkSummary;
 185
 186    fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
 187        DiffHunkSummary {
 188            buffer_range: self.buffer_range.clone(),
 189            diff_base_byte_range: self.diff_base_byte_range.clone(),
 190        }
 191    }
 192}
 193
 194impl sum_tree::Item for PendingHunk {
 195    type Summary = DiffHunkSummary;
 196
 197    fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
 198        DiffHunkSummary {
 199            buffer_range: self.buffer_range.clone(),
 200            diff_base_byte_range: self.diff_base_byte_range.clone(),
 201        }
 202    }
 203}
 204
 205impl sum_tree::Summary for DiffHunkSummary {
 206    type Context<'a> = &'a text::BufferSnapshot;
 207
 208    fn zero(_cx: Self::Context<'_>) -> Self {
 209        DiffHunkSummary {
 210            buffer_range: Anchor::MIN..Anchor::MIN,
 211            diff_base_byte_range: 0..0,
 212        }
 213    }
 214
 215    fn add_summary(&mut self, other: &Self, buffer: Self::Context<'_>) {
 216        self.buffer_range.start = *self
 217            .buffer_range
 218            .start
 219            .min(&other.buffer_range.start, buffer);
 220        self.buffer_range.end = *self.buffer_range.end.max(&other.buffer_range.end, buffer);
 221
 222        self.diff_base_byte_range.start = self
 223            .diff_base_byte_range
 224            .start
 225            .min(other.diff_base_byte_range.start);
 226        self.diff_base_byte_range.end = self
 227            .diff_base_byte_range
 228            .end
 229            .max(other.diff_base_byte_range.end);
 230    }
 231}
 232
 233impl sum_tree::SeekTarget<'_, DiffHunkSummary, DiffHunkSummary> for Anchor {
 234    fn cmp(&self, cursor_location: &DiffHunkSummary, buffer: &text::BufferSnapshot) -> Ordering {
 235        if self
 236            .cmp(&cursor_location.buffer_range.start, buffer)
 237            .is_lt()
 238        {
 239            Ordering::Less
 240        } else if self.cmp(&cursor_location.buffer_range.end, buffer).is_gt() {
 241            Ordering::Greater
 242        } else {
 243            Ordering::Equal
 244        }
 245    }
 246}
 247
 248impl sum_tree::SeekTarget<'_, DiffHunkSummary, DiffHunkSummary> for usize {
 249    fn cmp(&self, cursor_location: &DiffHunkSummary, _cx: &text::BufferSnapshot) -> Ordering {
 250        if *self < cursor_location.diff_base_byte_range.start {
 251            Ordering::Less
 252        } else if *self > cursor_location.diff_base_byte_range.end {
 253            Ordering::Greater
 254        } else {
 255            Ordering::Equal
 256        }
 257    }
 258}
 259
 260impl std::fmt::Debug for BufferDiffInner<language::BufferSnapshot> {
 261    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 262        f.debug_struct("BufferDiffSnapshot")
 263            .field("hunks", &self.hunks)
 264            .field("remote_id", &self.base_text.remote_id())
 265            .finish()
 266    }
 267}
 268
 269impl BufferDiffSnapshot {
 270    #[cfg(test)]
 271    fn new_sync(
 272        buffer: &text::BufferSnapshot,
 273        diff_base: String,
 274        cx: &mut gpui::TestAppContext,
 275    ) -> BufferDiffSnapshot {
 276        let buffer_diff = cx.new(|cx| BufferDiff::new_with_base_text(&diff_base, buffer, cx));
 277        buffer_diff.update(cx, |buffer_diff, cx| buffer_diff.snapshot(cx))
 278    }
 279
 280    pub fn is_empty(&self) -> bool {
 281        self.inner.hunks.is_empty()
 282    }
 283
 284    pub fn base_text_string(&self) -> Option<String> {
 285        self.inner
 286            .base_text_exists
 287            .then(|| self.inner.base_text.text())
 288    }
 289
 290    pub fn secondary_diff(&self) -> Option<&BufferDiffSnapshot> {
 291        self.secondary_diff.as_deref()
 292    }
 293
 294    pub fn buffer_version(&self) -> &clock::Global {
 295        self.inner.buffer_version()
 296    }
 297
 298    fn original_buffer_snapshot(&self) -> &text::BufferSnapshot {
 299        &self.inner.buffer_snapshot
 300    }
 301
 302    #[ztracing::instrument(skip_all)]
 303    pub fn hunks_intersecting_range<'a>(
 304        &'a self,
 305        range: Range<Anchor>,
 306        buffer: &'a text::BufferSnapshot,
 307    ) -> impl 'a + Iterator<Item = DiffHunk> {
 308        let unstaged_counterpart = self.secondary_diff.as_ref().map(|diff| &diff.inner);
 309        self.inner
 310            .hunks_intersecting_range(range, buffer, unstaged_counterpart)
 311    }
 312
 313    pub fn hunks_intersecting_range_rev<'a>(
 314        &'a self,
 315        range: Range<Anchor>,
 316        buffer: &'a text::BufferSnapshot,
 317    ) -> impl 'a + Iterator<Item = DiffHunk> {
 318        let filter = move |summary: &DiffHunkSummary| {
 319            let before_start = summary.buffer_range.end.cmp(&range.start, buffer).is_lt();
 320            let after_end = summary.buffer_range.start.cmp(&range.end, buffer).is_gt();
 321            !before_start && !after_end
 322        };
 323        self.inner.hunks_intersecting_range_rev_impl(filter, buffer)
 324    }
 325
 326    pub fn hunks_intersecting_base_text_range<'a>(
 327        &'a self,
 328        range: Range<usize>,
 329        main_buffer: &'a text::BufferSnapshot,
 330    ) -> impl 'a + Iterator<Item = DiffHunk> {
 331        let unstaged_counterpart = self.secondary_diff.as_ref().map(|diff| &diff.inner);
 332        let filter = move |summary: &DiffHunkSummary| {
 333            let before_start = summary.diff_base_byte_range.end < range.start;
 334            let after_end = summary.diff_base_byte_range.start > range.end;
 335            !before_start && !after_end
 336        };
 337        self.inner
 338            .hunks_intersecting_range_impl(filter, main_buffer, unstaged_counterpart)
 339    }
 340
 341    pub fn hunks_intersecting_base_text_range_rev<'a>(
 342        &'a self,
 343        range: Range<usize>,
 344        main_buffer: &'a text::BufferSnapshot,
 345    ) -> impl 'a + Iterator<Item = DiffHunk> {
 346        let filter = move |summary: &DiffHunkSummary| {
 347            let before_start = summary.diff_base_byte_range.end.cmp(&range.start).is_lt();
 348            let after_end = summary.diff_base_byte_range.start.cmp(&range.end).is_gt();
 349            !before_start && !after_end
 350        };
 351        self.inner
 352            .hunks_intersecting_range_rev_impl(filter, main_buffer)
 353    }
 354
 355    pub fn hunks<'a>(
 356        &'a self,
 357        buffer_snapshot: &'a text::BufferSnapshot,
 358    ) -> impl 'a + Iterator<Item = DiffHunk> {
 359        self.hunks_intersecting_range(
 360            Anchor::min_max_range_for_buffer(buffer_snapshot.remote_id()),
 361            buffer_snapshot,
 362        )
 363    }
 364
 365    pub fn hunks_in_row_range<'a>(
 366        &'a self,
 367        range: Range<u32>,
 368        buffer: &'a text::BufferSnapshot,
 369    ) -> impl 'a + Iterator<Item = DiffHunk> {
 370        let start = buffer.anchor_before(Point::new(range.start, 0));
 371        let end = buffer.anchor_after(Point::new(range.end, 0));
 372        self.hunks_intersecting_range(start..end, buffer)
 373    }
 374
 375    pub fn range_to_hunk_range(
 376        &self,
 377        range: Range<Anchor>,
 378        buffer: &text::BufferSnapshot,
 379    ) -> (Option<Range<Anchor>>, Option<Range<usize>>) {
 380        let first_hunk = self.hunks_intersecting_range(range.clone(), buffer).next();
 381        let last_hunk = self.hunks_intersecting_range_rev(range, buffer).next();
 382        let range = first_hunk
 383            .as_ref()
 384            .zip(last_hunk.as_ref())
 385            .map(|(first, last)| first.buffer_range.start..last.buffer_range.end);
 386        let base_text_range = first_hunk
 387            .zip(last_hunk)
 388            .map(|(first, last)| first.diff_base_byte_range.start..last.diff_base_byte_range.end);
 389        (range, base_text_range)
 390    }
 391
 392    pub fn base_text(&self) -> &language::BufferSnapshot {
 393        &self.inner.base_text
 394    }
 395
 396    /// If this function returns `true`, the base texts are equal. If this
 397    /// function returns `false`, they might be equal, but might not. This
 398    /// result is used to avoid recalculating diffs in situations where we know
 399    /// nothing has changed.
 400    pub fn base_texts_definitely_eq(&self, other: &Self) -> bool {
 401        if self.inner.base_text_exists != other.inner.base_text_exists {
 402            return false;
 403        }
 404        let left = &self.inner.base_text;
 405        let right = &other.inner.base_text;
 406        let (old_id, old_version, old_empty) = (left.remote_id(), left.version(), left.is_empty());
 407        let (new_id, new_version, new_empty) =
 408            (right.remote_id(), right.version(), right.is_empty());
 409        (new_id == old_id && new_version == old_version) || (new_empty && old_empty)
 410    }
 411
 412    /// Returns the last hunk whose start is less than or equal to the given position.
 413    fn hunk_before_base_text_offset<'a>(
 414        &self,
 415        target: usize,
 416        cursor: &mut sum_tree::Cursor<'a, '_, InternalDiffHunk, DiffHunkSummary>,
 417    ) -> Option<&'a InternalDiffHunk> {
 418        cursor.seek_forward(&target, Bias::Left);
 419        if cursor
 420            .item()
 421            .is_none_or(|hunk| target < hunk.diff_base_byte_range.start)
 422        {
 423            cursor.prev();
 424        }
 425        cursor
 426            .item()
 427            .filter(|hunk| target >= hunk.diff_base_byte_range.start)
 428    }
 429
 430    fn hunk_before_buffer_anchor<'a>(
 431        &self,
 432        target: Anchor,
 433        cursor: &mut sum_tree::Cursor<'a, '_, InternalDiffHunk, DiffHunkSummary>,
 434        buffer: &text::BufferSnapshot,
 435    ) -> Option<&'a InternalDiffHunk> {
 436        cursor.seek_forward(&target, Bias::Left);
 437        if cursor
 438            .item()
 439            .is_none_or(|hunk| target.cmp(&hunk.buffer_range.start, buffer).is_lt())
 440        {
 441            cursor.prev();
 442        }
 443        cursor
 444            .item()
 445            .filter(|hunk| target.cmp(&hunk.buffer_range.start, buffer).is_ge())
 446    }
 447
 448    /// Returns a patch mapping the provided main buffer snapshot to the base text of this diff.
 449    ///
 450    /// The returned patch is guaranteed to be accurate for all main buffer points in the provided range,
 451    /// but not necessarily for points outside that range.
 452    pub fn patch_for_buffer_range<'a>(
 453        &'a self,
 454        range: RangeInclusive<Point>,
 455        buffer: &'a text::BufferSnapshot,
 456    ) -> Patch<Point> {
 457        if !self.inner.base_text_exists {
 458            return Patch::new(vec![Edit {
 459                old: Point::zero()..buffer.max_point(),
 460                new: Point::zero()..Point::zero(),
 461            }]);
 462        }
 463
 464        let mut edits_since_diff = Patch::new(
 465            buffer
 466                .edits_since::<Point>(&self.inner.buffer_snapshot.version)
 467                .collect::<Vec<_>>(),
 468        );
 469        edits_since_diff.invert();
 470
 471        let mut start_point = edits_since_diff.old_to_new(*range.start());
 472        if let Some(first_edit) = edits_since_diff.edits().first() {
 473            start_point = start_point.min(first_edit.new.start);
 474        }
 475
 476        let original_snapshot = self.original_buffer_snapshot();
 477        let base_text = self.base_text();
 478
 479        let mut cursor = self.inner.hunks.cursor(original_snapshot);
 480        self.hunk_before_buffer_anchor(
 481            original_snapshot.anchor_before(start_point),
 482            &mut cursor,
 483            original_snapshot,
 484        );
 485        if cursor.item().is_none() {
 486            cursor.next();
 487        }
 488
 489        let mut prefix_edit = cursor.prev_item().map(|prev_hunk| Edit {
 490            old: Point::zero()..prev_hunk.buffer_range.end.to_point(original_snapshot),
 491            new: Point::zero()..prev_hunk.diff_base_byte_range.end.to_point(base_text),
 492        });
 493
 494        let mut range_end = edits_since_diff.old_to_new(*range.end());
 495        if let Some(last_edit) = edits_since_diff.edits().last() {
 496            range_end = range_end.max(last_edit.new.end);
 497        }
 498        let range_end = original_snapshot.anchor_before(range_end);
 499
 500        let hunk_iter = std::iter::from_fn(move || {
 501            if let Some(edit) = prefix_edit.take() {
 502                return Some(edit);
 503            }
 504            let hunk = cursor.item()?;
 505            if hunk
 506                .buffer_range
 507                .start
 508                .cmp(&range_end, original_snapshot)
 509                .is_gt()
 510            {
 511                return None;
 512            }
 513            let edit = Edit {
 514                old: hunk.buffer_range.to_point(original_snapshot),
 515                new: hunk.diff_base_byte_range.to_point(base_text),
 516            };
 517            cursor.next();
 518            Some(edit)
 519        });
 520
 521        edits_since_diff.compose(hunk_iter)
 522    }
 523
 524    #[cfg(test)]
 525    pub(crate) fn patch_for_buffer_range_naive<'a>(
 526        &'a self,
 527        buffer: &'a text::BufferSnapshot,
 528    ) -> Patch<Point> {
 529        let original_snapshot = self.original_buffer_snapshot();
 530
 531        let edits_since: Vec<Edit<Point>> = buffer
 532            .edits_since::<Point>(original_snapshot.version())
 533            .collect();
 534        let mut inverted_edits_since = Patch::new(edits_since);
 535        inverted_edits_since.invert();
 536
 537        inverted_edits_since.compose(
 538            self.inner
 539                .hunks
 540                .iter()
 541                .map(|hunk| {
 542                    let old_start = hunk.buffer_range.start.to_point(original_snapshot);
 543                    let old_end = hunk.buffer_range.end.to_point(original_snapshot);
 544                    let new_start = self
 545                        .base_text()
 546                        .offset_to_point(hunk.diff_base_byte_range.start);
 547                    let new_end = self
 548                        .base_text()
 549                        .offset_to_point(hunk.diff_base_byte_range.end);
 550                    Edit {
 551                        old: old_start..old_end,
 552                        new: new_start..new_end,
 553                    }
 554                })
 555                .chain(
 556                    if !self.inner.base_text_exists && self.inner.hunks.is_empty() {
 557                        Some(Edit {
 558                            old: Point::zero()..original_snapshot.max_point(),
 559                            new: Point::zero()..Point::zero(),
 560                        })
 561                    } else {
 562                        None
 563                    },
 564                ),
 565        )
 566    }
 567
 568    /// Returns a patch mapping the base text of this diff to the provided main buffer snapshot.
 569    ///
 570    /// The returned patch is guaranteed to be accurate for all base text points in the provided range,
 571    /// but not necessarily for points outside that range.
 572    pub fn patch_for_base_text_range<'a>(
 573        &'a self,
 574        range: RangeInclusive<Point>,
 575        buffer: &'a text::BufferSnapshot,
 576    ) -> Patch<Point> {
 577        if !self.inner.base_text_exists {
 578            return Patch::new(vec![Edit {
 579                old: Point::zero()..Point::zero(),
 580                new: Point::zero()..buffer.max_point(),
 581            }]);
 582        }
 583
 584        let edits_since_diff = buffer
 585            .edits_since::<Point>(&self.inner.buffer_snapshot.version)
 586            .collect::<Vec<_>>();
 587
 588        let mut hunk_patch = Vec::new();
 589        let mut cursor = self.inner.hunks.cursor(self.original_buffer_snapshot());
 590        let hunk_before = self
 591            .hunk_before_base_text_offset(range.start().to_offset(self.base_text()), &mut cursor);
 592
 593        if let Some(hunk) = hunk_before
 594            && let Some(first_edit) = edits_since_diff.first()
 595            && hunk
 596                .buffer_range
 597                .start
 598                .to_point(self.original_buffer_snapshot())
 599                > first_edit.old.start
 600        {
 601            cursor.reset();
 602            self.hunk_before_buffer_anchor(
 603                self.original_buffer_snapshot()
 604                    .anchor_before(first_edit.old.start),
 605                &mut cursor,
 606                self.original_buffer_snapshot(),
 607            );
 608        }
 609        if cursor.item().is_none() {
 610            cursor.next();
 611        }
 612        if let Some(prev_hunk) = cursor.prev_item() {
 613            hunk_patch.push(Edit {
 614                old: Point::zero()
 615                    ..prev_hunk
 616                        .diff_base_byte_range
 617                        .end
 618                        .to_point(self.base_text()),
 619                new: Point::zero()
 620                    ..prev_hunk
 621                        .buffer_range
 622                        .end
 623                        .to_point(self.original_buffer_snapshot()),
 624            })
 625        }
 626        let range_end = range.end().to_offset(self.base_text());
 627        while let Some(hunk) = cursor.item()
 628            && (hunk.diff_base_byte_range.start <= range_end
 629                || edits_since_diff.last().is_some_and(|last_edit| {
 630                    hunk.buffer_range
 631                        .start
 632                        .to_point(self.original_buffer_snapshot())
 633                        <= last_edit.old.end
 634                }))
 635        {
 636            hunk_patch.push(Edit {
 637                old: hunk.diff_base_byte_range.to_point(self.base_text()),
 638                new: hunk.buffer_range.to_point(self.original_buffer_snapshot()),
 639            });
 640            cursor.next();
 641        }
 642
 643        Patch::new(hunk_patch).compose(edits_since_diff)
 644    }
 645
 646    #[cfg(test)]
 647    pub(crate) fn patch_for_base_text_range_naive<'a>(
 648        &'a self,
 649        buffer: &'a text::BufferSnapshot,
 650    ) -> Patch<Point> {
 651        let original_snapshot = self.original_buffer_snapshot();
 652
 653        let mut hunk_edits: Vec<Edit<Point>> = Vec::new();
 654        for hunk in self.inner.hunks.iter() {
 655            let old_start = self
 656                .base_text()
 657                .offset_to_point(hunk.diff_base_byte_range.start);
 658            let old_end = self
 659                .base_text()
 660                .offset_to_point(hunk.diff_base_byte_range.end);
 661            let new_start = hunk.buffer_range.start.to_point(original_snapshot);
 662            let new_end = hunk.buffer_range.end.to_point(original_snapshot);
 663            hunk_edits.push(Edit {
 664                old: old_start..old_end,
 665                new: new_start..new_end,
 666            });
 667        }
 668        if !self.inner.base_text_exists && hunk_edits.is_empty() {
 669            hunk_edits.push(Edit {
 670                old: Point::zero()..Point::zero(),
 671                new: Point::zero()..original_snapshot.max_point(),
 672            })
 673        }
 674        let hunk_patch = Patch::new(hunk_edits);
 675
 676        hunk_patch.compose(buffer.edits_since::<Point>(original_snapshot.version()))
 677    }
 678
 679    pub fn buffer_point_to_base_text_range(
 680        &self,
 681        point: Point,
 682        buffer: &text::BufferSnapshot,
 683    ) -> Range<Point> {
 684        let patch = self.patch_for_buffer_range(point..=point, buffer);
 685        let edit = patch.edit_for_old_position(point);
 686        edit.new
 687    }
 688
 689    pub fn base_text_point_to_buffer_range(
 690        &self,
 691        point: Point,
 692        buffer: &text::BufferSnapshot,
 693    ) -> Range<Point> {
 694        let patch = self.patch_for_base_text_range(point..=point, buffer);
 695        let edit = patch.edit_for_old_position(point);
 696        edit.new
 697    }
 698
 699    pub fn buffer_point_to_base_text_point(
 700        &self,
 701        point: Point,
 702        buffer: &text::BufferSnapshot,
 703    ) -> Point {
 704        let patch = self.patch_for_buffer_range(point..=point, buffer);
 705        let edit = patch.edit_for_old_position(point);
 706        if point == edit.old.end {
 707            edit.new.end
 708        } else {
 709            edit.new.start
 710        }
 711    }
 712
 713    pub fn base_text_point_to_buffer_point(
 714        &self,
 715        point: Point,
 716        buffer: &text::BufferSnapshot,
 717    ) -> Point {
 718        let patch = self.patch_for_base_text_range(point..=point, buffer);
 719        let edit = patch.edit_for_old_position(point);
 720        if point == edit.old.end {
 721            edit.new.end
 722        } else {
 723            edit.new.start
 724        }
 725    }
 726}
 727
 728impl BufferDiffInner<Entity<language::Buffer>> {
 729    /// Returns the new index text and new pending hunks.
 730    fn stage_or_unstage_hunks_impl(
 731        &mut self,
 732        unstaged_diff: &Self,
 733        stage: bool,
 734        hunks: &[DiffHunk],
 735        buffer: &text::BufferSnapshot,
 736        file_exists: bool,
 737        cx: &mut Context<BufferDiff>,
 738    ) -> Option<Rope> {
 739        let head_text = self
 740            .base_text_exists
 741            .then(|| self.base_text.read(cx).as_rope().clone());
 742        let index_text = unstaged_diff
 743            .base_text_exists
 744            .then(|| unstaged_diff.base_text.read(cx).as_rope().clone());
 745
 746        // If the file doesn't exist in either HEAD or the index, then the
 747        // entire file must be either created or deleted in the index.
 748        let (index_text, head_text) = match (index_text, head_text) {
 749            (Some(index_text), Some(head_text)) if file_exists || !stage => (index_text, head_text),
 750            (index_text, head_text) => {
 751                let (new_index_text, new_status) = if stage {
 752                    log::debug!("stage all");
 753                    (
 754                        file_exists.then(|| buffer.as_rope().clone()),
 755                        DiffHunkSecondaryStatus::SecondaryHunkRemovalPending,
 756                    )
 757                } else {
 758                    log::debug!("unstage all");
 759                    (
 760                        head_text,
 761                        DiffHunkSecondaryStatus::SecondaryHunkAdditionPending,
 762                    )
 763                };
 764
 765                let hunk = PendingHunk {
 766                    buffer_range: Anchor::min_max_range_for_buffer(buffer.remote_id()),
 767                    diff_base_byte_range: 0..index_text.map_or(0, |rope| rope.len()),
 768                    buffer_version: buffer.version().clone(),
 769                    new_status,
 770                };
 771                self.pending_hunks = SumTree::from_item(hunk, buffer);
 772                return new_index_text;
 773            }
 774        };
 775
 776        let mut pending_hunks = SumTree::new(buffer);
 777        let mut old_pending_hunks = self.pending_hunks.cursor::<DiffHunkSummary>(buffer);
 778
 779        // first, merge new hunks into pending_hunks
 780        for DiffHunk {
 781            buffer_range,
 782            diff_base_byte_range,
 783            secondary_status,
 784            ..
 785        } in hunks.iter().cloned()
 786        {
 787            let preceding_pending_hunks = old_pending_hunks.slice(&buffer_range.start, Bias::Left);
 788            pending_hunks.append(preceding_pending_hunks, buffer);
 789
 790            // Skip all overlapping or adjacent old pending hunks
 791            while old_pending_hunks.item().is_some_and(|old_hunk| {
 792                old_hunk
 793                    .buffer_range
 794                    .start
 795                    .cmp(&buffer_range.end, buffer)
 796                    .is_le()
 797            }) {
 798                old_pending_hunks.next();
 799            }
 800
 801            if (stage && secondary_status == DiffHunkSecondaryStatus::NoSecondaryHunk)
 802                || (!stage && secondary_status == DiffHunkSecondaryStatus::HasSecondaryHunk)
 803            {
 804                continue;
 805            }
 806
 807            pending_hunks.push(
 808                PendingHunk {
 809                    buffer_range,
 810                    diff_base_byte_range,
 811                    buffer_version: buffer.version().clone(),
 812                    new_status: if stage {
 813                        DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
 814                    } else {
 815                        DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
 816                    },
 817                },
 818                buffer,
 819            );
 820        }
 821        // append the remainder
 822        pending_hunks.append(old_pending_hunks.suffix(), buffer);
 823
 824        let mut unstaged_hunk_cursor = unstaged_diff.hunks.cursor::<DiffHunkSummary>(buffer);
 825        unstaged_hunk_cursor.next();
 826
 827        // then, iterate over all pending hunks (both new ones and the existing ones) and compute the edits
 828        let mut prev_unstaged_hunk_buffer_end = 0;
 829        let mut prev_unstaged_hunk_base_text_end = 0;
 830        let mut edits = Vec::<(Range<usize>, String)>::new();
 831        let mut pending_hunks_iter = pending_hunks.iter().cloned().peekable();
 832        while let Some(PendingHunk {
 833            buffer_range,
 834            diff_base_byte_range,
 835            new_status,
 836            ..
 837        }) = pending_hunks_iter.next()
 838        {
 839            // Advance unstaged_hunk_cursor to skip unstaged hunks before current hunk
 840            let skipped_unstaged = unstaged_hunk_cursor.slice(&buffer_range.start, Bias::Left);
 841
 842            if let Some(unstaged_hunk) = skipped_unstaged.last() {
 843                prev_unstaged_hunk_base_text_end = unstaged_hunk.diff_base_byte_range.end;
 844                prev_unstaged_hunk_buffer_end = unstaged_hunk.buffer_range.end.to_offset(buffer);
 845            }
 846
 847            // Find where this hunk is in the index if it doesn't overlap
 848            let mut buffer_offset_range = buffer_range.to_offset(buffer);
 849            let start_overshoot = buffer_offset_range.start - prev_unstaged_hunk_buffer_end;
 850            let mut index_start = prev_unstaged_hunk_base_text_end + start_overshoot;
 851
 852            loop {
 853                // Merge this hunk with any overlapping unstaged hunks.
 854                if let Some(unstaged_hunk) = unstaged_hunk_cursor.item() {
 855                    let unstaged_hunk_offset_range = unstaged_hunk.buffer_range.to_offset(buffer);
 856                    if unstaged_hunk_offset_range.start <= buffer_offset_range.end {
 857                        prev_unstaged_hunk_base_text_end = unstaged_hunk.diff_base_byte_range.end;
 858                        prev_unstaged_hunk_buffer_end = unstaged_hunk_offset_range.end;
 859
 860                        index_start = index_start.min(unstaged_hunk.diff_base_byte_range.start);
 861                        buffer_offset_range.start = buffer_offset_range
 862                            .start
 863                            .min(unstaged_hunk_offset_range.start);
 864                        buffer_offset_range.end =
 865                            buffer_offset_range.end.max(unstaged_hunk_offset_range.end);
 866
 867                        unstaged_hunk_cursor.next();
 868                        continue;
 869                    }
 870                }
 871
 872                // If any unstaged hunks were merged, then subsequent pending hunks may
 873                // now overlap this hunk. Merge them.
 874                if let Some(next_pending_hunk) = pending_hunks_iter.peek() {
 875                    let next_pending_hunk_offset_range =
 876                        next_pending_hunk.buffer_range.to_offset(buffer);
 877                    if next_pending_hunk_offset_range.start <= buffer_offset_range.end {
 878                        buffer_offset_range.end = buffer_offset_range
 879                            .end
 880                            .max(next_pending_hunk_offset_range.end);
 881                        pending_hunks_iter.next();
 882                        continue;
 883                    }
 884                }
 885
 886                break;
 887            }
 888
 889            let end_overshoot = buffer_offset_range
 890                .end
 891                .saturating_sub(prev_unstaged_hunk_buffer_end);
 892            let index_end = prev_unstaged_hunk_base_text_end + end_overshoot;
 893            let index_byte_range = index_start..index_end;
 894
 895            let replacement_text = match new_status {
 896                DiffHunkSecondaryStatus::SecondaryHunkRemovalPending => {
 897                    log::debug!("staging hunk {:?}", buffer_offset_range);
 898                    buffer
 899                        .text_for_range(buffer_offset_range)
 900                        .collect::<String>()
 901                }
 902                DiffHunkSecondaryStatus::SecondaryHunkAdditionPending => {
 903                    log::debug!("unstaging hunk {:?}", buffer_offset_range);
 904                    head_text
 905                        .chunks_in_range(diff_base_byte_range.clone())
 906                        .collect::<String>()
 907                }
 908                _ => {
 909                    debug_assert!(false);
 910                    continue;
 911                }
 912            };
 913
 914            edits.push((index_byte_range, replacement_text));
 915        }
 916        drop(pending_hunks_iter);
 917        drop(old_pending_hunks);
 918        self.pending_hunks = pending_hunks;
 919
 920        #[cfg(debug_assertions)] // invariants: non-overlapping and sorted
 921        {
 922            for window in edits.windows(2) {
 923                let (range_a, range_b) = (&window[0].0, &window[1].0);
 924                debug_assert!(range_a.end < range_b.start);
 925            }
 926        }
 927
 928        let mut new_index_text = Rope::new();
 929        let mut index_cursor = index_text.cursor(0);
 930
 931        for (old_range, replacement_text) in edits {
 932            new_index_text.append(index_cursor.slice(old_range.start));
 933            index_cursor.seek_forward(old_range.end);
 934            new_index_text.push(&replacement_text);
 935        }
 936        new_index_text.append(index_cursor.suffix());
 937        Some(new_index_text)
 938    }
 939}
 940
 941impl BufferDiffInner<language::BufferSnapshot> {
 942    fn hunks_intersecting_range<'a>(
 943        &'a self,
 944        range: Range<Anchor>,
 945        buffer: &'a text::BufferSnapshot,
 946        secondary: Option<&'a Self>,
 947    ) -> impl 'a + Iterator<Item = DiffHunk> {
 948        let range = range.to_offset(buffer);
 949        let filter = move |summary: &DiffHunkSummary| {
 950            let summary_range = summary.buffer_range.to_offset(buffer);
 951            let before_start = summary_range.end < range.start;
 952            let after_end = summary_range.start > range.end;
 953            !before_start && !after_end
 954        };
 955        self.hunks_intersecting_range_impl(filter, buffer, secondary)
 956    }
 957
 958    fn hunks_intersecting_range_impl<'a>(
 959        &'a self,
 960        filter: impl 'a + Fn(&DiffHunkSummary) -> bool,
 961        buffer: &'a text::BufferSnapshot,
 962        secondary: Option<&'a Self>,
 963    ) -> impl 'a + Iterator<Item = DiffHunk> {
 964        let anchor_iter = self
 965            .hunks
 966            .filter::<_, DiffHunkSummary>(buffer, filter)
 967            .flat_map(move |hunk| {
 968                [
 969                    (
 970                        &hunk.buffer_range.start,
 971                        (
 972                            hunk.buffer_range.start,
 973                            hunk.diff_base_byte_range.start,
 974                            hunk,
 975                        ),
 976                    ),
 977                    (
 978                        &hunk.buffer_range.end,
 979                        (hunk.buffer_range.end, hunk.diff_base_byte_range.end, hunk),
 980                    ),
 981                ]
 982            });
 983
 984        let mut pending_hunks_cursor = self.pending_hunks.cursor::<DiffHunkSummary>(buffer);
 985        pending_hunks_cursor.next();
 986
 987        let mut secondary_cursor = None;
 988        if let Some(secondary) = secondary.as_ref() {
 989            let mut cursor = secondary.hunks.cursor::<DiffHunkSummary>(buffer);
 990            cursor.next();
 991            secondary_cursor = Some(cursor);
 992        }
 993
 994        let max_point = buffer.max_point();
 995        let mut summaries = buffer.summaries_for_anchors_with_payload::<Point, _, _>(anchor_iter);
 996        iter::from_fn(move || {
 997            loop {
 998                let (start_point, (start_anchor, start_base, hunk)) = summaries.next()?;
 999                let (mut end_point, (mut end_anchor, end_base, _)) = summaries.next()?;
1000
1001                let base_word_diffs = hunk.base_word_diffs.clone();
1002                let buffer_word_diffs = hunk.buffer_word_diffs.clone();
1003
1004                if !start_anchor.is_valid(buffer) {
1005                    continue;
1006                }
1007
1008                if end_point.column > 0 && end_point < max_point {
1009                    end_point.row += 1;
1010                    end_point.column = 0;
1011                    end_anchor = buffer.anchor_before(end_point);
1012                }
1013
1014                let mut secondary_status = DiffHunkSecondaryStatus::NoSecondaryHunk;
1015
1016                let mut has_pending = false;
1017                if start_anchor
1018                    .cmp(&pending_hunks_cursor.start().buffer_range.start, buffer)
1019                    .is_gt()
1020                {
1021                    pending_hunks_cursor.seek_forward(&start_anchor, Bias::Left);
1022                }
1023
1024                if let Some(pending_hunk) = pending_hunks_cursor.item() {
1025                    let mut pending_range = pending_hunk.buffer_range.to_point(buffer);
1026                    if pending_range.end.column > 0 {
1027                        pending_range.end.row += 1;
1028                        pending_range.end.column = 0;
1029                    }
1030
1031                    if pending_range == (start_point..end_point)
1032                        && !buffer.has_edits_since_in_range(
1033                            &pending_hunk.buffer_version,
1034                            start_anchor..end_anchor,
1035                        )
1036                    {
1037                        has_pending = true;
1038                        secondary_status = pending_hunk.new_status;
1039                    }
1040                }
1041
1042                if let (Some(secondary_cursor), false) = (secondary_cursor.as_mut(), has_pending) {
1043                    if start_anchor
1044                        .cmp(&secondary_cursor.start().buffer_range.start, buffer)
1045                        .is_gt()
1046                    {
1047                        secondary_cursor.seek_forward(&start_anchor, Bias::Left);
1048                    }
1049
1050                    if let Some(secondary_hunk) = secondary_cursor.item() {
1051                        let mut secondary_range = secondary_hunk.buffer_range.to_point(buffer);
1052                        if secondary_range.end.column > 0 {
1053                            secondary_range.end.row += 1;
1054                            secondary_range.end.column = 0;
1055                        }
1056                        if secondary_range.is_empty()
1057                            && secondary_hunk.diff_base_byte_range.is_empty()
1058                        {
1059                            // ignore
1060                        } else if secondary_range == (start_point..end_point) {
1061                            secondary_status = DiffHunkSecondaryStatus::HasSecondaryHunk;
1062                        } else if secondary_range.start <= end_point {
1063                            secondary_status = DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk;
1064                        }
1065                    }
1066                }
1067
1068                return Some(DiffHunk {
1069                    range: start_point..end_point,
1070                    diff_base_byte_range: start_base..end_base,
1071                    buffer_range: start_anchor..end_anchor,
1072                    base_word_diffs,
1073                    buffer_word_diffs,
1074                    secondary_status,
1075                });
1076            }
1077        })
1078    }
1079
1080    fn hunks_intersecting_range_rev_impl<'a>(
1081        &'a self,
1082        filter: impl 'a + Fn(&DiffHunkSummary) -> bool,
1083        buffer: &'a text::BufferSnapshot,
1084    ) -> impl 'a + Iterator<Item = DiffHunk> {
1085        let mut cursor = self.hunks.filter::<_, DiffHunkSummary>(buffer, filter);
1086
1087        iter::from_fn(move || {
1088            cursor.prev();
1089
1090            let hunk = cursor.item()?;
1091            let range = hunk.buffer_range.to_point(buffer);
1092
1093            Some(DiffHunk {
1094                range,
1095                diff_base_byte_range: hunk.diff_base_byte_range.clone(),
1096                buffer_range: hunk.buffer_range.clone(),
1097                // The secondary status is not used by callers of this method.
1098                secondary_status: DiffHunkSecondaryStatus::NoSecondaryHunk,
1099                base_word_diffs: hunk.base_word_diffs.clone(),
1100                buffer_word_diffs: hunk.buffer_word_diffs.clone(),
1101            })
1102        })
1103    }
1104}
1105
1106fn build_diff_options(
1107    file: Option<&Arc<dyn File>>,
1108    language: Option<LanguageName>,
1109    language_scope: Option<language::LanguageScope>,
1110    cx: &App,
1111) -> Option<DiffOptions> {
1112    #[cfg(any(test, feature = "test-support"))]
1113    {
1114        if !cx.has_global::<settings::SettingsStore>() {
1115            return Some(DiffOptions {
1116                language_scope,
1117                max_word_diff_line_count: MAX_WORD_DIFF_LINE_COUNT,
1118                ..Default::default()
1119            });
1120        }
1121    }
1122
1123    language_settings(language, file, cx)
1124        .word_diff_enabled
1125        .then_some(DiffOptions {
1126            language_scope,
1127            max_word_diff_line_count: MAX_WORD_DIFF_LINE_COUNT,
1128            ..Default::default()
1129        })
1130}
1131
1132fn compute_hunks(
1133    diff_base: Option<(Arc<str>, Rope)>,
1134    buffer: &text::BufferSnapshot,
1135    diff_options: Option<DiffOptions>,
1136) -> SumTree<InternalDiffHunk> {
1137    let mut tree = SumTree::new(buffer);
1138
1139    if let Some((diff_base, diff_base_rope)) = diff_base {
1140        let buffer_text = buffer.as_rope().to_string();
1141
1142        let mut options = GitOptions::default();
1143        options.context_lines(0);
1144        let patch = GitPatch::from_buffers(
1145            diff_base.as_bytes(),
1146            None,
1147            buffer_text.as_bytes(),
1148            None,
1149            Some(&mut options),
1150        )
1151        .log_err();
1152
1153        // A common case in Zed is that the empty buffer is represented as just a newline,
1154        // but if we just compute a naive diff you get a "preserved" line in the middle,
1155        // which is a bit odd.
1156        if buffer_text == "\n" && diff_base.ends_with("\n") && diff_base.len() > 1 {
1157            tree.push(
1158                InternalDiffHunk {
1159                    buffer_range: buffer.anchor_before(0)..buffer.anchor_before(0),
1160                    diff_base_byte_range: 0..diff_base.len() - 1,
1161                    base_word_diffs: Vec::default(),
1162                    buffer_word_diffs: Vec::default(),
1163                },
1164                buffer,
1165            );
1166            return tree;
1167        }
1168
1169        if let Some(patch) = patch {
1170            let mut divergence = 0;
1171            for hunk_index in 0..patch.num_hunks() {
1172                let hunk = process_patch_hunk(
1173                    &patch,
1174                    hunk_index,
1175                    &diff_base_rope,
1176                    buffer,
1177                    &mut divergence,
1178                    diff_options.as_ref(),
1179                );
1180                tree.push(hunk, buffer);
1181            }
1182        }
1183    } else {
1184        tree.push(
1185            InternalDiffHunk {
1186                buffer_range: Anchor::min_max_range_for_buffer(buffer.remote_id()),
1187                diff_base_byte_range: 0..0,
1188                base_word_diffs: Vec::default(),
1189                buffer_word_diffs: Vec::default(),
1190            },
1191            buffer,
1192        );
1193    }
1194
1195    tree
1196}
1197
1198fn compare_hunks(
1199    new_hunks: &SumTree<InternalDiffHunk>,
1200    old_hunks: &SumTree<InternalDiffHunk>,
1201    old_snapshot: &text::BufferSnapshot,
1202    new_snapshot: &text::BufferSnapshot,
1203    old_base_text: &text::BufferSnapshot,
1204    new_base_text: &text::BufferSnapshot,
1205) -> DiffChanged {
1206    let mut new_cursor = new_hunks.cursor::<()>(new_snapshot);
1207    let mut old_cursor = old_hunks.cursor::<()>(new_snapshot);
1208    old_cursor.next();
1209    new_cursor.next();
1210    let mut start = None;
1211    let mut end = None;
1212    let mut base_text_start: Option<Anchor> = None;
1213    let mut base_text_end: Option<Anchor> = None;
1214
1215    let mut last_unchanged_new_hunk_end: Option<text::Anchor> = None;
1216    let mut has_changes = false;
1217    let mut extended_end_candidate: Option<text::Anchor> = None;
1218
1219    loop {
1220        match (new_cursor.item(), old_cursor.item()) {
1221            (Some(new_hunk), Some(old_hunk)) => {
1222                match new_hunk
1223                    .buffer_range
1224                    .start
1225                    .cmp(&old_hunk.buffer_range.start, new_snapshot)
1226                {
1227                    Ordering::Less => {
1228                        has_changes = true;
1229                        extended_end_candidate = None;
1230                        start.get_or_insert(new_hunk.buffer_range.start);
1231                        base_text_start.get_or_insert(
1232                            new_base_text.anchor_before(new_hunk.diff_base_byte_range.start),
1233                        );
1234                        end.replace(new_hunk.buffer_range.end);
1235                        let new_diff_range_end =
1236                            new_base_text.anchor_after(new_hunk.diff_base_byte_range.end);
1237                        if base_text_end.is_none_or(|base_text_end| {
1238                            new_diff_range_end
1239                                .cmp(&base_text_end, &new_base_text)
1240                                .is_gt()
1241                        }) {
1242                            base_text_end = Some(new_diff_range_end)
1243                        }
1244                        new_cursor.next();
1245                    }
1246                    Ordering::Equal => {
1247                        if new_hunk != old_hunk {
1248                            has_changes = true;
1249                            extended_end_candidate = None;
1250                            start.get_or_insert(new_hunk.buffer_range.start);
1251                            base_text_start.get_or_insert(
1252                                new_base_text.anchor_before(new_hunk.diff_base_byte_range.start),
1253                            );
1254                            if old_hunk
1255                                .buffer_range
1256                                .end
1257                                .cmp(&new_hunk.buffer_range.end, new_snapshot)
1258                                .is_ge()
1259                            {
1260                                end.replace(old_hunk.buffer_range.end);
1261                            } else {
1262                                end.replace(new_hunk.buffer_range.end);
1263                            }
1264
1265                            let old_hunk_diff_base_range_end =
1266                                old_base_text.anchor_after(old_hunk.diff_base_byte_range.end);
1267                            let new_hunk_diff_base_range_end =
1268                                new_base_text.anchor_after(new_hunk.diff_base_byte_range.end);
1269
1270                            base_text_end.replace(
1271                                *old_hunk_diff_base_range_end
1272                                    .max(&new_hunk_diff_base_range_end, new_base_text),
1273                            );
1274                        } else {
1275                            if !has_changes {
1276                                last_unchanged_new_hunk_end = Some(new_hunk.buffer_range.end);
1277                            } else if extended_end_candidate.is_none() {
1278                                extended_end_candidate = Some(new_hunk.buffer_range.start);
1279                            }
1280                        }
1281
1282                        new_cursor.next();
1283                        old_cursor.next();
1284                    }
1285                    Ordering::Greater => {
1286                        has_changes = true;
1287                        extended_end_candidate = None;
1288                        start.get_or_insert(old_hunk.buffer_range.start);
1289                        base_text_start.get_or_insert(
1290                            old_base_text.anchor_after(old_hunk.diff_base_byte_range.start),
1291                        );
1292                        end.replace(old_hunk.buffer_range.end);
1293                        let old_diff_range_end =
1294                            old_base_text.anchor_after(old_hunk.diff_base_byte_range.end);
1295                        if base_text_end.is_none_or(|base_text_end| {
1296                            old_diff_range_end
1297                                .cmp(&base_text_end, new_base_text)
1298                                .is_gt()
1299                        }) {
1300                            base_text_end = Some(old_diff_range_end)
1301                        }
1302                        old_cursor.next();
1303                    }
1304                }
1305            }
1306            (Some(new_hunk), None) => {
1307                has_changes = true;
1308                extended_end_candidate = None;
1309                start.get_or_insert(new_hunk.buffer_range.start);
1310                base_text_start
1311                    .get_or_insert(new_base_text.anchor_after(new_hunk.diff_base_byte_range.start));
1312                if end.is_none_or(|end| end.cmp(&new_hunk.buffer_range.end, &new_snapshot).is_le())
1313                {
1314                    end.replace(new_hunk.buffer_range.end);
1315                }
1316                let new_base_text_end =
1317                    new_base_text.anchor_after(new_hunk.diff_base_byte_range.end);
1318                if base_text_end.is_none_or(|base_text_end| {
1319                    new_base_text_end.cmp(&base_text_end, new_base_text).is_gt()
1320                }) {
1321                    base_text_end = Some(new_base_text_end)
1322                }
1323                new_cursor.next();
1324            }
1325            (None, Some(old_hunk)) => {
1326                has_changes = true;
1327                extended_end_candidate = None;
1328                start.get_or_insert(old_hunk.buffer_range.start);
1329                base_text_start
1330                    .get_or_insert(old_base_text.anchor_after(old_hunk.diff_base_byte_range.start));
1331                if end.is_none_or(|end| end.cmp(&old_hunk.buffer_range.end, &new_snapshot).is_le())
1332                {
1333                    end.replace(old_hunk.buffer_range.end);
1334                }
1335                let old_base_text_end =
1336                    old_base_text.anchor_after(old_hunk.diff_base_byte_range.end);
1337                if base_text_end.is_none_or(|base_text_end| {
1338                    old_base_text_end.cmp(&base_text_end, new_base_text).is_gt()
1339                }) {
1340                    base_text_end = Some(old_base_text_end);
1341                }
1342                old_cursor.next();
1343            }
1344            (None, None) => break,
1345        }
1346    }
1347
1348    let changed_range = start.zip(end).map(|(start, end)| start..end);
1349    let base_text_changed_range = base_text_start
1350        .zip(base_text_end)
1351        .map(|(start, end)| (start..end).to_offset(new_base_text));
1352
1353    let extended_range = if has_changes && let Some(changed_range) = changed_range.clone() {
1354        let extended_start = *last_unchanged_new_hunk_end
1355            .unwrap_or(text::Anchor::min_for_buffer(new_snapshot.remote_id()))
1356            .min(&changed_range.start, new_snapshot);
1357        let extended_start = new_snapshot
1358            .anchored_edits_since_in_range::<usize>(
1359                &old_snapshot.version(),
1360                extended_start..changed_range.start,
1361            )
1362            .map(|(_, anchors)| anchors.start)
1363            .min_by(|a, b| a.cmp(b, new_snapshot))
1364            .unwrap_or(changed_range.start);
1365
1366        let extended_end = *extended_end_candidate
1367            .unwrap_or(text::Anchor::max_for_buffer(new_snapshot.remote_id()))
1368            .max(&changed_range.end, new_snapshot);
1369        let extended_end = new_snapshot
1370            .anchored_edits_since_in_range::<usize>(
1371                &old_snapshot.version(),
1372                changed_range.end..extended_end,
1373            )
1374            .map(|(_, anchors)| anchors.end)
1375            .max_by(|a, b| a.cmp(b, new_snapshot))
1376            .unwrap_or(changed_range.end);
1377
1378        Some(extended_start..extended_end)
1379    } else {
1380        None
1381    };
1382
1383    DiffChanged {
1384        changed_range,
1385        base_text_changed_range,
1386        extended_range,
1387    }
1388}
1389
1390fn process_patch_hunk(
1391    patch: &GitPatch<'_>,
1392    hunk_index: usize,
1393    diff_base: &Rope,
1394    buffer: &text::BufferSnapshot,
1395    buffer_row_divergence: &mut i64,
1396    diff_options: Option<&DiffOptions>,
1397) -> InternalDiffHunk {
1398    let line_item_count = patch.num_lines_in_hunk(hunk_index).unwrap();
1399    assert!(line_item_count > 0);
1400
1401    let mut first_deletion_buffer_row: Option<u32> = None;
1402    let mut buffer_row_range: Option<Range<u32>> = None;
1403    let mut diff_base_byte_range: Option<Range<usize>> = None;
1404    let mut first_addition_old_row: Option<u32> = None;
1405
1406    for line_index in 0..line_item_count {
1407        let line = patch.line_in_hunk(hunk_index, line_index).unwrap();
1408        let kind = line.origin_value();
1409        let content_offset = line.content_offset() as isize;
1410        let content_len = line.content().len() as isize;
1411        match kind {
1412            GitDiffLineType::Addition => {
1413                if first_addition_old_row.is_none() {
1414                    first_addition_old_row = Some(
1415                        (line.new_lineno().unwrap() as i64 - *buffer_row_divergence - 1) as u32,
1416                    );
1417                }
1418                *buffer_row_divergence += 1;
1419                let row = line.new_lineno().unwrap().saturating_sub(1);
1420
1421                match &mut buffer_row_range {
1422                    Some(Range { end, .. }) => *end = row + 1,
1423                    None => buffer_row_range = Some(row..row + 1),
1424                }
1425            }
1426            GitDiffLineType::Deletion => {
1427                let end = content_offset + content_len;
1428
1429                match &mut diff_base_byte_range {
1430                    Some(head_byte_range) => head_byte_range.end = end as usize,
1431                    None => diff_base_byte_range = Some(content_offset as usize..end as usize),
1432                }
1433
1434                if first_deletion_buffer_row.is_none() {
1435                    let old_row = line.old_lineno().unwrap().saturating_sub(1);
1436                    let row = old_row as i64 + *buffer_row_divergence;
1437                    first_deletion_buffer_row = Some(row as u32);
1438                }
1439
1440                *buffer_row_divergence -= 1;
1441            }
1442            _ => {}
1443        }
1444    }
1445
1446    let buffer_row_range = buffer_row_range.unwrap_or_else(|| {
1447        // Pure deletion hunk without addition.
1448        let row = first_deletion_buffer_row.unwrap();
1449        row..row
1450    });
1451    let diff_base_byte_range = diff_base_byte_range.unwrap_or_else(|| {
1452        // Pure addition hunk without deletion.
1453        let row = first_addition_old_row.unwrap();
1454        let offset = diff_base.point_to_offset(Point::new(row, 0));
1455        offset..offset
1456    });
1457
1458    let start = Point::new(buffer_row_range.start, 0);
1459    let end = Point::new(buffer_row_range.end, 0);
1460    let buffer_range = buffer.anchor_before(start)..buffer.anchor_before(end);
1461
1462    let (base_word_diffs, buffer_word_diffs) = if let Some(options) = diff_options {
1463        word_diffs(
1464            diff_base,
1465            diff_base_byte_range.clone(),
1466            buffer,
1467            buffer_range.clone(),
1468            options,
1469        )
1470    } else {
1471        (Vec::new(), Vec::new())
1472    };
1473
1474    InternalDiffHunk {
1475        buffer_range,
1476        diff_base_byte_range,
1477        base_word_diffs,
1478        buffer_word_diffs,
1479    }
1480}
1481
1482fn word_diffs(
1483    diff_base: &Rope,
1484    diff_base_byte_range: Range<usize>,
1485    buffer: &text::BufferSnapshot,
1486    buffer_range: Range<Anchor>,
1487    options: &DiffOptions,
1488) -> (Vec<Range<usize>>, Vec<Range<Anchor>>) {
1489    let buffer_rows = buffer.text_summary_for_range::<Point, _>(buffer_range.clone());
1490    let base_text_rows = diff_base
1491        .cursor(diff_base_byte_range.start)
1492        .summary::<Point>(diff_base_byte_range.end);
1493    if buffer_rows == Point::zero()
1494        || base_text_rows.row != buffer_rows.row
1495        || base_text_rows.row > options.max_word_diff_line_count as u32
1496    {
1497        return (Vec::default(), Vec::default());
1498    }
1499
1500    let base_text: String = diff_base.chunks_in_range(diff_base_byte_range).collect();
1501
1502    let buffer_text: String = buffer.text_for_range(buffer_range.clone()).collect();
1503
1504    let (base_word_diffs, buffer_word_diffs_relative) =
1505        word_diff_ranges(&base_text, &buffer_text, options.clone());
1506
1507    let buffer_start_offset = buffer_range.start.to_offset(buffer);
1508    let buffer_word_diffs = buffer_word_diffs_relative
1509        .into_iter()
1510        .map(|range| {
1511            let start = buffer.anchor_after(buffer_start_offset + range.start);
1512            let end = buffer.anchor_after(buffer_start_offset + range.end);
1513            start..end
1514        })
1515        .collect();
1516
1517    (base_word_diffs, buffer_word_diffs)
1518}
1519
1520impl std::fmt::Debug for BufferDiff {
1521    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1522        f.debug_struct("BufferChangeSet")
1523            .field("buffer_id", &self.buffer_id)
1524            .finish()
1525    }
1526}
1527
1528#[derive(Clone, Debug, Default)]
1529pub struct DiffChanged {
1530    pub changed_range: Option<Range<text::Anchor>>,
1531    pub base_text_changed_range: Option<Range<usize>>,
1532    pub extended_range: Option<Range<text::Anchor>>,
1533}
1534
1535#[derive(Clone, Debug)]
1536pub enum BufferDiffEvent {
1537    DiffChanged(DiffChanged),
1538    LanguageChanged,
1539    HunksStagedOrUnstaged(Option<Rope>),
1540}
1541
1542impl EventEmitter<BufferDiffEvent> for BufferDiff {}
1543
1544impl BufferDiff {
1545    pub fn new(buffer: &text::BufferSnapshot, cx: &mut App) -> Self {
1546        let base_text = cx.new(|cx| {
1547            let mut buffer = language::Buffer::local("", cx);
1548            buffer.set_capability(Capability::ReadOnly, cx);
1549            buffer
1550        });
1551
1552        BufferDiff {
1553            buffer_id: buffer.remote_id(),
1554            inner: BufferDiffInner {
1555                base_text,
1556                hunks: SumTree::new(buffer),
1557                pending_hunks: SumTree::new(buffer),
1558                base_text_exists: false,
1559                buffer_snapshot: buffer.clone(),
1560            },
1561            secondary_diff: None,
1562        }
1563    }
1564
1565    pub fn new_unchanged(buffer: &text::BufferSnapshot, cx: &mut Context<Self>) -> Self {
1566        let base_text = buffer.text();
1567        let base_text = cx.new(|cx| {
1568            let mut buffer = language::Buffer::local(base_text, cx);
1569            buffer.set_capability(Capability::ReadOnly, cx);
1570            buffer
1571        });
1572
1573        BufferDiff {
1574            buffer_id: buffer.remote_id(),
1575            inner: BufferDiffInner {
1576                base_text,
1577                hunks: SumTree::new(buffer),
1578                pending_hunks: SumTree::new(buffer),
1579                base_text_exists: true,
1580                buffer_snapshot: buffer.clone(),
1581            },
1582            secondary_diff: None,
1583        }
1584    }
1585
1586    #[cfg(any(test, feature = "test-support"))]
1587    pub fn new_with_base_text(
1588        base_text: &str,
1589        buffer: &text::BufferSnapshot,
1590        cx: &mut Context<Self>,
1591    ) -> Self {
1592        let mut this = BufferDiff::new(&buffer, cx);
1593        let mut base_text = base_text.to_owned();
1594        text::LineEnding::normalize(&mut base_text);
1595        let inner = cx.foreground_executor().block_on(this.update_diff(
1596            buffer.clone(),
1597            Some(Arc::from(base_text)),
1598            Some(false),
1599            None,
1600            cx,
1601        ));
1602        this.set_snapshot(inner, &buffer, cx).detach();
1603        this
1604    }
1605
1606    pub fn set_secondary_diff(&mut self, diff: Entity<BufferDiff>) {
1607        self.secondary_diff = Some(diff);
1608    }
1609
1610    pub fn secondary_diff(&self) -> Option<Entity<BufferDiff>> {
1611        self.secondary_diff.clone()
1612    }
1613
1614    pub fn clear_pending_hunks(&mut self, cx: &mut Context<Self>) {
1615        if self.secondary_diff.is_some() {
1616            self.inner.pending_hunks = SumTree::from_summary(DiffHunkSummary {
1617                buffer_range: Anchor::min_min_range_for_buffer(self.buffer_id),
1618                diff_base_byte_range: 0..0,
1619            });
1620            let changed_range = Some(Anchor::min_max_range_for_buffer(self.buffer_id));
1621            let base_text_range = Some(0..self.base_text(cx).len());
1622            cx.emit(BufferDiffEvent::DiffChanged(DiffChanged {
1623                changed_range: changed_range.clone(),
1624                base_text_changed_range: base_text_range,
1625                extended_range: changed_range,
1626            }));
1627        }
1628    }
1629
1630    pub fn stage_or_unstage_hunks(
1631        &mut self,
1632        stage: bool,
1633        hunks: &[DiffHunk],
1634        buffer: &text::BufferSnapshot,
1635        file_exists: bool,
1636        cx: &mut Context<Self>,
1637    ) -> Option<Rope> {
1638        let new_index_text = self
1639            .secondary_diff
1640            .as_ref()?
1641            .update(cx, |secondary_diff, cx| {
1642                self.inner.stage_or_unstage_hunks_impl(
1643                    &secondary_diff.inner,
1644                    stage,
1645                    hunks,
1646                    buffer,
1647                    file_exists,
1648                    cx,
1649                )
1650            });
1651
1652        cx.emit(BufferDiffEvent::HunksStagedOrUnstaged(
1653            new_index_text.clone(),
1654        ));
1655        if let Some((first, last)) = hunks.first().zip(hunks.last()) {
1656            let changed_range = Some(first.buffer_range.start..last.buffer_range.end);
1657            let base_text_changed_range =
1658                Some(first.diff_base_byte_range.start..last.diff_base_byte_range.end);
1659            cx.emit(BufferDiffEvent::DiffChanged(DiffChanged {
1660                changed_range: changed_range.clone(),
1661                base_text_changed_range,
1662                extended_range: changed_range,
1663            }));
1664        }
1665        new_index_text
1666    }
1667
1668    pub fn stage_or_unstage_all_hunks(
1669        &mut self,
1670        stage: bool,
1671        buffer: &text::BufferSnapshot,
1672        file_exists: bool,
1673        cx: &mut Context<Self>,
1674    ) {
1675        let hunks = self
1676            .snapshot(cx)
1677            .hunks_intersecting_range(Anchor::MIN..Anchor::MAX, buffer)
1678            .collect::<Vec<_>>();
1679        let Some(secondary) = self.secondary_diff.clone() else {
1680            return;
1681        };
1682        let secondary = secondary.read(cx).inner.clone();
1683        self.inner
1684            .stage_or_unstage_hunks_impl(&secondary, stage, &hunks, buffer, file_exists, cx);
1685        if let Some((first, last)) = hunks.first().zip(hunks.last()) {
1686            let changed_range = Some(first.buffer_range.start..last.buffer_range.end);
1687            let base_text_changed_range =
1688                Some(first.diff_base_byte_range.start..last.diff_base_byte_range.end);
1689            cx.emit(BufferDiffEvent::DiffChanged(DiffChanged {
1690                changed_range: changed_range.clone(),
1691                base_text_changed_range,
1692                extended_range: changed_range,
1693            }));
1694        }
1695    }
1696
1697    pub fn update_diff(
1698        &self,
1699        buffer: text::BufferSnapshot,
1700        base_text: Option<Arc<str>>,
1701        base_text_change: Option<bool>,
1702        language: Option<Arc<Language>>,
1703        cx: &App,
1704    ) -> Task<BufferDiffUpdate> {
1705        let prev_base_text = self.base_text(cx).as_rope().clone();
1706        let base_text_changed = base_text_change.is_some();
1707        let compute_base_text_edits = base_text_change == Some(true);
1708        let diff_options = build_diff_options(
1709            None,
1710            language.as_ref().map(|l| l.name()),
1711            language.as_ref().map(|l| l.default_scope()),
1712            cx,
1713        );
1714        let buffer_snapshot = buffer.clone();
1715
1716        let base_text_diff_task = if base_text_changed && compute_base_text_edits {
1717            base_text
1718                .as_ref()
1719                .map(|new_text| self.inner.base_text.read(cx).diff(new_text.clone(), cx))
1720        } else {
1721            None
1722        };
1723
1724        let hunk_task = cx.background_executor().spawn({
1725            let buffer_snapshot = buffer_snapshot.clone();
1726            async move {
1727                let base_text_rope = if let Some(base_text) = &base_text {
1728                    if base_text_changed {
1729                        Rope::from(base_text.as_ref())
1730                    } else {
1731                        prev_base_text
1732                    }
1733                } else {
1734                    Rope::new()
1735                };
1736                let base_text_exists = base_text.is_some();
1737                let hunks = compute_hunks(
1738                    base_text
1739                        .clone()
1740                        .map(|base_text| (base_text, base_text_rope.clone())),
1741                    &buffer,
1742                    diff_options,
1743                );
1744                let base_text = base_text.unwrap_or_default();
1745                BufferDiffInner {
1746                    base_text,
1747                    hunks,
1748                    base_text_exists,
1749                    pending_hunks: SumTree::new(&buffer),
1750                    buffer_snapshot,
1751                }
1752            }
1753        });
1754
1755        cx.background_executor().spawn(async move {
1756            let (inner, base_text_edits) = match base_text_diff_task {
1757                Some(diff_task) => {
1758                    let (inner, diff) = futures::join!(hunk_task, diff_task);
1759                    (inner, Some(diff))
1760                }
1761                None => (hunk_task.await, None),
1762            };
1763
1764            BufferDiffUpdate {
1765                inner,
1766                buffer_snapshot,
1767                base_text_edits,
1768                base_text_changed,
1769            }
1770        })
1771    }
1772
1773    #[ztracing::instrument(skip_all)]
1774    pub fn language_changed(
1775        &mut self,
1776        language: Option<Arc<Language>>,
1777        language_registry: Option<Arc<LanguageRegistry>>,
1778        cx: &mut Context<Self>,
1779    ) {
1780        let fut = self.inner.base_text.update(cx, |base_text, cx| {
1781            if let Some(language_registry) = language_registry {
1782                base_text.set_language_registry(language_registry);
1783            }
1784            base_text.set_language_async(language, cx);
1785            base_text.parsing_idle()
1786        });
1787        cx.spawn(async move |this, cx| {
1788            fut.await;
1789            this.update(cx, |_, cx| {
1790                cx.emit(BufferDiffEvent::LanguageChanged);
1791            })
1792            .ok();
1793        })
1794        .detach();
1795    }
1796
1797    fn set_snapshot_with_secondary_inner(
1798        &mut self,
1799        update: BufferDiffUpdate,
1800        buffer: &text::BufferSnapshot,
1801        secondary_diff_change: Option<Range<Anchor>>,
1802        clear_pending_hunks: bool,
1803        cx: &mut Context<Self>,
1804    ) -> impl Future<Output = DiffChanged> + use<> {
1805        log::debug!("set snapshot with secondary {secondary_diff_change:?}");
1806
1807        let old_snapshot = self.snapshot(cx);
1808        let new_state = update.inner;
1809        let base_text_changed = update.base_text_changed;
1810
1811        let state = &mut self.inner;
1812        state.base_text_exists = new_state.base_text_exists;
1813        let should_compare_hunks = update.base_text_edits.is_some() || !base_text_changed;
1814        let parsing_idle = if let Some(diff) = update.base_text_edits {
1815            state.base_text.update(cx, |base_text, cx| {
1816                base_text.set_sync_parse_timeout(None);
1817                base_text.set_capability(Capability::ReadWrite, cx);
1818                base_text.apply_diff(diff, cx);
1819                base_text.set_capability(Capability::ReadOnly, cx);
1820                Some(base_text.parsing_idle())
1821            })
1822        } else if update.base_text_changed {
1823            state.base_text.update(cx, |base_text, cx| {
1824                base_text.set_sync_parse_timeout(None);
1825                base_text.set_capability(Capability::ReadWrite, cx);
1826                base_text.set_text(new_state.base_text.clone(), cx);
1827                base_text.set_capability(Capability::ReadOnly, cx);
1828                Some(base_text.parsing_idle())
1829            })
1830        } else {
1831            None
1832        };
1833
1834        let old_buffer_snapshot = &old_snapshot.inner.buffer_snapshot;
1835        let old_base_snapshot = &old_snapshot.inner.base_text;
1836        let new_base_snapshot = state.base_text.read(cx).snapshot();
1837        let DiffChanged {
1838            mut changed_range,
1839            mut base_text_changed_range,
1840            mut extended_range,
1841        } = match (state.base_text_exists, new_state.base_text_exists) {
1842            (false, false) => DiffChanged::default(),
1843            (true, true) if should_compare_hunks => compare_hunks(
1844                &new_state.hunks,
1845                &old_snapshot.inner.hunks,
1846                old_buffer_snapshot,
1847                buffer,
1848                old_base_snapshot,
1849                &new_base_snapshot,
1850            ),
1851            _ => {
1852                let full_range = text::Anchor::min_max_range_for_buffer(self.buffer_id);
1853                let full_base_range = 0..new_state.base_text.len();
1854                DiffChanged {
1855                    changed_range: Some(full_range.clone()),
1856                    base_text_changed_range: Some(full_base_range),
1857                    extended_range: Some(full_range),
1858                }
1859            }
1860        };
1861        state.hunks = new_state.hunks;
1862        state.buffer_snapshot = update.buffer_snapshot;
1863
1864        if base_text_changed || clear_pending_hunks {
1865            if let Some((first, last)) = state.pending_hunks.first().zip(state.pending_hunks.last())
1866            {
1867                let pending_range = first.buffer_range.start..last.buffer_range.end;
1868                if let Some(range) = &mut changed_range {
1869                    range.start = *range.start.min(&pending_range.start, buffer);
1870                    range.end = *range.end.max(&pending_range.end, buffer);
1871                } else {
1872                    changed_range = Some(pending_range.clone());
1873                }
1874
1875                if let Some(base_text_range) = base_text_changed_range.as_mut() {
1876                    base_text_range.start =
1877                        base_text_range.start.min(first.diff_base_byte_range.start);
1878                    base_text_range.end = base_text_range.end.max(last.diff_base_byte_range.end);
1879                } else {
1880                    base_text_changed_range =
1881                        Some(first.diff_base_byte_range.start..last.diff_base_byte_range.end);
1882                }
1883
1884                if let Some(ext) = &mut extended_range {
1885                    ext.start = *ext.start.min(&pending_range.start, buffer);
1886                    ext.end = *ext.end.max(&pending_range.end, buffer);
1887                } else {
1888                    extended_range = Some(pending_range);
1889                }
1890            }
1891            state.pending_hunks = SumTree::new(buffer);
1892        }
1893
1894        if let Some(secondary_changed_range) = secondary_diff_change
1895            && let (Some(secondary_hunk_range), Some(secondary_base_range)) =
1896                old_snapshot.range_to_hunk_range(secondary_changed_range, buffer)
1897        {
1898            if let Some(range) = &mut changed_range {
1899                range.start = *secondary_hunk_range.start.min(&range.start, buffer);
1900                range.end = *secondary_hunk_range.end.max(&range.end, buffer);
1901            } else {
1902                changed_range = Some(secondary_hunk_range.clone());
1903            }
1904
1905            if let Some(base_text_range) = base_text_changed_range.as_mut() {
1906                base_text_range.start = secondary_base_range.start.min(base_text_range.start);
1907                base_text_range.end = secondary_base_range.end.max(base_text_range.end);
1908            } else {
1909                base_text_changed_range = Some(secondary_base_range);
1910            }
1911
1912            if let Some(ext) = &mut extended_range {
1913                ext.start = *ext.start.min(&secondary_hunk_range.start, buffer);
1914                ext.end = *ext.end.max(&secondary_hunk_range.end, buffer);
1915            } else {
1916                extended_range = Some(secondary_hunk_range);
1917            }
1918        }
1919
1920        async move {
1921            if let Some(parsing_idle) = parsing_idle {
1922                parsing_idle.await;
1923            }
1924            DiffChanged {
1925                changed_range,
1926                base_text_changed_range,
1927                extended_range,
1928            }
1929        }
1930    }
1931
1932    pub fn set_snapshot(
1933        &mut self,
1934        new_state: BufferDiffUpdate,
1935        buffer: &text::BufferSnapshot,
1936        cx: &mut Context<Self>,
1937    ) -> Task<Option<Range<Anchor>>> {
1938        self.set_snapshot_with_secondary(new_state, buffer, None, false, cx)
1939    }
1940
1941    pub fn set_snapshot_with_secondary(
1942        &mut self,
1943        update: BufferDiffUpdate,
1944        buffer: &text::BufferSnapshot,
1945        secondary_diff_change: Option<Range<Anchor>>,
1946        clear_pending_hunks: bool,
1947        cx: &mut Context<Self>,
1948    ) -> Task<Option<Range<Anchor>>> {
1949        let fut = self.set_snapshot_with_secondary_inner(
1950            update,
1951            buffer,
1952            secondary_diff_change,
1953            clear_pending_hunks,
1954            cx,
1955        );
1956
1957        cx.spawn(async move |this, cx| {
1958            let change = fut.await;
1959            this.update(cx, |_, cx| {
1960                cx.emit(BufferDiffEvent::DiffChanged(change.clone()));
1961            })
1962            .ok();
1963            change.changed_range
1964        })
1965    }
1966
1967    pub fn base_text(&self, cx: &App) -> language::BufferSnapshot {
1968        self.inner.base_text.read(cx).snapshot()
1969    }
1970
1971    pub fn base_text_exists(&self) -> bool {
1972        self.inner.base_text_exists
1973    }
1974
1975    pub fn snapshot(&self, cx: &App) -> BufferDiffSnapshot {
1976        BufferDiffSnapshot {
1977            inner: BufferDiffInner {
1978                hunks: self.inner.hunks.clone(),
1979                pending_hunks: self.inner.pending_hunks.clone(),
1980                base_text: self.inner.base_text.read(cx).snapshot(),
1981                base_text_exists: self.inner.base_text_exists,
1982                buffer_snapshot: self.inner.buffer_snapshot.clone(),
1983            },
1984            secondary_diff: self.secondary_diff.as_ref().map(|diff| {
1985                debug_assert!(diff.read(cx).secondary_diff.is_none());
1986                Arc::new(diff.read(cx).snapshot(cx))
1987            }),
1988        }
1989    }
1990
1991    /// Used in cases where the change set isn't derived from git.
1992    pub fn set_base_text(
1993        &mut self,
1994        base_text: Option<Arc<str>>,
1995        language: Option<Arc<Language>>,
1996        buffer: text::BufferSnapshot,
1997        cx: &mut Context<Self>,
1998    ) -> oneshot::Receiver<()> {
1999        let (tx, rx) = oneshot::channel();
2000        let complete_on_drop = util::defer(|| {
2001            tx.send(()).ok();
2002        });
2003        cx.spawn(async move |this, cx| {
2004            let Some(state) = this
2005                .update(cx, |this, cx| {
2006                    this.update_diff(buffer.clone(), base_text, Some(false), language, cx)
2007                })
2008                .log_err()
2009            else {
2010                return;
2011            };
2012            let state = state.await;
2013            if let Some(task) = this
2014                .update(cx, |this, cx| this.set_snapshot(state, &buffer, cx))
2015                .log_err()
2016            {
2017                task.await;
2018            }
2019            drop(complete_on_drop)
2020        })
2021        .detach();
2022        rx
2023    }
2024
2025    pub fn base_text_string(&self, cx: &App) -> Option<String> {
2026        self.inner
2027            .base_text_exists
2028            .then(|| self.inner.base_text.read(cx).text())
2029    }
2030
2031    #[cfg(any(test, feature = "test-support"))]
2032    pub fn recalculate_diff_sync(&mut self, buffer: &text::BufferSnapshot, cx: &mut Context<Self>) {
2033        let language = self.base_text(cx).language().cloned();
2034        let base_text = self.base_text_string(cx).map(|s| s.as_str().into());
2035        let fut = self.update_diff(buffer.clone(), base_text, None, language, cx);
2036        let fg_executor = cx.foreground_executor().clone();
2037        let snapshot = fg_executor.block_on(fut);
2038        let fut = self.set_snapshot_with_secondary_inner(snapshot, buffer, None, false, cx);
2039        let change = fg_executor.block_on(fut);
2040        cx.emit(BufferDiffEvent::DiffChanged(change));
2041    }
2042
2043    pub fn base_text_buffer(&self) -> &Entity<language::Buffer> {
2044        &self.inner.base_text
2045    }
2046}
2047
2048impl DiffHunk {
2049    pub fn is_created_file(&self) -> bool {
2050        self.diff_base_byte_range == (0..0)
2051            && self.buffer_range.start.is_min()
2052            && self.buffer_range.end.is_max()
2053    }
2054
2055    pub fn status(&self) -> DiffHunkStatus {
2056        let kind = if self.buffer_range.start == self.buffer_range.end {
2057            DiffHunkStatusKind::Deleted
2058        } else if self.diff_base_byte_range.is_empty() {
2059            DiffHunkStatusKind::Added
2060        } else {
2061            DiffHunkStatusKind::Modified
2062        };
2063        DiffHunkStatus {
2064            kind,
2065            secondary: self.secondary_status,
2066        }
2067    }
2068}
2069
2070impl DiffHunkStatus {
2071    pub fn has_secondary_hunk(&self) -> bool {
2072        matches!(
2073            self.secondary,
2074            DiffHunkSecondaryStatus::HasSecondaryHunk
2075                | DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
2076                | DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk
2077        )
2078    }
2079
2080    pub fn is_pending(&self) -> bool {
2081        matches!(
2082            self.secondary,
2083            DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
2084                | DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
2085        )
2086    }
2087
2088    pub fn is_deleted(&self) -> bool {
2089        self.kind == DiffHunkStatusKind::Deleted
2090    }
2091
2092    pub fn is_added(&self) -> bool {
2093        self.kind == DiffHunkStatusKind::Added
2094    }
2095
2096    pub fn is_modified(&self) -> bool {
2097        self.kind == DiffHunkStatusKind::Modified
2098    }
2099
2100    pub fn added(secondary: DiffHunkSecondaryStatus) -> Self {
2101        Self {
2102            kind: DiffHunkStatusKind::Added,
2103            secondary,
2104        }
2105    }
2106
2107    pub fn modified(secondary: DiffHunkSecondaryStatus) -> Self {
2108        Self {
2109            kind: DiffHunkStatusKind::Modified,
2110            secondary,
2111        }
2112    }
2113
2114    pub fn deleted(secondary: DiffHunkSecondaryStatus) -> Self {
2115        Self {
2116            kind: DiffHunkStatusKind::Deleted,
2117            secondary,
2118        }
2119    }
2120
2121    pub fn deleted_none() -> Self {
2122        Self {
2123            kind: DiffHunkStatusKind::Deleted,
2124            secondary: DiffHunkSecondaryStatus::NoSecondaryHunk,
2125        }
2126    }
2127
2128    pub fn added_none() -> Self {
2129        Self {
2130            kind: DiffHunkStatusKind::Added,
2131            secondary: DiffHunkSecondaryStatus::NoSecondaryHunk,
2132        }
2133    }
2134
2135    pub fn modified_none() -> Self {
2136        Self {
2137            kind: DiffHunkStatusKind::Modified,
2138            secondary: DiffHunkSecondaryStatus::NoSecondaryHunk,
2139        }
2140    }
2141}
2142
2143#[cfg(any(test, feature = "test-support"))]
2144#[track_caller]
2145pub fn assert_hunks<ExpectedText, HunkIter>(
2146    diff_hunks: HunkIter,
2147    buffer: &text::BufferSnapshot,
2148    diff_base: &str,
2149    // Line range, deleted, added, status
2150    expected_hunks: &[(Range<u32>, ExpectedText, ExpectedText, DiffHunkStatus)],
2151) where
2152    HunkIter: Iterator<Item = DiffHunk>,
2153    ExpectedText: AsRef<str>,
2154{
2155    let actual_hunks = diff_hunks
2156        .map(|hunk| {
2157            (
2158                hunk.range.clone(),
2159                &diff_base[hunk.diff_base_byte_range.clone()],
2160                buffer
2161                    .text_for_range(hunk.range.clone())
2162                    .collect::<String>(),
2163                hunk.status(),
2164            )
2165        })
2166        .collect::<Vec<_>>();
2167
2168    let expected_hunks: Vec<_> = expected_hunks
2169        .iter()
2170        .map(|(line_range, deleted_text, added_text, status)| {
2171            (
2172                Point::new(line_range.start, 0)..Point::new(line_range.end, 0),
2173                deleted_text.as_ref(),
2174                added_text.as_ref().to_string(),
2175                *status,
2176            )
2177        })
2178        .collect();
2179
2180    pretty_assertions::assert_eq!(actual_hunks, expected_hunks);
2181}
2182
2183#[cfg(test)]
2184mod tests {
2185    use std::{fmt::Write as _, sync::mpsc};
2186
2187    use super::*;
2188    use gpui::TestAppContext;
2189    use pretty_assertions::{assert_eq, assert_ne};
2190    use rand::{Rng as _, rngs::StdRng};
2191    use text::{Buffer, BufferId, ReplicaId, Rope};
2192    use unindent::Unindent as _;
2193    use util::test::marked_text_ranges;
2194
2195    #[ctor::ctor]
2196    fn init_logger() {
2197        zlog::init_test();
2198    }
2199
2200    #[gpui::test]
2201    async fn test_buffer_diff_simple(cx: &mut gpui::TestAppContext) {
2202        let diff_base = "
2203            one
2204            two
2205            three
2206        "
2207        .unindent();
2208
2209        let buffer_text = "
2210            one
2211            HELLO
2212            three
2213        "
2214        .unindent();
2215
2216        let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
2217        let mut diff = BufferDiffSnapshot::new_sync(&buffer, diff_base.clone(), cx);
2218        assert_hunks(
2219            diff.hunks_intersecting_range(
2220                Anchor::min_max_range_for_buffer(buffer.remote_id()),
2221                &buffer,
2222            ),
2223            &buffer,
2224            &diff_base,
2225            &[(1..2, "two\n", "HELLO\n", DiffHunkStatus::modified_none())],
2226        );
2227
2228        buffer.edit([(0..0, "point five\n")]);
2229        diff = BufferDiffSnapshot::new_sync(&buffer, diff_base.clone(), cx);
2230        assert_hunks(
2231            diff.hunks_intersecting_range(
2232                Anchor::min_max_range_for_buffer(buffer.remote_id()),
2233                &buffer,
2234            ),
2235            &buffer,
2236            &diff_base,
2237            &[
2238                (0..1, "", "point five\n", DiffHunkStatus::added_none()),
2239                (2..3, "two\n", "HELLO\n", DiffHunkStatus::modified_none()),
2240            ],
2241        );
2242
2243        diff = cx.update(|cx| BufferDiff::new(&buffer, cx).snapshot(cx));
2244        assert_hunks::<&str, _>(
2245            diff.hunks_intersecting_range(
2246                Anchor::min_max_range_for_buffer(buffer.remote_id()),
2247                &buffer,
2248            ),
2249            &buffer,
2250            &diff_base,
2251            &[],
2252        );
2253    }
2254
2255    #[gpui::test]
2256    async fn test_buffer_diff_with_secondary(cx: &mut gpui::TestAppContext) {
2257        let head_text = "
2258            zero
2259            one
2260            two
2261            three
2262            four
2263            five
2264            six
2265            seven
2266            eight
2267            nine
2268        "
2269        .unindent();
2270
2271        let index_text = "
2272            zero
2273            one
2274            TWO
2275            three
2276            FOUR
2277            five
2278            six
2279            seven
2280            eight
2281            NINE
2282        "
2283        .unindent();
2284
2285        let buffer_text = "
2286            zero
2287            one
2288            TWO
2289            three
2290            FOUR
2291            FIVE
2292            six
2293            SEVEN
2294            eight
2295            nine
2296        "
2297        .unindent();
2298
2299        let buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
2300        let unstaged_diff = BufferDiffSnapshot::new_sync(&buffer, index_text, cx);
2301        let mut uncommitted_diff = BufferDiffSnapshot::new_sync(&buffer, head_text.clone(), cx);
2302        uncommitted_diff.secondary_diff = Some(Arc::new(unstaged_diff));
2303
2304        let expected_hunks = vec![
2305            (2..3, "two\n", "TWO\n", DiffHunkStatus::modified_none()),
2306            (
2307                4..6,
2308                "four\nfive\n",
2309                "FOUR\nFIVE\n",
2310                DiffHunkStatus::modified(DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk),
2311            ),
2312            (
2313                7..8,
2314                "seven\n",
2315                "SEVEN\n",
2316                DiffHunkStatus::modified(DiffHunkSecondaryStatus::HasSecondaryHunk),
2317            ),
2318        ];
2319
2320        assert_hunks(
2321            uncommitted_diff.hunks_intersecting_range(
2322                Anchor::min_max_range_for_buffer(buffer.remote_id()),
2323                &buffer,
2324            ),
2325            &buffer,
2326            &head_text,
2327            &expected_hunks,
2328        );
2329    }
2330
2331    #[gpui::test]
2332    async fn test_buffer_diff_range(cx: &mut TestAppContext) {
2333        let diff_base = "
2334            one
2335            two
2336            three
2337            four
2338            five
2339            six
2340            seven
2341            eight
2342            nine
2343            ten
2344        "
2345        .unindent();
2346
2347        let buffer_text = "
2348            A
2349            one
2350            B
2351            two
2352            C
2353            three
2354            HELLO
2355            four
2356            five
2357            SIXTEEN
2358            seven
2359            eight
2360            WORLD
2361            nine
2362
2363            ten
2364
2365        "
2366        .unindent();
2367
2368        let buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
2369        let diff = BufferDiffSnapshot::new_sync(buffer.snapshot(), diff_base.clone(), cx);
2370        assert_eq!(
2371            diff.hunks_intersecting_range(
2372                Anchor::min_max_range_for_buffer(buffer.remote_id()),
2373                &buffer
2374            )
2375            .count(),
2376            8
2377        );
2378
2379        assert_hunks(
2380            diff.hunks_intersecting_range(
2381                buffer.anchor_before(Point::new(7, 0))..buffer.anchor_before(Point::new(12, 0)),
2382                &buffer,
2383            ),
2384            &buffer,
2385            &diff_base,
2386            &[
2387                (6..7, "", "HELLO\n", DiffHunkStatus::added_none()),
2388                (9..10, "six\n", "SIXTEEN\n", DiffHunkStatus::modified_none()),
2389                (12..13, "", "WORLD\n", DiffHunkStatus::added_none()),
2390            ],
2391        );
2392    }
2393
2394    #[gpui::test]
2395    async fn test_stage_hunk(cx: &mut TestAppContext) {
2396        struct Example {
2397            name: &'static str,
2398            head_text: String,
2399            index_text: String,
2400            buffer_marked_text: String,
2401            final_index_text: String,
2402        }
2403
2404        let table = [
2405            Example {
2406                name: "uncommitted hunk straddles end of unstaged hunk",
2407                head_text: "
2408                    one
2409                    two
2410                    three
2411                    four
2412                    five
2413                "
2414                .unindent(),
2415                index_text: "
2416                    one
2417                    TWO_HUNDRED
2418                    three
2419                    FOUR_HUNDRED
2420                    five
2421                "
2422                .unindent(),
2423                buffer_marked_text: "
2424                    ZERO
2425                    one
2426                    two
2427                    «THREE_HUNDRED
2428                    FOUR_HUNDRED»
2429                    five
2430                    SIX
2431                "
2432                .unindent(),
2433                final_index_text: "
2434                    one
2435                    two
2436                    THREE_HUNDRED
2437                    FOUR_HUNDRED
2438                    five
2439                "
2440                .unindent(),
2441            },
2442            Example {
2443                name: "uncommitted hunk straddles start of unstaged hunk",
2444                head_text: "
2445                    one
2446                    two
2447                    three
2448                    four
2449                    five
2450                "
2451                .unindent(),
2452                index_text: "
2453                    one
2454                    TWO_HUNDRED
2455                    three
2456                    FOUR_HUNDRED
2457                    five
2458                "
2459                .unindent(),
2460                buffer_marked_text: "
2461                    ZERO
2462                    one
2463                    «TWO_HUNDRED
2464                    THREE_HUNDRED»
2465                    four
2466                    five
2467                    SIX
2468                "
2469                .unindent(),
2470                final_index_text: "
2471                    one
2472                    TWO_HUNDRED
2473                    THREE_HUNDRED
2474                    four
2475                    five
2476                "
2477                .unindent(),
2478            },
2479            Example {
2480                name: "uncommitted hunk strictly contains unstaged hunks",
2481                head_text: "
2482                    one
2483                    two
2484                    three
2485                    four
2486                    five
2487                    six
2488                    seven
2489                "
2490                .unindent(),
2491                index_text: "
2492                    one
2493                    TWO
2494                    THREE
2495                    FOUR
2496                    FIVE
2497                    SIX
2498                    seven
2499                "
2500                .unindent(),
2501                buffer_marked_text: "
2502                    one
2503                    TWO
2504                    «THREE_HUNDRED
2505                    FOUR
2506                    FIVE_HUNDRED»
2507                    SIX
2508                    seven
2509                "
2510                .unindent(),
2511                final_index_text: "
2512                    one
2513                    TWO
2514                    THREE_HUNDRED
2515                    FOUR
2516                    FIVE_HUNDRED
2517                    SIX
2518                    seven
2519                "
2520                .unindent(),
2521            },
2522            Example {
2523                name: "uncommitted deletion hunk",
2524                head_text: "
2525                    one
2526                    two
2527                    three
2528                    four
2529                    five
2530                "
2531                .unindent(),
2532                index_text: "
2533                    one
2534                    two
2535                    three
2536                    four
2537                    five
2538                "
2539                .unindent(),
2540                buffer_marked_text: "
2541                    one
2542                    ˇfive
2543                "
2544                .unindent(),
2545                final_index_text: "
2546                    one
2547                    five
2548                "
2549                .unindent(),
2550            },
2551            Example {
2552                name: "one unstaged hunk that contains two uncommitted hunks",
2553                head_text: "
2554                    one
2555                    two
2556
2557                    three
2558                    four
2559                "
2560                .unindent(),
2561                index_text: "
2562                    one
2563                    two
2564                    three
2565                    four
2566                "
2567                .unindent(),
2568                buffer_marked_text: "
2569                    «one
2570
2571                    three // modified
2572                    four»
2573                "
2574                .unindent(),
2575                final_index_text: "
2576                    one
2577
2578                    three // modified
2579                    four
2580                "
2581                .unindent(),
2582            },
2583            Example {
2584                name: "one uncommitted hunk that contains two unstaged hunks",
2585                head_text: "
2586                    one
2587                    two
2588                    three
2589                    four
2590                    five
2591                "
2592                .unindent(),
2593                index_text: "
2594                    ZERO
2595                    one
2596                    TWO
2597                    THREE
2598                    FOUR
2599                    five
2600                "
2601                .unindent(),
2602                buffer_marked_text: "
2603                    «one
2604                    TWO_HUNDRED
2605                    THREE
2606                    FOUR_HUNDRED
2607                    five»
2608                "
2609                .unindent(),
2610                final_index_text: "
2611                    ZERO
2612                    one
2613                    TWO_HUNDRED
2614                    THREE
2615                    FOUR_HUNDRED
2616                    five
2617                "
2618                .unindent(),
2619            },
2620        ];
2621
2622        for example in table {
2623            let (buffer_text, ranges) = marked_text_ranges(&example.buffer_marked_text, false);
2624            let buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
2625            let hunk_range =
2626                buffer.anchor_before(ranges[0].start)..buffer.anchor_before(ranges[0].end);
2627
2628            let unstaged_diff =
2629                cx.new(|cx| BufferDiff::new_with_base_text(&example.index_text, &buffer, cx));
2630
2631            let uncommitted_diff = cx.new(|cx| {
2632                let mut diff = BufferDiff::new_with_base_text(&example.head_text, &buffer, cx);
2633                diff.set_secondary_diff(unstaged_diff);
2634                diff
2635            });
2636
2637            uncommitted_diff.update(cx, |diff, cx| {
2638                let hunks = diff
2639                    .snapshot(cx)
2640                    .hunks_intersecting_range(hunk_range.clone(), &buffer)
2641                    .collect::<Vec<_>>();
2642                for hunk in &hunks {
2643                    assert_ne!(
2644                        hunk.secondary_status,
2645                        DiffHunkSecondaryStatus::NoSecondaryHunk
2646                    )
2647                }
2648
2649                let new_index_text = diff
2650                    .stage_or_unstage_hunks(true, &hunks, &buffer, true, cx)
2651                    .unwrap()
2652                    .to_string();
2653
2654                let hunks = diff
2655                    .snapshot(cx)
2656                    .hunks_intersecting_range(hunk_range.clone(), &buffer)
2657                    .collect::<Vec<_>>();
2658                for hunk in &hunks {
2659                    assert_eq!(
2660                        hunk.secondary_status,
2661                        DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
2662                    )
2663                }
2664
2665                pretty_assertions::assert_eq!(
2666                    new_index_text,
2667                    example.final_index_text,
2668                    "example: {}",
2669                    example.name
2670                );
2671            });
2672        }
2673    }
2674
2675    #[gpui::test]
2676    async fn test_stage_all_with_nested_hunks(cx: &mut TestAppContext) {
2677        // This test reproduces a crash where staging all hunks would cause an underflow
2678        // when there's one large unstaged hunk containing multiple uncommitted hunks.
2679        let head_text = "
2680            aaa
2681            bbb
2682            ccc
2683            ddd
2684            eee
2685            fff
2686            ggg
2687            hhh
2688            iii
2689            jjj
2690            kkk
2691            lll
2692        "
2693        .unindent();
2694
2695        let index_text = "
2696            aaa
2697            bbb
2698            CCC-index
2699            DDD-index
2700            EEE-index
2701            FFF-index
2702            GGG-index
2703            HHH-index
2704            III-index
2705            JJJ-index
2706            kkk
2707            lll
2708        "
2709        .unindent();
2710
2711        let buffer_text = "
2712            aaa
2713            bbb
2714            ccc-modified
2715            ddd
2716            eee-modified
2717            fff
2718            ggg
2719            hhh-modified
2720            iii
2721            jjj
2722            kkk
2723            lll
2724        "
2725        .unindent();
2726
2727        let buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
2728
2729        let unstaged_diff = cx.new(|cx| BufferDiff::new_with_base_text(&index_text, &buffer, cx));
2730        let uncommitted_diff = cx.new(|cx| {
2731            let mut diff = BufferDiff::new_with_base_text(&head_text, &buffer, cx);
2732            diff.set_secondary_diff(unstaged_diff);
2733            diff
2734        });
2735
2736        uncommitted_diff.update(cx, |diff, cx| {
2737            diff.stage_or_unstage_all_hunks(true, &buffer, true, cx);
2738        });
2739    }
2740
2741    #[gpui::test]
2742    async fn test_toggling_stage_and_unstage_same_hunk(cx: &mut TestAppContext) {
2743        let head_text = "
2744            one
2745            two
2746            three
2747        "
2748        .unindent();
2749        let index_text = head_text.clone();
2750        let buffer_text = "
2751            one
2752            three
2753        "
2754        .unindent();
2755
2756        let buffer = Buffer::new(
2757            ReplicaId::LOCAL,
2758            BufferId::new(1).unwrap(),
2759            buffer_text.clone(),
2760        );
2761        let unstaged_diff = cx.new(|cx| BufferDiff::new_with_base_text(&index_text, &buffer, cx));
2762        let uncommitted_diff = cx.new(|cx| {
2763            let mut diff = BufferDiff::new_with_base_text(&head_text, &buffer, cx);
2764            diff.set_secondary_diff(unstaged_diff.clone());
2765            diff
2766        });
2767
2768        uncommitted_diff.update(cx, |diff, cx| {
2769            let hunk = diff.snapshot(cx).hunks(&buffer).next().unwrap();
2770
2771            let new_index_text = diff
2772                .stage_or_unstage_hunks(true, std::slice::from_ref(&hunk), &buffer, true, cx)
2773                .unwrap()
2774                .to_string();
2775            assert_eq!(new_index_text, buffer_text);
2776
2777            let hunk = diff.snapshot(cx).hunks(&buffer).next().unwrap();
2778            assert_eq!(
2779                hunk.secondary_status,
2780                DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
2781            );
2782
2783            let index_text = diff
2784                .stage_or_unstage_hunks(false, &[hunk], &buffer, true, cx)
2785                .unwrap()
2786                .to_string();
2787            assert_eq!(index_text, head_text);
2788
2789            let hunk = diff.snapshot(cx).hunks(&buffer).next().unwrap();
2790            // optimistically unstaged (fine, could also be HasSecondaryHunk)
2791            assert_eq!(
2792                hunk.secondary_status,
2793                DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
2794            );
2795        });
2796    }
2797
2798    #[gpui::test]
2799    async fn test_buffer_diff_compare(cx: &mut TestAppContext) {
2800        let base_text = "
2801            zero
2802            one
2803            two
2804            three
2805            four
2806            five
2807            six
2808            seven
2809            eight
2810            nine
2811        "
2812        .unindent();
2813
2814        let buffer_text_1 = "
2815            one
2816            three
2817            four
2818            five
2819            SIX
2820            seven
2821            eight
2822            NINE
2823        "
2824        .unindent();
2825
2826        let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text_1);
2827
2828        let empty_diff = cx.update(|cx| BufferDiff::new(&buffer, cx).snapshot(cx));
2829        let diff_1 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
2830        let DiffChanged {
2831            changed_range,
2832            base_text_changed_range,
2833            extended_range: _,
2834        } = compare_hunks(
2835            &diff_1.inner.hunks,
2836            &empty_diff.inner.hunks,
2837            &buffer,
2838            &buffer,
2839            &diff_1.base_text(),
2840            &diff_1.base_text(),
2841        );
2842        let range = changed_range.unwrap();
2843        assert_eq!(range.to_point(&buffer), Point::new(0, 0)..Point::new(8, 0));
2844        let base_text_range = base_text_changed_range.unwrap();
2845        assert_eq!(
2846            base_text_range.to_point(diff_1.base_text()),
2847            Point::new(0, 0)..Point::new(10, 0)
2848        );
2849
2850        // Edit does affects the diff because it recalculates word diffs.
2851        buffer.edit_via_marked_text(
2852            &"
2853                one
2854                three
2855                four
2856                five
2857                «SIX.5»
2858                seven
2859                eight
2860                NINE
2861            "
2862            .unindent(),
2863        );
2864        let diff_2 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
2865        let DiffChanged {
2866            changed_range,
2867            base_text_changed_range,
2868            extended_range: _,
2869        } = compare_hunks(
2870            &diff_2.inner.hunks,
2871            &diff_1.inner.hunks,
2872            &buffer,
2873            &buffer,
2874            diff_2.base_text(),
2875            diff_2.base_text(),
2876        );
2877        assert_eq!(
2878            changed_range.unwrap().to_point(&buffer),
2879            Point::new(4, 0)..Point::new(5, 0),
2880        );
2881        assert_eq!(
2882            base_text_changed_range
2883                .unwrap()
2884                .to_point(diff_2.base_text()),
2885            Point::new(6, 0)..Point::new(7, 0),
2886        );
2887
2888        // Edit turns a deletion hunk into a modification.
2889        buffer.edit_via_marked_text(
2890            &"
2891                one
2892                «THREE»
2893                four
2894                five
2895                SIX.5
2896                seven
2897                eight
2898                NINE
2899            "
2900            .unindent(),
2901        );
2902        let diff_3 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
2903        let DiffChanged {
2904            changed_range,
2905            base_text_changed_range,
2906            extended_range: _,
2907        } = compare_hunks(
2908            &diff_3.inner.hunks,
2909            &diff_2.inner.hunks,
2910            &buffer,
2911            &buffer,
2912            diff_3.base_text(),
2913            diff_3.base_text(),
2914        );
2915        let range = changed_range.unwrap();
2916        assert_eq!(range.to_point(&buffer), Point::new(1, 0)..Point::new(2, 0));
2917        let base_text_range = base_text_changed_range.unwrap();
2918        assert_eq!(
2919            base_text_range.to_point(diff_3.base_text()),
2920            Point::new(2, 0)..Point::new(4, 0)
2921        );
2922
2923        // Edit turns a modification hunk into a deletion.
2924        buffer.edit_via_marked_text(
2925            &"
2926                one
2927                THREE
2928                four
2929                five«»
2930                seven
2931                eight
2932                NINE
2933            "
2934            .unindent(),
2935        );
2936        let diff_4 = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
2937        let DiffChanged {
2938            changed_range,
2939            base_text_changed_range,
2940            extended_range: _,
2941        } = compare_hunks(
2942            &diff_4.inner.hunks,
2943            &diff_3.inner.hunks,
2944            &buffer,
2945            &buffer,
2946            diff_4.base_text(),
2947            diff_4.base_text(),
2948        );
2949        let range = changed_range.unwrap();
2950        assert_eq!(range.to_point(&buffer), Point::new(3, 4)..Point::new(4, 0));
2951        let base_text_range = base_text_changed_range.unwrap();
2952        assert_eq!(
2953            base_text_range.to_point(diff_4.base_text()),
2954            Point::new(6, 0)..Point::new(7, 0)
2955        );
2956
2957        // Edit introduces a new insertion hunk.
2958        buffer.edit_via_marked_text(
2959            &"
2960                one
2961                THREE
2962                four«
2963                FOUR.5
2964                »five
2965                seven
2966                eight
2967                NINE
2968            "
2969            .unindent(),
2970        );
2971        let diff_5 = BufferDiffSnapshot::new_sync(buffer.snapshot(), base_text.clone(), cx);
2972        let DiffChanged {
2973            changed_range,
2974            base_text_changed_range,
2975            extended_range: _,
2976        } = compare_hunks(
2977            &diff_5.inner.hunks,
2978            &diff_4.inner.hunks,
2979            &buffer,
2980            &buffer,
2981            diff_5.base_text(),
2982            diff_5.base_text(),
2983        );
2984        let range = changed_range.unwrap();
2985        assert_eq!(range.to_point(&buffer), Point::new(3, 0)..Point::new(4, 0));
2986        let base_text_range = base_text_changed_range.unwrap();
2987        assert_eq!(
2988            base_text_range.to_point(diff_5.base_text()),
2989            Point::new(5, 0)..Point::new(5, 0)
2990        );
2991
2992        // Edit removes a hunk.
2993        buffer.edit_via_marked_text(
2994            &"
2995                one
2996                THREE
2997                four
2998                FOUR.5
2999                five
3000                seven
3001                eight
3002                «nine»
3003            "
3004            .unindent(),
3005        );
3006        let diff_6 = BufferDiffSnapshot::new_sync(buffer.snapshot(), base_text.clone(), cx);
3007        let DiffChanged {
3008            changed_range,
3009            base_text_changed_range,
3010            extended_range: _,
3011        } = compare_hunks(
3012            &diff_6.inner.hunks,
3013            &diff_5.inner.hunks,
3014            &buffer,
3015            &buffer,
3016            diff_6.base_text(),
3017            diff_6.base_text(),
3018        );
3019        let range = changed_range.unwrap();
3020        assert_eq!(range.to_point(&buffer), Point::new(7, 0)..Point::new(8, 0));
3021        let base_text_range = base_text_changed_range.unwrap();
3022        assert_eq!(
3023            base_text_range.to_point(diff_6.base_text()),
3024            Point::new(9, 0)..Point::new(10, 0)
3025        );
3026
3027        buffer.edit_via_marked_text(
3028            &"
3029                one
3030                THREE
3031                four«»
3032                five
3033                seven
3034                eight
3035                «NINE»
3036            "
3037            .unindent(),
3038        );
3039
3040        let diff_7 = BufferDiffSnapshot::new_sync(buffer.snapshot(), base_text.clone(), cx);
3041        let DiffChanged {
3042            changed_range,
3043            base_text_changed_range,
3044            extended_range: _,
3045        } = compare_hunks(
3046            &diff_7.inner.hunks,
3047            &diff_6.inner.hunks,
3048            &buffer,
3049            &buffer,
3050            diff_7.base_text(),
3051            diff_7.base_text(),
3052        );
3053        let range = changed_range.unwrap();
3054        assert_eq!(range.to_point(&buffer), Point::new(2, 4)..Point::new(7, 0));
3055        let base_text_range = base_text_changed_range.unwrap();
3056        assert_eq!(
3057            base_text_range.to_point(diff_7.base_text()),
3058            Point::new(5, 0)..Point::new(10, 0)
3059        );
3060
3061        buffer.edit_via_marked_text(
3062            &"
3063                one
3064                THREE
3065                four
3066                five«»seven
3067                eight
3068                NINE
3069            "
3070            .unindent(),
3071        );
3072
3073        let diff_8 = BufferDiffSnapshot::new_sync(buffer.snapshot(), base_text, cx);
3074        let DiffChanged {
3075            changed_range,
3076            base_text_changed_range,
3077            extended_range: _,
3078        } = compare_hunks(
3079            &diff_8.inner.hunks,
3080            &diff_7.inner.hunks,
3081            &buffer,
3082            &buffer,
3083            diff_8.base_text(),
3084            diff_8.base_text(),
3085        );
3086        let range = changed_range.unwrap();
3087        assert_eq!(range.to_point(&buffer), Point::new(3, 0)..Point::new(3, 4));
3088        let base_text_range = base_text_changed_range.unwrap();
3089        assert_eq!(
3090            base_text_range.to_point(diff_8.base_text()),
3091            Point::new(5, 0)..Point::new(8, 0)
3092        );
3093    }
3094
3095    #[gpui::test(iterations = 100)]
3096    async fn test_staging_and_unstaging_hunks(cx: &mut TestAppContext, mut rng: StdRng) {
3097        fn gen_line(rng: &mut StdRng) -> String {
3098            if rng.random_bool(0.2) {
3099                "\n".to_owned()
3100            } else {
3101                let c = rng.random_range('A'..='Z');
3102                format!("{c}{c}{c}\n")
3103            }
3104        }
3105
3106        fn gen_working_copy(rng: &mut StdRng, head: &str) -> String {
3107            let mut old_lines = {
3108                let mut old_lines = Vec::new();
3109                let old_lines_iter = head.lines();
3110                for line in old_lines_iter {
3111                    assert!(!line.ends_with("\n"));
3112                    old_lines.push(line.to_owned());
3113                }
3114                if old_lines.last().is_some_and(|line| line.is_empty()) {
3115                    old_lines.pop();
3116                }
3117                old_lines.into_iter()
3118            };
3119            let mut result = String::new();
3120            let unchanged_count = rng.random_range(0..=old_lines.len());
3121            result +=
3122                &old_lines
3123                    .by_ref()
3124                    .take(unchanged_count)
3125                    .fold(String::new(), |mut s, line| {
3126                        writeln!(&mut s, "{line}").unwrap();
3127                        s
3128                    });
3129            while old_lines.len() > 0 {
3130                let deleted_count = rng.random_range(0..=old_lines.len());
3131                let _advance = old_lines
3132                    .by_ref()
3133                    .take(deleted_count)
3134                    .map(|line| line.len() + 1)
3135                    .sum::<usize>();
3136                let minimum_added = if deleted_count == 0 { 1 } else { 0 };
3137                let added_count = rng.random_range(minimum_added..=5);
3138                let addition = (0..added_count).map(|_| gen_line(rng)).collect::<String>();
3139                result += &addition;
3140
3141                if old_lines.len() > 0 {
3142                    let blank_lines = old_lines.clone().take_while(|line| line.is_empty()).count();
3143                    if blank_lines == old_lines.len() {
3144                        break;
3145                    };
3146                    let unchanged_count =
3147                        rng.random_range((blank_lines + 1).max(1)..=old_lines.len());
3148                    result += &old_lines.by_ref().take(unchanged_count).fold(
3149                        String::new(),
3150                        |mut s, line| {
3151                            writeln!(&mut s, "{line}").unwrap();
3152                            s
3153                        },
3154                    );
3155                }
3156            }
3157            result
3158        }
3159
3160        fn uncommitted_diff(
3161            working_copy: &language::BufferSnapshot,
3162            index_text: &Rope,
3163            head_text: String,
3164            cx: &mut TestAppContext,
3165        ) -> Entity<BufferDiff> {
3166            let secondary = cx.new(|cx| {
3167                BufferDiff::new_with_base_text(&index_text.to_string(), &working_copy.text, cx)
3168            });
3169            cx.new(|cx| {
3170                let mut diff = BufferDiff::new_with_base_text(&head_text, &working_copy.text, cx);
3171                diff.secondary_diff = Some(secondary);
3172                diff
3173            })
3174        }
3175
3176        let operations = std::env::var("OPERATIONS")
3177            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
3178            .unwrap_or(10);
3179
3180        let rng = &mut rng;
3181        let head_text = ('a'..='z').fold(String::new(), |mut s, c| {
3182            writeln!(&mut s, "{c}{c}{c}").unwrap();
3183            s
3184        });
3185        let working_copy = gen_working_copy(rng, &head_text);
3186        let working_copy = cx.new(|cx| {
3187            language::Buffer::local_normalized(
3188                Rope::from(working_copy.as_str()),
3189                text::LineEnding::default(),
3190                cx,
3191            )
3192        });
3193        let working_copy = working_copy.read_with(cx, |working_copy, _| working_copy.snapshot());
3194        let mut index_text = if rng.random() {
3195            Rope::from(head_text.as_str())
3196        } else {
3197            working_copy.as_rope().clone()
3198        };
3199
3200        let mut diff = uncommitted_diff(&working_copy, &index_text, head_text.clone(), cx);
3201        let mut hunks = diff.update(cx, |diff, cx| {
3202            diff.snapshot(cx)
3203                .hunks_intersecting_range(
3204                    Anchor::min_max_range_for_buffer(diff.buffer_id),
3205                    &working_copy,
3206                )
3207                .collect::<Vec<_>>()
3208        });
3209        if hunks.is_empty() {
3210            return;
3211        }
3212
3213        for _ in 0..operations {
3214            let i = rng.random_range(0..hunks.len());
3215            let hunk = &mut hunks[i];
3216            let hunk_to_change = hunk.clone();
3217            let stage = match hunk.secondary_status {
3218                DiffHunkSecondaryStatus::HasSecondaryHunk => {
3219                    hunk.secondary_status = DiffHunkSecondaryStatus::NoSecondaryHunk;
3220                    true
3221                }
3222                DiffHunkSecondaryStatus::NoSecondaryHunk => {
3223                    hunk.secondary_status = DiffHunkSecondaryStatus::HasSecondaryHunk;
3224                    false
3225                }
3226                _ => unreachable!(),
3227            };
3228
3229            index_text = diff.update(cx, |diff, cx| {
3230                diff.stage_or_unstage_hunks(stage, &[hunk_to_change], &working_copy, true, cx)
3231                    .unwrap()
3232            });
3233
3234            diff = uncommitted_diff(&working_copy, &index_text, head_text.clone(), cx);
3235            let found_hunks = diff.update(cx, |diff, cx| {
3236                diff.snapshot(cx)
3237                    .hunks_intersecting_range(
3238                        Anchor::min_max_range_for_buffer(diff.buffer_id),
3239                        &working_copy,
3240                    )
3241                    .collect::<Vec<_>>()
3242            });
3243            assert_eq!(hunks.len(), found_hunks.len());
3244
3245            for (expected_hunk, found_hunk) in hunks.iter().zip(&found_hunks) {
3246                assert_eq!(
3247                    expected_hunk.buffer_range.to_point(&working_copy),
3248                    found_hunk.buffer_range.to_point(&working_copy)
3249                );
3250                assert_eq!(
3251                    expected_hunk.diff_base_byte_range,
3252                    found_hunk.diff_base_byte_range
3253                );
3254                assert_eq!(expected_hunk.secondary_status, found_hunk.secondary_status);
3255            }
3256            hunks = found_hunks;
3257        }
3258    }
3259
3260    #[gpui::test]
3261    async fn test_changed_ranges(cx: &mut gpui::TestAppContext) {
3262        let base_text = "
3263            one
3264            two
3265            three
3266            four
3267            five
3268            six
3269        "
3270        .unindent();
3271        let buffer_text = "
3272            one
3273            TWO
3274            three
3275            four
3276            FIVE
3277            six
3278        "
3279        .unindent();
3280        let buffer = cx.new(|cx| language::Buffer::local(buffer_text, cx));
3281        let diff = cx.new(|cx| {
3282            BufferDiff::new_with_base_text(&base_text, &buffer.read(cx).text_snapshot(), cx)
3283        });
3284        cx.run_until_parked();
3285        let (tx, rx) = mpsc::channel();
3286        let subscription =
3287            cx.update(|cx| cx.subscribe(&diff, move |_, event, _| tx.send(event.clone()).unwrap()));
3288
3289        let snapshot = buffer.update(cx, |buffer, cx| {
3290            buffer.set_text(
3291                "
3292                ONE
3293                TWO
3294                THREE
3295                FOUR
3296                FIVE
3297                SIX
3298            "
3299                .unindent(),
3300                cx,
3301            );
3302            buffer.text_snapshot()
3303        });
3304        let update = diff
3305            .update(cx, |diff, cx| {
3306                diff.update_diff(
3307                    snapshot.clone(),
3308                    Some(base_text.as_str().into()),
3309                    None,
3310                    None,
3311                    cx,
3312                )
3313            })
3314            .await;
3315        diff.update(cx, |diff, cx| diff.set_snapshot(update, &snapshot, cx))
3316            .await;
3317        cx.run_until_parked();
3318        drop(subscription);
3319        let events = rx.into_iter().collect::<Vec<_>>();
3320        match events.as_slice() {
3321            [
3322                BufferDiffEvent::DiffChanged(DiffChanged {
3323                    changed_range: _,
3324                    base_text_changed_range,
3325                    extended_range: _,
3326                }),
3327            ] => {
3328                // TODO(cole) this seems like it should pass but currently fails (see compare_hunks)
3329                // assert_eq!(
3330                //     *changed_range,
3331                //     Some(Anchor::min_max_range_for_buffer(
3332                //         buffer.read_with(cx, |buffer, _| buffer.remote_id())
3333                //     ))
3334                // );
3335                assert_eq!(*base_text_changed_range, Some(0..base_text.len()));
3336            }
3337            _ => panic!("unexpected events: {:?}", events),
3338        }
3339    }
3340
3341    #[gpui::test]
3342    async fn test_extended_range(cx: &mut TestAppContext) {
3343        let base_text = "
3344            aaa
3345            bbb
3346
3347
3348
3349
3350
3351            ccc
3352            ddd
3353        "
3354        .unindent();
3355
3356        let buffer_text = "
3357            aaa
3358            bbb
3359
3360
3361
3362
3363
3364            CCC
3365            ddd
3366        "
3367        .unindent();
3368
3369        let mut buffer = Buffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), buffer_text);
3370        let old_buffer = buffer.snapshot().clone();
3371        let diff_a = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
3372
3373        buffer.edit([(Point::new(1, 3)..Point::new(1, 3), "\n")]);
3374        let diff_b = BufferDiffSnapshot::new_sync(&buffer, base_text, cx);
3375
3376        let DiffChanged {
3377            changed_range,
3378            base_text_changed_range: _,
3379            extended_range,
3380        } = compare_hunks(
3381            &diff_b.inner.hunks,
3382            &diff_a.inner.hunks,
3383            &old_buffer,
3384            &buffer,
3385            &diff_a.base_text(),
3386            &diff_a.base_text(),
3387        );
3388
3389        let changed_range = changed_range.unwrap();
3390        assert_eq!(
3391            changed_range.to_point(&buffer),
3392            Point::new(7, 0)..Point::new(9, 0),
3393            "changed_range should span from old hunk position to new hunk end"
3394        );
3395
3396        let extended_range = extended_range.unwrap();
3397        assert_eq!(
3398            extended_range.start.to_point(&buffer),
3399            Point::new(1, 3),
3400            "extended_range.start should extend to include the edit outside changed_range"
3401        );
3402        assert_eq!(
3403            extended_range.end.to_point(&buffer),
3404            Point::new(9, 0),
3405            "extended_range.end should collapse to changed_range.end when no edits in end margin"
3406        );
3407
3408        let base_text_2 = "
3409            one
3410            two
3411            three
3412            four
3413            five
3414            six
3415            seven
3416            eight
3417        "
3418        .unindent();
3419
3420        let buffer_text_2 = "
3421            ONE
3422            two
3423            THREE
3424            four
3425            FIVE
3426            six
3427            SEVEN
3428            eight
3429        "
3430        .unindent();
3431
3432        let mut buffer_2 = Buffer::new(ReplicaId::LOCAL, BufferId::new(2).unwrap(), buffer_text_2);
3433        let old_buffer_2 = buffer_2.snapshot().clone();
3434        let diff_2a = BufferDiffSnapshot::new_sync(&buffer_2, base_text_2.clone(), cx);
3435
3436        buffer_2.edit([(Point::new(4, 0)..Point::new(4, 4), "FIVE_CHANGED")]);
3437        let diff_2b = BufferDiffSnapshot::new_sync(&buffer_2, base_text_2, cx);
3438
3439        let DiffChanged {
3440            changed_range,
3441            base_text_changed_range: _,
3442            extended_range,
3443        } = compare_hunks(
3444            &diff_2b.inner.hunks,
3445            &diff_2a.inner.hunks,
3446            &old_buffer_2,
3447            &buffer_2,
3448            &diff_2a.base_text(),
3449            &diff_2a.base_text(),
3450        );
3451
3452        let changed_range = changed_range.unwrap();
3453        assert_eq!(
3454            changed_range.to_point(&buffer_2),
3455            Point::new(4, 0)..Point::new(5, 0),
3456            "changed_range should be just the hunk that changed (FIVE)"
3457        );
3458
3459        let extended_range = extended_range.unwrap();
3460        assert_eq!(
3461            extended_range.to_point(&buffer_2),
3462            Point::new(4, 0)..Point::new(5, 0),
3463            "extended_range should equal changed_range when edit is within the hunk"
3464        );
3465    }
3466
3467    #[gpui::test]
3468    async fn test_buffer_diff_compare_with_base_text_change(_cx: &mut TestAppContext) {
3469        // Use a shared base text buffer so that anchors from old and new snapshots
3470        // share the same remote_id and resolve correctly across versions.
3471        let initial_base = "aaa\nbbb\nccc\nddd\neee\n";
3472        let mut base_text_buffer = Buffer::new(
3473            ReplicaId::LOCAL,
3474            BufferId::new(99).unwrap(),
3475            initial_base.to_string(),
3476        );
3477
3478        // --- Scenario 1: Base text gains a line, producing a new deletion hunk ---
3479        //
3480        // Buffer has a modification (ccc → CCC). When the base text gains
3481        // a new line "XXX" after "aaa", the diff now also contains a
3482        // deletion for that line, and the modification hunk shifts in the
3483        // base text.
3484        let buffer_text_1 = "aaa\nbbb\nCCC\nddd\neee\n";
3485        let buffer = Buffer::new(
3486            ReplicaId::LOCAL,
3487            BufferId::new(1).unwrap(),
3488            buffer_text_1.to_string(),
3489        );
3490
3491        let old_base_snapshot_1 = base_text_buffer.snapshot().clone();
3492        let old_hunks_1 = compute_hunks(
3493            Some((Arc::from(initial_base), Rope::from(initial_base))),
3494            buffer.snapshot(),
3495            None,
3496        );
3497
3498        // Insert "XXX\n" after "aaa\n" in the base text.
3499        base_text_buffer.edit([(4..4, "XXX\n")]);
3500        let new_base_str_1: Arc<str> = Arc::from(base_text_buffer.text().as_str());
3501        let new_base_snapshot_1 = base_text_buffer.snapshot();
3502
3503        let new_hunks_1 = compute_hunks(
3504            Some((new_base_str_1.clone(), Rope::from(new_base_str_1.as_ref()))),
3505            buffer.snapshot(),
3506            None,
3507        );
3508
3509        let DiffChanged {
3510            changed_range,
3511            base_text_changed_range,
3512            extended_range: _,
3513        } = compare_hunks(
3514            &new_hunks_1,
3515            &old_hunks_1,
3516            &buffer.snapshot(),
3517            &buffer.snapshot(),
3518            &old_base_snapshot_1,
3519            &new_base_snapshot_1,
3520        );
3521
3522        // The new deletion hunk (XXX) starts at buffer row 1 and the
3523        // modification hunk (ccc → CCC) now has a different
3524        // diff_base_byte_range, so the changed range spans both.
3525        let range = changed_range.unwrap();
3526        assert_eq!(range.to_point(&buffer), Point::new(1, 0)..Point::new(3, 0),);
3527        let base_range = base_text_changed_range.unwrap();
3528        assert_eq!(
3529            base_range.to_point(&new_base_snapshot_1),
3530            Point::new(1, 0)..Point::new(4, 0),
3531        );
3532
3533        // --- Scenario 2: Base text changes to match the buffer (hunk disappears) ---
3534        //
3535        // Start fresh with a simple base text.
3536        let simple_base = "one\ntwo\nthree\n";
3537        let mut base_buf_2 = Buffer::new(
3538            ReplicaId::LOCAL,
3539            BufferId::new(100).unwrap(),
3540            simple_base.to_string(),
3541        );
3542
3543        let buffer_text_2 = "one\nTWO\nthree\n";
3544        let buffer_2 = Buffer::new(
3545            ReplicaId::LOCAL,
3546            BufferId::new(2).unwrap(),
3547            buffer_text_2.to_string(),
3548        );
3549
3550        let old_base_snapshot_2 = base_buf_2.snapshot().clone();
3551        let old_hunks_2 = compute_hunks(
3552            Some((Arc::from(simple_base), Rope::from(simple_base))),
3553            buffer_2.snapshot(),
3554            None,
3555        );
3556
3557        // The base text is edited so "two" becomes "TWO", now matching the buffer.
3558        base_buf_2.edit([(4..7, "TWO")]);
3559        let new_base_str_2: Arc<str> = Arc::from(base_buf_2.text().as_str());
3560        let new_base_snapshot_2 = base_buf_2.snapshot();
3561
3562        let new_hunks_2 = compute_hunks(
3563            Some((new_base_str_2.clone(), Rope::from(new_base_str_2.as_ref()))),
3564            buffer_2.snapshot(),
3565            None,
3566        );
3567
3568        let DiffChanged {
3569            changed_range,
3570            base_text_changed_range,
3571            extended_range: _,
3572        } = compare_hunks(
3573            &new_hunks_2,
3574            &old_hunks_2,
3575            &buffer_2.snapshot(),
3576            &buffer_2.snapshot(),
3577            &old_base_snapshot_2,
3578            &new_base_snapshot_2,
3579        );
3580
3581        // The old modification hunk (two → TWO) is now gone because the
3582        // base text matches the buffer. The changed range covers where the
3583        // old hunk used to be.
3584        let range = changed_range.unwrap();
3585        assert_eq!(
3586            range.to_point(&buffer_2),
3587            Point::new(1, 0)..Point::new(2, 0),
3588        );
3589        let base_range = base_text_changed_range.unwrap();
3590        // The old hunk's diff_base_byte_range covered "two\n" (bytes 4..8).
3591        // anchor_after(4) is right-biased at the start of the deleted "two",
3592        // so after the edit replacing "two" with "TWO" it resolves past the
3593        // insertion to Point(1, 3).
3594        assert_eq!(
3595            base_range.to_point(&new_base_snapshot_2),
3596            Point::new(1, 3)..Point::new(2, 0),
3597        );
3598
3599        // --- Scenario 3: Base text edit changes one hunk but not another ---
3600        //
3601        // Two modification hunks exist. Only one of them is resolved by
3602        // the base text change; the other remains identical.
3603        let base_3 = "aaa\nbbb\nccc\nddd\neee\n";
3604        let mut base_buf_3 = Buffer::new(
3605            ReplicaId::LOCAL,
3606            BufferId::new(101).unwrap(),
3607            base_3.to_string(),
3608        );
3609
3610        let buffer_text_3 = "aaa\nBBB\nccc\nDDD\neee\n";
3611        let buffer_3 = Buffer::new(
3612            ReplicaId::LOCAL,
3613            BufferId::new(3).unwrap(),
3614            buffer_text_3.to_string(),
3615        );
3616
3617        let old_base_snapshot_3 = base_buf_3.snapshot().clone();
3618        let old_hunks_3 = compute_hunks(
3619            Some((Arc::from(base_3), Rope::from(base_3))),
3620            buffer_3.snapshot(),
3621            None,
3622        );
3623
3624        // Change "ddd" to "DDD" in the base text so that hunk disappears,
3625        // but "bbb" stays, so its hunk remains.
3626        base_buf_3.edit([(12..15, "DDD")]);
3627        let new_base_str_3: Arc<str> = Arc::from(base_buf_3.text().as_str());
3628        let new_base_snapshot_3 = base_buf_3.snapshot();
3629
3630        let new_hunks_3 = compute_hunks(
3631            Some((new_base_str_3.clone(), Rope::from(new_base_str_3.as_ref()))),
3632            buffer_3.snapshot(),
3633            None,
3634        );
3635
3636        let DiffChanged {
3637            changed_range,
3638            base_text_changed_range,
3639            extended_range: _,
3640        } = compare_hunks(
3641            &new_hunks_3,
3642            &old_hunks_3,
3643            &buffer_3.snapshot(),
3644            &buffer_3.snapshot(),
3645            &old_base_snapshot_3,
3646            &new_base_snapshot_3,
3647        );
3648
3649        // Only the second hunk (ddd → DDD) disappeared; the first hunk
3650        // (bbb → BBB) is unchanged, so the changed range covers only line 3.
3651        let range = changed_range.unwrap();
3652        assert_eq!(
3653            range.to_point(&buffer_3),
3654            Point::new(3, 0)..Point::new(4, 0),
3655        );
3656        let base_range = base_text_changed_range.unwrap();
3657        // anchor_after(12) is right-biased at the start of deleted "ddd",
3658        // so after the edit replacing "ddd" with "DDD" it resolves past
3659        // the insertion to Point(3, 3).
3660        assert_eq!(
3661            base_range.to_point(&new_base_snapshot_3),
3662            Point::new(3, 3)..Point::new(4, 0),
3663        );
3664
3665        // --- Scenario 4: Both buffer and base text change simultaneously ---
3666        //
3667        // The buffer gains an edit that introduces a new hunk while the
3668        // base text also changes.
3669        let base_4 = "alpha\nbeta\ngamma\ndelta\n";
3670        let mut base_buf_4 = Buffer::new(
3671            ReplicaId::LOCAL,
3672            BufferId::new(102).unwrap(),
3673            base_4.to_string(),
3674        );
3675
3676        let buffer_text_4 = "alpha\nBETA\ngamma\ndelta\n";
3677        let mut buffer_4 = Buffer::new(
3678            ReplicaId::LOCAL,
3679            BufferId::new(4).unwrap(),
3680            buffer_text_4.to_string(),
3681        );
3682
3683        let old_base_snapshot_4 = base_buf_4.snapshot().clone();
3684        let old_buffer_snapshot_4 = buffer_4.snapshot().clone();
3685        let old_hunks_4 = compute_hunks(
3686            Some((Arc::from(base_4), Rope::from(base_4))),
3687            buffer_4.snapshot(),
3688            None,
3689        );
3690
3691        // Edit the buffer: change "delta" to "DELTA" (new modification hunk).
3692        buffer_4.edit_via_marked_text(
3693            &"
3694                alpha
3695                BETA
3696                gamma
3697                «DELTA»
3698            "
3699            .unindent(),
3700        );
3701
3702        // Edit the base text: change "beta" to "BETA" (resolves that hunk).
3703        base_buf_4.edit([(6..10, "BETA")]);
3704        let new_base_str_4: Arc<str> = Arc::from(base_buf_4.text().as_str());
3705        let new_base_snapshot_4 = base_buf_4.snapshot();
3706
3707        let new_hunks_4 = compute_hunks(
3708            Some((new_base_str_4.clone(), Rope::from(new_base_str_4.as_ref()))),
3709            buffer_4.snapshot(),
3710            None,
3711        );
3712
3713        let DiffChanged {
3714            changed_range,
3715            base_text_changed_range,
3716            extended_range: _,
3717        } = compare_hunks(
3718            &new_hunks_4,
3719            &old_hunks_4,
3720            &old_buffer_snapshot_4,
3721            &buffer_4.snapshot(),
3722            &old_base_snapshot_4,
3723            &new_base_snapshot_4,
3724        );
3725
3726        // The old BETA hunk (line 1) is gone and a new DELTA hunk (line 3)
3727        // appeared, so the changed range spans from line 1 through line 4.
3728        let range = changed_range.unwrap();
3729        assert_eq!(
3730            range.to_point(&buffer_4),
3731            Point::new(1, 0)..Point::new(4, 0),
3732        );
3733        let base_range = base_text_changed_range.unwrap();
3734        // The old BETA hunk's base range started at byte 6 ("beta"). After
3735        // the base text edit replacing "beta" with "BETA", anchor_after(6)
3736        // resolves past the insertion to Point(1, 4).
3737        assert_eq!(
3738            base_range.to_point(&new_base_snapshot_4),
3739            Point::new(1, 4)..Point::new(4, 0),
3740        );
3741    }
3742
3743    #[gpui::test(iterations = 100)]
3744    async fn test_patch_for_range_random(cx: &mut TestAppContext, mut rng: StdRng) {
3745        fn gen_line(rng: &mut StdRng) -> String {
3746            if rng.random_bool(0.2) {
3747                "\n".to_owned()
3748            } else {
3749                let c = rng.random_range('A'..='Z');
3750                format!("{c}{c}{c}\n")
3751            }
3752        }
3753
3754        fn gen_text(rng: &mut StdRng, line_count: usize) -> String {
3755            (0..line_count).map(|_| gen_line(rng)).collect()
3756        }
3757
3758        fn gen_edits_from(rng: &mut StdRng, base: &str) -> String {
3759            let mut old_lines: Vec<&str> = base.lines().collect();
3760            let mut result = String::new();
3761
3762            while !old_lines.is_empty() {
3763                let unchanged_count = rng.random_range(0..=old_lines.len());
3764                for _ in 0..unchanged_count {
3765                    if old_lines.is_empty() {
3766                        break;
3767                    }
3768                    result.push_str(old_lines.remove(0));
3769                    result.push('\n');
3770                }
3771
3772                if old_lines.is_empty() {
3773                    break;
3774                }
3775
3776                let deleted_count = rng.random_range(0..=old_lines.len().min(3));
3777                for _ in 0..deleted_count {
3778                    if old_lines.is_empty() {
3779                        break;
3780                    }
3781                    old_lines.remove(0);
3782                }
3783
3784                let minimum_added = if deleted_count == 0 { 1 } else { 0 };
3785                let added_count = rng.random_range(minimum_added..=3);
3786                for _ in 0..added_count {
3787                    result.push_str(&gen_line(rng));
3788                }
3789            }
3790
3791            result
3792        }
3793
3794        fn random_point_in_text(rng: &mut StdRng, lines: &[&str]) -> Point {
3795            if lines.is_empty() {
3796                return Point::zero();
3797            }
3798            let row = rng.random_range(0..lines.len() as u32);
3799            let line = lines[row as usize];
3800            let col = if line.is_empty() {
3801                0
3802            } else {
3803                rng.random_range(0..=line.len() as u32)
3804            };
3805            Point::new(row, col)
3806        }
3807
3808        fn random_range_in_text(rng: &mut StdRng, lines: &[&str]) -> RangeInclusive<Point> {
3809            let start = random_point_in_text(rng, lines);
3810            let end = random_point_in_text(rng, lines);
3811            if start <= end {
3812                start..=end
3813            } else {
3814                end..=start
3815            }
3816        }
3817
3818        fn points_in_range(range: &RangeInclusive<Point>, lines: &[&str]) -> Vec<Point> {
3819            let mut points = Vec::new();
3820            for row in range.start().row..=range.end().row {
3821                if row as usize >= lines.len() {
3822                    points.push(Point::new(row, 0));
3823                    continue;
3824                }
3825                let line = lines[row as usize];
3826                let start_col = if row == range.start().row {
3827                    range.start().column
3828                } else {
3829                    0
3830                };
3831                let end_col = if row == range.end().row {
3832                    range.end().column
3833                } else {
3834                    line.len() as u32
3835                };
3836                for col in start_col..=end_col {
3837                    points.push(Point::new(row, col));
3838                }
3839            }
3840            points
3841        }
3842
3843        let rng = &mut rng;
3844
3845        let line_count = rng.random_range(5..20);
3846        let base_text = gen_text(rng, line_count);
3847        let initial_buffer_text = gen_edits_from(rng, &base_text);
3848
3849        let mut buffer = Buffer::new(
3850            ReplicaId::LOCAL,
3851            BufferId::new(1).unwrap(),
3852            initial_buffer_text.clone(),
3853        );
3854
3855        let diff = BufferDiffSnapshot::new_sync(&buffer, base_text.clone(), cx);
3856
3857        let edit_count = rng.random_range(1..=5);
3858        for _ in 0..edit_count {
3859            let buffer_text = buffer.text();
3860            if buffer_text.is_empty() {
3861                buffer.edit([(0..0, gen_line(rng))]);
3862            } else {
3863                let lines: Vec<&str> = buffer_text.lines().collect();
3864                let start_row = rng.random_range(0..lines.len());
3865                let end_row = rng.random_range(start_row..=lines.len().min(start_row + 3));
3866
3867                let start_col = if start_row < lines.len() {
3868                    rng.random_range(0..=lines[start_row].len())
3869                } else {
3870                    0
3871                };
3872                let end_col = if end_row < lines.len() {
3873                    rng.random_range(0..=lines[end_row].len())
3874                } else {
3875                    0
3876                };
3877
3878                let start_offset = buffer
3879                    .point_to_offset(Point::new(start_row as u32, start_col as u32))
3880                    .min(buffer.len());
3881                let end_offset = buffer
3882                    .point_to_offset(Point::new(end_row as u32, end_col as u32))
3883                    .min(buffer.len());
3884
3885                let (start, end) = if start_offset <= end_offset {
3886                    (start_offset, end_offset)
3887                } else {
3888                    (end_offset, start_offset)
3889                };
3890
3891                let new_text = if rng.random_bool(0.3) {
3892                    String::new()
3893                } else {
3894                    let line_count = rng.random_range(0..=2);
3895                    gen_text(rng, line_count)
3896                };
3897
3898                buffer.edit([(start..end, new_text)]);
3899            }
3900        }
3901
3902        let buffer_snapshot = buffer.snapshot();
3903
3904        let buffer_text = buffer_snapshot.text();
3905        let buffer_lines: Vec<&str> = buffer_text.lines().collect();
3906        let base_lines: Vec<&str> = base_text.lines().collect();
3907
3908        let test_count = 10;
3909        for _ in 0..test_count {
3910            let range = random_range_in_text(rng, &buffer_lines);
3911            let points = points_in_range(&range, &buffer_lines);
3912
3913            let optimized_patch = diff.patch_for_buffer_range(range.clone(), &buffer_snapshot);
3914            let naive_patch = diff.patch_for_buffer_range_naive(&buffer_snapshot);
3915
3916            for point in points {
3917                let optimized_edit = optimized_patch.edit_for_old_position(point);
3918                let naive_edit = naive_patch.edit_for_old_position(point);
3919
3920                assert_eq!(
3921                    optimized_edit,
3922                    naive_edit,
3923                    "patch_for_buffer_range mismatch at point {:?} in range {:?}\nbase_text: {:?}\ninitial_buffer: {:?}\ncurrent_buffer: {:?}",
3924                    point,
3925                    range,
3926                    base_text,
3927                    initial_buffer_text,
3928                    buffer_snapshot.text()
3929                );
3930            }
3931        }
3932
3933        for _ in 0..test_count {
3934            let range = random_range_in_text(rng, &base_lines);
3935            let points = points_in_range(&range, &base_lines);
3936
3937            let optimized_patch = diff.patch_for_base_text_range(range.clone(), &buffer_snapshot);
3938            let naive_patch = diff.patch_for_base_text_range_naive(&buffer_snapshot);
3939
3940            for point in points {
3941                let optimized_edit = optimized_patch.edit_for_old_position(point);
3942                let naive_edit = naive_patch.edit_for_old_position(point);
3943
3944                assert_eq!(
3945                    optimized_edit,
3946                    naive_edit,
3947                    "patch_for_base_text_range mismatch at point {:?} in range {:?}\nbase_text: {:?}\ninitial_buffer: {:?}\ncurrent_buffer: {:?}",
3948                    point,
3949                    range,
3950                    base_text,
3951                    initial_buffer_text,
3952                    buffer_snapshot.text()
3953                );
3954            }
3955        }
3956    }
3957}