buffer_diff.rs

   1use futures::{channel::oneshot, future::OptionFuture};
   2use git2::{DiffLineType as GitDiffLineType, DiffOptions as GitOptions, Patch as GitPatch};
   3use gpui::{App, AsyncApp, Context, Entity, EventEmitter};
   4use language::{Language, LanguageRegistry};
   5use rope::Rope;
   6use std::{cmp, future::Future, iter, ops::Range, sync::Arc};
   7use sum_tree::SumTree;
   8use text::{Anchor, Bias, BufferId, OffsetRangeExt, Point};
   9use util::ResultExt;
  10
  11pub struct BufferDiff {
  12    pub buffer_id: BufferId,
  13    inner: BufferDiffInner,
  14    secondary_diff: Option<Entity<BufferDiff>>,
  15}
  16
  17#[derive(Clone)]
  18pub struct BufferDiffSnapshot {
  19    inner: BufferDiffInner,
  20    secondary_diff: Option<Box<BufferDiffSnapshot>>,
  21}
  22
  23#[derive(Clone)]
  24struct BufferDiffInner {
  25    hunks: SumTree<InternalDiffHunk>,
  26    base_text: Option<language::BufferSnapshot>,
  27}
  28
  29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  30pub enum DiffHunkStatus {
  31    Added(DiffHunkSecondaryStatus),
  32    Modified(DiffHunkSecondaryStatus),
  33    Removed(DiffHunkSecondaryStatus),
  34}
  35
  36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  37pub enum DiffHunkSecondaryStatus {
  38    HasSecondaryHunk,
  39    OverlapsWithSecondaryHunk,
  40    None,
  41}
  42
  43// to stage a hunk:
  44// - assume hunk starts out as not staged
  45// - hunk exists with the same buffer range in the unstaged diff and the uncommitted diff
  46// - we want to construct a "version" of the file that
  47//   - starts from the index base text
  48//   - has the single hunk applied to it
  49//     - the hunk is the one from the UNSTAGED diff, so that the diff base offset range is correct to apply to that diff base
  50// - write that new version of the file into the index
  51
  52// to unstage a hunk
  53// - no hunk in the unstaged diff intersects this hunk from the uncommitted diff
  54// - we want to compute the hunk that
  55//   - we can apply to the index text
  56//   - at the end of applying it,
  57
  58/// A diff hunk resolved to rows in the buffer.
  59#[derive(Debug, Clone, PartialEq, Eq)]
  60pub struct DiffHunk {
  61    /// The buffer range, expressed in terms of rows.
  62    pub row_range: Range<u32>,
  63    /// The range in the buffer to which this hunk corresponds.
  64    pub buffer_range: Range<Anchor>,
  65    /// The range in the buffer's diff base text to which this hunk corresponds.
  66    pub diff_base_byte_range: Range<usize>,
  67    pub secondary_status: DiffHunkSecondaryStatus,
  68}
  69
  70/// We store [`InternalDiffHunk`]s internally so we don't need to store the additional row range.
  71#[derive(Debug, Clone, PartialEq, Eq)]
  72struct InternalDiffHunk {
  73    buffer_range: Range<Anchor>,
  74    diff_base_byte_range: Range<usize>,
  75}
  76
  77impl sum_tree::Item for InternalDiffHunk {
  78    type Summary = DiffHunkSummary;
  79
  80    fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
  81        DiffHunkSummary {
  82            buffer_range: self.buffer_range.clone(),
  83        }
  84    }
  85}
  86
  87#[derive(Debug, Default, Clone)]
  88pub struct DiffHunkSummary {
  89    buffer_range: Range<Anchor>,
  90}
  91
  92impl sum_tree::Summary for DiffHunkSummary {
  93    type Context = text::BufferSnapshot;
  94
  95    fn zero(_cx: &Self::Context) -> Self {
  96        Default::default()
  97    }
  98
  99    fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
 100        self.buffer_range.start = self
 101            .buffer_range
 102            .start
 103            .min(&other.buffer_range.start, buffer);
 104        self.buffer_range.end = self.buffer_range.end.max(&other.buffer_range.end, buffer);
 105    }
 106}
 107
 108impl<'a> sum_tree::SeekTarget<'a, DiffHunkSummary, DiffHunkSummary> for Anchor {
 109    fn cmp(
 110        &self,
 111        cursor_location: &DiffHunkSummary,
 112        buffer: &text::BufferSnapshot,
 113    ) -> cmp::Ordering {
 114        self.cmp(&cursor_location.buffer_range.end, buffer)
 115    }
 116}
 117
 118impl std::fmt::Debug for BufferDiffInner {
 119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 120        f.debug_struct("BufferDiffSnapshot")
 121            .field("hunks", &self.hunks)
 122            .finish()
 123    }
 124}
 125
 126impl BufferDiffSnapshot {
 127    pub fn is_empty(&self) -> bool {
 128        self.inner.hunks.is_empty()
 129    }
 130
 131    pub fn secondary_diff(&self) -> Option<&BufferDiffSnapshot> {
 132        self.secondary_diff.as_deref()
 133    }
 134
 135    pub fn hunks_intersecting_range<'a>(
 136        &'a self,
 137        range: Range<Anchor>,
 138        buffer: &'a text::BufferSnapshot,
 139    ) -> impl 'a + Iterator<Item = DiffHunk> {
 140        let unstaged_counterpart = self.secondary_diff.as_ref().map(|diff| &diff.inner);
 141        self.inner
 142            .hunks_intersecting_range(range, buffer, unstaged_counterpart)
 143    }
 144
 145    pub fn hunks_intersecting_range_rev<'a>(
 146        &'a self,
 147        range: Range<Anchor>,
 148        buffer: &'a text::BufferSnapshot,
 149    ) -> impl 'a + Iterator<Item = DiffHunk> {
 150        self.inner.hunks_intersecting_range_rev(range, buffer)
 151    }
 152
 153    pub fn base_text(&self) -> Option<&language::BufferSnapshot> {
 154        self.inner.base_text.as_ref()
 155    }
 156
 157    pub fn base_texts_eq(&self, other: &Self) -> bool {
 158        match (other.base_text(), self.base_text()) {
 159            (None, None) => true,
 160            (None, Some(_)) => false,
 161            (Some(_), None) => false,
 162            (Some(old), Some(new)) => {
 163                let (old_id, old_empty) = (old.remote_id(), old.is_empty());
 164                let (new_id, new_empty) = (new.remote_id(), new.is_empty());
 165                new_id == old_id || (new_empty && old_empty)
 166            }
 167        }
 168    }
 169}
 170
 171impl BufferDiffInner {
 172    fn hunks_intersecting_range<'a>(
 173        &'a self,
 174        range: Range<Anchor>,
 175        buffer: &'a text::BufferSnapshot,
 176        secondary: Option<&'a Self>,
 177    ) -> impl 'a + Iterator<Item = DiffHunk> {
 178        let range = range.to_offset(buffer);
 179
 180        let mut cursor = self
 181            .hunks
 182            .filter::<_, DiffHunkSummary>(buffer, move |summary| {
 183                let summary_range = summary.buffer_range.to_offset(buffer);
 184                let before_start = summary_range.end < range.start;
 185                let after_end = summary_range.start > range.end;
 186                !before_start && !after_end
 187            });
 188
 189        let anchor_iter = iter::from_fn(move || {
 190            cursor.next(buffer);
 191            cursor.item()
 192        })
 193        .flat_map(move |hunk| {
 194            [
 195                (
 196                    &hunk.buffer_range.start,
 197                    (hunk.buffer_range.start, hunk.diff_base_byte_range.start),
 198                ),
 199                (
 200                    &hunk.buffer_range.end,
 201                    (hunk.buffer_range.end, hunk.diff_base_byte_range.end),
 202                ),
 203            ]
 204        });
 205
 206        let mut secondary_cursor = secondary.as_ref().map(|diff| {
 207            let mut cursor = diff.hunks.cursor::<DiffHunkSummary>(buffer);
 208            cursor.next(buffer);
 209            cursor
 210        });
 211
 212        let mut summaries = buffer.summaries_for_anchors_with_payload::<Point, _, _>(anchor_iter);
 213        iter::from_fn(move || loop {
 214            let (start_point, (start_anchor, start_base)) = summaries.next()?;
 215            let (mut end_point, (mut end_anchor, end_base)) = summaries.next()?;
 216
 217            if !start_anchor.is_valid(buffer) {
 218                continue;
 219            }
 220
 221            if end_point.column > 0 {
 222                end_point.row += 1;
 223                end_point.column = 0;
 224                end_anchor = buffer.anchor_before(end_point);
 225            }
 226
 227            let mut secondary_status = DiffHunkSecondaryStatus::None;
 228            if let Some(secondary_cursor) = secondary_cursor.as_mut() {
 229                if start_anchor
 230                    .cmp(&secondary_cursor.start().buffer_range.start, buffer)
 231                    .is_gt()
 232                {
 233                    secondary_cursor.seek_forward(&end_anchor, Bias::Left, buffer);
 234                }
 235
 236                if let Some(secondary_hunk) = secondary_cursor.item() {
 237                    let secondary_range = secondary_hunk.buffer_range.to_point(buffer);
 238                    if secondary_range == (start_point..end_point) {
 239                        secondary_status = DiffHunkSecondaryStatus::HasSecondaryHunk;
 240                    } else if secondary_range.start <= end_point {
 241                        secondary_status = DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk;
 242                    }
 243                }
 244            }
 245
 246            return Some(DiffHunk {
 247                row_range: start_point.row..end_point.row,
 248                diff_base_byte_range: start_base..end_base,
 249                buffer_range: start_anchor..end_anchor,
 250                secondary_status,
 251            });
 252        })
 253    }
 254
 255    fn hunks_intersecting_range_rev<'a>(
 256        &'a self,
 257        range: Range<Anchor>,
 258        buffer: &'a text::BufferSnapshot,
 259    ) -> impl 'a + Iterator<Item = DiffHunk> {
 260        let mut cursor = self
 261            .hunks
 262            .filter::<_, DiffHunkSummary>(buffer, move |summary| {
 263                let before_start = summary.buffer_range.end.cmp(&range.start, buffer).is_lt();
 264                let after_end = summary.buffer_range.start.cmp(&range.end, buffer).is_gt();
 265                !before_start && !after_end
 266            });
 267
 268        iter::from_fn(move || {
 269            cursor.prev(buffer);
 270
 271            let hunk = cursor.item()?;
 272            let range = hunk.buffer_range.to_point(buffer);
 273            let end_row = if range.end.column > 0 {
 274                range.end.row + 1
 275            } else {
 276                range.end.row
 277            };
 278
 279            Some(DiffHunk {
 280                row_range: range.start.row..end_row,
 281                diff_base_byte_range: hunk.diff_base_byte_range.clone(),
 282                buffer_range: hunk.buffer_range.clone(),
 283                // The secondary status is not used by callers of this method.
 284                secondary_status: DiffHunkSecondaryStatus::None,
 285            })
 286        })
 287    }
 288
 289    fn compare(&self, old: &Self, new_snapshot: &text::BufferSnapshot) -> Option<Range<Anchor>> {
 290        let mut new_cursor = self.hunks.cursor::<()>(new_snapshot);
 291        let mut old_cursor = old.hunks.cursor::<()>(new_snapshot);
 292        old_cursor.next(new_snapshot);
 293        new_cursor.next(new_snapshot);
 294        let mut start = None;
 295        let mut end = None;
 296
 297        loop {
 298            match (new_cursor.item(), old_cursor.item()) {
 299                (Some(new_hunk), Some(old_hunk)) => {
 300                    match new_hunk
 301                        .buffer_range
 302                        .start
 303                        .cmp(&old_hunk.buffer_range.start, new_snapshot)
 304                    {
 305                        cmp::Ordering::Less => {
 306                            start.get_or_insert(new_hunk.buffer_range.start);
 307                            end.replace(new_hunk.buffer_range.end);
 308                            new_cursor.next(new_snapshot);
 309                        }
 310                        cmp::Ordering::Equal => {
 311                            if new_hunk != old_hunk {
 312                                start.get_or_insert(new_hunk.buffer_range.start);
 313                                if old_hunk
 314                                    .buffer_range
 315                                    .end
 316                                    .cmp(&new_hunk.buffer_range.end, new_snapshot)
 317                                    .is_ge()
 318                                {
 319                                    end.replace(old_hunk.buffer_range.end);
 320                                } else {
 321                                    end.replace(new_hunk.buffer_range.end);
 322                                }
 323                            }
 324
 325                            new_cursor.next(new_snapshot);
 326                            old_cursor.next(new_snapshot);
 327                        }
 328                        cmp::Ordering::Greater => {
 329                            start.get_or_insert(old_hunk.buffer_range.start);
 330                            end.replace(old_hunk.buffer_range.end);
 331                            old_cursor.next(new_snapshot);
 332                        }
 333                    }
 334                }
 335                (Some(new_hunk), None) => {
 336                    start.get_or_insert(new_hunk.buffer_range.start);
 337                    end.replace(new_hunk.buffer_range.end);
 338                    new_cursor.next(new_snapshot);
 339                }
 340                (None, Some(old_hunk)) => {
 341                    start.get_or_insert(old_hunk.buffer_range.start);
 342                    end.replace(old_hunk.buffer_range.end);
 343                    old_cursor.next(new_snapshot);
 344                }
 345                (None, None) => break,
 346            }
 347        }
 348
 349        start.zip(end).map(|(start, end)| start..end)
 350    }
 351}
 352
 353fn compute_hunks(
 354    diff_base: Option<Arc<String>>,
 355    buffer: text::BufferSnapshot,
 356) -> SumTree<InternalDiffHunk> {
 357    let mut tree = SumTree::new(&buffer);
 358
 359    if let Some(diff_base) = diff_base {
 360        let buffer_text = buffer.as_rope().to_string();
 361
 362        let mut options = GitOptions::default();
 363        options.context_lines(0);
 364        let patch = GitPatch::from_buffers(
 365            diff_base.as_bytes(),
 366            None,
 367            buffer_text.as_bytes(),
 368            None,
 369            Some(&mut options),
 370        )
 371        .log_err();
 372
 373        // A common case in Zed is that the empty buffer is represented as just a newline,
 374        // but if we just compute a naive diff you get a "preserved" line in the middle,
 375        // which is a bit odd.
 376        if buffer_text == "\n" && diff_base.ends_with("\n") && diff_base.len() > 1 {
 377            tree.push(
 378                InternalDiffHunk {
 379                    buffer_range: buffer.anchor_before(0)..buffer.anchor_before(0),
 380                    diff_base_byte_range: 0..diff_base.len() - 1,
 381                },
 382                &buffer,
 383            );
 384            return tree;
 385        }
 386
 387        if let Some(patch) = patch {
 388            let mut divergence = 0;
 389            for hunk_index in 0..patch.num_hunks() {
 390                let hunk = process_patch_hunk(&patch, hunk_index, &buffer, &mut divergence);
 391                tree.push(hunk, &buffer);
 392            }
 393        }
 394    }
 395
 396    tree
 397}
 398
 399fn process_patch_hunk(
 400    patch: &GitPatch<'_>,
 401    hunk_index: usize,
 402    buffer: &text::BufferSnapshot,
 403    buffer_row_divergence: &mut i64,
 404) -> InternalDiffHunk {
 405    let line_item_count = patch.num_lines_in_hunk(hunk_index).unwrap();
 406    assert!(line_item_count > 0);
 407
 408    let mut first_deletion_buffer_row: Option<u32> = None;
 409    let mut buffer_row_range: Option<Range<u32>> = None;
 410    let mut diff_base_byte_range: Option<Range<usize>> = None;
 411
 412    for line_index in 0..line_item_count {
 413        let line = patch.line_in_hunk(hunk_index, line_index).unwrap();
 414        let kind = line.origin_value();
 415        let content_offset = line.content_offset() as isize;
 416        let content_len = line.content().len() as isize;
 417
 418        if kind == GitDiffLineType::Addition {
 419            *buffer_row_divergence += 1;
 420            let row = line.new_lineno().unwrap().saturating_sub(1);
 421
 422            match &mut buffer_row_range {
 423                Some(buffer_row_range) => buffer_row_range.end = row + 1,
 424                None => buffer_row_range = Some(row..row + 1),
 425            }
 426        }
 427
 428        if kind == GitDiffLineType::Deletion {
 429            let end = content_offset + content_len;
 430
 431            match &mut diff_base_byte_range {
 432                Some(head_byte_range) => head_byte_range.end = end as usize,
 433                None => diff_base_byte_range = Some(content_offset as usize..end as usize),
 434            }
 435
 436            if first_deletion_buffer_row.is_none() {
 437                let old_row = line.old_lineno().unwrap().saturating_sub(1);
 438                let row = old_row as i64 + *buffer_row_divergence;
 439                first_deletion_buffer_row = Some(row as u32);
 440            }
 441
 442            *buffer_row_divergence -= 1;
 443        }
 444    }
 445
 446    //unwrap_or deletion without addition
 447    let buffer_row_range = buffer_row_range.unwrap_or_else(|| {
 448        //we cannot have an addition-less hunk without deletion(s) or else there would be no hunk
 449        let row = first_deletion_buffer_row.unwrap();
 450        row..row
 451    });
 452
 453    //unwrap_or addition without deletion
 454    let diff_base_byte_range = diff_base_byte_range.unwrap_or(0..0);
 455
 456    let start = Point::new(buffer_row_range.start, 0);
 457    let end = Point::new(buffer_row_range.end, 0);
 458    let buffer_range = buffer.anchor_before(start)..buffer.anchor_before(end);
 459    InternalDiffHunk {
 460        buffer_range,
 461        diff_base_byte_range,
 462    }
 463}
 464
 465impl std::fmt::Debug for BufferDiff {
 466    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 467        f.debug_struct("BufferChangeSet")
 468            .field("buffer_id", &self.buffer_id)
 469            .field("snapshot", &self.inner)
 470            .finish()
 471    }
 472}
 473
 474pub enum BufferDiffEvent {
 475    DiffChanged {
 476        changed_range: Option<Range<text::Anchor>>,
 477    },
 478    LanguageChanged,
 479}
 480
 481impl EventEmitter<BufferDiffEvent> for BufferDiff {}
 482
 483impl BufferDiff {
 484    #[cfg(test)]
 485    fn build_sync(
 486        buffer: text::BufferSnapshot,
 487        diff_base: String,
 488        cx: &mut gpui::TestAppContext,
 489    ) -> BufferDiffInner {
 490        let snapshot =
 491            cx.update(|cx| Self::build(buffer, Some(Arc::new(diff_base)), None, None, cx));
 492        cx.executor().block(snapshot)
 493    }
 494
 495    fn build(
 496        buffer: text::BufferSnapshot,
 497        diff_base: Option<Arc<String>>,
 498        language: Option<Arc<Language>>,
 499        language_registry: Option<Arc<LanguageRegistry>>,
 500        cx: &mut App,
 501    ) -> impl Future<Output = BufferDiffInner> {
 502        let base_text_snapshot = diff_base.as_ref().map(|base_text| {
 503            language::Buffer::build_snapshot(
 504                Rope::from(base_text.as_str()),
 505                language.clone(),
 506                language_registry.clone(),
 507                cx,
 508            )
 509        });
 510        let base_text_snapshot = cx
 511            .background_executor()
 512            .spawn(OptionFuture::from(base_text_snapshot));
 513
 514        let hunks = cx.background_executor().spawn({
 515            let buffer = buffer.clone();
 516            async move { compute_hunks(diff_base, buffer) }
 517        });
 518
 519        async move {
 520            let (base_text, hunks) = futures::join!(base_text_snapshot, hunks);
 521            BufferDiffInner { base_text, hunks }
 522        }
 523    }
 524
 525    fn build_with_base_buffer(
 526        buffer: text::BufferSnapshot,
 527        diff_base: Option<Arc<String>>,
 528        diff_base_buffer: Option<language::BufferSnapshot>,
 529        cx: &App,
 530    ) -> impl Future<Output = BufferDiffInner> {
 531        cx.background_executor().spawn(async move {
 532            BufferDiffInner {
 533                hunks: compute_hunks(diff_base, buffer),
 534                base_text: diff_base_buffer,
 535            }
 536        })
 537    }
 538
 539    fn build_empty(buffer: &text::BufferSnapshot) -> BufferDiffInner {
 540        BufferDiffInner {
 541            hunks: SumTree::new(buffer),
 542            base_text: None,
 543        }
 544    }
 545
 546    pub fn build_with_single_insertion(
 547        insertion_present_in_secondary_diff: bool,
 548        cx: &mut App,
 549    ) -> BufferDiffSnapshot {
 550        let base_text = language::Buffer::build_empty_snapshot(cx);
 551        let hunks = SumTree::from_item(
 552            InternalDiffHunk {
 553                buffer_range: Anchor::MIN..Anchor::MAX,
 554                diff_base_byte_range: 0..0,
 555            },
 556            &base_text,
 557        );
 558        BufferDiffSnapshot {
 559            inner: BufferDiffInner {
 560                hunks: hunks.clone(),
 561                base_text: Some(base_text.clone()),
 562            },
 563            secondary_diff: if insertion_present_in_secondary_diff {
 564                Some(Box::new(BufferDiffSnapshot {
 565                    inner: BufferDiffInner {
 566                        hunks,
 567                        base_text: Some(base_text),
 568                    },
 569                    secondary_diff: None,
 570                }))
 571            } else {
 572                None
 573            },
 574        }
 575    }
 576
 577    pub fn set_secondary_diff(&mut self, diff: Entity<BufferDiff>) {
 578        self.secondary_diff = Some(diff);
 579    }
 580
 581    pub fn secondary_diff(&self) -> Option<Entity<BufferDiff>> {
 582        Some(self.secondary_diff.as_ref()?.clone())
 583    }
 584
 585    pub fn range_to_hunk_range(
 586        &self,
 587        range: Range<Anchor>,
 588        buffer: &text::BufferSnapshot,
 589        cx: &App,
 590    ) -> Range<Anchor> {
 591        let start = self
 592            .hunks_intersecting_range(range.clone(), &buffer, cx)
 593            .next()
 594            .map_or(Anchor::MIN, |hunk| hunk.buffer_range.start);
 595        let end = self
 596            .hunks_intersecting_range_rev(range.clone(), &buffer)
 597            .next()
 598            .map_or(Anchor::MAX, |hunk| hunk.buffer_range.end);
 599        start..end
 600    }
 601
 602    #[allow(clippy::too_many_arguments)]
 603    pub async fn update_diff(
 604        this: Entity<BufferDiff>,
 605        buffer: text::BufferSnapshot,
 606        base_text: Option<Arc<String>>,
 607        base_text_changed: bool,
 608        language_changed: bool,
 609        language: Option<Arc<Language>>,
 610        language_registry: Option<Arc<LanguageRegistry>>,
 611        cx: &mut AsyncApp,
 612    ) -> anyhow::Result<Option<Range<Anchor>>> {
 613        let snapshot = if base_text_changed || language_changed {
 614            cx.update(|cx| {
 615                Self::build(
 616                    buffer.clone(),
 617                    base_text,
 618                    language.clone(),
 619                    language_registry.clone(),
 620                    cx,
 621                )
 622            })?
 623            .await
 624        } else {
 625            this.read_with(cx, |this, cx| {
 626                Self::build_with_base_buffer(
 627                    buffer.clone(),
 628                    base_text,
 629                    this.base_text().cloned(),
 630                    cx,
 631                )
 632            })?
 633            .await
 634        };
 635
 636        this.update(cx, |this, _| this.set_state(snapshot, &buffer))
 637    }
 638
 639    pub fn update_diff_from(
 640        &mut self,
 641        buffer: &text::BufferSnapshot,
 642        other: &Entity<Self>,
 643        cx: &mut Context<Self>,
 644    ) -> Option<Range<Anchor>> {
 645        let other = other.read(cx).inner.clone();
 646        self.set_state(other, buffer)
 647    }
 648
 649    fn set_state(
 650        &mut self,
 651        inner: BufferDiffInner,
 652        buffer: &text::BufferSnapshot,
 653    ) -> Option<Range<Anchor>> {
 654        let changed_range = match (self.inner.base_text.as_ref(), inner.base_text.as_ref()) {
 655            (None, None) => None,
 656            (Some(old), Some(new)) if old.remote_id() == new.remote_id() => {
 657                inner.compare(&self.inner, buffer)
 658            }
 659            _ => Some(text::Anchor::MIN..text::Anchor::MAX),
 660        };
 661        self.inner = inner;
 662        changed_range
 663    }
 664
 665    pub fn base_text(&self) -> Option<&language::BufferSnapshot> {
 666        self.inner.base_text.as_ref()
 667    }
 668
 669    pub fn snapshot(&self, cx: &App) -> BufferDiffSnapshot {
 670        BufferDiffSnapshot {
 671            inner: self.inner.clone(),
 672            secondary_diff: self
 673                .secondary_diff
 674                .as_ref()
 675                .map(|diff| Box::new(diff.read(cx).snapshot(cx))),
 676        }
 677    }
 678
 679    pub fn hunks_intersecting_range<'a>(
 680        &'a self,
 681        range: Range<text::Anchor>,
 682        buffer_snapshot: &'a text::BufferSnapshot,
 683        cx: &'a App,
 684    ) -> impl 'a + Iterator<Item = DiffHunk> {
 685        let unstaged_counterpart = self
 686            .secondary_diff
 687            .as_ref()
 688            .map(|diff| &diff.read(cx).inner);
 689        self.inner
 690            .hunks_intersecting_range(range, buffer_snapshot, unstaged_counterpart)
 691    }
 692
 693    pub fn hunks_intersecting_range_rev<'a>(
 694        &'a self,
 695        range: Range<text::Anchor>,
 696        buffer_snapshot: &'a text::BufferSnapshot,
 697    ) -> impl 'a + Iterator<Item = DiffHunk> {
 698        self.inner
 699            .hunks_intersecting_range_rev(range, buffer_snapshot)
 700    }
 701
 702    pub fn hunks_in_row_range<'a>(
 703        &'a self,
 704        range: Range<u32>,
 705        buffer: &'a text::BufferSnapshot,
 706        cx: &'a App,
 707    ) -> impl 'a + Iterator<Item = DiffHunk> {
 708        let start = buffer.anchor_before(Point::new(range.start, 0));
 709        let end = buffer.anchor_after(Point::new(range.end, 0));
 710        self.hunks_intersecting_range(start..end, buffer, cx)
 711    }
 712
 713    /// Used in cases where the change set isn't derived from git.
 714    pub fn set_base_text(
 715        &mut self,
 716        base_buffer: Entity<language::Buffer>,
 717        buffer: text::BufferSnapshot,
 718        cx: &mut Context<Self>,
 719    ) -> oneshot::Receiver<()> {
 720        let (tx, rx) = oneshot::channel();
 721        let this = cx.weak_entity();
 722        let base_buffer = base_buffer.read(cx);
 723        let language_registry = base_buffer.language_registry();
 724        let base_buffer = base_buffer.snapshot();
 725        let base_text = Arc::new(base_buffer.text());
 726
 727        let snapshot = BufferDiff::build(
 728            buffer.clone(),
 729            Some(base_text),
 730            base_buffer.language().cloned(),
 731            language_registry,
 732            cx,
 733        );
 734        let complete_on_drop = util::defer(|| {
 735            tx.send(()).ok();
 736        });
 737        cx.spawn(|_, mut cx| async move {
 738            let snapshot = snapshot.await;
 739            let Some(this) = this.upgrade() else {
 740                return;
 741            };
 742            this.update(&mut cx, |this, _| {
 743                this.set_state(snapshot, &buffer);
 744            })
 745            .log_err();
 746            drop(complete_on_drop)
 747        })
 748        .detach();
 749        rx
 750    }
 751
 752    #[cfg(any(test, feature = "test-support"))]
 753    pub fn base_text_string(&self) -> Option<String> {
 754        self.inner.base_text.as_ref().map(|buffer| buffer.text())
 755    }
 756
 757    pub fn new(buffer: &text::BufferSnapshot) -> Self {
 758        BufferDiff {
 759            buffer_id: buffer.remote_id(),
 760            inner: BufferDiff::build_empty(buffer),
 761            secondary_diff: None,
 762        }
 763    }
 764
 765    #[cfg(any(test, feature = "test-support"))]
 766    pub fn new_with_base_text(
 767        base_text: &str,
 768        buffer: &Entity<language::Buffer>,
 769        cx: &mut App,
 770    ) -> Self {
 771        let mut base_text = base_text.to_owned();
 772        text::LineEnding::normalize(&mut base_text);
 773        let snapshot = BufferDiff::build(
 774            buffer.read(cx).text_snapshot(),
 775            Some(base_text.into()),
 776            None,
 777            None,
 778            cx,
 779        );
 780        let snapshot = cx.background_executor().block(snapshot);
 781        BufferDiff {
 782            buffer_id: buffer.read(cx).remote_id(),
 783            inner: snapshot,
 784            secondary_diff: None,
 785        }
 786    }
 787
 788    #[cfg(any(test, feature = "test-support"))]
 789    pub fn recalculate_diff_sync(&mut self, buffer: text::BufferSnapshot, cx: &mut Context<Self>) {
 790        let base_text = self
 791            .inner
 792            .base_text
 793            .as_ref()
 794            .map(|base_text| base_text.text());
 795        let snapshot = BufferDiff::build_with_base_buffer(
 796            buffer.clone(),
 797            base_text.clone().map(Arc::new),
 798            self.inner.base_text.clone(),
 799            cx,
 800        );
 801        let snapshot = cx.background_executor().block(snapshot);
 802        let changed_range = self.set_state(snapshot, &buffer);
 803        cx.emit(BufferDiffEvent::DiffChanged { changed_range });
 804    }
 805}
 806
 807impl DiffHunk {
 808    pub fn status(&self) -> DiffHunkStatus {
 809        if self.buffer_range.start == self.buffer_range.end {
 810            DiffHunkStatus::Removed(self.secondary_status)
 811        } else if self.diff_base_byte_range.is_empty() {
 812            DiffHunkStatus::Added(self.secondary_status)
 813        } else {
 814            DiffHunkStatus::Modified(self.secondary_status)
 815        }
 816    }
 817}
 818
 819impl DiffHunkStatus {
 820    pub fn is_removed(&self) -> bool {
 821        matches!(self, DiffHunkStatus::Removed(_))
 822    }
 823
 824    #[cfg(any(test, feature = "test-support"))]
 825    pub fn removed() -> Self {
 826        DiffHunkStatus::Removed(DiffHunkSecondaryStatus::None)
 827    }
 828
 829    #[cfg(any(test, feature = "test-support"))]
 830    pub fn added() -> Self {
 831        DiffHunkStatus::Added(DiffHunkSecondaryStatus::None)
 832    }
 833
 834    #[cfg(any(test, feature = "test-support"))]
 835    pub fn modified() -> Self {
 836        DiffHunkStatus::Modified(DiffHunkSecondaryStatus::None)
 837    }
 838}
 839
 840/// Range (crossing new lines), old, new
 841#[cfg(any(test, feature = "test-support"))]
 842#[track_caller]
 843pub fn assert_hunks<Iter>(
 844    diff_hunks: Iter,
 845    buffer: &text::BufferSnapshot,
 846    diff_base: &str,
 847    expected_hunks: &[(Range<u32>, &str, &str, DiffHunkStatus)],
 848) where
 849    Iter: Iterator<Item = DiffHunk>,
 850{
 851    let actual_hunks = diff_hunks
 852        .map(|hunk| {
 853            (
 854                hunk.row_range.clone(),
 855                &diff_base[hunk.diff_base_byte_range.clone()],
 856                buffer
 857                    .text_for_range(
 858                        Point::new(hunk.row_range.start, 0)..Point::new(hunk.row_range.end, 0),
 859                    )
 860                    .collect::<String>(),
 861                hunk.status(),
 862            )
 863        })
 864        .collect::<Vec<_>>();
 865
 866    let expected_hunks: Vec<_> = expected_hunks
 867        .iter()
 868        .map(|(r, s, h, status)| (r.clone(), *s, h.to_string(), *status))
 869        .collect();
 870
 871    assert_eq!(actual_hunks, expected_hunks);
 872}
 873
 874#[cfg(test)]
 875mod tests {
 876    use std::assert_eq;
 877
 878    use super::*;
 879    use gpui::TestAppContext;
 880    use text::{Buffer, BufferId};
 881    use unindent::Unindent as _;
 882
 883    #[gpui::test]
 884    async fn test_buffer_diff_simple(cx: &mut gpui::TestAppContext) {
 885        let diff_base = "
 886            one
 887            two
 888            three
 889        "
 890        .unindent();
 891
 892        let buffer_text = "
 893            one
 894            HELLO
 895            three
 896        "
 897        .unindent();
 898
 899        let mut buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
 900        let mut diff = BufferDiff::build_sync(buffer.clone(), diff_base.clone(), cx);
 901        assert_hunks(
 902            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
 903            &buffer,
 904            &diff_base,
 905            &[(1..2, "two\n", "HELLO\n", DiffHunkStatus::modified())],
 906        );
 907
 908        buffer.edit([(0..0, "point five\n")]);
 909        diff = BufferDiff::build_sync(buffer.clone(), diff_base.clone(), cx);
 910        assert_hunks(
 911            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
 912            &buffer,
 913            &diff_base,
 914            &[
 915                (0..1, "", "point five\n", DiffHunkStatus::added()),
 916                (2..3, "two\n", "HELLO\n", DiffHunkStatus::modified()),
 917            ],
 918        );
 919
 920        diff = BufferDiff::build_empty(&buffer);
 921        assert_hunks(
 922            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
 923            &buffer,
 924            &diff_base,
 925            &[],
 926        );
 927    }
 928
 929    #[gpui::test]
 930    async fn test_buffer_diff_with_secondary(cx: &mut gpui::TestAppContext) {
 931        let head_text = "
 932            zero
 933            one
 934            two
 935            three
 936            four
 937            five
 938            six
 939            seven
 940            eight
 941            nine
 942        "
 943        .unindent();
 944
 945        let index_text = "
 946            zero
 947            one
 948            TWO
 949            three
 950            FOUR
 951            five
 952            six
 953            seven
 954            eight
 955            NINE
 956        "
 957        .unindent();
 958
 959        let buffer_text = "
 960            zero
 961            one
 962            TWO
 963            three
 964            FOUR
 965            FIVE
 966            six
 967            SEVEN
 968            eight
 969            nine
 970        "
 971        .unindent();
 972
 973        let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
 974        let unstaged_diff = BufferDiff::build_sync(buffer.clone(), index_text.clone(), cx);
 975
 976        let uncommitted_diff = BufferDiff::build_sync(buffer.clone(), head_text.clone(), cx);
 977
 978        let expected_hunks = vec![
 979            (
 980                2..3,
 981                "two\n",
 982                "TWO\n",
 983                DiffHunkStatus::Modified(DiffHunkSecondaryStatus::None),
 984            ),
 985            (
 986                4..6,
 987                "four\nfive\n",
 988                "FOUR\nFIVE\n",
 989                DiffHunkStatus::Modified(DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk),
 990            ),
 991            (
 992                7..8,
 993                "seven\n",
 994                "SEVEN\n",
 995                DiffHunkStatus::Modified(DiffHunkSecondaryStatus::HasSecondaryHunk),
 996            ),
 997        ];
 998
 999        assert_hunks(
1000            uncommitted_diff.hunks_intersecting_range(
1001                Anchor::MIN..Anchor::MAX,
1002                &buffer,
1003                Some(&unstaged_diff),
1004            ),
1005            &buffer,
1006            &head_text,
1007            &expected_hunks,
1008        );
1009    }
1010
1011    #[gpui::test]
1012    async fn test_buffer_diff_range(cx: &mut TestAppContext) {
1013        let diff_base = Arc::new(
1014            "
1015            one
1016            two
1017            three
1018            four
1019            five
1020            six
1021            seven
1022            eight
1023            nine
1024            ten
1025        "
1026            .unindent(),
1027        );
1028
1029        let buffer_text = "
1030            A
1031            one
1032            B
1033            two
1034            C
1035            three
1036            HELLO
1037            four
1038            five
1039            SIXTEEN
1040            seven
1041            eight
1042            WORLD
1043            nine
1044
1045            ten
1046
1047        "
1048        .unindent();
1049
1050        let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1051        let diff = cx
1052            .update(|cx| {
1053                BufferDiff::build(buffer.snapshot(), Some(diff_base.clone()), None, None, cx)
1054            })
1055            .await;
1056        assert_eq!(
1057            diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None)
1058                .count(),
1059            8
1060        );
1061
1062        assert_hunks(
1063            diff.hunks_intersecting_range(
1064                buffer.anchor_before(Point::new(7, 0))..buffer.anchor_before(Point::new(12, 0)),
1065                &buffer,
1066                None,
1067            ),
1068            &buffer,
1069            &diff_base,
1070            &[
1071                (6..7, "", "HELLO\n", DiffHunkStatus::added()),
1072                (9..10, "six\n", "SIXTEEN\n", DiffHunkStatus::modified()),
1073                (12..13, "", "WORLD\n", DiffHunkStatus::added()),
1074            ],
1075        );
1076    }
1077
1078    #[gpui::test]
1079    async fn test_buffer_diff_compare(cx: &mut TestAppContext) {
1080        let base_text = "
1081            zero
1082            one
1083            two
1084            three
1085            four
1086            five
1087            six
1088            seven
1089            eight
1090            nine
1091        "
1092        .unindent();
1093
1094        let buffer_text_1 = "
1095            one
1096            three
1097            four
1098            five
1099            SIX
1100            seven
1101            eight
1102            NINE
1103        "
1104        .unindent();
1105
1106        let mut buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text_1);
1107
1108        let empty_diff = BufferDiff::build_empty(&buffer);
1109        let diff_1 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1110        let range = diff_1.compare(&empty_diff, &buffer).unwrap();
1111        assert_eq!(range.to_point(&buffer), Point::new(0, 0)..Point::new(8, 0));
1112
1113        // Edit does not affect the diff.
1114        buffer.edit_via_marked_text(
1115            &"
1116                one
1117                three
1118                four
1119                five
1120                «SIX.5»
1121                seven
1122                eight
1123                NINE
1124            "
1125            .unindent(),
1126        );
1127        let diff_2 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1128        assert_eq!(None, diff_2.compare(&diff_1, &buffer));
1129
1130        // Edit turns a deletion hunk into a modification.
1131        buffer.edit_via_marked_text(
1132            &"
1133                one
1134                «THREE»
1135                four
1136                five
1137                SIX.5
1138                seven
1139                eight
1140                NINE
1141            "
1142            .unindent(),
1143        );
1144        let diff_3 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1145        let range = diff_3.compare(&diff_2, &buffer).unwrap();
1146        assert_eq!(range.to_point(&buffer), Point::new(1, 0)..Point::new(2, 0));
1147
1148        // Edit turns a modification hunk into a deletion.
1149        buffer.edit_via_marked_text(
1150            &"
1151                one
1152                THREE
1153                four
1154                five«»
1155                seven
1156                eight
1157                NINE
1158            "
1159            .unindent(),
1160        );
1161        let diff_4 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1162        let range = diff_4.compare(&diff_3, &buffer).unwrap();
1163        assert_eq!(range.to_point(&buffer), Point::new(3, 4)..Point::new(4, 0));
1164
1165        // Edit introduces a new insertion hunk.
1166        buffer.edit_via_marked_text(
1167            &"
1168                one
1169                THREE
1170                four«
1171                FOUR.5
1172                »five
1173                seven
1174                eight
1175                NINE
1176            "
1177            .unindent(),
1178        );
1179        let diff_5 = BufferDiff::build_sync(buffer.snapshot(), base_text.clone(), cx);
1180        let range = diff_5.compare(&diff_4, &buffer).unwrap();
1181        assert_eq!(range.to_point(&buffer), Point::new(3, 0)..Point::new(4, 0));
1182
1183        // Edit removes a hunk.
1184        buffer.edit_via_marked_text(
1185            &"
1186                one
1187                THREE
1188                four
1189                FOUR.5
1190                five
1191                seven
1192                eight
1193                «nine»
1194            "
1195            .unindent(),
1196        );
1197        let diff_6 = BufferDiff::build_sync(buffer.snapshot(), base_text, cx);
1198        let range = diff_6.compare(&diff_5, &buffer).unwrap();
1199        assert_eq!(range.to_point(&buffer), Point::new(7, 0)..Point::new(8, 0));
1200    }
1201}