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