buffer_diff.rs

   1use futures::channel::oneshot;
   2use git2::{DiffLineType as GitDiffLineType, DiffOptions as GitOptions, Patch as GitPatch};
   3use gpui::{App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Task};
   4use language::{Language, LanguageRegistry};
   5use rope::Rope;
   6use std::cmp::Ordering;
   7use std::mem;
   8use std::{future::Future, iter, ops::Range, sync::Arc};
   9use sum_tree::{SumTree, TreeMap};
  10use text::ToOffset as _;
  11use text::{Anchor, Bias, BufferId, OffsetRangeExt, Point};
  12use util::ResultExt;
  13
  14pub struct BufferDiff {
  15    pub buffer_id: BufferId,
  16    inner: BufferDiffInner,
  17    secondary_diff: Option<Entity<BufferDiff>>,
  18}
  19
  20#[derive(Clone, Debug)]
  21pub struct BufferDiffSnapshot {
  22    inner: BufferDiffInner,
  23    secondary_diff: Option<Box<BufferDiffSnapshot>>,
  24}
  25
  26#[derive(Clone)]
  27struct BufferDiffInner {
  28    hunks: SumTree<InternalDiffHunk>,
  29    pending_hunks: TreeMap<usize, PendingHunk>,
  30    base_text: language::BufferSnapshot,
  31    base_text_exists: bool,
  32}
  33
  34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  35pub struct DiffHunkStatus {
  36    pub kind: DiffHunkStatusKind,
  37    pub secondary: DiffHunkSecondaryStatus,
  38}
  39
  40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  41pub enum DiffHunkStatusKind {
  42    Added,
  43    Modified,
  44    Deleted,
  45}
  46
  47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  48pub enum DiffHunkSecondaryStatus {
  49    HasSecondaryHunk,
  50    OverlapsWithSecondaryHunk,
  51    None,
  52    SecondaryHunkAdditionPending,
  53    SecondaryHunkRemovalPending,
  54}
  55
  56/// A diff hunk resolved to rows in the buffer.
  57#[derive(Debug, Clone, PartialEq, Eq)]
  58pub struct DiffHunk {
  59    /// The buffer range, expressed in terms of rows.
  60    pub row_range: Range<u32>,
  61    /// The range in the buffer to which this hunk corresponds.
  62    pub buffer_range: Range<Anchor>,
  63    /// The range in the buffer's diff base text to which this hunk corresponds.
  64    pub diff_base_byte_range: Range<usize>,
  65    pub secondary_status: DiffHunkSecondaryStatus,
  66}
  67
  68/// We store [`InternalDiffHunk`]s internally so we don't need to store the additional row range.
  69#[derive(Debug, Clone, PartialEq, Eq)]
  70struct InternalDiffHunk {
  71    buffer_range: Range<Anchor>,
  72    diff_base_byte_range: Range<usize>,
  73}
  74
  75#[derive(Debug, Clone, PartialEq, Eq)]
  76struct PendingHunk {
  77    buffer_version: clock::Global,
  78    new_status: DiffHunkSecondaryStatus,
  79}
  80
  81#[derive(Debug, Default, Clone)]
  82pub struct DiffHunkSummary {
  83    buffer_range: Range<Anchor>,
  84}
  85
  86impl sum_tree::Item for InternalDiffHunk {
  87    type Summary = DiffHunkSummary;
  88
  89    fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
  90        DiffHunkSummary {
  91            buffer_range: self.buffer_range.clone(),
  92        }
  93    }
  94}
  95
  96impl sum_tree::Summary for DiffHunkSummary {
  97    type Context = text::BufferSnapshot;
  98
  99    fn zero(_cx: &Self::Context) -> Self {
 100        Default::default()
 101    }
 102
 103    fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
 104        self.buffer_range.start = self
 105            .buffer_range
 106            .start
 107            .min(&other.buffer_range.start, buffer);
 108        self.buffer_range.end = self.buffer_range.end.max(&other.buffer_range.end, buffer);
 109    }
 110}
 111
 112impl sum_tree::SeekTarget<'_, DiffHunkSummary, DiffHunkSummary> for Anchor {
 113    fn cmp(&self, cursor_location: &DiffHunkSummary, buffer: &text::BufferSnapshot) -> Ordering {
 114        if self
 115            .cmp(&cursor_location.buffer_range.start, buffer)
 116            .is_lt()
 117        {
 118            Ordering::Less
 119        } else if self.cmp(&cursor_location.buffer_range.end, buffer).is_gt() {
 120            Ordering::Greater
 121        } else {
 122            Ordering::Equal
 123        }
 124    }
 125}
 126
 127impl std::fmt::Debug for BufferDiffInner {
 128    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 129        f.debug_struct("BufferDiffSnapshot")
 130            .field("hunks", &self.hunks)
 131            .finish()
 132    }
 133}
 134
 135impl BufferDiffSnapshot {
 136    pub fn is_empty(&self) -> bool {
 137        self.inner.hunks.is_empty()
 138    }
 139
 140    pub fn secondary_diff(&self) -> Option<&BufferDiffSnapshot> {
 141        self.secondary_diff.as_deref()
 142    }
 143
 144    pub fn hunks_intersecting_range<'a>(
 145        &'a self,
 146        range: Range<Anchor>,
 147        buffer: &'a text::BufferSnapshot,
 148    ) -> impl 'a + Iterator<Item = DiffHunk> {
 149        let unstaged_counterpart = self.secondary_diff.as_ref().map(|diff| &diff.inner);
 150        self.inner
 151            .hunks_intersecting_range(range, buffer, unstaged_counterpart)
 152    }
 153
 154    pub fn hunks_intersecting_range_rev<'a>(
 155        &'a self,
 156        range: Range<Anchor>,
 157        buffer: &'a text::BufferSnapshot,
 158    ) -> impl 'a + Iterator<Item = DiffHunk> {
 159        self.inner.hunks_intersecting_range_rev(range, buffer)
 160    }
 161
 162    pub fn base_text(&self) -> &language::BufferSnapshot {
 163        &self.inner.base_text
 164    }
 165
 166    pub fn base_texts_eq(&self, other: &Self) -> bool {
 167        if self.inner.base_text_exists != other.inner.base_text_exists {
 168            return false;
 169        }
 170        let left = &self.inner.base_text;
 171        let right = &other.inner.base_text;
 172        let (old_id, old_empty) = (left.remote_id(), left.is_empty());
 173        let (new_id, new_empty) = (right.remote_id(), right.is_empty());
 174        new_id == old_id || (new_empty && old_empty)
 175    }
 176}
 177
 178impl BufferDiffInner {
 179    fn stage_or_unstage_hunks(
 180        &mut self,
 181        unstaged_diff: &Self,
 182        stage: bool,
 183        hunks: &[DiffHunk],
 184        buffer: &text::BufferSnapshot,
 185        file_exists: bool,
 186    ) -> (Option<Rope>, Vec<(usize, PendingHunk)>) {
 187        let head_text = self
 188            .base_text_exists
 189            .then(|| self.base_text.as_rope().clone());
 190        let index_text = unstaged_diff
 191            .base_text_exists
 192            .then(|| unstaged_diff.base_text.as_rope().clone());
 193
 194        // If the file doesn't exist in either HEAD or the index, then the
 195        // entire file must be either created or deleted in the index.
 196        let (index_text, head_text) = match (index_text, head_text) {
 197            (Some(index_text), Some(head_text)) if file_exists || !stage => (index_text, head_text),
 198            (_, head_text @ _) => {
 199                if stage {
 200                    log::debug!("stage all");
 201                    return (
 202                        file_exists.then(|| buffer.as_rope().clone()),
 203                        vec![(
 204                            0,
 205                            PendingHunk {
 206                                buffer_version: buffer.version().clone(),
 207                                new_status: DiffHunkSecondaryStatus::SecondaryHunkRemovalPending,
 208                            },
 209                        )],
 210                    );
 211                } else {
 212                    log::debug!("unstage all");
 213                    return (
 214                        head_text,
 215                        vec![(
 216                            0,
 217                            PendingHunk {
 218                                buffer_version: buffer.version().clone(),
 219                                new_status: DiffHunkSecondaryStatus::SecondaryHunkAdditionPending,
 220                            },
 221                        )],
 222                    );
 223                }
 224            }
 225        };
 226
 227        let mut unstaged_hunk_cursor = unstaged_diff.hunks.cursor::<DiffHunkSummary>(buffer);
 228        unstaged_hunk_cursor.next(buffer);
 229        let mut edits = Vec::new();
 230        let mut pending_hunks = Vec::new();
 231        let mut prev_unstaged_hunk_buffer_offset = 0;
 232        let mut prev_unstaged_hunk_base_text_offset = 0;
 233        for DiffHunk {
 234            buffer_range,
 235            diff_base_byte_range,
 236            secondary_status,
 237            ..
 238        } in hunks.iter().cloned()
 239        {
 240            if (stage && secondary_status == DiffHunkSecondaryStatus::None)
 241                || (!stage && secondary_status == DiffHunkSecondaryStatus::HasSecondaryHunk)
 242            {
 243                continue;
 244            }
 245
 246            let skipped_hunks = unstaged_hunk_cursor.slice(&buffer_range.start, Bias::Left, buffer);
 247
 248            if let Some(secondary_hunk) = skipped_hunks.last() {
 249                prev_unstaged_hunk_base_text_offset = secondary_hunk.diff_base_byte_range.end;
 250                prev_unstaged_hunk_buffer_offset =
 251                    secondary_hunk.buffer_range.end.to_offset(buffer);
 252            }
 253
 254            let mut buffer_offset_range = buffer_range.to_offset(buffer);
 255            let start_overshoot = buffer_offset_range.start - prev_unstaged_hunk_buffer_offset;
 256            let mut index_start = prev_unstaged_hunk_base_text_offset + start_overshoot;
 257
 258            while let Some(unstaged_hunk) = unstaged_hunk_cursor.item().filter(|item| {
 259                item.buffer_range
 260                    .start
 261                    .cmp(&buffer_range.end, buffer)
 262                    .is_le()
 263            }) {
 264                let unstaged_hunk_offset_range = unstaged_hunk.buffer_range.to_offset(buffer);
 265                prev_unstaged_hunk_base_text_offset = unstaged_hunk.diff_base_byte_range.end;
 266                prev_unstaged_hunk_buffer_offset = unstaged_hunk_offset_range.end;
 267
 268                index_start = index_start.min(unstaged_hunk.diff_base_byte_range.start);
 269                buffer_offset_range.start = buffer_offset_range
 270                    .start
 271                    .min(unstaged_hunk_offset_range.start);
 272
 273                unstaged_hunk_cursor.next(buffer);
 274            }
 275
 276            let end_overshoot = buffer_offset_range
 277                .end
 278                .saturating_sub(prev_unstaged_hunk_buffer_offset);
 279            let index_end = prev_unstaged_hunk_base_text_offset + end_overshoot;
 280
 281            let index_range = index_start..index_end;
 282            buffer_offset_range.end = buffer_offset_range
 283                .end
 284                .max(prev_unstaged_hunk_buffer_offset);
 285
 286            let replacement_text = if stage {
 287                log::debug!("stage hunk {:?}", buffer_offset_range);
 288                buffer
 289                    .text_for_range(buffer_offset_range)
 290                    .collect::<String>()
 291            } else {
 292                log::debug!("unstage hunk {:?}", buffer_offset_range);
 293                head_text
 294                    .chunks_in_range(diff_base_byte_range.clone())
 295                    .collect::<String>()
 296            };
 297            pending_hunks.push((
 298                diff_base_byte_range.start,
 299                PendingHunk {
 300                    buffer_version: buffer.version().clone(),
 301                    new_status: if stage {
 302                        DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
 303                    } else {
 304                        DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
 305                    },
 306                },
 307            ));
 308            edits.push((index_range, replacement_text));
 309        }
 310
 311        let mut new_index_text = Rope::new();
 312        let mut index_cursor = index_text.cursor(0);
 313        for (old_range, replacement_text) in edits {
 314            new_index_text.append(index_cursor.slice(old_range.start));
 315            index_cursor.seek_forward(old_range.end);
 316            new_index_text.push(&replacement_text);
 317        }
 318        new_index_text.append(index_cursor.suffix());
 319        (Some(new_index_text), pending_hunks)
 320    }
 321
 322    fn hunks_intersecting_range<'a>(
 323        &'a self,
 324        range: Range<Anchor>,
 325        buffer: &'a text::BufferSnapshot,
 326        secondary: Option<&'a Self>,
 327    ) -> impl 'a + Iterator<Item = DiffHunk> {
 328        let range = range.to_offset(buffer);
 329
 330        let mut cursor = self
 331            .hunks
 332            .filter::<_, DiffHunkSummary>(buffer, move |summary| {
 333                let summary_range = summary.buffer_range.to_offset(buffer);
 334                let before_start = summary_range.end < range.start;
 335                let after_end = summary_range.start > range.end;
 336                !before_start && !after_end
 337            });
 338
 339        let anchor_iter = iter::from_fn(move || {
 340            cursor.next(buffer);
 341            cursor.item()
 342        })
 343        .flat_map(move |hunk| {
 344            [
 345                (
 346                    &hunk.buffer_range.start,
 347                    (hunk.buffer_range.start, hunk.diff_base_byte_range.start),
 348                ),
 349                (
 350                    &hunk.buffer_range.end,
 351                    (hunk.buffer_range.end, hunk.diff_base_byte_range.end),
 352                ),
 353            ]
 354        });
 355
 356        let mut secondary_cursor = None;
 357        let mut pending_hunks = TreeMap::default();
 358        if let Some(secondary) = secondary.as_ref() {
 359            let mut cursor = secondary.hunks.cursor::<DiffHunkSummary>(buffer);
 360            cursor.next(buffer);
 361            secondary_cursor = Some(cursor);
 362            pending_hunks = secondary.pending_hunks.clone();
 363        }
 364
 365        let mut summaries = buffer.summaries_for_anchors_with_payload::<Point, _, _>(anchor_iter);
 366        iter::from_fn(move || loop {
 367            let (start_point, (start_anchor, start_base)) = summaries.next()?;
 368            let (mut end_point, (mut end_anchor, end_base)) = summaries.next()?;
 369
 370            if !start_anchor.is_valid(buffer) {
 371                continue;
 372            }
 373
 374            if end_point.column > 0 {
 375                end_point.row += 1;
 376                end_point.column = 0;
 377                end_anchor = buffer.anchor_before(end_point);
 378            }
 379
 380            let mut secondary_status = DiffHunkSecondaryStatus::None;
 381
 382            let mut has_pending = false;
 383            if let Some(pending_hunk) = pending_hunks.get(&start_base) {
 384                if !buffer.has_edits_since_in_range(
 385                    &pending_hunk.buffer_version,
 386                    start_anchor..end_anchor,
 387                ) {
 388                    has_pending = true;
 389                    secondary_status = pending_hunk.new_status;
 390                }
 391            }
 392
 393            if let (Some(secondary_cursor), false) = (secondary_cursor.as_mut(), has_pending) {
 394                if start_anchor
 395                    .cmp(&secondary_cursor.start().buffer_range.start, buffer)
 396                    .is_gt()
 397                {
 398                    secondary_cursor.seek_forward(&start_anchor, Bias::Left, buffer);
 399                }
 400
 401                if let Some(secondary_hunk) = secondary_cursor.item() {
 402                    let mut secondary_range = secondary_hunk.buffer_range.to_point(buffer);
 403                    if secondary_range.end.column > 0 {
 404                        secondary_range.end.row += 1;
 405                        secondary_range.end.column = 0;
 406                    }
 407                    if secondary_range.is_empty() && secondary_hunk.diff_base_byte_range.is_empty()
 408                    {
 409                        // ignore
 410                    } else if secondary_range == (start_point..end_point) {
 411                        secondary_status = DiffHunkSecondaryStatus::HasSecondaryHunk;
 412                    } else if secondary_range.start <= end_point {
 413                        secondary_status = DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk;
 414                    }
 415                }
 416            }
 417
 418            return Some(DiffHunk {
 419                row_range: start_point.row..end_point.row,
 420                diff_base_byte_range: start_base..end_base,
 421                buffer_range: start_anchor..end_anchor,
 422                secondary_status,
 423            });
 424        })
 425    }
 426
 427    fn hunks_intersecting_range_rev<'a>(
 428        &'a self,
 429        range: Range<Anchor>,
 430        buffer: &'a text::BufferSnapshot,
 431    ) -> impl 'a + Iterator<Item = DiffHunk> {
 432        let mut cursor = self
 433            .hunks
 434            .filter::<_, DiffHunkSummary>(buffer, move |summary| {
 435                let before_start = summary.buffer_range.end.cmp(&range.start, buffer).is_lt();
 436                let after_end = summary.buffer_range.start.cmp(&range.end, buffer).is_gt();
 437                !before_start && !after_end
 438            });
 439
 440        iter::from_fn(move || {
 441            cursor.prev(buffer);
 442
 443            let hunk = cursor.item()?;
 444            let range = hunk.buffer_range.to_point(buffer);
 445            let end_row = if range.end.column > 0 {
 446                range.end.row + 1
 447            } else {
 448                range.end.row
 449            };
 450
 451            Some(DiffHunk {
 452                row_range: range.start.row..end_row,
 453                diff_base_byte_range: hunk.diff_base_byte_range.clone(),
 454                buffer_range: hunk.buffer_range.clone(),
 455                // The secondary status is not used by callers of this method.
 456                secondary_status: DiffHunkSecondaryStatus::None,
 457            })
 458        })
 459    }
 460
 461    fn compare(&self, old: &Self, new_snapshot: &text::BufferSnapshot) -> Option<Range<Anchor>> {
 462        let mut new_cursor = self.hunks.cursor::<()>(new_snapshot);
 463        let mut old_cursor = old.hunks.cursor::<()>(new_snapshot);
 464        old_cursor.next(new_snapshot);
 465        new_cursor.next(new_snapshot);
 466        let mut start = None;
 467        let mut end = None;
 468
 469        loop {
 470            match (new_cursor.item(), old_cursor.item()) {
 471                (Some(new_hunk), Some(old_hunk)) => {
 472                    match new_hunk
 473                        .buffer_range
 474                        .start
 475                        .cmp(&old_hunk.buffer_range.start, new_snapshot)
 476                    {
 477                        Ordering::Less => {
 478                            start.get_or_insert(new_hunk.buffer_range.start);
 479                            end.replace(new_hunk.buffer_range.end);
 480                            new_cursor.next(new_snapshot);
 481                        }
 482                        Ordering::Equal => {
 483                            if new_hunk != old_hunk {
 484                                start.get_or_insert(new_hunk.buffer_range.start);
 485                                if old_hunk
 486                                    .buffer_range
 487                                    .end
 488                                    .cmp(&new_hunk.buffer_range.end, new_snapshot)
 489                                    .is_ge()
 490                                {
 491                                    end.replace(old_hunk.buffer_range.end);
 492                                } else {
 493                                    end.replace(new_hunk.buffer_range.end);
 494                                }
 495                            }
 496
 497                            new_cursor.next(new_snapshot);
 498                            old_cursor.next(new_snapshot);
 499                        }
 500                        Ordering::Greater => {
 501                            start.get_or_insert(old_hunk.buffer_range.start);
 502                            end.replace(old_hunk.buffer_range.end);
 503                            old_cursor.next(new_snapshot);
 504                        }
 505                    }
 506                }
 507                (Some(new_hunk), None) => {
 508                    start.get_or_insert(new_hunk.buffer_range.start);
 509                    end.replace(new_hunk.buffer_range.end);
 510                    new_cursor.next(new_snapshot);
 511                }
 512                (None, Some(old_hunk)) => {
 513                    start.get_or_insert(old_hunk.buffer_range.start);
 514                    end.replace(old_hunk.buffer_range.end);
 515                    old_cursor.next(new_snapshot);
 516                }
 517                (None, None) => break,
 518            }
 519        }
 520
 521        start.zip(end).map(|(start, end)| start..end)
 522    }
 523}
 524
 525fn compute_hunks(
 526    diff_base: Option<(Arc<String>, Rope)>,
 527    buffer: text::BufferSnapshot,
 528) -> SumTree<InternalDiffHunk> {
 529    let mut tree = SumTree::new(&buffer);
 530
 531    if let Some((diff_base, diff_base_rope)) = diff_base {
 532        let buffer_text = buffer.as_rope().to_string();
 533
 534        let mut options = GitOptions::default();
 535        options.context_lines(0);
 536        let patch = GitPatch::from_buffers(
 537            diff_base.as_bytes(),
 538            None,
 539            buffer_text.as_bytes(),
 540            None,
 541            Some(&mut options),
 542        )
 543        .log_err();
 544
 545        // A common case in Zed is that the empty buffer is represented as just a newline,
 546        // but if we just compute a naive diff you get a "preserved" line in the middle,
 547        // which is a bit odd.
 548        if buffer_text == "\n" && diff_base.ends_with("\n") && diff_base.len() > 1 {
 549            tree.push(
 550                InternalDiffHunk {
 551                    buffer_range: buffer.anchor_before(0)..buffer.anchor_before(0),
 552                    diff_base_byte_range: 0..diff_base.len() - 1,
 553                },
 554                &buffer,
 555            );
 556            return tree;
 557        }
 558
 559        if let Some(patch) = patch {
 560            let mut divergence = 0;
 561            for hunk_index in 0..patch.num_hunks() {
 562                let hunk = process_patch_hunk(
 563                    &patch,
 564                    hunk_index,
 565                    &diff_base_rope,
 566                    &buffer,
 567                    &mut divergence,
 568                );
 569                tree.push(hunk, &buffer);
 570            }
 571        }
 572    } else {
 573        tree.push(
 574            InternalDiffHunk {
 575                buffer_range: Anchor::MIN..Anchor::MAX,
 576                diff_base_byte_range: 0..0,
 577            },
 578            &buffer,
 579        );
 580    }
 581
 582    tree
 583}
 584
 585fn process_patch_hunk(
 586    patch: &GitPatch<'_>,
 587    hunk_index: usize,
 588    diff_base: &Rope,
 589    buffer: &text::BufferSnapshot,
 590    buffer_row_divergence: &mut i64,
 591) -> InternalDiffHunk {
 592    let line_item_count = patch.num_lines_in_hunk(hunk_index).unwrap();
 593    assert!(line_item_count > 0);
 594
 595    let mut first_deletion_buffer_row: Option<u32> = None;
 596    let mut buffer_row_range: Option<Range<u32>> = None;
 597    let mut diff_base_byte_range: Option<Range<usize>> = None;
 598    let mut first_addition_old_row: Option<u32> = None;
 599
 600    for line_index in 0..line_item_count {
 601        let line = patch.line_in_hunk(hunk_index, line_index).unwrap();
 602        let kind = line.origin_value();
 603        let content_offset = line.content_offset() as isize;
 604        let content_len = line.content().len() as isize;
 605        match kind {
 606            GitDiffLineType::Addition => {
 607                if first_addition_old_row.is_none() {
 608                    first_addition_old_row = Some(
 609                        (line.new_lineno().unwrap() as i64 - *buffer_row_divergence - 1) as u32,
 610                    );
 611                }
 612                *buffer_row_divergence += 1;
 613                let row = line.new_lineno().unwrap().saturating_sub(1);
 614
 615                match &mut buffer_row_range {
 616                    Some(Range { end, .. }) => *end = row + 1,
 617                    None => buffer_row_range = Some(row..row + 1),
 618                }
 619            }
 620            GitDiffLineType::Deletion => {
 621                let end = content_offset + content_len;
 622
 623                match &mut diff_base_byte_range {
 624                    Some(head_byte_range) => head_byte_range.end = end as usize,
 625                    None => diff_base_byte_range = Some(content_offset as usize..end as usize),
 626                }
 627
 628                if first_deletion_buffer_row.is_none() {
 629                    let old_row = line.old_lineno().unwrap().saturating_sub(1);
 630                    let row = old_row as i64 + *buffer_row_divergence;
 631                    first_deletion_buffer_row = Some(row as u32);
 632                }
 633
 634                *buffer_row_divergence -= 1;
 635            }
 636            _ => {}
 637        }
 638    }
 639
 640    let buffer_row_range = buffer_row_range.unwrap_or_else(|| {
 641        // Pure deletion hunk without addition.
 642        let row = first_deletion_buffer_row.unwrap();
 643        row..row
 644    });
 645    let diff_base_byte_range = diff_base_byte_range.unwrap_or_else(|| {
 646        // Pure addition hunk without deletion.
 647        let row = first_addition_old_row.unwrap();
 648        let offset = diff_base.point_to_offset(Point::new(row, 0));
 649        offset..offset
 650    });
 651
 652    let start = Point::new(buffer_row_range.start, 0);
 653    let end = Point::new(buffer_row_range.end, 0);
 654    let buffer_range = buffer.anchor_before(start)..buffer.anchor_before(end);
 655    InternalDiffHunk {
 656        buffer_range,
 657        diff_base_byte_range,
 658    }
 659}
 660
 661impl std::fmt::Debug for BufferDiff {
 662    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 663        f.debug_struct("BufferChangeSet")
 664            .field("buffer_id", &self.buffer_id)
 665            .field("snapshot", &self.inner)
 666            .finish()
 667    }
 668}
 669
 670pub enum BufferDiffEvent {
 671    DiffChanged {
 672        changed_range: Option<Range<text::Anchor>>,
 673    },
 674    LanguageChanged,
 675}
 676
 677impl EventEmitter<BufferDiffEvent> for BufferDiff {}
 678
 679impl BufferDiff {
 680    #[cfg(test)]
 681    fn build_sync(
 682        buffer: text::BufferSnapshot,
 683        diff_base: String,
 684        cx: &mut gpui::TestAppContext,
 685    ) -> BufferDiffInner {
 686        let snapshot =
 687            cx.update(|cx| Self::build(buffer, Some(Arc::new(diff_base)), None, None, cx));
 688        cx.executor().block(snapshot)
 689    }
 690
 691    fn build(
 692        buffer: text::BufferSnapshot,
 693        base_text: Option<Arc<String>>,
 694        language: Option<Arc<Language>>,
 695        language_registry: Option<Arc<LanguageRegistry>>,
 696        cx: &mut App,
 697    ) -> impl Future<Output = BufferDiffInner> {
 698        let base_text_pair;
 699        let base_text_exists;
 700        let base_text_snapshot;
 701        if let Some(text) = &base_text {
 702            let base_text_rope = Rope::from(text.as_str());
 703            base_text_pair = Some((text.clone(), base_text_rope.clone()));
 704            let snapshot = language::Buffer::build_snapshot(
 705                base_text_rope,
 706                language.clone(),
 707                language_registry.clone(),
 708                cx,
 709            );
 710            base_text_snapshot = cx.background_spawn(snapshot);
 711            base_text_exists = true;
 712        } else {
 713            base_text_pair = None;
 714            base_text_snapshot = Task::ready(language::Buffer::build_empty_snapshot(cx));
 715            base_text_exists = false;
 716        };
 717
 718        let hunks = cx.background_spawn({
 719            let buffer = buffer.clone();
 720            async move { compute_hunks(base_text_pair, buffer) }
 721        });
 722
 723        async move {
 724            let (base_text, hunks) = futures::join!(base_text_snapshot, hunks);
 725            BufferDiffInner {
 726                base_text,
 727                hunks,
 728                base_text_exists,
 729                pending_hunks: TreeMap::default(),
 730            }
 731        }
 732    }
 733
 734    fn build_with_base_buffer(
 735        buffer: text::BufferSnapshot,
 736        base_text: Option<Arc<String>>,
 737        base_text_snapshot: language::BufferSnapshot,
 738        cx: &App,
 739    ) -> impl Future<Output = BufferDiffInner> {
 740        let base_text_exists = base_text.is_some();
 741        let base_text_pair = base_text.map(|text| (text, base_text_snapshot.as_rope().clone()));
 742        cx.background_spawn(async move {
 743            BufferDiffInner {
 744                base_text: base_text_snapshot,
 745                hunks: compute_hunks(base_text_pair, buffer),
 746                pending_hunks: TreeMap::default(),
 747                base_text_exists,
 748            }
 749        })
 750    }
 751
 752    fn build_empty(buffer: &text::BufferSnapshot, cx: &mut App) -> BufferDiffInner {
 753        BufferDiffInner {
 754            base_text: language::Buffer::build_empty_snapshot(cx),
 755            hunks: SumTree::new(buffer),
 756            pending_hunks: TreeMap::default(),
 757            base_text_exists: false,
 758        }
 759    }
 760
 761    pub fn set_secondary_diff(&mut self, diff: Entity<BufferDiff>) {
 762        self.secondary_diff = Some(diff);
 763    }
 764
 765    pub fn secondary_diff(&self) -> Option<Entity<BufferDiff>> {
 766        self.secondary_diff.clone()
 767    }
 768
 769    pub fn stage_or_unstage_hunks(
 770        &mut self,
 771        stage: bool,
 772        hunks: &[DiffHunk],
 773        buffer: &text::BufferSnapshot,
 774        file_exists: bool,
 775        cx: &mut Context<Self>,
 776    ) -> Option<Rope> {
 777        let (new_index_text, pending_hunks) = self.inner.stage_or_unstage_hunks(
 778            &self.secondary_diff.as_ref()?.read(cx).inner,
 779            stage,
 780            &hunks,
 781            buffer,
 782            file_exists,
 783        );
 784        if let Some(unstaged_diff) = &self.secondary_diff {
 785            unstaged_diff.update(cx, |diff, _| {
 786                for (offset, pending_hunk) in pending_hunks {
 787                    diff.inner.pending_hunks.insert(offset, pending_hunk);
 788                }
 789            });
 790        }
 791        if let Some((first, last)) = hunks.first().zip(hunks.last()) {
 792            let changed_range = first.buffer_range.start..last.buffer_range.end;
 793            cx.emit(BufferDiffEvent::DiffChanged {
 794                changed_range: Some(changed_range),
 795            });
 796        }
 797        new_index_text
 798    }
 799
 800    pub fn range_to_hunk_range(
 801        &self,
 802        range: Range<Anchor>,
 803        buffer: &text::BufferSnapshot,
 804        cx: &App,
 805    ) -> Option<Range<Anchor>> {
 806        let start = self
 807            .hunks_intersecting_range(range.clone(), &buffer, cx)
 808            .next()?
 809            .buffer_range
 810            .start;
 811        let end = self
 812            .hunks_intersecting_range_rev(range.clone(), &buffer)
 813            .next()?
 814            .buffer_range
 815            .end;
 816        Some(start..end)
 817    }
 818
 819    #[allow(clippy::too_many_arguments)]
 820    pub async fn update_diff(
 821        this: Entity<BufferDiff>,
 822        buffer: text::BufferSnapshot,
 823        base_text: Option<Arc<String>>,
 824        base_text_changed: bool,
 825        language_changed: bool,
 826        language: Option<Arc<Language>>,
 827        language_registry: Option<Arc<LanguageRegistry>>,
 828        cx: &mut AsyncApp,
 829    ) -> anyhow::Result<Option<Range<Anchor>>> {
 830        let snapshot = if base_text_changed || language_changed {
 831            cx.update(|cx| {
 832                Self::build(
 833                    buffer.clone(),
 834                    base_text,
 835                    language.clone(),
 836                    language_registry.clone(),
 837                    cx,
 838                )
 839            })?
 840            .await
 841        } else {
 842            this.read_with(cx, |this, cx| {
 843                Self::build_with_base_buffer(
 844                    buffer.clone(),
 845                    base_text,
 846                    this.base_text().clone(),
 847                    cx,
 848                )
 849            })?
 850            .await
 851        };
 852
 853        this.update(cx, |this, _| this.set_state(snapshot, &buffer))
 854    }
 855
 856    pub fn update_diff_from(
 857        &mut self,
 858        buffer: &text::BufferSnapshot,
 859        other: &Entity<Self>,
 860        cx: &mut Context<Self>,
 861    ) -> Option<Range<Anchor>> {
 862        let other = other.read(cx).inner.clone();
 863        self.set_state(other, buffer)
 864    }
 865
 866    fn set_state(
 867        &mut self,
 868        new_state: BufferDiffInner,
 869        buffer: &text::BufferSnapshot,
 870    ) -> Option<Range<Anchor>> {
 871        let (base_text_changed, changed_range) =
 872            match (self.inner.base_text_exists, new_state.base_text_exists) {
 873                (false, false) => (true, None),
 874                (true, true)
 875                    if self.inner.base_text.remote_id() == new_state.base_text.remote_id() =>
 876                {
 877                    (false, new_state.compare(&self.inner, buffer))
 878                }
 879                _ => (true, Some(text::Anchor::MIN..text::Anchor::MAX)),
 880            };
 881        let pending_hunks = mem::take(&mut self.inner.pending_hunks);
 882        self.inner = new_state;
 883        if !base_text_changed {
 884            self.inner.pending_hunks = pending_hunks;
 885        }
 886        changed_range
 887    }
 888
 889    pub fn base_text(&self) -> &language::BufferSnapshot {
 890        &self.inner.base_text
 891    }
 892
 893    pub fn base_text_exists(&self) -> bool {
 894        self.inner.base_text_exists
 895    }
 896
 897    pub fn snapshot(&self, cx: &App) -> BufferDiffSnapshot {
 898        BufferDiffSnapshot {
 899            inner: self.inner.clone(),
 900            secondary_diff: self
 901                .secondary_diff
 902                .as_ref()
 903                .map(|diff| Box::new(diff.read(cx).snapshot(cx))),
 904        }
 905    }
 906
 907    pub fn hunks_intersecting_range<'a>(
 908        &'a self,
 909        range: Range<text::Anchor>,
 910        buffer_snapshot: &'a text::BufferSnapshot,
 911        cx: &'a App,
 912    ) -> impl 'a + Iterator<Item = DiffHunk> {
 913        let unstaged_counterpart = self
 914            .secondary_diff
 915            .as_ref()
 916            .map(|diff| &diff.read(cx).inner);
 917        self.inner
 918            .hunks_intersecting_range(range, buffer_snapshot, unstaged_counterpart)
 919    }
 920
 921    pub fn hunks_intersecting_range_rev<'a>(
 922        &'a self,
 923        range: Range<text::Anchor>,
 924        buffer_snapshot: &'a text::BufferSnapshot,
 925    ) -> impl 'a + Iterator<Item = DiffHunk> {
 926        self.inner
 927            .hunks_intersecting_range_rev(range, buffer_snapshot)
 928    }
 929
 930    pub fn hunks_in_row_range<'a>(
 931        &'a self,
 932        range: Range<u32>,
 933        buffer: &'a text::BufferSnapshot,
 934        cx: &'a App,
 935    ) -> impl 'a + Iterator<Item = DiffHunk> {
 936        let start = buffer.anchor_before(Point::new(range.start, 0));
 937        let end = buffer.anchor_after(Point::new(range.end, 0));
 938        self.hunks_intersecting_range(start..end, buffer, cx)
 939    }
 940
 941    /// Used in cases where the change set isn't derived from git.
 942    pub fn set_base_text(
 943        &mut self,
 944        base_buffer: Entity<language::Buffer>,
 945        buffer: text::BufferSnapshot,
 946        cx: &mut Context<Self>,
 947    ) -> oneshot::Receiver<()> {
 948        let (tx, rx) = oneshot::channel();
 949        let this = cx.weak_entity();
 950        let base_buffer = base_buffer.read(cx);
 951        let language_registry = base_buffer.language_registry();
 952        let base_buffer = base_buffer.snapshot();
 953        let base_text = Arc::new(base_buffer.text());
 954
 955        let snapshot = BufferDiff::build(
 956            buffer.clone(),
 957            Some(base_text),
 958            base_buffer.language().cloned(),
 959            language_registry,
 960            cx,
 961        );
 962        let complete_on_drop = util::defer(|| {
 963            tx.send(()).ok();
 964        });
 965        cx.spawn(|_, mut cx| async move {
 966            let snapshot = snapshot.await;
 967            let Some(this) = this.upgrade() else {
 968                return;
 969            };
 970            this.update(&mut cx, |this, _| {
 971                this.set_state(snapshot, &buffer);
 972            })
 973            .log_err();
 974            drop(complete_on_drop)
 975        })
 976        .detach();
 977        rx
 978    }
 979
 980    pub fn base_text_string(&self) -> Option<String> {
 981        self.inner
 982            .base_text_exists
 983            .then(|| self.inner.base_text.text())
 984    }
 985
 986    pub fn new(buffer: &text::BufferSnapshot, cx: &mut App) -> Self {
 987        BufferDiff {
 988            buffer_id: buffer.remote_id(),
 989            inner: BufferDiff::build_empty(buffer, cx),
 990            secondary_diff: None,
 991        }
 992    }
 993
 994    #[cfg(any(test, feature = "test-support"))]
 995    pub fn new_with_base_text(
 996        base_text: &str,
 997        buffer: &Entity<language::Buffer>,
 998        cx: &mut App,
 999    ) -> Self {
1000        let mut base_text = base_text.to_owned();
1001        text::LineEnding::normalize(&mut base_text);
1002        let snapshot = BufferDiff::build(
1003            buffer.read(cx).text_snapshot(),
1004            Some(base_text.into()),
1005            None,
1006            None,
1007            cx,
1008        );
1009        let snapshot = cx.background_executor().block(snapshot);
1010        BufferDiff {
1011            buffer_id: buffer.read(cx).remote_id(),
1012            inner: snapshot,
1013            secondary_diff: None,
1014        }
1015    }
1016
1017    #[cfg(any(test, feature = "test-support"))]
1018    pub fn recalculate_diff_sync(&mut self, buffer: text::BufferSnapshot, cx: &mut Context<Self>) {
1019        let base_text = self.base_text_string().map(Arc::new);
1020        let snapshot = BufferDiff::build_with_base_buffer(
1021            buffer.clone(),
1022            base_text,
1023            self.inner.base_text.clone(),
1024            cx,
1025        );
1026        let snapshot = cx.background_executor().block(snapshot);
1027        let changed_range = self.set_state(snapshot, &buffer);
1028        cx.emit(BufferDiffEvent::DiffChanged { changed_range });
1029    }
1030}
1031
1032impl DiffHunk {
1033    pub fn is_created_file(&self) -> bool {
1034        self.diff_base_byte_range == (0..0) && self.buffer_range == (Anchor::MIN..Anchor::MAX)
1035    }
1036
1037    pub fn status(&self) -> DiffHunkStatus {
1038        let kind = if self.buffer_range.start == self.buffer_range.end {
1039            DiffHunkStatusKind::Deleted
1040        } else if self.diff_base_byte_range.is_empty() {
1041            DiffHunkStatusKind::Added
1042        } else {
1043            DiffHunkStatusKind::Modified
1044        };
1045        DiffHunkStatus {
1046            kind,
1047            secondary: self.secondary_status,
1048        }
1049    }
1050}
1051
1052impl DiffHunkStatus {
1053    pub fn has_secondary_hunk(&self) -> bool {
1054        matches!(
1055            self.secondary,
1056            DiffHunkSecondaryStatus::HasSecondaryHunk
1057                | DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
1058                | DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk
1059        )
1060    }
1061
1062    pub fn is_pending(&self) -> bool {
1063        matches!(
1064            self.secondary,
1065            DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
1066                | DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
1067        )
1068    }
1069
1070    pub fn is_deleted(&self) -> bool {
1071        self.kind == DiffHunkStatusKind::Deleted
1072    }
1073
1074    pub fn is_added(&self) -> bool {
1075        self.kind == DiffHunkStatusKind::Added
1076    }
1077
1078    pub fn is_modified(&self) -> bool {
1079        self.kind == DiffHunkStatusKind::Modified
1080    }
1081
1082    pub fn added(secondary: DiffHunkSecondaryStatus) -> Self {
1083        Self {
1084            kind: DiffHunkStatusKind::Added,
1085            secondary,
1086        }
1087    }
1088
1089    pub fn modified(secondary: DiffHunkSecondaryStatus) -> Self {
1090        Self {
1091            kind: DiffHunkStatusKind::Modified,
1092            secondary,
1093        }
1094    }
1095
1096    pub fn deleted(secondary: DiffHunkSecondaryStatus) -> Self {
1097        Self {
1098            kind: DiffHunkStatusKind::Deleted,
1099            secondary,
1100        }
1101    }
1102
1103    pub fn deleted_none() -> Self {
1104        Self {
1105            kind: DiffHunkStatusKind::Deleted,
1106            secondary: DiffHunkSecondaryStatus::None,
1107        }
1108    }
1109
1110    pub fn added_none() -> Self {
1111        Self {
1112            kind: DiffHunkStatusKind::Added,
1113            secondary: DiffHunkSecondaryStatus::None,
1114        }
1115    }
1116
1117    pub fn modified_none() -> Self {
1118        Self {
1119            kind: DiffHunkStatusKind::Modified,
1120            secondary: DiffHunkSecondaryStatus::None,
1121        }
1122    }
1123}
1124
1125/// Range (crossing new lines), old, new
1126#[cfg(any(test, feature = "test-support"))]
1127#[track_caller]
1128pub fn assert_hunks<Iter>(
1129    diff_hunks: Iter,
1130    buffer: &text::BufferSnapshot,
1131    diff_base: &str,
1132    expected_hunks: &[(Range<u32>, &str, &str, DiffHunkStatus)],
1133) where
1134    Iter: Iterator<Item = DiffHunk>,
1135{
1136    let actual_hunks = diff_hunks
1137        .map(|hunk| {
1138            (
1139                hunk.row_range.clone(),
1140                &diff_base[hunk.diff_base_byte_range.clone()],
1141                buffer
1142                    .text_for_range(
1143                        Point::new(hunk.row_range.start, 0)..Point::new(hunk.row_range.end, 0),
1144                    )
1145                    .collect::<String>(),
1146                hunk.status(),
1147            )
1148        })
1149        .collect::<Vec<_>>();
1150
1151    let expected_hunks: Vec<_> = expected_hunks
1152        .iter()
1153        .map(|(r, s, h, status)| (r.clone(), *s, h.to_string(), *status))
1154        .collect();
1155
1156    assert_eq!(actual_hunks, expected_hunks);
1157}
1158
1159#[cfg(test)]
1160mod tests {
1161    use std::fmt::Write as _;
1162
1163    use super::*;
1164    use gpui::TestAppContext;
1165    use rand::{rngs::StdRng, Rng as _};
1166    use text::{Buffer, BufferId, Rope};
1167    use unindent::Unindent as _;
1168    use util::test::marked_text_ranges;
1169
1170    #[ctor::ctor]
1171    fn init_logger() {
1172        if std::env::var("RUST_LOG").is_ok() {
1173            env_logger::init();
1174        }
1175    }
1176
1177    #[gpui::test]
1178    async fn test_buffer_diff_simple(cx: &mut gpui::TestAppContext) {
1179        let diff_base = "
1180            one
1181            two
1182            three
1183        "
1184        .unindent();
1185
1186        let buffer_text = "
1187            one
1188            HELLO
1189            three
1190        "
1191        .unindent();
1192
1193        let mut buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1194        let mut diff = BufferDiff::build_sync(buffer.clone(), diff_base.clone(), cx);
1195        assert_hunks(
1196            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
1197            &buffer,
1198            &diff_base,
1199            &[(1..2, "two\n", "HELLO\n", DiffHunkStatus::modified_none())],
1200        );
1201
1202        buffer.edit([(0..0, "point five\n")]);
1203        diff = BufferDiff::build_sync(buffer.clone(), diff_base.clone(), cx);
1204        assert_hunks(
1205            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
1206            &buffer,
1207            &diff_base,
1208            &[
1209                (0..1, "", "point five\n", DiffHunkStatus::added_none()),
1210                (2..3, "two\n", "HELLO\n", DiffHunkStatus::modified_none()),
1211            ],
1212        );
1213
1214        diff = cx.update(|cx| BufferDiff::build_empty(&buffer, cx));
1215        assert_hunks(
1216            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
1217            &buffer,
1218            &diff_base,
1219            &[],
1220        );
1221    }
1222
1223    #[gpui::test]
1224    async fn test_buffer_diff_with_secondary(cx: &mut gpui::TestAppContext) {
1225        let head_text = "
1226            zero
1227            one
1228            two
1229            three
1230            four
1231            five
1232            six
1233            seven
1234            eight
1235            nine
1236        "
1237        .unindent();
1238
1239        let index_text = "
1240            zero
1241            one
1242            TWO
1243            three
1244            FOUR
1245            five
1246            six
1247            seven
1248            eight
1249            NINE
1250        "
1251        .unindent();
1252
1253        let buffer_text = "
1254            zero
1255            one
1256            TWO
1257            three
1258            FOUR
1259            FIVE
1260            six
1261            SEVEN
1262            eight
1263            nine
1264        "
1265        .unindent();
1266
1267        let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1268        let unstaged_diff = BufferDiff::build_sync(buffer.clone(), index_text.clone(), cx);
1269
1270        let uncommitted_diff = BufferDiff::build_sync(buffer.clone(), head_text.clone(), cx);
1271
1272        let expected_hunks = vec![
1273            (2..3, "two\n", "TWO\n", DiffHunkStatus::modified_none()),
1274            (
1275                4..6,
1276                "four\nfive\n",
1277                "FOUR\nFIVE\n",
1278                DiffHunkStatus::modified(DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk),
1279            ),
1280            (
1281                7..8,
1282                "seven\n",
1283                "SEVEN\n",
1284                DiffHunkStatus::modified(DiffHunkSecondaryStatus::HasSecondaryHunk),
1285            ),
1286        ];
1287
1288        assert_hunks(
1289            uncommitted_diff.hunks_intersecting_range(
1290                Anchor::MIN..Anchor::MAX,
1291                &buffer,
1292                Some(&unstaged_diff),
1293            ),
1294            &buffer,
1295            &head_text,
1296            &expected_hunks,
1297        );
1298    }
1299
1300    #[gpui::test]
1301    async fn test_buffer_diff_range(cx: &mut TestAppContext) {
1302        let diff_base = Arc::new(
1303            "
1304            one
1305            two
1306            three
1307            four
1308            five
1309            six
1310            seven
1311            eight
1312            nine
1313            ten
1314        "
1315            .unindent(),
1316        );
1317
1318        let buffer_text = "
1319            A
1320            one
1321            B
1322            two
1323            C
1324            three
1325            HELLO
1326            four
1327            five
1328            SIXTEEN
1329            seven
1330            eight
1331            WORLD
1332            nine
1333
1334            ten
1335
1336        "
1337        .unindent();
1338
1339        let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1340        let diff = cx
1341            .update(|cx| {
1342                BufferDiff::build(buffer.snapshot(), Some(diff_base.clone()), None, None, cx)
1343            })
1344            .await;
1345        assert_eq!(
1346            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None)
1347                .count(),
1348            8
1349        );
1350
1351        assert_hunks(
1352            diff.hunks_intersecting_range(
1353                buffer.anchor_before(Point::new(7, 0))..buffer.anchor_before(Point::new(12, 0)),
1354                &buffer,
1355                None,
1356            ),
1357            &buffer,
1358            &diff_base,
1359            &[
1360                (6..7, "", "HELLO\n", DiffHunkStatus::added_none()),
1361                (9..10, "six\n", "SIXTEEN\n", DiffHunkStatus::modified_none()),
1362                (12..13, "", "WORLD\n", DiffHunkStatus::added_none()),
1363            ],
1364        );
1365    }
1366
1367    #[gpui::test]
1368    async fn test_stage_hunk(cx: &mut TestAppContext) {
1369        struct Example {
1370            name: &'static str,
1371            head_text: String,
1372            index_text: String,
1373            buffer_marked_text: String,
1374            final_index_text: String,
1375        }
1376
1377        let table = [
1378            Example {
1379                name: "uncommitted hunk straddles end of unstaged hunk",
1380                head_text: "
1381                    one
1382                    two
1383                    three
1384                    four
1385                    five
1386                "
1387                .unindent(),
1388                index_text: "
1389                    one
1390                    TWO_HUNDRED
1391                    three
1392                    FOUR_HUNDRED
1393                    five
1394                "
1395                .unindent(),
1396                buffer_marked_text: "
1397                    ZERO
1398                    one
1399                    two
1400                    «THREE_HUNDRED
1401                    FOUR_HUNDRED»
1402                    five
1403                    SIX
1404                "
1405                .unindent(),
1406                final_index_text: "
1407                    one
1408                    two
1409                    THREE_HUNDRED
1410                    FOUR_HUNDRED
1411                    five
1412                "
1413                .unindent(),
1414            },
1415            Example {
1416                name: "uncommitted hunk straddles start of unstaged hunk",
1417                head_text: "
1418                    one
1419                    two
1420                    three
1421                    four
1422                    five
1423                "
1424                .unindent(),
1425                index_text: "
1426                    one
1427                    TWO_HUNDRED
1428                    three
1429                    FOUR_HUNDRED
1430                    five
1431                "
1432                .unindent(),
1433                buffer_marked_text: "
1434                    ZERO
1435                    one
1436                    «TWO_HUNDRED
1437                    THREE_HUNDRED»
1438                    four
1439                    five
1440                    SIX
1441                "
1442                .unindent(),
1443                final_index_text: "
1444                    one
1445                    TWO_HUNDRED
1446                    THREE_HUNDRED
1447                    four
1448                    five
1449                "
1450                .unindent(),
1451            },
1452            Example {
1453                name: "uncommitted hunk strictly contains unstaged hunks",
1454                head_text: "
1455                    one
1456                    two
1457                    three
1458                    four
1459                    five
1460                    six
1461                    seven
1462                "
1463                .unindent(),
1464                index_text: "
1465                    one
1466                    TWO
1467                    THREE
1468                    FOUR
1469                    FIVE
1470                    SIX
1471                    seven
1472                "
1473                .unindent(),
1474                buffer_marked_text: "
1475                    one
1476                    TWO
1477                    «THREE_HUNDRED
1478                    FOUR
1479                    FIVE_HUNDRED»
1480                    SIX
1481                    seven
1482                "
1483                .unindent(),
1484                final_index_text: "
1485                    one
1486                    TWO
1487                    THREE_HUNDRED
1488                    FOUR
1489                    FIVE_HUNDRED
1490                    SIX
1491                    seven
1492                "
1493                .unindent(),
1494            },
1495            Example {
1496                name: "uncommitted deletion hunk",
1497                head_text: "
1498                    one
1499                    two
1500                    three
1501                    four
1502                    five
1503                "
1504                .unindent(),
1505                index_text: "
1506                    one
1507                    two
1508                    three
1509                    four
1510                    five
1511                "
1512                .unindent(),
1513                buffer_marked_text: "
1514                    one
1515                    ˇfive
1516                "
1517                .unindent(),
1518                final_index_text: "
1519                    one
1520                    five
1521                "
1522                .unindent(),
1523            },
1524        ];
1525
1526        for example in table {
1527            let (buffer_text, ranges) = marked_text_ranges(&example.buffer_marked_text, false);
1528            let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1529            let hunk_range =
1530                buffer.anchor_before(ranges[0].start)..buffer.anchor_before(ranges[0].end);
1531
1532            let unstaged = BufferDiff::build_sync(buffer.clone(), example.index_text.clone(), cx);
1533            let uncommitted = BufferDiff::build_sync(buffer.clone(), example.head_text.clone(), cx);
1534
1535            let unstaged_diff = cx.new(|cx| {
1536                let mut diff = BufferDiff::new(&buffer, cx);
1537                diff.set_state(unstaged, &buffer);
1538                diff
1539            });
1540
1541            let uncommitted_diff = cx.new(|cx| {
1542                let mut diff = BufferDiff::new(&buffer, cx);
1543                diff.set_state(uncommitted, &buffer);
1544                diff.set_secondary_diff(unstaged_diff);
1545                diff
1546            });
1547
1548            uncommitted_diff.update(cx, |diff, cx| {
1549                let hunks = diff
1550                    .hunks_intersecting_range(hunk_range.clone(), &buffer, &cx)
1551                    .collect::<Vec<_>>();
1552                for hunk in &hunks {
1553                    assert_ne!(hunk.secondary_status, DiffHunkSecondaryStatus::None)
1554                }
1555
1556                let new_index_text = diff
1557                    .stage_or_unstage_hunks(true, &hunks, &buffer, true, cx)
1558                    .unwrap()
1559                    .to_string();
1560
1561                let hunks = diff
1562                    .hunks_intersecting_range(hunk_range.clone(), &buffer, &cx)
1563                    .collect::<Vec<_>>();
1564                for hunk in &hunks {
1565                    assert_eq!(
1566                        hunk.secondary_status,
1567                        DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
1568                    )
1569                }
1570
1571                pretty_assertions::assert_eq!(
1572                    new_index_text,
1573                    example.final_index_text,
1574                    "example: {}",
1575                    example.name
1576                );
1577            });
1578        }
1579    }
1580
1581    #[gpui::test]
1582    async fn test_buffer_diff_compare(cx: &mut TestAppContext) {
1583        let base_text = "
1584            zero
1585            one
1586            two
1587            three
1588            four
1589            five
1590            six
1591            seven
1592            eight
1593            nine
1594        "
1595        .unindent();
1596
1597        let buffer_text_1 = "
1598            one
1599            three
1600            four
1601            five
1602            SIX
1603            seven
1604            eight
1605            NINE
1606        "
1607        .unindent();
1608
1609        let mut buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text_1);
1610
1611        let empty_diff = cx.update(|cx| BufferDiff::build_empty(&buffer, cx));
1612        let diff_1 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1613        let range = diff_1.compare(&empty_diff, &buffer).unwrap();
1614        assert_eq!(range.to_point(&buffer), Point::new(0, 0)..Point::new(8, 0));
1615
1616        // Edit does not affect the diff.
1617        buffer.edit_via_marked_text(
1618            &"
1619                one
1620                three
1621                four
1622                five
1623                «SIX.5»
1624                seven
1625                eight
1626                NINE
1627            "
1628            .unindent(),
1629        );
1630        let diff_2 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1631        assert_eq!(None, diff_2.compare(&diff_1, &buffer));
1632
1633        // Edit turns a deletion hunk into a modification.
1634        buffer.edit_via_marked_text(
1635            &"
1636                one
1637                «THREE»
1638                four
1639                five
1640                SIX.5
1641                seven
1642                eight
1643                NINE
1644            "
1645            .unindent(),
1646        );
1647        let diff_3 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1648        let range = diff_3.compare(&diff_2, &buffer).unwrap();
1649        assert_eq!(range.to_point(&buffer), Point::new(1, 0)..Point::new(2, 0));
1650
1651        // Edit turns a modification hunk into a deletion.
1652        buffer.edit_via_marked_text(
1653            &"
1654                one
1655                THREE
1656                four
1657                five«»
1658                seven
1659                eight
1660                NINE
1661            "
1662            .unindent(),
1663        );
1664        let diff_4 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1665        let range = diff_4.compare(&diff_3, &buffer).unwrap();
1666        assert_eq!(range.to_point(&buffer), Point::new(3, 4)..Point::new(4, 0));
1667
1668        // Edit introduces a new insertion hunk.
1669        buffer.edit_via_marked_text(
1670            &"
1671                one
1672                THREE
1673                four«
1674                FOUR.5
1675                »five
1676                seven
1677                eight
1678                NINE
1679            "
1680            .unindent(),
1681        );
1682        let diff_5 = BufferDiff::build_sync(buffer.snapshot(), base_text.clone(), cx);
1683        let range = diff_5.compare(&diff_4, &buffer).unwrap();
1684        assert_eq!(range.to_point(&buffer), Point::new(3, 0)..Point::new(4, 0));
1685
1686        // Edit removes a hunk.
1687        buffer.edit_via_marked_text(
1688            &"
1689                one
1690                THREE
1691                four
1692                FOUR.5
1693                five
1694                seven
1695                eight
1696                «nine»
1697            "
1698            .unindent(),
1699        );
1700        let diff_6 = BufferDiff::build_sync(buffer.snapshot(), base_text, cx);
1701        let range = diff_6.compare(&diff_5, &buffer).unwrap();
1702        assert_eq!(range.to_point(&buffer), Point::new(7, 0)..Point::new(8, 0));
1703    }
1704
1705    #[gpui::test(iterations = 100)]
1706    async fn test_staging_and_unstaging_hunks(cx: &mut TestAppContext, mut rng: StdRng) {
1707        fn gen_line(rng: &mut StdRng) -> String {
1708            if rng.gen_bool(0.2) {
1709                "\n".to_owned()
1710            } else {
1711                let c = rng.gen_range('A'..='Z');
1712                format!("{c}{c}{c}\n")
1713            }
1714        }
1715
1716        fn gen_working_copy(rng: &mut StdRng, head: &str) -> String {
1717            let mut old_lines = {
1718                let mut old_lines = Vec::new();
1719                let mut old_lines_iter = head.lines();
1720                while let Some(line) = old_lines_iter.next() {
1721                    assert!(!line.ends_with("\n"));
1722                    old_lines.push(line.to_owned());
1723                }
1724                if old_lines.last().is_some_and(|line| line.is_empty()) {
1725                    old_lines.pop();
1726                }
1727                old_lines.into_iter()
1728            };
1729            let mut result = String::new();
1730            let unchanged_count = rng.gen_range(0..=old_lines.len());
1731            result +=
1732                &old_lines
1733                    .by_ref()
1734                    .take(unchanged_count)
1735                    .fold(String::new(), |mut s, line| {
1736                        writeln!(&mut s, "{line}").unwrap();
1737                        s
1738                    });
1739            while old_lines.len() > 0 {
1740                let deleted_count = rng.gen_range(0..=old_lines.len());
1741                let _advance = old_lines
1742                    .by_ref()
1743                    .take(deleted_count)
1744                    .map(|line| line.len() + 1)
1745                    .sum::<usize>();
1746                let minimum_added = if deleted_count == 0 { 1 } else { 0 };
1747                let added_count = rng.gen_range(minimum_added..=5);
1748                let addition = (0..added_count).map(|_| gen_line(rng)).collect::<String>();
1749                result += &addition;
1750
1751                if old_lines.len() > 0 {
1752                    let blank_lines = old_lines.clone().take_while(|line| line.is_empty()).count();
1753                    if blank_lines == old_lines.len() {
1754                        break;
1755                    };
1756                    let unchanged_count = rng.gen_range((blank_lines + 1).max(1)..=old_lines.len());
1757                    result += &old_lines.by_ref().take(unchanged_count).fold(
1758                        String::new(),
1759                        |mut s, line| {
1760                            writeln!(&mut s, "{line}").unwrap();
1761                            s
1762                        },
1763                    );
1764                }
1765            }
1766            result
1767        }
1768
1769        fn uncommitted_diff(
1770            working_copy: &language::BufferSnapshot,
1771            index_text: &Rope,
1772            head_text: String,
1773            cx: &mut TestAppContext,
1774        ) -> Entity<BufferDiff> {
1775            let inner = BufferDiff::build_sync(working_copy.text.clone(), head_text, cx);
1776            let secondary = BufferDiff {
1777                buffer_id: working_copy.remote_id(),
1778                inner: BufferDiff::build_sync(
1779                    working_copy.text.clone(),
1780                    index_text.to_string(),
1781                    cx,
1782                ),
1783                secondary_diff: None,
1784            };
1785            let secondary = cx.new(|_| secondary);
1786            cx.new(|_| BufferDiff {
1787                buffer_id: working_copy.remote_id(),
1788                inner,
1789                secondary_diff: Some(secondary),
1790            })
1791        }
1792
1793        let operations = std::env::var("OPERATIONS")
1794            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1795            .unwrap_or(10);
1796
1797        let rng = &mut rng;
1798        let head_text = ('a'..='z').fold(String::new(), |mut s, c| {
1799            writeln!(&mut s, "{c}{c}{c}").unwrap();
1800            s
1801        });
1802        let working_copy = gen_working_copy(rng, &head_text);
1803        let working_copy = cx.new(|cx| {
1804            language::Buffer::local_normalized(
1805                Rope::from(working_copy.as_str()),
1806                text::LineEnding::default(),
1807                cx,
1808            )
1809        });
1810        let working_copy = working_copy.read_with(cx, |working_copy, _| working_copy.snapshot());
1811        let mut index_text = if rng.gen() {
1812            Rope::from(head_text.as_str())
1813        } else {
1814            working_copy.as_rope().clone()
1815        };
1816
1817        let mut diff = uncommitted_diff(&working_copy, &index_text, head_text.clone(), cx);
1818        let mut hunks = diff.update(cx, |diff, cx| {
1819            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &working_copy, cx)
1820                .collect::<Vec<_>>()
1821        });
1822        if hunks.len() == 0 {
1823            return;
1824        }
1825
1826        for _ in 0..operations {
1827            let i = rng.gen_range(0..hunks.len());
1828            let hunk = &mut hunks[i];
1829            let hunk_to_change = hunk.clone();
1830            let stage = match hunk.secondary_status {
1831                DiffHunkSecondaryStatus::HasSecondaryHunk => {
1832                    hunk.secondary_status = DiffHunkSecondaryStatus::None;
1833                    true
1834                }
1835                DiffHunkSecondaryStatus::None => {
1836                    hunk.secondary_status = DiffHunkSecondaryStatus::HasSecondaryHunk;
1837                    false
1838                }
1839                _ => unreachable!(),
1840            };
1841
1842            index_text = diff.update(cx, |diff, cx| {
1843                diff.stage_or_unstage_hunks(stage, &[hunk_to_change], &working_copy, true, cx)
1844                    .unwrap()
1845            });
1846
1847            diff = uncommitted_diff(&working_copy, &index_text, head_text.clone(), cx);
1848            let found_hunks = diff.update(cx, |diff, cx| {
1849                diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &working_copy, cx)
1850                    .collect::<Vec<_>>()
1851            });
1852            assert_eq!(hunks.len(), found_hunks.len());
1853
1854            for (expected_hunk, found_hunk) in hunks.iter().zip(&found_hunks) {
1855                assert_eq!(
1856                    expected_hunk.buffer_range.to_point(&working_copy),
1857                    found_hunk.buffer_range.to_point(&working_copy)
1858                );
1859                assert_eq!(
1860                    expected_hunk.diff_base_byte_range,
1861                    found_hunk.diff_base_byte_range
1862                );
1863                assert_eq!(expected_hunk.secondary_status, found_hunk.secondary_status);
1864            }
1865            hunks = found_hunks;
1866        }
1867    }
1868}