multi_buffer_tests.rs

   1use super::*;
   2use buffer_diff::{DiffHunkStatus, DiffHunkStatusKind};
   3use gpui::{App, TestAppContext};
   4use indoc::indoc;
   5use language::{Buffer, Rope};
   6use parking_lot::RwLock;
   7use rand::prelude::*;
   8use settings::SettingsStore;
   9use std::env;
  10use std::time::{Duration, Instant};
  11use util::RandomCharIter;
  12use util::rel_path::rel_path;
  13use util::test::sample_text;
  14
  15#[ctor::ctor]
  16fn init_logger() {
  17    zlog::init_test();
  18}
  19
  20#[gpui::test]
  21fn test_empty_singleton(cx: &mut App) {
  22    let buffer = cx.new(|cx| Buffer::local("", cx));
  23    let buffer_id = buffer.read(cx).remote_id();
  24    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
  25    let snapshot = multibuffer.read(cx).snapshot(cx);
  26    assert_eq!(snapshot.text(), "");
  27    assert_eq!(
  28        snapshot.row_infos(MultiBufferRow(0)).collect::<Vec<_>>(),
  29        [RowInfo {
  30            buffer_id: Some(buffer_id),
  31            buffer_row: Some(0),
  32            multibuffer_row: Some(MultiBufferRow(0)),
  33            diff_status: None,
  34            expand_info: None,
  35            wrapped_buffer_row: None,
  36        }]
  37    );
  38}
  39
  40#[gpui::test]
  41fn test_singleton(cx: &mut App) {
  42    let buffer = cx.new(|cx| Buffer::local(sample_text(6, 6, 'a'), cx));
  43    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
  44
  45    let snapshot = multibuffer.read(cx).snapshot(cx);
  46    assert_eq!(snapshot.text(), buffer.read(cx).text());
  47
  48    assert_eq!(
  49        snapshot
  50            .row_infos(MultiBufferRow(0))
  51            .map(|info| info.buffer_row)
  52            .collect::<Vec<_>>(),
  53        (0..buffer.read(cx).row_count())
  54            .map(Some)
  55            .collect::<Vec<_>>()
  56    );
  57    assert_consistent_line_numbers(&snapshot);
  58
  59    buffer.update(cx, |buffer, cx| buffer.edit([(1..3, "XXX\n")], None, cx));
  60    let snapshot = multibuffer.read(cx).snapshot(cx);
  61
  62    assert_eq!(snapshot.text(), buffer.read(cx).text());
  63    assert_eq!(
  64        snapshot
  65            .row_infos(MultiBufferRow(0))
  66            .map(|info| info.buffer_row)
  67            .collect::<Vec<_>>(),
  68        (0..buffer.read(cx).row_count())
  69            .map(Some)
  70            .collect::<Vec<_>>()
  71    );
  72    assert_consistent_line_numbers(&snapshot);
  73}
  74
  75#[gpui::test]
  76fn test_remote(cx: &mut App) {
  77    let host_buffer = cx.new(|cx| Buffer::local("a", cx));
  78    let guest_buffer = cx.new(|cx| {
  79        let state = host_buffer.read(cx).to_proto(cx);
  80        let ops = cx
  81            .foreground_executor()
  82            .block_on(host_buffer.read(cx).serialize_ops(None, cx));
  83        let mut buffer =
  84            Buffer::from_proto(ReplicaId::REMOTE_SERVER, Capability::ReadWrite, state, None)
  85                .unwrap();
  86        buffer.apply_ops(
  87            ops.into_iter()
  88                .map(|op| language::proto::deserialize_operation(op).unwrap()),
  89            cx,
  90        );
  91        buffer
  92    });
  93    let multibuffer = cx.new(|cx| MultiBuffer::singleton(guest_buffer.clone(), cx));
  94    let snapshot = multibuffer.read(cx).snapshot(cx);
  95    assert_eq!(snapshot.text(), "a");
  96
  97    guest_buffer.update(cx, |buffer, cx| buffer.edit([(1..1, "b")], None, cx));
  98    let snapshot = multibuffer.read(cx).snapshot(cx);
  99    assert_eq!(snapshot.text(), "ab");
 100
 101    guest_buffer.update(cx, |buffer, cx| buffer.edit([(2..2, "c")], None, cx));
 102    let snapshot = multibuffer.read(cx).snapshot(cx);
 103    assert_eq!(snapshot.text(), "abc");
 104}
 105
 106#[gpui::test]
 107fn test_excerpt_boundaries_and_clipping(cx: &mut App) {
 108    let buffer_1 = cx.new(|cx| Buffer::local(sample_text(7, 6, 'a'), cx));
 109    let buffer_2 = cx.new(|cx| Buffer::local(sample_text(7, 6, 'g'), cx));
 110    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
 111
 112    let events = Arc::new(RwLock::new(Vec::<Event>::new()));
 113    multibuffer.update(cx, |_, cx| {
 114        let events = events.clone();
 115        cx.subscribe(&multibuffer, move |_, _, event, _| {
 116            if let Event::Edited { .. } = event {
 117                events.write().push(event.clone())
 118            }
 119        })
 120        .detach();
 121    });
 122
 123    let subscription = multibuffer.update(cx, |multibuffer, cx| {
 124        let subscription = multibuffer.subscribe();
 125        multibuffer.set_excerpt_ranges_for_path(
 126            PathKey::sorted(0),
 127            buffer_1.clone(),
 128            &buffer_1.read(cx).snapshot(),
 129            vec![ExcerptRange::new(Point::new(1, 2)..Point::new(2, 5))],
 130            cx,
 131        );
 132        assert_eq!(
 133            subscription.consume().into_inner(),
 134            [Edit {
 135                old: MultiBufferOffset(0)..MultiBufferOffset(0),
 136                new: MultiBufferOffset(0)..MultiBufferOffset(10)
 137            }]
 138        );
 139
 140        multibuffer.set_excerpt_ranges_for_path(
 141            PathKey::sorted(0),
 142            buffer_1.clone(),
 143            &buffer_1.read(cx).snapshot(),
 144            vec![
 145                ExcerptRange::new(Point::new(1, 2)..Point::new(2, 5)),
 146                ExcerptRange::new(Point::new(5, 3)..Point::new(6, 4)),
 147            ],
 148            cx,
 149        );
 150        multibuffer.set_excerpt_ranges_for_path(
 151            PathKey::sorted(1),
 152            buffer_2.clone(),
 153            &buffer_2.read(cx).snapshot(),
 154            vec![ExcerptRange::new(Point::new(3, 1)..Point::new(3, 3))],
 155            cx,
 156        );
 157        assert_eq!(
 158            subscription.consume().into_inner(),
 159            [Edit {
 160                old: MultiBufferOffset(10)..MultiBufferOffset(10),
 161                new: MultiBufferOffset(10)..MultiBufferOffset(22)
 162            }]
 163        );
 164
 165        subscription
 166    });
 167
 168    // Adding excerpts emits an edited event.
 169    assert_eq!(
 170        events.read().as_slice(),
 171        &[
 172            Event::Edited {
 173                edited_buffer: None,
 174            },
 175            Event::Edited {
 176                edited_buffer: None,
 177            },
 178            Event::Edited {
 179                edited_buffer: None,
 180            }
 181        ]
 182    );
 183
 184    let snapshot = multibuffer.read(cx).snapshot(cx);
 185    assert_eq!(
 186        snapshot.text(),
 187        indoc!(
 188            "
 189            bbbb
 190            ccccc
 191            fff
 192            gggg
 193            jj"
 194        ),
 195    );
 196    assert_eq!(
 197        snapshot
 198            .row_infos(MultiBufferRow(0))
 199            .map(|info| info.buffer_row)
 200            .collect::<Vec<_>>(),
 201        [Some(1), Some(2), Some(5), Some(6), Some(3)]
 202    );
 203    assert_eq!(
 204        snapshot
 205            .row_infos(MultiBufferRow(2))
 206            .map(|info| info.buffer_row)
 207            .collect::<Vec<_>>(),
 208        [Some(5), Some(6), Some(3)]
 209    );
 210    assert_eq!(
 211        snapshot
 212            .row_infos(MultiBufferRow(4))
 213            .map(|info| info.buffer_row)
 214            .collect::<Vec<_>>(),
 215        [Some(3)]
 216    );
 217    assert!(
 218        snapshot
 219            .row_infos(MultiBufferRow(5))
 220            .map(|info| info.buffer_row)
 221            .collect::<Vec<_>>()
 222            .is_empty()
 223    );
 224
 225    assert_eq!(
 226        boundaries_in_range(Point::new(0, 0)..Point::new(4, 2), &snapshot),
 227        &[
 228            (MultiBufferRow(0), "bbbb\nccccc".to_string(), true),
 229            (MultiBufferRow(2), "fff\ngggg".to_string(), false),
 230            (MultiBufferRow(4), "jj".to_string(), true),
 231        ]
 232    );
 233    assert_eq!(
 234        boundaries_in_range(Point::new(0, 0)..Point::new(2, 0), &snapshot),
 235        &[(MultiBufferRow(0), "bbbb\nccccc".to_string(), true)]
 236    );
 237    assert_eq!(
 238        boundaries_in_range(Point::new(1, 0)..Point::new(1, 5), &snapshot),
 239        &[]
 240    );
 241    assert_eq!(
 242        boundaries_in_range(Point::new(1, 0)..Point::new(2, 0), &snapshot),
 243        &[]
 244    );
 245    assert_eq!(
 246        boundaries_in_range(Point::new(1, 0)..Point::new(4, 0), &snapshot),
 247        &[(MultiBufferRow(2), "fff\ngggg".to_string(), false)]
 248    );
 249    assert_eq!(
 250        boundaries_in_range(Point::new(1, 0)..Point::new(4, 0), &snapshot),
 251        &[(MultiBufferRow(2), "fff\ngggg".to_string(), false)]
 252    );
 253    assert_eq!(
 254        boundaries_in_range(Point::new(2, 0)..Point::new(3, 0), &snapshot),
 255        &[(MultiBufferRow(2), "fff\ngggg".to_string(), false)]
 256    );
 257    assert_eq!(
 258        boundaries_in_range(Point::new(4, 0)..Point::new(4, 2), &snapshot),
 259        &[(MultiBufferRow(4), "jj".to_string(), true)]
 260    );
 261    assert_eq!(
 262        boundaries_in_range(Point::new(4, 2)..Point::new(4, 2), &snapshot),
 263        &[]
 264    );
 265
 266    buffer_1.update(cx, |buffer, cx| {
 267        let text = "\n";
 268        buffer.edit(
 269            [
 270                (Point::new(0, 0)..Point::new(0, 0), text),
 271                (Point::new(2, 1)..Point::new(2, 3), text),
 272            ],
 273            None,
 274            cx,
 275        );
 276    });
 277
 278    let snapshot = multibuffer.read(cx).snapshot(cx);
 279    assert_eq!(
 280        snapshot.text(),
 281        concat!(
 282            "bbbb\n", // Preserve newlines
 283            "c\n",    //
 284            "cc\n",   //
 285            "fff\n",  //
 286            "gggg\n", //
 287            "jj"      //
 288        )
 289    );
 290
 291    assert_eq!(
 292        subscription.consume().into_inner(),
 293        [Edit {
 294            old: MultiBufferOffset(6)..MultiBufferOffset(8),
 295            new: MultiBufferOffset(6)..MultiBufferOffset(7)
 296        }]
 297    );
 298
 299    let snapshot = multibuffer.read(cx).snapshot(cx);
 300    assert_eq!(
 301        snapshot.clip_point(Point::new(0, 5), Bias::Left),
 302        Point::new(0, 4)
 303    );
 304    assert_eq!(
 305        snapshot.clip_point(Point::new(0, 5), Bias::Right),
 306        Point::new(0, 4)
 307    );
 308    assert_eq!(
 309        snapshot.clip_point(Point::new(5, 1), Bias::Right),
 310        Point::new(5, 1)
 311    );
 312    assert_eq!(
 313        snapshot.clip_point(Point::new(5, 2), Bias::Right),
 314        Point::new(5, 2)
 315    );
 316    assert_eq!(
 317        snapshot.clip_point(Point::new(5, 3), Bias::Right),
 318        Point::new(5, 2)
 319    );
 320
 321    let snapshot = multibuffer.update(cx, |multibuffer, cx| {
 322        multibuffer.remove_excerpts_for_path(PathKey::sorted(1), cx);
 323        multibuffer.snapshot(cx)
 324    });
 325
 326    assert_eq!(
 327        snapshot.text(),
 328        concat!(
 329            "bbbb\n", // Preserve newlines
 330            "c\n",    //
 331            "cc\n",   //
 332            "fff\n",  //
 333            "gggg",   //
 334        )
 335    );
 336
 337    fn boundaries_in_range(
 338        range: Range<Point>,
 339        snapshot: &MultiBufferSnapshot,
 340    ) -> Vec<(MultiBufferRow, String, bool)> {
 341        snapshot
 342            .excerpt_boundaries_in_range(range)
 343            .map(|boundary| {
 344                let starts_new_buffer = boundary.starts_new_buffer();
 345                (
 346                    boundary.row,
 347                    boundary
 348                        .next
 349                        .buffer
 350                        .text_for_range(boundary.next.range.context)
 351                        .collect::<String>(),
 352                    starts_new_buffer,
 353                )
 354            })
 355            .collect::<Vec<_>>()
 356    }
 357}
 358
 359#[gpui::test]
 360async fn test_diff_boundary_anchors(cx: &mut TestAppContext) {
 361    let base_text = "one\ntwo\nthree\n";
 362    let text = "one\nthree\n";
 363    let buffer = cx.new(|cx| Buffer::local(text, cx));
 364    let diff = cx
 365        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
 366    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
 367    multibuffer.update(cx, |multibuffer, cx| multibuffer.add_diff(diff, cx));
 368
 369    let (before, after) = multibuffer.update(cx, |multibuffer, cx| {
 370        let before = multibuffer.snapshot(cx).anchor_before(Point::new(1, 0));
 371        let after = multibuffer.snapshot(cx).anchor_after(Point::new(1, 0));
 372        multibuffer.set_all_diff_hunks_expanded(cx);
 373        (before, after)
 374    });
 375    cx.run_until_parked();
 376
 377    let snapshot = multibuffer.read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx));
 378    let actual_text = snapshot.text();
 379    let actual_row_infos = snapshot.row_infos(MultiBufferRow(0)).collect::<Vec<_>>();
 380    let actual_diff = format_diff(&actual_text, &actual_row_infos, &Default::default(), None);
 381    pretty_assertions::assert_eq!(
 382        actual_diff,
 383        indoc! {
 384            "  one
 385             - two
 386               three
 387             "
 388        },
 389    );
 390
 391    multibuffer.update(cx, |multibuffer, cx| {
 392        let snapshot = multibuffer.snapshot(cx);
 393        assert_eq!(before.to_point(&snapshot), Point::new(1, 0));
 394        assert_eq!(after.to_point(&snapshot), Point::new(2, 0));
 395        assert_eq!(
 396            vec![Point::new(1, 0), Point::new(2, 0),],
 397            snapshot.summaries_for_anchors::<Point, _>(&[before, after]),
 398        )
 399    })
 400}
 401
 402#[gpui::test]
 403async fn test_diff_hunks_in_range(cx: &mut TestAppContext) {
 404    let base_text = "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n";
 405    let text = "one\nfour\nseven\n";
 406    let buffer = cx.new(|cx| Buffer::local(text, cx));
 407    let diff = cx
 408        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
 409    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
 410    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
 411        (multibuffer.snapshot(cx), multibuffer.subscribe())
 412    });
 413
 414    multibuffer.update(cx, |multibuffer, cx| {
 415        multibuffer.add_diff(diff, cx);
 416        multibuffer.expand_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
 417    });
 418
 419    assert_new_snapshot(
 420        &multibuffer,
 421        &mut snapshot,
 422        &mut subscription,
 423        cx,
 424        indoc! {
 425            "  one
 426             - two
 427             - three
 428               four
 429             - five
 430             - six
 431               seven
 432             - eight
 433            "
 434        },
 435    );
 436
 437    assert_eq!(
 438        snapshot
 439            .diff_hunks_in_range(Point::new(1, 0)..Point::MAX)
 440            .map(|hunk| hunk.row_range.start.0..hunk.row_range.end.0)
 441            .collect::<Vec<_>>(),
 442        vec![1..3, 4..6, 7..8]
 443    );
 444
 445    assert_eq!(snapshot.diff_hunk_before(Point::new(1, 1)), None,);
 446    assert_eq!(
 447        snapshot.diff_hunk_before(Point::new(7, 0)),
 448        Some(MultiBufferRow(4))
 449    );
 450    assert_eq!(
 451        snapshot.diff_hunk_before(Point::new(4, 0)),
 452        Some(MultiBufferRow(1))
 453    );
 454
 455    multibuffer.update(cx, |multibuffer, cx| {
 456        multibuffer.collapse_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
 457    });
 458
 459    assert_new_snapshot(
 460        &multibuffer,
 461        &mut snapshot,
 462        &mut subscription,
 463        cx,
 464        indoc! {
 465            "
 466            one
 467            four
 468            seven
 469            "
 470        },
 471    );
 472
 473    assert_eq!(
 474        snapshot.diff_hunk_before(Point::new(2, 0)),
 475        Some(MultiBufferRow(1)),
 476    );
 477    assert_eq!(
 478        snapshot.diff_hunk_before(Point::new(4, 0)),
 479        Some(MultiBufferRow(2))
 480    );
 481}
 482
 483#[gpui::test]
 484async fn test_diff_hunks_in_range_query_starting_at_added_row(cx: &mut TestAppContext) {
 485    let base_text = "one\ntwo\nthree\n";
 486    let text = "one\nTWO\nthree\n";
 487    let buffer = cx.new(|cx| Buffer::local(text, cx));
 488    let diff = cx
 489        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
 490    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
 491    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
 492        (multibuffer.snapshot(cx), multibuffer.subscribe())
 493    });
 494
 495    multibuffer.update(cx, |multibuffer, cx| {
 496        multibuffer.add_diff(diff, cx);
 497        multibuffer.expand_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
 498    });
 499
 500    assert_new_snapshot(
 501        &multibuffer,
 502        &mut snapshot,
 503        &mut subscription,
 504        cx,
 505        indoc! {
 506            "  one
 507             - two
 508             + TWO
 509               three
 510            "
 511        },
 512    );
 513
 514    assert_eq!(
 515        snapshot
 516            .diff_hunks_in_range(Point::new(2, 0)..Point::MAX)
 517            .map(|hunk| hunk.row_range.start.0..hunk.row_range.end.0)
 518            .collect::<Vec<_>>(),
 519        vec![1..3],
 520        "querying starting at the added row should still return the full hunk including deleted lines"
 521    );
 522}
 523
 524#[gpui::test]
 525async fn test_inverted_diff_hunks_in_range(cx: &mut TestAppContext) {
 526    let base_text = "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n";
 527    let text = "ZERO\none\nTHREE\nfour\nseven\nEIGHT\nNINE\n";
 528    let buffer = cx.new(|cx| Buffer::local(text, cx));
 529    let diff = cx
 530        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
 531    let base_text_buffer = diff.read_with(cx, |diff, _| diff.base_text_buffer().clone());
 532    let multibuffer = cx.new(|cx| MultiBuffer::singleton(base_text_buffer.clone(), cx));
 533    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
 534        (multibuffer.snapshot(cx), multibuffer.subscribe())
 535    });
 536
 537    multibuffer.update(cx, |multibuffer, cx| {
 538        multibuffer.add_inverted_diff(diff, buffer.clone(), cx);
 539    });
 540
 541    assert_new_snapshot(
 542        &multibuffer,
 543        &mut snapshot,
 544        &mut subscription,
 545        cx,
 546        indoc! {
 547            "  one
 548             - two
 549             - three
 550               four
 551             - five
 552             - six
 553               seven
 554             - eight
 555            "
 556        },
 557    );
 558
 559    assert_eq!(
 560        snapshot
 561            .diff_hunks_in_range(Point::new(0, 0)..Point::MAX)
 562            .map(|hunk| hunk.row_range.start.0..hunk.row_range.end.0)
 563            .collect::<Vec<_>>(),
 564        vec![0..0, 1..3, 4..6, 7..8]
 565    );
 566
 567    assert_eq!(
 568        snapshot.diff_hunk_before(Point::new(1, 1)),
 569        Some(MultiBufferRow(0))
 570    );
 571    assert_eq!(
 572        snapshot.diff_hunk_before(Point::new(7, 0)),
 573        Some(MultiBufferRow(4))
 574    );
 575    assert_eq!(
 576        snapshot.diff_hunk_before(Point::new(4, 0)),
 577        Some(MultiBufferRow(1))
 578    );
 579}
 580
 581#[gpui::test]
 582async fn test_editing_text_in_diff_hunks(cx: &mut TestAppContext) {
 583    let base_text = "one\ntwo\nfour\nfive\nsix\nseven\n";
 584    let text = "one\ntwo\nTHREE\nfour\nfive\nseven\n";
 585    let buffer = cx.new(|cx| Buffer::local(text, cx));
 586    let diff = cx
 587        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
 588    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
 589
 590    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
 591        multibuffer.add_diff(diff.clone(), cx);
 592        (multibuffer.snapshot(cx), multibuffer.subscribe())
 593    });
 594
 595    cx.executor().run_until_parked();
 596    multibuffer.update(cx, |multibuffer, cx| {
 597        multibuffer.set_all_diff_hunks_expanded(cx);
 598    });
 599
 600    assert_new_snapshot(
 601        &multibuffer,
 602        &mut snapshot,
 603        &mut subscription,
 604        cx,
 605        indoc! {
 606            "
 607              one
 608              two
 609            + THREE
 610              four
 611              five
 612            - six
 613              seven
 614            "
 615        },
 616    );
 617
 618    // Insert a newline within an insertion hunk
 619    multibuffer.update(cx, |multibuffer, cx| {
 620        multibuffer.edit([(Point::new(2, 0)..Point::new(2, 0), "__\n__")], None, cx);
 621    });
 622    assert_new_snapshot(
 623        &multibuffer,
 624        &mut snapshot,
 625        &mut subscription,
 626        cx,
 627        indoc! {
 628            "
 629              one
 630              two
 631            + __
 632            + __THREE
 633              four
 634              five
 635            - six
 636              seven
 637            "
 638        },
 639    );
 640
 641    // Delete the newline before a deleted hunk.
 642    multibuffer.update(cx, |multibuffer, cx| {
 643        multibuffer.edit([(Point::new(5, 4)..Point::new(6, 0), "")], None, cx);
 644    });
 645    assert_new_snapshot(
 646        &multibuffer,
 647        &mut snapshot,
 648        &mut subscription,
 649        cx,
 650        indoc! {
 651            "
 652              one
 653              two
 654            + __
 655            + __THREE
 656              four
 657              fiveseven
 658            "
 659        },
 660    );
 661
 662    multibuffer.update(cx, |multibuffer, cx| multibuffer.undo(cx));
 663    assert_new_snapshot(
 664        &multibuffer,
 665        &mut snapshot,
 666        &mut subscription,
 667        cx,
 668        indoc! {
 669            "
 670              one
 671              two
 672            + __
 673            + __THREE
 674              four
 675              five
 676            - six
 677              seven
 678            "
 679        },
 680    );
 681
 682    // Cannot (yet) insert at the beginning of a deleted hunk.
 683    // (because it would put the newline in the wrong place)
 684    multibuffer.update(cx, |multibuffer, cx| {
 685        multibuffer.edit([(Point::new(6, 0)..Point::new(6, 0), "\n")], None, cx);
 686    });
 687    assert_new_snapshot(
 688        &multibuffer,
 689        &mut snapshot,
 690        &mut subscription,
 691        cx,
 692        indoc! {
 693            "
 694              one
 695              two
 696            + __
 697            + __THREE
 698              four
 699              five
 700            - six
 701              seven
 702            "
 703        },
 704    );
 705
 706    // Replace a range that ends in a deleted hunk.
 707    multibuffer.update(cx, |multibuffer, cx| {
 708        multibuffer.edit([(Point::new(5, 2)..Point::new(6, 2), "fty-")], None, cx);
 709    });
 710    assert_new_snapshot(
 711        &multibuffer,
 712        &mut snapshot,
 713        &mut subscription,
 714        cx,
 715        indoc! {
 716            "
 717              one
 718              two
 719            + __
 720            + __THREE
 721              four
 722              fifty-seven
 723            "
 724        },
 725    );
 726}
 727
 728#[gpui::test]
 729fn test_excerpt_events(cx: &mut App) {
 730    let buffer_1 = cx.new(|cx| Buffer::local(sample_text(10, 3, 'a'), cx));
 731    let buffer_2 = cx.new(|cx| Buffer::local(sample_text(10, 3, 'm'), cx));
 732
 733    let leader_multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
 734    let follower_multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
 735    let follower_edit_event_count = Arc::new(RwLock::new(0));
 736
 737    follower_multibuffer.update(cx, |_, cx| {
 738        let follower_edit_event_count = follower_edit_event_count.clone();
 739        cx.subscribe(
 740            &leader_multibuffer,
 741            move |follower, _, event, cx| match event.clone() {
 742                Event::BufferUpdated {
 743                    buffer,
 744                    path_key,
 745                    ranges,
 746                } => {
 747                    let buffer_snapshot = buffer.read(cx).snapshot();
 748                    follower.set_merged_excerpt_ranges_for_path(
 749                        path_key,
 750                        buffer,
 751                        &buffer_snapshot,
 752                        ranges,
 753                        cx,
 754                    );
 755                }
 756                Event::BuffersRemoved {
 757                    removed_buffer_ids, ..
 758                } => {
 759                    for id in removed_buffer_ids {
 760                        follower.remove_excerpts_for_buffer(id, cx);
 761                    }
 762                }
 763                Event::Edited { .. } => {
 764                    *follower_edit_event_count.write() += 1;
 765                }
 766                _ => {}
 767            },
 768        )
 769        .detach();
 770    });
 771
 772    let buffer_1_snapshot = buffer_1.read(cx).snapshot();
 773    let buffer_2_snapshot = buffer_2.read(cx).snapshot();
 774    leader_multibuffer.update(cx, |leader, cx| {
 775        leader.set_excerpt_ranges_for_path(
 776            PathKey::sorted(0),
 777            buffer_1.clone(),
 778            &buffer_1_snapshot,
 779            vec![
 780                ExcerptRange::new((0..8).to_point(&buffer_1_snapshot)),
 781                ExcerptRange::new((22..26).to_point(&buffer_1_snapshot)),
 782            ],
 783            cx,
 784        );
 785        leader.set_excerpt_ranges_for_path(
 786            PathKey::sorted(1),
 787            buffer_2.clone(),
 788            &buffer_2_snapshot,
 789            vec![
 790                ExcerptRange::new((0..5).to_point(&buffer_2_snapshot)),
 791                ExcerptRange::new((20..25).to_point(&buffer_2_snapshot)),
 792            ],
 793            cx,
 794        );
 795    });
 796    assert_eq!(
 797        leader_multibuffer.read(cx).snapshot(cx).text(),
 798        follower_multibuffer.read(cx).snapshot(cx).text(),
 799    );
 800    assert_eq!(*follower_edit_event_count.read(), 2);
 801
 802    leader_multibuffer.update(cx, |leader, cx| {
 803        leader.set_excerpt_ranges_for_path(
 804            PathKey::sorted(0),
 805            buffer_1.clone(),
 806            &buffer_1_snapshot,
 807            vec![ExcerptRange::new((0..8).to_point(&buffer_1_snapshot))],
 808            cx,
 809        );
 810        leader.set_excerpt_ranges_for_path(
 811            PathKey::sorted(1),
 812            buffer_2,
 813            &buffer_2_snapshot,
 814            vec![ExcerptRange::new((0..5).to_point(&buffer_2_snapshot))],
 815            cx,
 816        );
 817    });
 818    assert_eq!(
 819        leader_multibuffer.read(cx).snapshot(cx).text(),
 820        follower_multibuffer.read(cx).snapshot(cx).text(),
 821    );
 822    assert_eq!(*follower_edit_event_count.read(), 4);
 823
 824    leader_multibuffer.update(cx, |leader, cx| {
 825        leader.clear(cx);
 826    });
 827    assert_eq!(
 828        leader_multibuffer.read(cx).snapshot(cx).text(),
 829        follower_multibuffer.read(cx).snapshot(cx).text(),
 830    );
 831    assert_eq!(*follower_edit_event_count.read(), 5);
 832}
 833
 834#[gpui::test]
 835fn test_expand_excerpts(cx: &mut App) {
 836    let buffer = cx.new(|cx| Buffer::local(sample_text(20, 3, 'a'), cx));
 837    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
 838
 839    multibuffer.update(cx, |multibuffer, cx| {
 840        multibuffer.set_excerpts_for_path(
 841            PathKey::for_buffer(&buffer, cx),
 842            buffer,
 843            vec![
 844                // Note that in this test, this first excerpt
 845                // does not contain a new line
 846                Point::new(3, 2)..Point::new(3, 3),
 847                Point::new(7, 1)..Point::new(7, 3),
 848                Point::new(15, 0)..Point::new(15, 0),
 849            ],
 850            1,
 851            cx,
 852        )
 853    });
 854
 855    let snapshot = multibuffer.read(cx).snapshot(cx);
 856
 857    assert_eq!(
 858        snapshot.text(),
 859        concat!(
 860            "ccc\n", //
 861            "ddd\n", //
 862            "eee",   //
 863            "\n",    // End of excerpt
 864            "ggg\n", //
 865            "hhh\n", //
 866            "iii",   //
 867            "\n",    // End of excerpt
 868            "ooo\n", //
 869            "ppp\n", //
 870            "qqq",   // End of excerpt
 871        )
 872    );
 873    drop(snapshot);
 874
 875    multibuffer.update(cx, |multibuffer, cx| {
 876        let line_zero = multibuffer.snapshot(cx).anchor_before(Point::new(0, 0));
 877
 878        multibuffer.expand_excerpts_with_paths(
 879            multibuffer
 880                .snapshot(cx)
 881                .excerpts()
 882                .map(|(_, info)| Anchor::in_buffer(info.path_key_index, info.range.context.end)),
 883            1,
 884            ExpandExcerptDirection::UpAndDown,
 885            cx,
 886        );
 887        let snapshot = multibuffer.snapshot(cx);
 888        let line_two = snapshot.anchor_before(Point::new(2, 0));
 889        assert_eq!(line_two.cmp(&line_zero, &snapshot), cmp::Ordering::Greater);
 890    });
 891
 892    let snapshot = multibuffer.read(cx).snapshot(cx);
 893
 894    assert_eq!(
 895        snapshot.text(),
 896        concat!(
 897            "bbb\n", //
 898            "ccc\n", //
 899            "ddd\n", //
 900            "eee\n", //
 901            "fff\n", //
 902            "ggg\n", //
 903            "hhh\n", //
 904            "iii\n", //
 905            "jjj\n", // End of excerpt
 906            "nnn\n", //
 907            "ooo\n", //
 908            "ppp\n", //
 909            "qqq\n", //
 910            "rrr",   // End of excerpt
 911        )
 912    );
 913}
 914
 915#[gpui::test(iterations = 100)]
 916async fn test_set_anchored_excerpts_for_path(cx: &mut TestAppContext) {
 917    let buffer_1 = cx.new(|cx| Buffer::local(sample_text(20, 3, 'a'), cx));
 918    let buffer_2 = cx.new(|cx| Buffer::local(sample_text(15, 4, 'a'), cx));
 919    let snapshot_1 = buffer_1.update(cx, |buffer, _| buffer.snapshot());
 920    let snapshot_2 = buffer_2.update(cx, |buffer, _| buffer.snapshot());
 921    let ranges_1 = vec![
 922        snapshot_1.anchor_before(Point::new(3, 2))..snapshot_1.anchor_before(Point::new(4, 2)),
 923        snapshot_1.anchor_before(Point::new(7, 1))..snapshot_1.anchor_before(Point::new(7, 3)),
 924        snapshot_1.anchor_before(Point::new(15, 0))..snapshot_1.anchor_before(Point::new(15, 0)),
 925    ];
 926    let ranges_2 = vec![
 927        snapshot_2.anchor_before(Point::new(2, 1))..snapshot_2.anchor_before(Point::new(3, 1)),
 928        snapshot_2.anchor_before(Point::new(10, 0))..snapshot_2.anchor_before(Point::new(10, 2)),
 929    ];
 930
 931    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
 932    let anchor_ranges_1 = multibuffer
 933        .update(cx, |multibuffer, cx| {
 934            multibuffer.set_anchored_excerpts_for_path(
 935                PathKey::for_buffer(&buffer_1, cx),
 936                buffer_1.clone(),
 937                ranges_1,
 938                2,
 939                cx,
 940            )
 941        })
 942        .await;
 943    let snapshot_1 = multibuffer.update(cx, |multibuffer, cx| multibuffer.snapshot(cx));
 944    assert_eq!(
 945        anchor_ranges_1
 946            .iter()
 947            .map(|range| range.to_point(&snapshot_1))
 948            .collect::<Vec<_>>(),
 949        vec![
 950            Point::new(2, 2)..Point::new(3, 2),
 951            Point::new(6, 1)..Point::new(6, 3),
 952            Point::new(11, 0)..Point::new(11, 0),
 953        ]
 954    );
 955    let anchor_ranges_2 = multibuffer
 956        .update(cx, |multibuffer, cx| {
 957            multibuffer.set_anchored_excerpts_for_path(
 958                PathKey::for_buffer(&buffer_2, cx),
 959                buffer_2.clone(),
 960                ranges_2,
 961                2,
 962                cx,
 963            )
 964        })
 965        .await;
 966    let snapshot_2 = multibuffer.update(cx, |multibuffer, cx| multibuffer.snapshot(cx));
 967    assert_eq!(
 968        anchor_ranges_2
 969            .iter()
 970            .map(|range| range.to_point(&snapshot_2))
 971            .collect::<Vec<_>>(),
 972        vec![
 973            Point::new(16, 1)..Point::new(17, 1),
 974            Point::new(22, 0)..Point::new(22, 2)
 975        ]
 976    );
 977
 978    let snapshot = multibuffer.update(cx, |multibuffer, cx| multibuffer.snapshot(cx));
 979    assert_eq!(
 980        snapshot.text(),
 981        concat!(
 982            "bbb\n", // buffer_1
 983            "ccc\n", //
 984            "ddd\n", // <-- excerpt 1
 985            "eee\n", // <-- excerpt 1
 986            "fff\n", //
 987            "ggg\n", //
 988            "hhh\n", // <-- excerpt 2
 989            "iii\n", //
 990            "jjj\n", //
 991            //
 992            "nnn\n", //
 993            "ooo\n", //
 994            "ppp\n", // <-- excerpt 3
 995            "qqq\n", //
 996            "rrr\n", //
 997            //
 998            "aaaa\n", // buffer 2
 999            "bbbb\n", //
1000            "cccc\n", // <-- excerpt 4
1001            "dddd\n", // <-- excerpt 4
1002            "eeee\n", //
1003            "ffff\n", //
1004            //
1005            "iiii\n", //
1006            "jjjj\n", //
1007            "kkkk\n", // <-- excerpt 5
1008            "llll\n", //
1009            "mmmm",   //
1010        )
1011    );
1012}
1013
1014#[gpui::test]
1015fn test_empty_multibuffer(cx: &mut App) {
1016    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
1017
1018    let snapshot = multibuffer.read(cx).snapshot(cx);
1019    assert_eq!(snapshot.text(), "");
1020    assert_eq!(
1021        snapshot
1022            .row_infos(MultiBufferRow(0))
1023            .map(|info| info.buffer_row)
1024            .collect::<Vec<_>>(),
1025        &[Some(0)]
1026    );
1027    assert!(
1028        snapshot
1029            .row_infos(MultiBufferRow(1))
1030            .map(|info| info.buffer_row)
1031            .collect::<Vec<_>>()
1032            .is_empty(),
1033    );
1034}
1035
1036#[gpui::test]
1037async fn test_empty_diff_excerpt(cx: &mut TestAppContext) {
1038    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
1039    let buffer = cx.new(|cx| Buffer::local("", cx));
1040    let base_text = "a\nb\nc";
1041
1042    let diff = cx
1043        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
1044    multibuffer.update(cx, |multibuffer, cx| {
1045        multibuffer.set_excerpt_ranges_for_path(
1046            PathKey::sorted(0),
1047            buffer.clone(),
1048            &buffer.read(cx).snapshot(),
1049            vec![ExcerptRange::new(Point::zero()..Point::zero())],
1050            cx,
1051        );
1052        multibuffer.set_all_diff_hunks_expanded(cx);
1053        multibuffer.add_diff(diff.clone(), cx);
1054    });
1055    cx.run_until_parked();
1056
1057    let snapshot = multibuffer.update(cx, |multibuffer, cx| multibuffer.snapshot(cx));
1058    assert_eq!(snapshot.text(), "a\nb\nc\n");
1059
1060    let hunk = snapshot
1061        .diff_hunks_in_range(Point::new(1, 1)..Point::new(1, 1))
1062        .next()
1063        .unwrap();
1064
1065    assert_eq!(hunk.diff_base_byte_range.start, BufferOffset(0));
1066
1067    let buf2 = cx.new(|cx| Buffer::local("X", cx));
1068    multibuffer.update(cx, |multibuffer, cx| {
1069        multibuffer.set_excerpts_for_path(
1070            PathKey::sorted(1),
1071            buf2,
1072            [Point::new(0, 0)..Point::new(0, 1)],
1073            0,
1074            cx,
1075        );
1076    });
1077
1078    buffer.update(cx, |buffer, cx| {
1079        buffer.edit([(0..0, "a\nb\nc")], None, cx);
1080        diff.update(cx, |diff, cx| {
1081            diff.recalculate_diff_sync(&buffer.text_snapshot(), cx);
1082        });
1083        assert_eq!(buffer.text(), "a\nb\nc")
1084    });
1085    cx.run_until_parked();
1086
1087    let snapshot = multibuffer.update(cx, |multibuffer, cx| multibuffer.snapshot(cx));
1088    assert_eq!(snapshot.text(), "a\nb\nc\nX");
1089
1090    buffer.update(cx, |buffer, cx| {
1091        buffer.undo(cx);
1092        diff.update(cx, |diff, cx| {
1093            diff.recalculate_diff_sync(&buffer.text_snapshot(), cx);
1094        });
1095        assert_eq!(buffer.text(), "")
1096    });
1097    cx.run_until_parked();
1098
1099    let snapshot = multibuffer.update(cx, |multibuffer, cx| multibuffer.snapshot(cx));
1100    assert_eq!(snapshot.text(), "a\nb\nc\n\nX");
1101}
1102
1103#[gpui::test]
1104fn test_singleton_multibuffer_anchors(cx: &mut App) {
1105    let buffer = cx.new(|cx| Buffer::local("abcd", cx));
1106    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
1107    let old_snapshot = multibuffer.read(cx).snapshot(cx);
1108    buffer.update(cx, |buffer, cx| {
1109        buffer.edit([(0..0, "X")], None, cx);
1110        buffer.edit([(5..5, "Y")], None, cx);
1111    });
1112    dbg!("-------");
1113    let new_snapshot = multibuffer.read(cx).snapshot(cx);
1114
1115    assert_eq!(old_snapshot.text(), "abcd");
1116    assert_eq!(new_snapshot.text(), "XabcdY");
1117
1118    assert_eq!(
1119        old_snapshot
1120            .anchor_before(MultiBufferOffset(0))
1121            .to_offset(&new_snapshot),
1122        MultiBufferOffset(0)
1123    );
1124    assert_eq!(
1125        old_snapshot
1126            .anchor_after(MultiBufferOffset(0))
1127            .to_offset(&new_snapshot),
1128        MultiBufferOffset(1)
1129    );
1130    assert_eq!(
1131        old_snapshot
1132            .anchor_before(MultiBufferOffset(4))
1133            .to_offset(&new_snapshot),
1134        MultiBufferOffset(5)
1135    );
1136    assert_eq!(
1137        old_snapshot
1138            .anchor_after(MultiBufferOffset(4))
1139            .to_offset(&new_snapshot),
1140        MultiBufferOffset(6)
1141    );
1142}
1143
1144#[gpui::test]
1145fn test_multibuffer_anchors(cx: &mut App) {
1146    let buffer_1 = cx.new(|cx| Buffer::local("abcd", cx));
1147    let buffer_2 = cx.new(|cx| Buffer::local("efghi", cx));
1148    let multibuffer = cx.new(|cx| {
1149        let mut multibuffer = MultiBuffer::new(Capability::ReadWrite);
1150        multibuffer.set_excerpts_for_path(
1151            PathKey::sorted(0),
1152            buffer_1.clone(),
1153            [Point::new(0, 0)..Point::new(0, 4)],
1154            0,
1155            cx,
1156        );
1157        multibuffer.set_excerpts_for_path(
1158            PathKey::sorted(1),
1159            buffer_2.clone(),
1160            [Point::new(0, 0)..Point::new(0, 5)],
1161            0,
1162            cx,
1163        );
1164        multibuffer
1165    });
1166    let old_snapshot = multibuffer.read(cx).snapshot(cx);
1167
1168    assert_eq!(
1169        old_snapshot
1170            .anchor_before(MultiBufferOffset(0))
1171            .to_offset(&old_snapshot),
1172        MultiBufferOffset(0)
1173    );
1174    assert_eq!(
1175        old_snapshot
1176            .anchor_after(MultiBufferOffset(0))
1177            .to_offset(&old_snapshot),
1178        MultiBufferOffset(0)
1179    );
1180    assert_eq!(Anchor::min().to_offset(&old_snapshot), MultiBufferOffset(0));
1181    assert_eq!(Anchor::min().to_offset(&old_snapshot), MultiBufferOffset(0));
1182    assert_eq!(
1183        Anchor::max().to_offset(&old_snapshot),
1184        MultiBufferOffset(10)
1185    );
1186    assert_eq!(
1187        Anchor::max().to_offset(&old_snapshot),
1188        MultiBufferOffset(10)
1189    );
1190
1191    buffer_1.update(cx, |buffer, cx| {
1192        buffer.edit([(0..0, "W")], None, cx);
1193        buffer.edit([(5..5, "X")], None, cx);
1194    });
1195    buffer_2.update(cx, |buffer, cx| {
1196        buffer.edit([(0..0, "Y")], None, cx);
1197        buffer.edit([(6..6, "Z")], None, cx);
1198    });
1199    let new_snapshot = multibuffer.read(cx).snapshot(cx);
1200
1201    assert_eq!(old_snapshot.text(), "abcd\nefghi");
1202    assert_eq!(new_snapshot.text(), "WabcdX\nYefghiZ");
1203
1204    assert_eq!(
1205        old_snapshot
1206            .anchor_before(MultiBufferOffset(0))
1207            .to_offset(&new_snapshot),
1208        MultiBufferOffset(0)
1209    );
1210    assert_eq!(
1211        old_snapshot
1212            .anchor_after(MultiBufferOffset(0))
1213            .to_offset(&new_snapshot),
1214        MultiBufferOffset(1)
1215    );
1216    assert_eq!(
1217        old_snapshot
1218            .anchor_before(MultiBufferOffset(1))
1219            .to_offset(&new_snapshot),
1220        MultiBufferOffset(2)
1221    );
1222    assert_eq!(
1223        old_snapshot
1224            .anchor_after(MultiBufferOffset(1))
1225            .to_offset(&new_snapshot),
1226        MultiBufferOffset(2)
1227    );
1228    assert_eq!(
1229        old_snapshot
1230            .anchor_before(MultiBufferOffset(2))
1231            .to_offset(&new_snapshot),
1232        MultiBufferOffset(3)
1233    );
1234    assert_eq!(
1235        old_snapshot
1236            .anchor_after(MultiBufferOffset(2))
1237            .to_offset(&new_snapshot),
1238        MultiBufferOffset(3)
1239    );
1240    assert_eq!(
1241        old_snapshot
1242            .anchor_before(MultiBufferOffset(5))
1243            .to_offset(&new_snapshot),
1244        MultiBufferOffset(7)
1245    );
1246    assert_eq!(
1247        old_snapshot
1248            .anchor_after(MultiBufferOffset(5))
1249            .to_offset(&new_snapshot),
1250        MultiBufferOffset(8)
1251    );
1252    assert_eq!(
1253        old_snapshot
1254            .anchor_before(MultiBufferOffset(10))
1255            .to_offset(&new_snapshot),
1256        MultiBufferOffset(13)
1257    );
1258    assert_eq!(
1259        old_snapshot
1260            .anchor_after(MultiBufferOffset(10))
1261            .to_offset(&new_snapshot),
1262        MultiBufferOffset(14)
1263    );
1264}
1265
1266#[gpui::test]
1267async fn test_basic_diff_hunks(cx: &mut TestAppContext) {
1268    let text = indoc!(
1269        "
1270        ZERO
1271        one
1272        TWO
1273        three
1274        six
1275        "
1276    );
1277    let base_text = indoc!(
1278        "
1279        one
1280        two
1281        three
1282        four
1283        five
1284        six
1285        "
1286    );
1287
1288    let buffer = cx.new(|cx| Buffer::local(text, cx));
1289    let diff = cx
1290        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
1291    cx.run_until_parked();
1292
1293    let multibuffer = cx.new(|cx| {
1294        let mut multibuffer = MultiBuffer::singleton(buffer.clone(), cx);
1295        multibuffer.add_diff(diff.clone(), cx);
1296        multibuffer
1297    });
1298
1299    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
1300        (multibuffer.snapshot(cx), multibuffer.subscribe())
1301    });
1302    assert_eq!(
1303        snapshot.text(),
1304        indoc!(
1305            "
1306            ZERO
1307            one
1308            TWO
1309            three
1310            six
1311            "
1312        ),
1313    );
1314
1315    multibuffer.update(cx, |multibuffer, cx| {
1316        multibuffer.expand_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
1317    });
1318
1319    assert_new_snapshot(
1320        &multibuffer,
1321        &mut snapshot,
1322        &mut subscription,
1323        cx,
1324        indoc!(
1325            "
1326            + ZERO
1327              one
1328            - two
1329            + TWO
1330              three
1331            - four
1332            - five
1333              six
1334            "
1335        ),
1336    );
1337
1338    assert_eq!(
1339        snapshot
1340            .row_infos(MultiBufferRow(0))
1341            .map(|info| (info.buffer_row, info.diff_status))
1342            .collect::<Vec<_>>(),
1343        vec![
1344            (Some(0), Some(DiffHunkStatus::added_none())),
1345            (Some(1), None),
1346            (Some(1), Some(DiffHunkStatus::deleted_none())),
1347            (Some(2), Some(DiffHunkStatus::added_none())),
1348            (Some(3), None),
1349            (Some(3), Some(DiffHunkStatus::deleted_none())),
1350            (Some(4), Some(DiffHunkStatus::deleted_none())),
1351            (Some(4), None),
1352            (Some(5), None)
1353        ]
1354    );
1355
1356    assert_chunks_in_ranges(&snapshot);
1357    assert_consistent_line_numbers(&snapshot);
1358    assert_position_translation(&snapshot);
1359    assert_line_indents(&snapshot);
1360
1361    multibuffer.update(cx, |multibuffer, cx| {
1362        multibuffer.collapse_diff_hunks(vec![Anchor::min()..Anchor::max()], cx)
1363    });
1364    assert_new_snapshot(
1365        &multibuffer,
1366        &mut snapshot,
1367        &mut subscription,
1368        cx,
1369        indoc!(
1370            "
1371            ZERO
1372            one
1373            TWO
1374            three
1375            six
1376            "
1377        ),
1378    );
1379
1380    assert_chunks_in_ranges(&snapshot);
1381    assert_consistent_line_numbers(&snapshot);
1382    assert_position_translation(&snapshot);
1383    assert_line_indents(&snapshot);
1384
1385    // Expand the first diff hunk
1386    multibuffer.update(cx, |multibuffer, cx| {
1387        let position = multibuffer.read(cx).anchor_before(Point::new(2, 2));
1388        multibuffer.expand_diff_hunks(vec![position..position], cx)
1389    });
1390    assert_new_snapshot(
1391        &multibuffer,
1392        &mut snapshot,
1393        &mut subscription,
1394        cx,
1395        indoc!(
1396            "
1397              ZERO
1398              one
1399            - two
1400            + TWO
1401              three
1402              six
1403            "
1404        ),
1405    );
1406
1407    // Expand the second diff hunk
1408    multibuffer.update(cx, |multibuffer, cx| {
1409        let start = multibuffer.read(cx).anchor_before(Point::new(4, 0));
1410        let end = multibuffer.read(cx).anchor_before(Point::new(5, 0));
1411        multibuffer.expand_diff_hunks(vec![start..end], cx)
1412    });
1413    assert_new_snapshot(
1414        &multibuffer,
1415        &mut snapshot,
1416        &mut subscription,
1417        cx,
1418        indoc!(
1419            "
1420              ZERO
1421              one
1422            - two
1423            + TWO
1424              three
1425            - four
1426            - five
1427              six
1428            "
1429        ),
1430    );
1431
1432    assert_chunks_in_ranges(&snapshot);
1433    assert_consistent_line_numbers(&snapshot);
1434    assert_position_translation(&snapshot);
1435    assert_line_indents(&snapshot);
1436
1437    // Edit the buffer before the first hunk
1438    buffer.update(cx, |buffer, cx| {
1439        buffer.edit_via_marked_text(
1440            indoc!(
1441                "
1442                ZERO
1443                one« hundred
1444                  thousand»
1445                TWO
1446                three
1447                six
1448                "
1449            ),
1450            None,
1451            cx,
1452        );
1453    });
1454    assert_new_snapshot(
1455        &multibuffer,
1456        &mut snapshot,
1457        &mut subscription,
1458        cx,
1459        indoc!(
1460            "
1461              ZERO
1462              one hundred
1463                thousand
1464            - two
1465            + TWO
1466              three
1467            - four
1468            - five
1469              six
1470            "
1471        ),
1472    );
1473
1474    assert_chunks_in_ranges(&snapshot);
1475    assert_consistent_line_numbers(&snapshot);
1476    assert_position_translation(&snapshot);
1477    assert_line_indents(&snapshot);
1478
1479    // Recalculate the diff, changing the first diff hunk.
1480    diff.update(cx, |diff, cx| {
1481        diff.recalculate_diff_sync(&buffer.read(cx).text_snapshot(), cx);
1482    });
1483    cx.run_until_parked();
1484    assert_new_snapshot(
1485        &multibuffer,
1486        &mut snapshot,
1487        &mut subscription,
1488        cx,
1489        indoc!(
1490            "
1491              ZERO
1492              one hundred
1493                thousand
1494              TWO
1495              three
1496            - four
1497            - five
1498              six
1499            "
1500        ),
1501    );
1502
1503    assert_eq!(
1504        snapshot
1505            .diff_hunks_in_range(MultiBufferOffset(0)..snapshot.len())
1506            .map(|hunk| hunk.row_range.start.0..hunk.row_range.end.0)
1507            .collect::<Vec<_>>(),
1508        &[0..4, 5..7]
1509    );
1510}
1511
1512#[gpui::test]
1513async fn test_repeatedly_expand_a_diff_hunk(cx: &mut TestAppContext) {
1514    let text = indoc!(
1515        "
1516        one
1517        TWO
1518        THREE
1519        four
1520        FIVE
1521        six
1522        "
1523    );
1524    let base_text = indoc!(
1525        "
1526        one
1527        four
1528        five
1529        six
1530        "
1531    );
1532
1533    let buffer = cx.new(|cx| Buffer::local(text, cx));
1534    let diff = cx
1535        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
1536    cx.run_until_parked();
1537
1538    let multibuffer = cx.new(|cx| {
1539        let mut multibuffer = MultiBuffer::singleton(buffer.clone(), cx);
1540        multibuffer.add_diff(diff.clone(), cx);
1541        multibuffer
1542    });
1543
1544    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
1545        (multibuffer.snapshot(cx), multibuffer.subscribe())
1546    });
1547
1548    multibuffer.update(cx, |multibuffer, cx| {
1549        multibuffer.expand_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
1550    });
1551
1552    assert_new_snapshot(
1553        &multibuffer,
1554        &mut snapshot,
1555        &mut subscription,
1556        cx,
1557        indoc!(
1558            "
1559              one
1560            + TWO
1561            + THREE
1562              four
1563            - five
1564            + FIVE
1565              six
1566            "
1567        ),
1568    );
1569
1570    // Regression test: expanding diff hunks that are already expanded should not change anything.
1571    multibuffer.update(cx, |multibuffer, cx| {
1572        multibuffer.expand_diff_hunks(
1573            vec![
1574                snapshot.anchor_before(Point::new(2, 0))..snapshot.anchor_before(Point::new(2, 0)),
1575            ],
1576            cx,
1577        );
1578    });
1579
1580    assert_new_snapshot(
1581        &multibuffer,
1582        &mut snapshot,
1583        &mut subscription,
1584        cx,
1585        indoc!(
1586            "
1587              one
1588            + TWO
1589            + THREE
1590              four
1591            - five
1592            + FIVE
1593              six
1594            "
1595        ),
1596    );
1597
1598    // Now collapse all diff hunks
1599    multibuffer.update(cx, |multibuffer, cx| {
1600        multibuffer.collapse_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
1601    });
1602
1603    assert_new_snapshot(
1604        &multibuffer,
1605        &mut snapshot,
1606        &mut subscription,
1607        cx,
1608        indoc!(
1609            "
1610            one
1611            TWO
1612            THREE
1613            four
1614            FIVE
1615            six
1616            "
1617        ),
1618    );
1619
1620    // Expand the hunks again, but this time provide two ranges that are both within the same hunk
1621    // Target the first hunk which is between "one" and "four"
1622    multibuffer.update(cx, |multibuffer, cx| {
1623        multibuffer.expand_diff_hunks(
1624            vec![
1625                snapshot.anchor_before(Point::new(4, 0))..snapshot.anchor_before(Point::new(4, 0)),
1626                snapshot.anchor_before(Point::new(4, 2))..snapshot.anchor_before(Point::new(4, 2)),
1627            ],
1628            cx,
1629        );
1630    });
1631    assert_new_snapshot(
1632        &multibuffer,
1633        &mut snapshot,
1634        &mut subscription,
1635        cx,
1636        indoc!(
1637            "
1638              one
1639              TWO
1640              THREE
1641              four
1642            - five
1643            + FIVE
1644              six
1645            "
1646        ),
1647    );
1648}
1649
1650#[gpui::test]
1651fn test_set_excerpts_for_buffer_ordering(cx: &mut TestAppContext) {
1652    let buf1 = cx.new(|cx| {
1653        Buffer::local(
1654            indoc! {
1655            "zero
1656            one
1657            two
1658            two.five
1659            three
1660            four
1661            five
1662            six
1663            seven
1664            eight
1665            nine
1666            ten
1667            eleven
1668            ",
1669            },
1670            cx,
1671        )
1672    });
1673    let path1: PathKey = PathKey::with_sort_prefix(0, rel_path("root").into_arc());
1674
1675    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
1676    multibuffer.update(cx, |multibuffer, cx| {
1677        multibuffer.set_excerpts_for_path(
1678            path1.clone(),
1679            buf1.clone(),
1680            vec![
1681                Point::row_range(1..2),
1682                Point::row_range(6..7),
1683                Point::row_range(11..12),
1684            ],
1685            1,
1686            cx,
1687        );
1688    });
1689
1690    assert_excerpts_match(
1691        &multibuffer,
1692        cx,
1693        indoc! {
1694            "-----
1695            zero
1696            one
1697            two
1698            two.five
1699            -----
1700            four
1701            five
1702            six
1703            seven
1704            -----
1705            nine
1706            ten
1707            eleven
1708            "
1709        },
1710    );
1711
1712    buf1.update(cx, |buffer, cx| buffer.edit([(0..5, "")], None, cx));
1713
1714    multibuffer.update(cx, |multibuffer, cx| {
1715        multibuffer.set_excerpts_for_path(
1716            path1.clone(),
1717            buf1.clone(),
1718            vec![
1719                Point::row_range(0..3),
1720                Point::row_range(5..7),
1721                Point::row_range(10..11),
1722            ],
1723            1,
1724            cx,
1725        );
1726    });
1727
1728    assert_excerpts_match(
1729        &multibuffer,
1730        cx,
1731        indoc! {
1732            "-----
1733             one
1734             two
1735             two.five
1736             three
1737             four
1738             five
1739             six
1740             seven
1741             eight
1742             nine
1743             ten
1744             eleven
1745            "
1746        },
1747    );
1748}
1749
1750#[gpui::test]
1751fn test_set_excerpts_for_buffer(cx: &mut TestAppContext) {
1752    let buf1 = cx.new(|cx| {
1753        Buffer::local(
1754            indoc! {
1755            "zero
1756            one
1757            two
1758            three
1759            four
1760            five
1761            six
1762            seven
1763            ",
1764            },
1765            cx,
1766        )
1767    });
1768    let path1: PathKey = PathKey::with_sort_prefix(0, rel_path("root").into_arc());
1769    let buf2 = cx.new(|cx| {
1770        Buffer::local(
1771            indoc! {
1772            "000
1773            111
1774            222
1775            333
1776            444
1777            555
1778            666
1779            777
1780            888
1781            999
1782            "
1783            },
1784            cx,
1785        )
1786    });
1787    let path2 = PathKey::with_sort_prefix(1, rel_path("root").into_arc());
1788
1789    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
1790    multibuffer.update(cx, |multibuffer, cx| {
1791        multibuffer.set_excerpts_for_path(
1792            path1.clone(),
1793            buf1.clone(),
1794            vec![Point::row_range(0..1)],
1795            2,
1796            cx,
1797        );
1798    });
1799
1800    assert_excerpts_match(
1801        &multibuffer,
1802        cx,
1803        indoc! {
1804        "-----
1805        zero
1806        one
1807        two
1808        three
1809        "
1810        },
1811    );
1812
1813    multibuffer.update(cx, |multibuffer, cx| {
1814        multibuffer.set_excerpts_for_path(path1.clone(), buf1.clone(), vec![], 2, cx);
1815    });
1816
1817    assert_excerpts_match(&multibuffer, cx, "");
1818
1819    multibuffer.update(cx, |multibuffer, cx| {
1820        multibuffer.set_excerpts_for_path(
1821            path1.clone(),
1822            buf1.clone(),
1823            vec![Point::row_range(0..1), Point::row_range(7..8)],
1824            2,
1825            cx,
1826        );
1827    });
1828
1829    assert_excerpts_match(
1830        &multibuffer,
1831        cx,
1832        indoc! {"-----
1833                zero
1834                one
1835                two
1836                three
1837                -----
1838                five
1839                six
1840                seven
1841                "},
1842    );
1843
1844    multibuffer.update(cx, |multibuffer, cx| {
1845        multibuffer.set_excerpts_for_path(
1846            path1.clone(),
1847            buf1.clone(),
1848            vec![Point::row_range(0..1), Point::row_range(5..6)],
1849            2,
1850            cx,
1851        );
1852    });
1853
1854    assert_excerpts_match(
1855        &multibuffer,
1856        cx,
1857        indoc! {"-----
1858                    zero
1859                    one
1860                    two
1861                    three
1862                    four
1863                    five
1864                    six
1865                    seven
1866                    "},
1867    );
1868
1869    multibuffer.update(cx, |multibuffer, cx| {
1870        multibuffer.set_excerpts_for_path(
1871            path2.clone(),
1872            buf2.clone(),
1873            vec![Point::row_range(2..3)],
1874            2,
1875            cx,
1876        );
1877    });
1878
1879    assert_excerpts_match(
1880        &multibuffer,
1881        cx,
1882        indoc! {"-----
1883                zero
1884                one
1885                two
1886                three
1887                four
1888                five
1889                six
1890                seven
1891                -----
1892                000
1893                111
1894                222
1895                333
1896                444
1897                555
1898                "},
1899    );
1900
1901    multibuffer.update(cx, |multibuffer, cx| {
1902        multibuffer.set_excerpts_for_path(path1.clone(), buf1.clone(), vec![], 2, cx);
1903    });
1904
1905    multibuffer.update(cx, |multibuffer, cx| {
1906        multibuffer.set_excerpts_for_path(
1907            path1.clone(),
1908            buf1.clone(),
1909            vec![Point::row_range(3..4)],
1910            2,
1911            cx,
1912        );
1913    });
1914
1915    assert_excerpts_match(
1916        &multibuffer,
1917        cx,
1918        indoc! {"-----
1919                one
1920                two
1921                three
1922                four
1923                five
1924                six
1925                -----
1926                000
1927                111
1928                222
1929                333
1930                444
1931                555
1932                "},
1933    );
1934
1935    multibuffer.update(cx, |multibuffer, cx| {
1936        multibuffer.set_excerpts_for_path(
1937            path1.clone(),
1938            buf1.clone(),
1939            vec![Point::row_range(3..4)],
1940            2,
1941            cx,
1942        );
1943    });
1944}
1945
1946#[gpui::test]
1947fn test_set_excerpts_for_buffer_rename(cx: &mut TestAppContext) {
1948    let buf1 = cx.new(|cx| {
1949        Buffer::local(
1950            indoc! {
1951            "zero
1952            one
1953            two
1954            three
1955            four
1956            five
1957            six
1958            seven
1959            ",
1960            },
1961            cx,
1962        )
1963    });
1964    let path: PathKey = PathKey::with_sort_prefix(0, rel_path("root").into_arc());
1965    let buf2 = cx.new(|cx| {
1966        Buffer::local(
1967            indoc! {
1968            "000
1969            111
1970            222
1971            333
1972            "
1973            },
1974            cx,
1975        )
1976    });
1977
1978    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
1979    multibuffer.update(cx, |multibuffer, cx| {
1980        multibuffer.set_excerpts_for_path(
1981            path.clone(),
1982            buf1.clone(),
1983            vec![Point::row_range(1..1), Point::row_range(4..5)],
1984            1,
1985            cx,
1986        );
1987    });
1988
1989    assert_excerpts_match(
1990        &multibuffer,
1991        cx,
1992        indoc! {
1993        "-----
1994        zero
1995        one
1996        two
1997        three
1998        four
1999        five
2000        six
2001        "
2002        },
2003    );
2004
2005    multibuffer.update(cx, |multibuffer, cx| {
2006        multibuffer.set_excerpts_for_path(
2007            path.clone(),
2008            buf2.clone(),
2009            vec![Point::row_range(0..1)],
2010            2,
2011            cx,
2012        );
2013    });
2014
2015    assert_excerpts_match(
2016        &multibuffer,
2017        cx,
2018        indoc! {"-----
2019                000
2020                111
2021                222
2022                333
2023                "},
2024    );
2025}
2026
2027#[gpui::test]
2028async fn test_diff_hunks_with_multiple_excerpts(cx: &mut TestAppContext) {
2029    let base_text_1 = indoc!(
2030        "
2031        one
2032        two
2033            three
2034        four
2035        five
2036        six
2037        "
2038    );
2039    let text_1 = indoc!(
2040        "
2041        ZERO
2042        one
2043        TWO
2044            three
2045        six
2046        "
2047    );
2048    let base_text_2 = indoc!(
2049        "
2050        seven
2051          eight
2052        nine
2053        ten
2054        eleven
2055        twelve
2056        "
2057    );
2058    let text_2 = indoc!(
2059        "
2060          eight
2061        nine
2062        eleven
2063        THIRTEEN
2064        FOURTEEN
2065        "
2066    );
2067
2068    let buffer_1 = cx.new(|cx| Buffer::local(text_1, cx));
2069    let buffer_2 = cx.new(|cx| Buffer::local(text_2, cx));
2070    let diff_1 = cx.new(|cx| {
2071        BufferDiff::new_with_base_text(base_text_1, &buffer_1.read(cx).text_snapshot(), cx)
2072    });
2073    let diff_2 = cx.new(|cx| {
2074        BufferDiff::new_with_base_text(base_text_2, &buffer_2.read(cx).text_snapshot(), cx)
2075    });
2076    cx.run_until_parked();
2077
2078    let multibuffer = cx.new(|cx| {
2079        let mut multibuffer = MultiBuffer::new(Capability::ReadWrite);
2080        multibuffer.set_excerpts_for_path(
2081            PathKey::sorted(0),
2082            buffer_1.clone(),
2083            [Point::zero()..buffer_1.read(cx).max_point()],
2084            0,
2085            cx,
2086        );
2087        multibuffer.set_excerpts_for_path(
2088            PathKey::sorted(1),
2089            buffer_2.clone(),
2090            [Point::zero()..buffer_2.read(cx).max_point()],
2091            0,
2092            cx,
2093        );
2094        multibuffer.add_diff(diff_1.clone(), cx);
2095        multibuffer.add_diff(diff_2.clone(), cx);
2096        multibuffer
2097    });
2098
2099    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
2100        (multibuffer.snapshot(cx), multibuffer.subscribe())
2101    });
2102    assert_eq!(
2103        snapshot.text(),
2104        indoc!(
2105            "
2106            ZERO
2107            one
2108            TWO
2109                three
2110            six
2111
2112              eight
2113            nine
2114            eleven
2115            THIRTEEN
2116            FOURTEEN
2117            "
2118        ),
2119    );
2120
2121    multibuffer.update(cx, |multibuffer, cx| {
2122        multibuffer.expand_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
2123    });
2124
2125    assert_new_snapshot(
2126        &multibuffer,
2127        &mut snapshot,
2128        &mut subscription,
2129        cx,
2130        indoc!(
2131            "
2132            + ZERO
2133              one
2134            - two
2135            + TWO
2136                  three
2137            - four
2138            - five
2139              six
2140
2141            - seven
2142                eight
2143              nine
2144            - ten
2145              eleven
2146            - twelve
2147            + THIRTEEN
2148            + FOURTEEN
2149            "
2150        ),
2151    );
2152
2153    let id_1 = buffer_1.read_with(cx, |buffer, _| buffer.remote_id());
2154    let id_2 = buffer_2.read_with(cx, |buffer, _| buffer.remote_id());
2155    let base_id_1 = diff_1.read_with(cx, |diff, cx| diff.base_text(cx).remote_id());
2156    let base_id_2 = diff_2.read_with(cx, |diff, cx| diff.base_text(cx).remote_id());
2157
2158    let buffer_lines = (0..=snapshot.max_row().0)
2159        .map(|row| {
2160            let (buffer, range) = snapshot.buffer_line_for_row(MultiBufferRow(row))?;
2161            Some((
2162                buffer.remote_id(),
2163                buffer.text_for_range(range).collect::<String>(),
2164            ))
2165        })
2166        .collect::<Vec<_>>();
2167    pretty_assertions::assert_eq!(
2168        buffer_lines,
2169        [
2170            Some((id_1, "ZERO".into())),
2171            Some((id_1, "one".into())),
2172            Some((base_id_1, "two".into())),
2173            Some((id_1, "TWO".into())),
2174            Some((id_1, "    three".into())),
2175            Some((base_id_1, "four".into())),
2176            Some((base_id_1, "five".into())),
2177            Some((id_1, "six".into())),
2178            Some((id_1, "".into())),
2179            Some((base_id_2, "seven".into())),
2180            Some((id_2, "  eight".into())),
2181            Some((id_2, "nine".into())),
2182            Some((base_id_2, "ten".into())),
2183            Some((id_2, "eleven".into())),
2184            Some((base_id_2, "twelve".into())),
2185            Some((id_2, "THIRTEEN".into())),
2186            Some((id_2, "FOURTEEN".into())),
2187            Some((id_2, "".into())),
2188        ]
2189    );
2190
2191    let buffer_ids_by_range = [
2192        (Point::new(0, 0)..Point::new(0, 0), &[id_1] as &[_]),
2193        (Point::new(0, 0)..Point::new(2, 0), &[id_1]),
2194        (Point::new(2, 0)..Point::new(2, 0), &[id_1]),
2195        (Point::new(3, 0)..Point::new(3, 0), &[id_1]),
2196        (Point::new(8, 0)..Point::new(9, 0), &[id_1]),
2197        (Point::new(8, 0)..Point::new(10, 0), &[id_1, id_2]),
2198        (Point::new(9, 0)..Point::new(9, 0), &[id_2]),
2199    ];
2200    for (range, buffer_ids) in buffer_ids_by_range {
2201        assert_eq!(
2202            snapshot
2203                .buffer_ids_for_range(range.clone())
2204                .collect::<Vec<_>>(),
2205            buffer_ids,
2206            "buffer_ids_for_range({range:?}"
2207        );
2208    }
2209
2210    assert_position_translation(&snapshot);
2211    assert_line_indents(&snapshot);
2212
2213    assert_eq!(
2214        snapshot
2215            .diff_hunks_in_range(MultiBufferOffset(0)..snapshot.len())
2216            .map(|hunk| hunk.row_range.start.0..hunk.row_range.end.0)
2217            .collect::<Vec<_>>(),
2218        &[0..1, 2..4, 5..7, 9..10, 12..13, 14..17]
2219    );
2220
2221    buffer_2.update(cx, |buffer, cx| {
2222        buffer.edit_via_marked_text(
2223            indoc!(
2224                "
2225                  eight
2226                «»eleven
2227                THIRTEEN
2228                FOURTEEN
2229                "
2230            ),
2231            None,
2232            cx,
2233        );
2234    });
2235
2236    assert_new_snapshot(
2237        &multibuffer,
2238        &mut snapshot,
2239        &mut subscription,
2240        cx,
2241        indoc!(
2242            "
2243            + ZERO
2244              one
2245            - two
2246            + TWO
2247                  three
2248            - four
2249            - five
2250              six
2251
2252            - seven
2253                eight
2254              eleven
2255            - twelve
2256            + THIRTEEN
2257            + FOURTEEN
2258            "
2259        ),
2260    );
2261
2262    assert_line_indents(&snapshot);
2263}
2264
2265/// A naive implementation of a multi-buffer that does not maintain
2266/// any derived state, used for comparison in a randomized test.
2267#[derive(Default)]
2268struct ReferenceMultibuffer {
2269    excerpts: Vec<ReferenceExcerpt>,
2270    diffs: HashMap<BufferId, Entity<BufferDiff>>,
2271    inverted_diffs: HashMap<BufferId, (Entity<BufferDiff>, Entity<language::Buffer>)>,
2272    expanded_diff_hunks_by_buffer: HashMap<BufferId, Vec<text::Anchor>>,
2273}
2274
2275#[derive(Debug)]
2276struct ReferenceExcerpt {
2277    path_key: PathKey,
2278    path_key_index: PathKeyIndex,
2279    buffer: Entity<Buffer>,
2280    range: Range<text::Anchor>,
2281}
2282
2283#[derive(Clone, Debug)]
2284struct ReferenceRegion {
2285    buffer_id: Option<BufferId>,
2286    range: Range<usize>,
2287    buffer_range: Option<Range<Point>>,
2288    status: Option<DiffHunkStatus>,
2289    excerpt_info: Option<ExcerptInfo>,
2290}
2291
2292impl ReferenceExcerpt {
2293    fn info(&self, cx: &App) -> ExcerptInfo {
2294        ExcerptInfo {
2295            path_key_index: self.path_key_index,
2296            buffer_id: self.buffer.read(cx).remote_id(),
2297            range: ExcerptRange::new(self.range.clone()),
2298        }
2299    }
2300}
2301
2302impl ReferenceMultibuffer {
2303    fn expand_excerpts(&mut self, excerpts: &HashSet<ExcerptInfo>, line_count: u32, cx: &App) {
2304        if line_count == 0 {
2305            return;
2306        }
2307
2308        for info in excerpts {
2309            let excerpt = self
2310                .excerpts
2311                .iter_mut()
2312                .find(|e| &e.info(cx) == info)
2313                .unwrap();
2314            let snapshot = excerpt.buffer.read(cx).snapshot();
2315            let mut point_range = excerpt.range.to_point(&snapshot);
2316            point_range.start = Point::new(point_range.start.row.saturating_sub(line_count), 0);
2317            point_range.end =
2318                snapshot.clip_point(Point::new(point_range.end.row + line_count, 0), Bias::Left);
2319            point_range.end.column = snapshot.line_len(point_range.end.row);
2320            excerpt.range =
2321                snapshot.anchor_before(point_range.start)..snapshot.anchor_after(point_range.end);
2322        }
2323    }
2324
2325    fn set_excerpts(
2326        &mut self,
2327        path_key: PathKey,
2328        path_key_index: PathKeyIndex,
2329        buffer: Entity<Buffer>,
2330        buffer_snapshot: &BufferSnapshot,
2331        ranges: Vec<ExcerptRange<Point>>,
2332    ) {
2333        self.excerpts.retain(|excerpt| {
2334            excerpt.path_key != path_key && excerpt.buffer.entity_id() != buffer.entity_id()
2335        });
2336
2337        let ranges = MultiBuffer::merge_excerpt_ranges(&ranges);
2338
2339        let (Ok(ix) | Err(ix)) = self
2340            .excerpts
2341            .binary_search_by(|probe| probe.path_key.cmp(&path_key));
2342        self.excerpts.splice(
2343            ix..ix,
2344            ranges.into_iter().map(|range| ReferenceExcerpt {
2345                path_key: path_key.clone(),
2346                path_key_index,
2347                buffer: buffer.clone(),
2348                range: buffer_snapshot.anchor_range_around(range.context),
2349            }),
2350        );
2351    }
2352
2353    fn expand_diff_hunks(&mut self, path_key: PathKey, range: Range<text::Anchor>, cx: &App) {
2354        let excerpt = self
2355            .excerpts
2356            .iter_mut()
2357            .find(|e| {
2358                e.path_key == path_key
2359                    && e.range
2360                        .start
2361                        .cmp(&range.start, &e.buffer.read(cx).snapshot())
2362                        .is_le()
2363            })
2364            .unwrap();
2365        let buffer = excerpt.buffer.read(cx).snapshot();
2366        let buffer_id = buffer.remote_id();
2367
2368        // Skip inverted excerpts - hunks are always expanded
2369        if self.inverted_diffs.contains_key(&buffer_id) {
2370            return;
2371        }
2372
2373        let Some(diff) = self.diffs.get(&buffer_id) else {
2374            return;
2375        };
2376        let excerpt_range = excerpt.range.to_offset(&buffer);
2377        let expanded_diff_hunks = self
2378            .expanded_diff_hunks_by_buffer
2379            .entry(buffer_id)
2380            .or_default();
2381        for hunk in diff
2382            .read(cx)
2383            .snapshot(cx)
2384            .hunks_intersecting_range(range, &buffer)
2385        {
2386            let hunk_range = hunk.buffer_range.to_offset(&buffer);
2387            if hunk_range.start < excerpt_range.start || hunk_range.start > excerpt_range.end {
2388                continue;
2389            }
2390            if let Err(ix) = expanded_diff_hunks
2391                .binary_search_by(|anchor| anchor.cmp(&hunk.buffer_range.start, &buffer))
2392            {
2393                log::info!(
2394                    "expanding diff hunk {:?}. excerpt range:{:?}",
2395                    hunk_range,
2396                    excerpt_range
2397                );
2398                expanded_diff_hunks.insert(ix, hunk.buffer_range.start);
2399            } else {
2400                log::trace!("hunk {hunk_range:?} already expanded in excerpt");
2401            }
2402        }
2403    }
2404
2405    fn expected_content(&self, cx: &App) -> (String, Vec<RowInfo>, HashSet<MultiBufferRow>) {
2406        let mut text = String::new();
2407        let mut regions = Vec::<ReferenceRegion>::new();
2408        let mut excerpt_boundary_rows = HashSet::default();
2409        for excerpt in &self.excerpts {
2410            excerpt_boundary_rows.insert(MultiBufferRow(text.matches('\n').count() as u32));
2411            let buffer = excerpt.buffer.read(cx);
2412            let buffer_id = buffer.remote_id();
2413            let buffer_range = excerpt.range.to_offset(buffer);
2414
2415            if let Some((diff, main_buffer)) = self.inverted_diffs.get(&buffer_id) {
2416                let diff_snapshot = diff.read(cx).snapshot(cx);
2417                let main_buffer_snapshot = main_buffer.read(cx).snapshot();
2418
2419                let mut offset = buffer_range.start;
2420                for hunk in diff_snapshot.hunks_intersecting_base_text_range(
2421                    buffer_range.clone(),
2422                    &main_buffer_snapshot.text,
2423                ) {
2424                    let mut hunk_base_range = hunk.diff_base_byte_range.clone();
2425
2426                    hunk_base_range.end = hunk_base_range.end.min(buffer_range.end);
2427                    if hunk_base_range.start > buffer_range.end
2428                        || hunk_base_range.start < buffer_range.start
2429                    {
2430                        continue;
2431                    }
2432
2433                    // Add the text before the hunk
2434                    if hunk_base_range.start >= offset {
2435                        let len = text.len();
2436                        text.extend(buffer.text_for_range(offset..hunk_base_range.start));
2437                        if text.len() > len {
2438                            regions.push(ReferenceRegion {
2439                                buffer_id: Some(buffer_id),
2440                                range: len..text.len(),
2441                                buffer_range: Some(
2442                                    (offset..hunk_base_range.start).to_point(&buffer),
2443                                ),
2444                                status: None,
2445                                excerpt_info: Some(excerpt.info(cx)),
2446                            });
2447                        }
2448                    }
2449
2450                    // Add the "deleted" region (base text that's not in main)
2451                    if !hunk_base_range.is_empty() {
2452                        let len = text.len();
2453                        text.extend(buffer.text_for_range(hunk_base_range.clone()));
2454                        regions.push(ReferenceRegion {
2455                            buffer_id: Some(buffer_id),
2456                            range: len..text.len(),
2457                            buffer_range: Some(hunk_base_range.to_point(&buffer)),
2458                            status: Some(DiffHunkStatus::deleted(hunk.secondary_status)),
2459                            excerpt_info: Some(excerpt.info(cx)),
2460                        });
2461                    }
2462
2463                    offset = hunk_base_range.end;
2464                }
2465
2466                // Add remaining buffer text
2467                let len = text.len();
2468                text.extend(buffer.text_for_range(offset..buffer_range.end));
2469                text.push('\n');
2470                regions.push(ReferenceRegion {
2471                    buffer_id: Some(buffer_id),
2472                    range: len..text.len(),
2473                    buffer_range: Some((offset..buffer_range.end).to_point(&buffer)),
2474                    status: None,
2475                    excerpt_info: Some(excerpt.info(cx)),
2476                });
2477            } else {
2478                let diff = self.diffs.get(&buffer_id).unwrap().read(cx).snapshot(cx);
2479                let base_buffer = diff.base_text();
2480
2481                let mut offset = buffer_range.start;
2482                let hunks = diff
2483                    .hunks_intersecting_range(excerpt.range.clone(), buffer)
2484                    .peekable();
2485
2486                for hunk in hunks {
2487                    // Ignore hunks that are outside the excerpt range.
2488                    let mut hunk_range = hunk.buffer_range.to_offset(buffer);
2489
2490                    hunk_range.end = hunk_range.end.min(buffer_range.end);
2491                    if hunk_range.start > buffer_range.end || hunk_range.start < buffer_range.start
2492                    {
2493                        log::trace!("skipping hunk outside excerpt range");
2494                        continue;
2495                    }
2496
2497                    if !self
2498                        .expanded_diff_hunks_by_buffer
2499                        .get(&buffer_id)
2500                        .cloned()
2501                        .into_iter()
2502                        .flatten()
2503                        .any(|expanded_anchor| {
2504                            expanded_anchor.to_offset(buffer).max(buffer_range.start)
2505                                == hunk_range.start.max(buffer_range.start)
2506                        })
2507                    {
2508                        log::trace!("skipping a hunk that's not marked as expanded");
2509                        continue;
2510                    }
2511
2512                    if !hunk.buffer_range.start.is_valid(buffer) {
2513                        log::trace!("skipping hunk with deleted start: {:?}", hunk.range);
2514                        continue;
2515                    }
2516
2517                    if hunk_range.start >= offset {
2518                        // Add the buffer text before the hunk
2519                        let len = text.len();
2520                        text.extend(buffer.text_for_range(offset..hunk_range.start));
2521                        if text.len() > len {
2522                            regions.push(ReferenceRegion {
2523                                buffer_id: Some(buffer_id),
2524                                range: len..text.len(),
2525                                buffer_range: Some((offset..hunk_range.start).to_point(&buffer)),
2526                                status: None,
2527                                excerpt_info: Some(excerpt.info(cx)),
2528                            });
2529                        }
2530
2531                        // Add the deleted text for the hunk.
2532                        if !hunk.diff_base_byte_range.is_empty() {
2533                            let mut base_text = base_buffer
2534                                .text_for_range(hunk.diff_base_byte_range.clone())
2535                                .collect::<String>();
2536                            if !base_text.ends_with('\n') {
2537                                base_text.push('\n');
2538                            }
2539                            let len = text.len();
2540                            text.push_str(&base_text);
2541                            regions.push(ReferenceRegion {
2542                                buffer_id: Some(base_buffer.remote_id()),
2543                                range: len..text.len(),
2544                                buffer_range: Some(
2545                                    hunk.diff_base_byte_range.to_point(&base_buffer),
2546                                ),
2547                                status: Some(DiffHunkStatus::deleted(hunk.secondary_status)),
2548                                excerpt_info: Some(excerpt.info(cx)),
2549                            });
2550                        }
2551
2552                        offset = hunk_range.start;
2553                    }
2554
2555                    // Add the inserted text for the hunk.
2556                    if hunk_range.end > offset {
2557                        let len = text.len();
2558                        text.extend(buffer.text_for_range(offset..hunk_range.end));
2559                        let range = len..text.len();
2560                        let region = ReferenceRegion {
2561                            buffer_id: Some(buffer_id),
2562                            range,
2563                            buffer_range: Some((offset..hunk_range.end).to_point(&buffer)),
2564                            status: Some(DiffHunkStatus::added(hunk.secondary_status)),
2565                            excerpt_info: Some(excerpt.info(cx)),
2566                        };
2567                        offset = hunk_range.end;
2568                        regions.push(region);
2569                    }
2570                }
2571
2572                // Add the buffer text for the rest of the excerpt.
2573                let len = text.len();
2574                text.extend(buffer.text_for_range(offset..buffer_range.end));
2575                text.push('\n');
2576                regions.push(ReferenceRegion {
2577                    buffer_id: Some(buffer_id),
2578                    range: len..text.len(),
2579                    buffer_range: Some((offset..buffer_range.end).to_point(&buffer)),
2580                    status: None,
2581                    excerpt_info: Some(excerpt.info(cx)),
2582                });
2583            }
2584        }
2585
2586        // Remove final trailing newline.
2587        if self.excerpts.is_empty() {
2588            regions.push(ReferenceRegion {
2589                buffer_id: None,
2590                range: 0..1,
2591                buffer_range: Some(Point::new(0, 0)..Point::new(0, 1)),
2592                status: None,
2593                excerpt_info: None,
2594            });
2595        } else {
2596            text.pop();
2597        }
2598
2599        // Retrieve the row info using the region that contains
2600        // the start of each multi-buffer line.
2601        let mut ix = 0;
2602        let row_infos = text
2603            .split('\n')
2604            .map(|line| {
2605                let row_info = regions
2606                    .iter()
2607                    .position(|region| region.range.contains(&ix))
2608                    .map_or(RowInfo::default(), |region_ix| {
2609                        let region = regions[region_ix].clone();
2610                        let buffer_row = region.buffer_range.as_ref().map(|buffer_range| {
2611                            buffer_range.start.row
2612                                + text[region.range.start..ix].matches('\n').count() as u32
2613                        });
2614                        let main_buffer = self
2615                            .excerpts
2616                            .iter()
2617                            .find(|e| e.info(cx) == region.excerpt_info.clone().unwrap())
2618                            .map(|e| e.buffer.clone());
2619                        let is_excerpt_start = region_ix == 0
2620                            || &regions[region_ix - 1].excerpt_info != &region.excerpt_info
2621                            || regions[region_ix - 1].range.is_empty();
2622                        let mut is_excerpt_end = region_ix == regions.len() - 1
2623                            || &regions[region_ix + 1].excerpt_info != &region.excerpt_info;
2624                        let is_start = !text[region.range.start..ix].contains('\n');
2625                        let mut is_end = if region.range.end > text.len() {
2626                            !text[ix..].contains('\n')
2627                        } else {
2628                            text[ix..region.range.end.min(text.len())]
2629                                .matches('\n')
2630                                .count()
2631                                == 1
2632                        };
2633                        if region_ix < regions.len() - 1
2634                            && !text[ix..].contains("\n")
2635                            && (region.status == Some(DiffHunkStatus::added_none())
2636                                || region.status.is_some_and(|s| s.is_deleted()))
2637                            && regions[region_ix + 1].excerpt_info == region.excerpt_info
2638                            && regions[region_ix + 1].range.start == text.len()
2639                        {
2640                            is_end = true;
2641                            is_excerpt_end = true;
2642                        }
2643                        let multibuffer_row =
2644                            MultiBufferRow(text[..ix].matches('\n').count() as u32);
2645                        let mut expand_direction = None;
2646                        if let Some(buffer) = &main_buffer {
2647                            let buffer_row = buffer_row.unwrap();
2648                            let needs_expand_up = is_excerpt_start && is_start && buffer_row > 0;
2649                            let needs_expand_down = is_excerpt_end
2650                                && is_end
2651                                && buffer.read(cx).max_point().row > buffer_row;
2652                            expand_direction = if needs_expand_up && needs_expand_down {
2653                                Some(ExpandExcerptDirection::UpAndDown)
2654                            } else if needs_expand_up {
2655                                Some(ExpandExcerptDirection::Up)
2656                            } else if needs_expand_down {
2657                                Some(ExpandExcerptDirection::Down)
2658                            } else {
2659                                None
2660                            };
2661                        }
2662                        RowInfo {
2663                            buffer_id: region.buffer_id,
2664                            diff_status: region.status,
2665                            buffer_row,
2666                            wrapped_buffer_row: None,
2667
2668                            multibuffer_row: Some(multibuffer_row),
2669                            expand_info: expand_direction.zip(region.excerpt_info.clone()).map(
2670                                |(direction, excerpt_info)| ExpandInfo {
2671                                    direction,
2672                                    excerpt_range: excerpt_info.range.context,
2673                                },
2674                            ),
2675                        }
2676                    });
2677                ix += line.len() + 1;
2678                row_info
2679            })
2680            .collect();
2681
2682        (text, row_infos, excerpt_boundary_rows)
2683    }
2684
2685    fn diffs_updated(&mut self, cx: &App) {
2686        for excerpt in &mut self.excerpts {
2687            let buffer = excerpt.buffer.read(cx).snapshot();
2688            let buffer_id = buffer.remote_id();
2689
2690            // Skip inverted diff excerpts - hunks are always expanded
2691            if self.inverted_diffs.contains_key(&buffer_id) {
2692                continue;
2693            }
2694
2695            let excerpt_range = excerpt.range.to_offset(&buffer);
2696            let Some(diff) = self.diffs.get(&buffer_id) else {
2697                continue;
2698            };
2699            let diff = diff.read(cx).snapshot(cx);
2700            let mut hunks = diff.hunks_in_row_range(0..u32::MAX, &buffer).peekable();
2701            self.expanded_diff_hunks_by_buffer
2702                .entry(buffer_id)
2703                .or_default()
2704                .retain(|hunk_anchor| {
2705                    if !hunk_anchor.is_valid(&buffer) {
2706                        return false;
2707                    }
2708                    while let Some(hunk) = hunks.peek() {
2709                        match hunk.buffer_range.start.cmp(hunk_anchor, &buffer) {
2710                            cmp::Ordering::Less => {
2711                                hunks.next();
2712                            }
2713                            cmp::Ordering::Equal => {
2714                                let hunk_range = hunk.buffer_range.to_offset(&buffer);
2715                                return hunk_range.end >= excerpt_range.start
2716                                    && hunk_range.start <= excerpt_range.end;
2717                            }
2718                            cmp::Ordering::Greater => break,
2719                        }
2720                    }
2721                    false
2722                });
2723        }
2724    }
2725
2726    fn add_diff(&mut self, diff: Entity<BufferDiff>, cx: &mut App) {
2727        let buffer_id = diff.read(cx).buffer_id;
2728        self.diffs.insert(buffer_id, diff);
2729    }
2730
2731    fn add_inverted_diff(
2732        &mut self,
2733        diff: Entity<BufferDiff>,
2734        main_buffer: Entity<language::Buffer>,
2735        cx: &App,
2736    ) {
2737        let base_text_buffer_id = diff.read(cx).base_text(cx).remote_id();
2738        self.inverted_diffs
2739            .insert(base_text_buffer_id, (diff, main_buffer));
2740    }
2741}
2742
2743#[gpui::test(iterations = 100)]
2744async fn test_random_set_ranges(cx: &mut TestAppContext, mut rng: StdRng) {
2745    let base_text = "a\n".repeat(100);
2746    let buf = cx.update(|cx| cx.new(|cx| Buffer::local(base_text, cx)));
2747    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
2748
2749    let operations = env::var("OPERATIONS")
2750        .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
2751        .unwrap_or(10);
2752
2753    fn row_ranges(ranges: &Vec<Range<Point>>) -> Vec<Range<u32>> {
2754        ranges
2755            .iter()
2756            .map(|range| range.start.row..range.end.row)
2757            .collect()
2758    }
2759
2760    for _ in 0..operations {
2761        let snapshot = buf.update(cx, |buf, _| buf.snapshot());
2762        let num_ranges = rng.random_range(0..=10);
2763        let max_row = snapshot.max_point().row;
2764        let mut ranges = (0..num_ranges)
2765            .map(|_| {
2766                let start = rng.random_range(0..max_row);
2767                let end = rng.random_range(start + 1..max_row + 1);
2768                Point::row_range(start..end)
2769            })
2770            .collect::<Vec<_>>();
2771        ranges.sort_by_key(|range| range.start);
2772        log::info!("Setting ranges: {:?}", row_ranges(&ranges));
2773        let (created, _) = multibuffer.update(cx, |multibuffer, cx| {
2774            multibuffer.set_excerpts_for_path(
2775                PathKey::for_buffer(&buf, cx),
2776                buf.clone(),
2777                ranges.clone(),
2778                2,
2779                cx,
2780            )
2781        });
2782
2783        assert_eq!(created.len(), ranges.len());
2784
2785        let snapshot = multibuffer.update(cx, |multibuffer, cx| multibuffer.snapshot(cx));
2786        let mut last_end = None;
2787        let mut seen_ranges = Vec::default();
2788
2789        for (buf, info) in snapshot.excerpts() {
2790            let start = info.range.context.start.to_point(buf);
2791            let end = info.range.context.end.to_point(buf);
2792            seen_ranges.push(start..end);
2793
2794            if let Some(last_end) = last_end.take() {
2795                assert!(
2796                    start > last_end,
2797                    "multibuffer has out-of-order ranges: {:?}; {:?} <= {:?}",
2798                    row_ranges(&seen_ranges),
2799                    start,
2800                    last_end
2801                )
2802            }
2803
2804            ranges.retain(|range| range.start < start || range.end > end);
2805
2806            last_end = Some(end)
2807        }
2808
2809        assert!(
2810            ranges.is_empty(),
2811            "multibuffer {:?} did not include all ranges: {:?}",
2812            row_ranges(&seen_ranges),
2813            row_ranges(&ranges)
2814        );
2815    }
2816}
2817
2818#[gpui::test(iterations = 100)]
2819async fn test_random_multibuffer(cx: &mut TestAppContext, mut rng: StdRng) {
2820    let operations = env::var("OPERATIONS")
2821        .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
2822        .unwrap_or(10);
2823    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
2824    let mut buffers: Vec<Entity<Buffer>> = Vec::new();
2825    let mut base_texts: HashMap<BufferId, String> = HashMap::default();
2826    let mut reference = ReferenceMultibuffer::default();
2827    let mut anchors = Vec::new();
2828    let mut old_versions = Vec::new();
2829    let mut needs_diff_calculation = false;
2830    let mut inverted_diff_main_buffers: HashMap<BufferId, Entity<BufferDiff>> = HashMap::default();
2831    for _ in 0..operations {
2832        match rng.random_range(0..100) {
2833            0..=14 if !buffers.is_empty() => {
2834                let buffer = buffers.choose(&mut rng).unwrap();
2835                buffer.update(cx, |buf, cx| {
2836                    let edit_count = rng.random_range(1..5);
2837                    buf.randomly_edit(&mut rng, edit_count, cx);
2838                    log::info!("buffer text:\n{}", buf.text());
2839                    needs_diff_calculation = true;
2840                });
2841                cx.update(|cx| reference.diffs_updated(cx));
2842            }
2843            15..=24 if !reference.excerpts.is_empty() => {
2844                multibuffer.update(cx, |multibuffer, cx| {
2845                    let snapshot = multibuffer.snapshot(cx);
2846                    let infos = snapshot
2847                        .excerpts()
2848                        .map(|(_, info)| info)
2849                        .collect::<Vec<_>>();
2850                    let mut excerpts = HashSet::default();
2851                    for _ in 0..rng.random_range(0..infos.len()) {
2852                        excerpts.extend(infos.choose(&mut rng).cloned());
2853                    }
2854
2855                    let line_count = rng.random_range(0..5);
2856
2857                    let excerpt_ixs = excerpts
2858                        .iter()
2859                        .map(|info| {
2860                            reference
2861                                .excerpts
2862                                .iter()
2863                                .position(|e| e.info(cx) == *info)
2864                                .unwrap()
2865                        })
2866                        .collect::<Vec<_>>();
2867                    log::info!("Expanding excerpts {excerpt_ixs:?} by {line_count} lines");
2868                    multibuffer.expand_excerpts_with_paths(
2869                        excerpts.iter().map(|info| {
2870                            Anchor::in_buffer(info.path_key_index, info.range.context.end)
2871                        }),
2872                        line_count,
2873                        ExpandExcerptDirection::UpAndDown,
2874                        cx,
2875                    );
2876
2877                    reference.expand_excerpts(&excerpts, line_count, cx);
2878                });
2879            }
2880            25..=34 if !reference.excerpts.is_empty() => {
2881                let multibuffer =
2882                    multibuffer.read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx));
2883                let offset = multibuffer.clip_offset(
2884                    MultiBufferOffset(rng.random_range(0..=multibuffer.len().0)),
2885                    Bias::Left,
2886                );
2887                let bias = if rng.random() {
2888                    Bias::Left
2889                } else {
2890                    Bias::Right
2891                };
2892                log::info!("Creating anchor at {} with bias {:?}", offset.0, bias);
2893                anchors.push(multibuffer.anchor_at(offset, bias));
2894                anchors.sort_by(|a, b| a.cmp(b, &multibuffer));
2895            }
2896            35..=45 if !reference.excerpts.is_empty() => {
2897                multibuffer.update(cx, |multibuffer, cx| {
2898                    let snapshot = multibuffer.snapshot(cx);
2899                    let excerpt_ix = rng.random_range(0..reference.excerpts.len());
2900                    let excerpt = &reference.excerpts[excerpt_ix];
2901
2902                    // Skip inverted excerpts - hunks can't be collapsed
2903                    let buffer_id = excerpt.buffer.read(cx).remote_id();
2904                    if reference.inverted_diffs.contains_key(&buffer_id) {
2905                        return;
2906                    }
2907
2908                    let start = excerpt.range.start;
2909                    let end = excerpt.range.end;
2910                    let range = snapshot.anchor_in_buffer(excerpt.info(cx).buffer_id, start).unwrap()
2911                        ..snapshot.anchor_in_buffer(excerpt.info(cx).buffer_id, end).unwrap();
2912
2913                    log::info!(
2914                        "expanding diff hunks in range {:?} (excerpt index {excerpt_ix:?}, buffer id {:?})",
2915                        range.to_offset(&snapshot),
2916                        buffer_id,
2917                    );
2918                    reference.expand_diff_hunks(excerpt.path_key.clone(), start..end, cx);
2919                    multibuffer.expand_diff_hunks(vec![range], cx);
2920                });
2921            }
2922            46..=75 if needs_diff_calculation => {
2923                multibuffer.update(cx, |multibuffer, cx| {
2924                    for buffer in multibuffer.all_buffers() {
2925                        let snapshot = buffer.read(cx).snapshot();
2926                        let buffer_id = snapshot.remote_id();
2927
2928                        if let Some(diff) = multibuffer.diff_for(buffer_id) {
2929                            diff.update(cx, |diff, cx| {
2930                                log::info!("recalculating diff for buffer {:?}", buffer_id,);
2931                                diff.recalculate_diff_sync(&snapshot.text, cx);
2932                            });
2933                        }
2934
2935                        if let Some(inverted_diff) = inverted_diff_main_buffers.get(&buffer_id) {
2936                            inverted_diff.update(cx, |diff, cx| {
2937                                log::info!(
2938                                    "recalculating inverted diff for main buffer {:?}",
2939                                    buffer_id,
2940                                );
2941                                diff.recalculate_diff_sync(&snapshot.text, cx);
2942                            });
2943                        }
2944                    }
2945                    reference.diffs_updated(cx);
2946                    needs_diff_calculation = false;
2947                });
2948            }
2949            _ => {
2950                // Decide if we're creating a new buffer or reusing an existing one
2951                let create_new_buffer = buffers.is_empty() || rng.random_bool(0.4);
2952
2953                let (excerpt_buffer, diff, inverted_main_buffer) = if create_new_buffer {
2954                    let create_inverted = rng.random_bool(0.3);
2955
2956                    if create_inverted {
2957                        let mut main_buffer_text = util::RandomCharIter::new(&mut rng)
2958                            .take(256)
2959                            .collect::<String>();
2960                        let main_buffer = cx.new(|cx| Buffer::local(main_buffer_text.clone(), cx));
2961                        text::LineEnding::normalize(&mut main_buffer_text);
2962                        let main_buffer_id =
2963                            main_buffer.read_with(cx, |buffer, _| buffer.remote_id());
2964                        base_texts.insert(main_buffer_id, main_buffer_text.clone());
2965                        buffers.push(main_buffer.clone());
2966
2967                        let diff = cx.new(|cx| {
2968                            BufferDiff::new_with_base_text(
2969                                &main_buffer_text,
2970                                &main_buffer.read(cx).text_snapshot(),
2971                                cx,
2972                            )
2973                        });
2974
2975                        let base_text_buffer =
2976                            diff.read_with(cx, |diff, _| diff.base_text_buffer().clone());
2977
2978                        // Track for recalculation when main buffer is edited
2979                        inverted_diff_main_buffers.insert(main_buffer_id, diff.clone());
2980
2981                        (base_text_buffer, diff, Some(main_buffer))
2982                    } else {
2983                        let mut base_text = util::RandomCharIter::new(&mut rng)
2984                            .take(256)
2985                            .collect::<String>();
2986
2987                        let buffer_handle = cx.new(|cx| Buffer::local(base_text.clone(), cx));
2988                        text::LineEnding::normalize(&mut base_text);
2989                        let buffer_id = buffer_handle.read_with(cx, |buffer, _| buffer.remote_id());
2990                        base_texts.insert(buffer_id, base_text.clone());
2991                        buffers.push(buffer_handle.clone());
2992
2993                        let diff = cx.new(|cx| {
2994                            BufferDiff::new_with_base_text(
2995                                &base_text,
2996                                &buffer_handle.read(cx).text_snapshot(),
2997                                cx,
2998                            )
2999                        });
3000
3001                        (buffer_handle, diff, None)
3002                    }
3003                } else {
3004                    // Reuse an existing buffer
3005                    let buffer_handle = buffers.choose(&mut rng).unwrap().clone();
3006                    let buffer_id = buffer_handle.read_with(cx, |buffer, _| buffer.remote_id());
3007
3008                    if let Some(diff) = inverted_diff_main_buffers.get(&buffer_id) {
3009                        let base_text_buffer =
3010                            diff.read_with(cx, |diff, _| diff.base_text_buffer().clone());
3011                        (base_text_buffer, diff.clone(), Some(buffer_handle))
3012                    } else {
3013                        // Get existing diff or create new one for regular buffer
3014                        let diff = multibuffer
3015                            .read_with(cx, |mb, _| mb.diff_for(buffer_id))
3016                            .unwrap_or_else(|| {
3017                                let base_text = base_texts.get(&buffer_id).unwrap();
3018                                cx.new(|cx| {
3019                                    BufferDiff::new_with_base_text(
3020                                        base_text,
3021                                        &buffer_handle.read(cx).text_snapshot(),
3022                                        cx,
3023                                    )
3024                                })
3025                            });
3026                        (buffer_handle, diff, None)
3027                    }
3028                };
3029
3030                let excerpt_buffer_snapshot =
3031                    excerpt_buffer.read_with(cx, |excerpt_buffer, _| excerpt_buffer.snapshot());
3032                let mut ranges = reference
3033                    .excerpts
3034                    .iter()
3035                    .filter(|excerpt| excerpt.buffer == excerpt_buffer)
3036                    .map(|excerpt| excerpt.range.to_point(&excerpt_buffer_snapshot))
3037                    .collect::<Vec<_>>();
3038                mutate_excerpt_ranges(&mut rng, &mut ranges, &excerpt_buffer_snapshot, 1);
3039                let ranges = ranges
3040                    .iter()
3041                    .cloned()
3042                    .map(ExcerptRange::new)
3043                    .collect::<Vec<_>>();
3044                let path = cx.update(|cx| PathKey::for_buffer(&excerpt_buffer, cx));
3045
3046                let (_, path_key_index, _) = multibuffer.update(cx, |multibuffer, cx| {
3047                    multibuffer.set_excerpt_ranges_for_path(
3048                        path.clone(),
3049                        excerpt_buffer.clone(),
3050                        &excerpt_buffer_snapshot,
3051                        ranges.clone(),
3052                        cx,
3053                    )
3054                });
3055
3056                reference.set_excerpts(
3057                    path,
3058                    path_key_index,
3059                    excerpt_buffer.clone(),
3060                    &excerpt_buffer_snapshot,
3061                    ranges,
3062                );
3063
3064                let excerpt_buffer_id =
3065                    excerpt_buffer.read_with(cx, |buffer, _| buffer.remote_id());
3066                multibuffer.update(cx, |multibuffer, cx| {
3067                    if multibuffer.diff_for(excerpt_buffer_id).is_none() {
3068                        if let Some(main_buffer) = inverted_main_buffer {
3069                            reference.add_inverted_diff(diff.clone(), main_buffer.clone(), cx);
3070                            multibuffer.add_inverted_diff(diff, main_buffer, cx);
3071                        } else {
3072                            reference.add_diff(diff.clone(), cx);
3073                            multibuffer.add_diff(diff, cx);
3074                        }
3075                    }
3076                });
3077            }
3078        }
3079
3080        if rng.random_bool(0.3) {
3081            multibuffer.update(cx, |multibuffer, cx| {
3082                old_versions.push((multibuffer.snapshot(cx), multibuffer.subscribe()));
3083            })
3084        }
3085
3086        multibuffer.read_with(cx, |multibuffer, cx| {
3087            check_multibuffer(multibuffer, &reference, &anchors, cx, &mut rng);
3088        });
3089    }
3090    let snapshot = multibuffer.read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx));
3091    for (old_snapshot, subscription) in old_versions {
3092        check_multibuffer_edits(&snapshot, &old_snapshot, subscription);
3093    }
3094}
3095
3096fn mutate_excerpt_ranges(
3097    rng: &mut StdRng,
3098    existing_ranges: &mut Vec<Range<Point>>,
3099    buffer: &BufferSnapshot,
3100    operations: u32,
3101) {
3102    let mut ranges_to_add = Vec::new();
3103
3104    for _ in 0..operations {
3105        // todo!() logging
3106        match rng.random_range(0..5) {
3107            0..=1 if !existing_ranges.is_empty() => {
3108                log::info!("Removing excerpt",);
3109                let index = rng.random_range(0..existing_ranges.len());
3110                existing_ranges.remove(index);
3111            }
3112            _ => {
3113                log::info!("Inserting excerpt",);
3114                let end_row = rng.random_range(0..=buffer.max_point().row);
3115                let start_row = rng.random_range(0..=end_row);
3116                ranges_to_add.push(Point::new(start_row, 0)..Point::new(end_row, 0));
3117            }
3118        }
3119    }
3120
3121    existing_ranges.extend(ranges_to_add);
3122    existing_ranges.sort_by(|l, r| l.start.cmp(&r.start));
3123}
3124
3125fn check_multibuffer(
3126    multibuffer: &MultiBuffer,
3127    reference: &ReferenceMultibuffer,
3128    anchors: &[Anchor],
3129    cx: &App,
3130    rng: &mut StdRng,
3131) {
3132    let snapshot = multibuffer.snapshot(cx);
3133    let actual_text = snapshot.text();
3134    let actual_boundary_rows = snapshot
3135        .excerpt_boundaries_in_range(MultiBufferOffset(0)..)
3136        .map(|b| b.row)
3137        .collect::<HashSet<_>>();
3138    let actual_row_infos = snapshot.row_infos(MultiBufferRow(0)).collect::<Vec<_>>();
3139
3140    let (expected_text, expected_row_infos, expected_boundary_rows) =
3141        reference.expected_content(cx);
3142
3143    let has_diff = actual_row_infos
3144        .iter()
3145        .any(|info| info.diff_status.is_some())
3146        || expected_row_infos
3147            .iter()
3148            .any(|info| info.diff_status.is_some());
3149    let actual_diff = format_diff(
3150        &actual_text,
3151        &actual_row_infos,
3152        &actual_boundary_rows,
3153        Some(has_diff),
3154    );
3155    let expected_diff = format_diff(
3156        &expected_text,
3157        &expected_row_infos,
3158        &expected_boundary_rows,
3159        Some(has_diff),
3160    );
3161
3162    log::info!("Multibuffer content:\n{}", actual_diff);
3163
3164    assert_eq!(
3165        actual_row_infos.len(),
3166        actual_text.split('\n').count(),
3167        "line count: {}",
3168        actual_text.split('\n').count()
3169    );
3170    pretty_assertions::assert_eq!(actual_diff, expected_diff);
3171    pretty_assertions::assert_eq!(actual_text, expected_text);
3172    pretty_assertions::assert_eq!(actual_row_infos, expected_row_infos);
3173
3174    for _ in 0..5 {
3175        let start_row = rng.random_range(0..=expected_row_infos.len());
3176        assert_eq!(
3177            snapshot
3178                .row_infos(MultiBufferRow(start_row as u32))
3179                .collect::<Vec<_>>(),
3180            &expected_row_infos[start_row..],
3181            "buffer_rows({})",
3182            start_row
3183        );
3184    }
3185
3186    assert_eq!(
3187        snapshot.widest_line_number(),
3188        expected_row_infos
3189            .into_iter()
3190            .filter_map(|info| {
3191                // For inverted diffs, deleted rows are visible and should be counted.
3192                // Only filter out deleted rows that are NOT from inverted diffs.
3193                let is_inverted_diff = info
3194                    .buffer_id
3195                    .is_some_and(|id| reference.inverted_diffs.contains_key(&id));
3196                if info.diff_status.is_some_and(|status| status.is_deleted()) && !is_inverted_diff {
3197                    None
3198                } else {
3199                    info.buffer_row
3200                }
3201            })
3202            .max()
3203            .unwrap()
3204            + 1
3205    );
3206    // let reference_ranges = reference
3207    //     .excerpts
3208    //     .iter()
3209    //     .map(|excerpt| {
3210    //         (
3211    //             excerpt.info(cx),
3212    //             excerpt.range.to_offset(&excerpt.buffer.read(cx).snapshot()),
3213    //         )
3214    //     })
3215    //     .collect::<Vec<_>>();
3216    for i in 0..snapshot.len().0 {
3217        // todo!() this seems not useful
3218        let excerpt = snapshot
3219            .excerpt_containing(MultiBufferOffset(i)..MultiBufferOffset(i))
3220            .unwrap();
3221        let reference_range = reference
3222            .excerpts
3223            .iter()
3224            .find(|reference_excerpt| reference_excerpt.info(cx) == excerpt.excerpt.info())
3225            .map(|excerpt| excerpt.range.to_offset(&excerpt.buffer.read(cx).snapshot()))
3226            .expect("corresponding excerpt should exist in reference multibuffer");
3227        assert_eq!(
3228            excerpt.buffer_range(&snapshot).start.0..excerpt.buffer_range(&snapshot).end.0,
3229            reference_range
3230        );
3231    }
3232
3233    assert_consistent_line_numbers(&snapshot);
3234    assert_position_translation(&snapshot);
3235
3236    for (row, line) in expected_text.split('\n').enumerate() {
3237        assert_eq!(
3238            snapshot.line_len(MultiBufferRow(row as u32)),
3239            line.len() as u32,
3240            "line_len({}).",
3241            row
3242        );
3243    }
3244
3245    let text_rope = Rope::from(expected_text.as_str());
3246    for _ in 0..10 {
3247        let end_ix = text_rope.clip_offset(rng.random_range(0..=text_rope.len()), Bias::Right);
3248        let start_ix = text_rope.clip_offset(rng.random_range(0..=end_ix), Bias::Left);
3249
3250        let text_for_range = snapshot
3251            .text_for_range(MultiBufferOffset(start_ix)..MultiBufferOffset(end_ix))
3252            .collect::<String>();
3253        assert_eq!(
3254            text_for_range,
3255            &expected_text[start_ix..end_ix],
3256            "incorrect text for range {:?}",
3257            start_ix..end_ix
3258        );
3259
3260        let expected_summary =
3261            MBTextSummary::from(TextSummary::from(&expected_text[start_ix..end_ix]));
3262        assert_eq!(
3263            snapshot.text_summary_for_range::<MBTextSummary, _>(
3264                MultiBufferOffset(start_ix)..MultiBufferOffset(end_ix)
3265            ),
3266            expected_summary,
3267            "incorrect summary for range {:?}",
3268            start_ix..end_ix
3269        );
3270    }
3271
3272    // Anchor resolution
3273    let summaries = snapshot.summaries_for_anchors::<MultiBufferOffset, _>(anchors);
3274    assert_eq!(anchors.len(), summaries.len());
3275    for (anchor, resolved_offset) in anchors.iter().zip(summaries) {
3276        assert!(resolved_offset <= snapshot.len());
3277        assert_eq!(
3278            snapshot.summary_for_anchor::<MultiBufferOffset>(anchor),
3279            resolved_offset,
3280            "anchor: {:?}",
3281            anchor
3282        );
3283    }
3284
3285    for _ in 0..10 {
3286        let end_ix = text_rope.clip_offset(rng.random_range(0..=text_rope.len()), Bias::Right);
3287        assert_eq!(
3288            snapshot
3289                .reversed_chars_at(MultiBufferOffset(end_ix))
3290                .collect::<String>(),
3291            expected_text[..end_ix].chars().rev().collect::<String>(),
3292        );
3293    }
3294
3295    for _ in 0..10 {
3296        let end_ix = rng.random_range(0..=text_rope.len());
3297        let end_ix = text_rope.floor_char_boundary(end_ix);
3298        let start_ix = rng.random_range(0..=end_ix);
3299        let start_ix = text_rope.floor_char_boundary(start_ix);
3300        assert_eq!(
3301            snapshot
3302                .bytes_in_range(MultiBufferOffset(start_ix)..MultiBufferOffset(end_ix))
3303                .flatten()
3304                .copied()
3305                .collect::<Vec<_>>(),
3306            expected_text.as_bytes()[start_ix..end_ix].to_vec(),
3307            "bytes_in_range({:?})",
3308            start_ix..end_ix,
3309        );
3310    }
3311}
3312
3313fn check_multibuffer_edits(
3314    snapshot: &MultiBufferSnapshot,
3315    old_snapshot: &MultiBufferSnapshot,
3316    subscription: Subscription<MultiBufferOffset>,
3317) {
3318    let edits = subscription.consume().into_inner();
3319
3320    log::info!(
3321        "applying subscription edits to old text: {:?}: {:#?}",
3322        old_snapshot.text(),
3323        edits,
3324    );
3325
3326    let mut text = old_snapshot.text();
3327    for edit in edits {
3328        let new_text: String = snapshot
3329            .text_for_range(edit.new.start..edit.new.end)
3330            .collect();
3331        text.replace_range(
3332            (edit.new.start.0..edit.new.start.0 + (edit.old.end.0 - edit.old.start.0)).clone(),
3333            &new_text,
3334        );
3335        pretty_assertions::assert_eq!(
3336            &text[0..edit.new.end.0],
3337            snapshot
3338                .text_for_range(MultiBufferOffset(0)..edit.new.end)
3339                .collect::<String>()
3340        );
3341    }
3342    pretty_assertions::assert_eq!(text, snapshot.text());
3343}
3344
3345#[gpui::test]
3346fn test_history(cx: &mut App) {
3347    let test_settings = SettingsStore::test(cx);
3348    cx.set_global(test_settings);
3349
3350    let group_interval: Duration = Duration::from_millis(1);
3351    let buffer_1 = cx.new(|cx| {
3352        let mut buf = Buffer::local("1234", cx);
3353        buf.set_group_interval(group_interval);
3354        buf
3355    });
3356    let buffer_2 = cx.new(|cx| {
3357        let mut buf = Buffer::local("5678", cx);
3358        buf.set_group_interval(group_interval);
3359        buf
3360    });
3361    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
3362    multibuffer.update(cx, |this, _| {
3363        this.set_group_interval(group_interval);
3364    });
3365    multibuffer.update(cx, |multibuffer, cx| {
3366        multibuffer.set_excerpts_for_path(
3367            PathKey::sorted(0),
3368            buffer_1.clone(),
3369            [Point::zero()..buffer_1.read(cx).max_point()],
3370            0,
3371            cx,
3372        );
3373        multibuffer.set_excerpts_for_path(
3374            PathKey::sorted(1),
3375            buffer_2.clone(),
3376            [Point::zero()..buffer_2.read(cx).max_point()],
3377            0,
3378            cx,
3379        );
3380    });
3381
3382    let mut now = Instant::now();
3383
3384    multibuffer.update(cx, |multibuffer, cx| {
3385        let transaction_1 = multibuffer.start_transaction_at(now, cx).unwrap();
3386        multibuffer.edit(
3387            [
3388                (Point::new(0, 0)..Point::new(0, 0), "A"),
3389                (Point::new(1, 0)..Point::new(1, 0), "A"),
3390            ],
3391            None,
3392            cx,
3393        );
3394        multibuffer.edit(
3395            [
3396                (Point::new(0, 1)..Point::new(0, 1), "B"),
3397                (Point::new(1, 1)..Point::new(1, 1), "B"),
3398            ],
3399            None,
3400            cx,
3401        );
3402        multibuffer.end_transaction_at(now, cx);
3403        assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
3404
3405        // Verify edited ranges for transaction 1
3406        assert_eq!(
3407            multibuffer.edited_ranges_for_transaction(transaction_1, cx),
3408            &[
3409                MultiBufferOffset(0)..MultiBufferOffset(2),
3410                MultiBufferOffset(7)..MultiBufferOffset(9),
3411            ]
3412        );
3413
3414        // Edit buffer 1 through the multibuffer
3415        now += 2 * group_interval;
3416        multibuffer.start_transaction_at(now, cx);
3417        multibuffer.edit(
3418            [(MultiBufferOffset(2)..MultiBufferOffset(2), "C")],
3419            None,
3420            cx,
3421        );
3422        multibuffer.end_transaction_at(now, cx);
3423        assert_eq!(multibuffer.read(cx).text(), "ABC1234\nAB5678");
3424
3425        // Edit buffer 1 independently
3426        buffer_1.update(cx, |buffer_1, cx| {
3427            buffer_1.start_transaction_at(now);
3428            buffer_1.edit([(3..3, "D")], None, cx);
3429            buffer_1.end_transaction_at(now, cx);
3430
3431            now += 2 * group_interval;
3432            buffer_1.start_transaction_at(now);
3433            buffer_1.edit([(4..4, "E")], None, cx);
3434            buffer_1.end_transaction_at(now, cx);
3435        });
3436        assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678");
3437
3438        // An undo in the multibuffer undoes the multibuffer transaction
3439        // and also any individual buffer edits that have occurred since
3440        // that transaction.
3441        multibuffer.undo(cx);
3442        assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
3443
3444        multibuffer.undo(cx);
3445        assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
3446
3447        multibuffer.redo(cx);
3448        assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
3449
3450        multibuffer.redo(cx);
3451        assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\nAB5678");
3452
3453        // Undo buffer 2 independently.
3454        buffer_2.update(cx, |buffer_2, cx| buffer_2.undo(cx));
3455        assert_eq!(multibuffer.read(cx).text(), "ABCDE1234\n5678");
3456
3457        // An undo in the multibuffer undoes the components of the
3458        // the last multibuffer transaction that are not already undone.
3459        multibuffer.undo(cx);
3460        assert_eq!(multibuffer.read(cx).text(), "AB1234\n5678");
3461
3462        multibuffer.undo(cx);
3463        assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
3464
3465        multibuffer.redo(cx);
3466        assert_eq!(multibuffer.read(cx).text(), "AB1234\nAB5678");
3467
3468        buffer_1.update(cx, |buffer_1, cx| buffer_1.redo(cx));
3469        assert_eq!(multibuffer.read(cx).text(), "ABCD1234\nAB5678");
3470
3471        // Redo stack gets cleared after an edit.
3472        now += 2 * group_interval;
3473        multibuffer.start_transaction_at(now, cx);
3474        multibuffer.edit(
3475            [(MultiBufferOffset(0)..MultiBufferOffset(0), "X")],
3476            None,
3477            cx,
3478        );
3479        multibuffer.end_transaction_at(now, cx);
3480        assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
3481        multibuffer.redo(cx);
3482        assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
3483        multibuffer.undo(cx);
3484        assert_eq!(multibuffer.read(cx).text(), "ABCD1234\nAB5678");
3485        multibuffer.undo(cx);
3486        assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
3487
3488        // Transactions can be grouped manually.
3489        multibuffer.redo(cx);
3490        multibuffer.redo(cx);
3491        assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
3492        multibuffer.group_until_transaction(transaction_1, cx);
3493        multibuffer.undo(cx);
3494        assert_eq!(multibuffer.read(cx).text(), "1234\n5678");
3495        multibuffer.redo(cx);
3496        assert_eq!(multibuffer.read(cx).text(), "XABCD1234\nAB5678");
3497    });
3498}
3499
3500#[gpui::test]
3501async fn test_enclosing_indent(cx: &mut TestAppContext) {
3502    async fn enclosing_indent(
3503        text: &str,
3504        buffer_row: u32,
3505        cx: &mut TestAppContext,
3506    ) -> Option<(Range<u32>, LineIndent)> {
3507        let buffer = cx.update(|cx| MultiBuffer::build_simple(text, cx));
3508        let snapshot = cx.read(|cx| buffer.read(cx).snapshot(cx));
3509        let (range, indent) = snapshot
3510            .enclosing_indent(MultiBufferRow(buffer_row))
3511            .await?;
3512        Some((range.start.0..range.end.0, indent))
3513    }
3514
3515    assert_eq!(
3516        enclosing_indent(
3517            indoc!(
3518                "
3519                fn b() {
3520                    if c {
3521                        let d = 2;
3522                    }
3523                }
3524                "
3525            ),
3526            1,
3527            cx,
3528        )
3529        .await,
3530        Some((
3531            1..2,
3532            LineIndent {
3533                tabs: 0,
3534                spaces: 4,
3535                line_blank: false,
3536            }
3537        ))
3538    );
3539
3540    assert_eq!(
3541        enclosing_indent(
3542            indoc!(
3543                "
3544                fn b() {
3545                    if c {
3546                        let d = 2;
3547                    }
3548                }
3549                "
3550            ),
3551            2,
3552            cx,
3553        )
3554        .await,
3555        Some((
3556            1..2,
3557            LineIndent {
3558                tabs: 0,
3559                spaces: 4,
3560                line_blank: false,
3561            }
3562        ))
3563    );
3564
3565    assert_eq!(
3566        enclosing_indent(
3567            indoc!(
3568                "
3569                fn b() {
3570                    if c {
3571                        let d = 2;
3572
3573                        let e = 5;
3574                    }
3575                }
3576                "
3577            ),
3578            3,
3579            cx,
3580        )
3581        .await,
3582        Some((
3583            1..4,
3584            LineIndent {
3585                tabs: 0,
3586                spaces: 4,
3587                line_blank: false,
3588            }
3589        ))
3590    );
3591}
3592
3593#[gpui::test]
3594async fn test_summaries_for_anchors(cx: &mut TestAppContext) {
3595    let base_text_1 = indoc!(
3596        "
3597        bar
3598        "
3599    );
3600    let text_1 = indoc!(
3601        "
3602        BAR
3603        "
3604    );
3605    let base_text_2 = indoc!(
3606        "
3607        foo
3608        "
3609    );
3610    let text_2 = indoc!(
3611        "
3612        FOO
3613        "
3614    );
3615
3616    let buffer_1 = cx.new(|cx| Buffer::local(text_1, cx));
3617    let buffer_2 = cx.new(|cx| Buffer::local(text_2, cx));
3618    let diff_1 = cx.new(|cx| {
3619        BufferDiff::new_with_base_text(base_text_1, &buffer_1.read(cx).text_snapshot(), cx)
3620    });
3621    let diff_2 = cx.new(|cx| {
3622        BufferDiff::new_with_base_text(base_text_2, &buffer_2.read(cx).text_snapshot(), cx)
3623    });
3624    cx.run_until_parked();
3625
3626    dbg!("BEFORE");
3627    let multibuffer = cx.new(|cx| {
3628        let mut multibuffer = MultiBuffer::new(Capability::ReadWrite);
3629        multibuffer.set_all_diff_hunks_expanded(cx);
3630        multibuffer.set_excerpts_for_path(
3631            PathKey::sorted(0),
3632            buffer_1.clone(),
3633            [Point::zero()..buffer_1.read(cx).max_point()],
3634            0,
3635            cx,
3636        );
3637        multibuffer.set_excerpts_for_path(
3638            PathKey::sorted(1),
3639            buffer_2.clone(),
3640            [Point::zero()..buffer_2.read(cx).max_point()],
3641            0,
3642            cx,
3643        );
3644        multibuffer.add_diff(diff_1.clone(), cx);
3645        multibuffer.add_diff(diff_2.clone(), cx);
3646        multibuffer
3647    });
3648
3649    dbg!("BEFORE");
3650    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
3651        (multibuffer.snapshot(cx), multibuffer.subscribe())
3652    });
3653
3654    dbg!("BEFORE");
3655    assert_new_snapshot(
3656        &multibuffer,
3657        &mut snapshot,
3658        &mut subscription,
3659        cx,
3660        indoc!(
3661            "
3662            - bar
3663            + BAR
3664
3665            - foo
3666            + FOO
3667            "
3668        ),
3669    );
3670
3671    dbg!("BEFORE");
3672    let anchor_1 = multibuffer.read_with(cx, |multibuffer, cx| {
3673        multibuffer
3674            .buffer_anchor_to_anchor(
3675                &buffer_1,
3676                text::Anchor::min_for_buffer(buffer_1.read(cx).remote_id()),
3677                cx,
3678            )
3679            .unwrap()
3680    });
3681    dbg!("BEFORE");
3682    let point_1 = snapshot.summaries_for_anchors::<Point, _>([&anchor_1])[0];
3683    assert_eq!(point_1, Point::new(0, 0));
3684
3685    dbg!("BEFORE");
3686    let anchor_2 = multibuffer.read_with(cx, |multibuffer, cx| {
3687        multibuffer
3688            .buffer_anchor_to_anchor(
3689                &buffer_2,
3690                text::Anchor::min_for_buffer(buffer_2.read(cx).remote_id()),
3691                cx,
3692            )
3693            .unwrap()
3694    });
3695    dbg!("BEFORE");
3696    let point_2 = snapshot.summaries_for_anchors::<Point, _>([&anchor_2])[0];
3697    dbg!("AFTER");
3698    assert_eq!(point_2, Point::new(3, 0));
3699}
3700
3701#[gpui::test]
3702async fn test_trailing_deletion_without_newline(cx: &mut TestAppContext) {
3703    let base_text_1 = "one\ntwo".to_owned();
3704    let text_1 = "one\n".to_owned();
3705
3706    let buffer_1 = cx.new(|cx| Buffer::local(text_1, cx));
3707    let diff_1 = cx.new(|cx| {
3708        BufferDiff::new_with_base_text(&base_text_1, &buffer_1.read(cx).text_snapshot(), cx)
3709    });
3710    cx.run_until_parked();
3711
3712    let multibuffer = cx.new(|cx| {
3713        let mut multibuffer = MultiBuffer::new(Capability::ReadWrite);
3714        multibuffer.set_excerpts_for_path(
3715            PathKey::sorted(0),
3716            buffer_1.clone(),
3717            [Point::zero()..buffer_1.read(cx).max_point()],
3718            0,
3719            cx,
3720        );
3721        multibuffer.add_diff(diff_1.clone(), cx);
3722        multibuffer.expand_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
3723        multibuffer
3724    });
3725
3726    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
3727        (multibuffer.snapshot(cx), multibuffer.subscribe())
3728    });
3729
3730    assert_new_snapshot(
3731        &multibuffer,
3732        &mut snapshot,
3733        &mut subscription,
3734        cx,
3735        indoc!(
3736            "
3737              one
3738            - two
3739            "
3740        ),
3741    );
3742
3743    assert_eq!(snapshot.max_point(), Point::new(2, 0));
3744    assert_eq!(snapshot.len().0, 8);
3745
3746    assert_eq!(
3747        snapshot
3748            .dimensions_from_points::<Point>([Point::new(2, 0)])
3749            .collect::<Vec<_>>(),
3750        vec![Point::new(2, 0)]
3751    );
3752
3753    let (_, translated_offset) = snapshot.point_to_buffer_offset(Point::new(2, 0)).unwrap();
3754    assert_eq!(translated_offset.0, "one\n".len());
3755    let (_, translated_point) = snapshot.point_to_buffer_point(Point::new(2, 0)).unwrap();
3756    assert_eq!(translated_point, Point::new(1, 0));
3757
3758    // The same, for an excerpt that's not at the end of the multibuffer.
3759
3760    let text_2 = "foo\n".to_owned();
3761    let buffer_2 = cx.new(|cx| Buffer::local(&text_2, cx));
3762    multibuffer.update(cx, |multibuffer, cx| {
3763        multibuffer.set_excerpt_ranges_for_path(
3764            PathKey::sorted(1),
3765            buffer_2.clone(),
3766            &buffer_2.read(cx).snapshot(),
3767            vec![ExcerptRange::new(Point::new(0, 0)..Point::new(1, 0))],
3768            cx,
3769        );
3770    });
3771
3772    assert_new_snapshot(
3773        &multibuffer,
3774        &mut snapshot,
3775        &mut subscription,
3776        cx,
3777        indoc!(
3778            "
3779              one
3780            - two
3781
3782              foo
3783            "
3784        ),
3785    );
3786
3787    assert_eq!(
3788        snapshot
3789            .dimensions_from_points::<Point>([Point::new(2, 0)])
3790            .collect::<Vec<_>>(),
3791        vec![Point::new(2, 0)]
3792    );
3793
3794    let buffer_1_id = buffer_1.read_with(cx, |buffer_1, _| buffer_1.remote_id());
3795    let (buffer, translated_offset) = snapshot.point_to_buffer_offset(Point::new(2, 0)).unwrap();
3796    assert_eq!(buffer.remote_id(), buffer_1_id);
3797    assert_eq!(translated_offset.0, "one\n".len());
3798    let (buffer, translated_point) = snapshot.point_to_buffer_point(Point::new(2, 0)).unwrap();
3799    assert_eq!(buffer.remote_id(), buffer_1_id);
3800    assert_eq!(translated_point, Point::new(1, 0));
3801}
3802
3803fn format_diff(
3804    text: &str,
3805    row_infos: &Vec<RowInfo>,
3806    boundary_rows: &HashSet<MultiBufferRow>,
3807    has_diff: Option<bool>,
3808) -> String {
3809    let has_diff =
3810        has_diff.unwrap_or_else(|| row_infos.iter().any(|info| info.diff_status.is_some()));
3811    text.split('\n')
3812        .enumerate()
3813        .zip(row_infos)
3814        .map(|((ix, line), info)| {
3815            let marker = match info.diff_status.map(|status| status.kind) {
3816                Some(DiffHunkStatusKind::Added) => "+ ",
3817                Some(DiffHunkStatusKind::Deleted) => "- ",
3818                Some(DiffHunkStatusKind::Modified) => unreachable!(),
3819                None => {
3820                    if has_diff && !line.is_empty() {
3821                        "  "
3822                    } else {
3823                        ""
3824                    }
3825                }
3826            };
3827            let boundary_row = if boundary_rows.contains(&MultiBufferRow(ix as u32)) {
3828                if has_diff {
3829                    "  ----------\n"
3830                } else {
3831                    "---------\n"
3832                }
3833            } else {
3834                ""
3835            };
3836            let expand = info
3837                .expand_info
3838                .as_ref()
3839                .map(|expand_info| match expand_info.direction {
3840                    ExpandExcerptDirection::Up => " [↑]",
3841                    ExpandExcerptDirection::Down => " [↓]",
3842                    ExpandExcerptDirection::UpAndDown => " [↕]",
3843                })
3844                .unwrap_or_default();
3845
3846            format!("{boundary_row}{marker}{line}{expand}")
3847            // let mbr = info
3848            //     .multibuffer_row
3849            //     .map(|row| format!("{:0>3}", row.0))
3850            //     .unwrap_or_else(|| "???".to_string());
3851            // let byte_range = format!("{byte_range_start:0>3}..{byte_range_end:0>3}");
3852            // format!("{boundary_row}Row: {mbr}, Bytes: {byte_range} | {marker}{line}{expand}")
3853        })
3854        .collect::<Vec<_>>()
3855        .join("\n")
3856}
3857
3858// fn format_transforms(snapshot: &MultiBufferSnapshot) -> String {
3859//     snapshot
3860//         .diff_transforms
3861//         .iter()
3862//         .map(|transform| {
3863//             let (kind, summary) = match transform {
3864//                 DiffTransform::DeletedHunk { summary, .. } => ("   Deleted", (*summary).into()),
3865//                 DiffTransform::FilteredInsertedHunk { summary, .. } => ("  Filtered", *summary),
3866//                 DiffTransform::InsertedHunk { summary, .. } => ("  Inserted", *summary),
3867//                 DiffTransform::Unmodified { summary, .. } => ("Unmodified", *summary),
3868//             };
3869//             format!("{kind}(len: {}, lines: {:?})", summary.len, summary.lines)
3870//         })
3871//         .join("\n")
3872// }
3873
3874// fn format_excerpts(snapshot: &MultiBufferSnapshot) -> String {
3875//     snapshot
3876//         .excerpts
3877//         .iter()
3878//         .map(|excerpt| {
3879//             format!(
3880//                 "Excerpt(buffer_range = {:?}, lines = {:?}, has_trailing_newline = {:?})",
3881//                 excerpt.range.context.to_point(&excerpt.buffer),
3882//                 excerpt.text_summary.lines,
3883//                 excerpt.has_trailing_newline
3884//             )
3885//         })
3886//         .join("\n")
3887// }
3888
3889#[gpui::test]
3890async fn test_singleton_with_inverted_diff(cx: &mut TestAppContext) {
3891    let text = indoc!(
3892        "
3893        ZERO
3894        one
3895        TWO
3896        three
3897        six
3898        "
3899    );
3900    let base_text = indoc!(
3901        "
3902        one
3903        two
3904        three
3905        four
3906        five
3907        six
3908        "
3909    );
3910
3911    let buffer = cx.new(|cx| Buffer::local(text, cx));
3912    let diff = cx
3913        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
3914    cx.run_until_parked();
3915
3916    let base_text_buffer = diff.read_with(cx, |diff, _| diff.base_text_buffer().clone());
3917
3918    let multibuffer = cx.new(|cx| {
3919        let mut multibuffer = MultiBuffer::singleton(base_text_buffer.clone(), cx);
3920        multibuffer.set_all_diff_hunks_expanded(cx);
3921        multibuffer.add_inverted_diff(diff.clone(), buffer.clone(), cx);
3922        multibuffer
3923    });
3924
3925    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
3926        (multibuffer.snapshot(cx), multibuffer.subscribe())
3927    });
3928
3929    assert_eq!(snapshot.text(), base_text);
3930    assert_new_snapshot(
3931        &multibuffer,
3932        &mut snapshot,
3933        &mut subscription,
3934        cx,
3935        indoc!(
3936            "
3937              one
3938            - two
3939              three
3940            - four
3941            - five
3942              six
3943            "
3944        ),
3945    );
3946
3947    buffer.update(cx, |buffer, cx| {
3948        buffer.edit_via_marked_text(
3949            indoc!(
3950                "
3951                ZERO
3952                one
3953                «<inserted>»W«O
3954                T»hree
3955                six
3956                "
3957            ),
3958            None,
3959            cx,
3960        );
3961    });
3962    cx.run_until_parked();
3963    let update = diff
3964        .update(cx, |diff, cx| {
3965            diff.update_diff(
3966                buffer.read(cx).text_snapshot(),
3967                Some(base_text.into()),
3968                None,
3969                None,
3970                cx,
3971            )
3972        })
3973        .await;
3974    diff.update(cx, |diff, cx| {
3975        diff.set_snapshot(update, &buffer.read(cx).text_snapshot(), cx)
3976    })
3977    .await;
3978    cx.run_until_parked();
3979
3980    assert_new_snapshot(
3981        &multibuffer,
3982        &mut snapshot,
3983        &mut subscription,
3984        cx,
3985        indoc! {
3986            "
3987              one
3988            - two
3989            - three
3990            - four
3991            - five
3992              six
3993            "
3994        },
3995    );
3996
3997    buffer.update(cx, |buffer, cx| {
3998        buffer.set_text("ZERO\nONE\nTWO\n", cx);
3999    });
4000    cx.run_until_parked();
4001    let update = diff
4002        .update(cx, |diff, cx| {
4003            diff.update_diff(
4004                buffer.read(cx).text_snapshot(),
4005                Some(base_text.into()),
4006                None,
4007                None,
4008                cx,
4009            )
4010        })
4011        .await;
4012    diff.update(cx, |diff, cx| {
4013        diff.set_snapshot(update, &buffer.read(cx).text_snapshot(), cx)
4014    })
4015    .await;
4016    cx.run_until_parked();
4017
4018    assert_new_snapshot(
4019        &multibuffer,
4020        &mut snapshot,
4021        &mut subscription,
4022        cx,
4023        indoc! {
4024            "
4025            - one
4026            - two
4027            - three
4028            - four
4029            - five
4030            - six
4031            "
4032        },
4033    );
4034
4035    diff.update(cx, |diff, cx| {
4036        diff.set_base_text(
4037            Some("new base\n".into()),
4038            None,
4039            buffer.read(cx).text_snapshot(),
4040            cx,
4041        )
4042    })
4043    .await
4044    .unwrap();
4045    cx.run_until_parked();
4046
4047    assert_new_snapshot(
4048        &multibuffer,
4049        &mut snapshot,
4050        &mut subscription,
4051        cx,
4052        indoc! {"
4053            - new base
4054        "},
4055    );
4056}
4057
4058#[gpui::test]
4059async fn test_inverted_diff_base_text_change(cx: &mut TestAppContext) {
4060    let base_text = "aaa\nbbb\nccc\n";
4061    let text = "ddd\n";
4062    let buffer = cx.new(|cx| Buffer::local(text, cx));
4063    let diff = cx
4064        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
4065    cx.run_until_parked();
4066
4067    let base_text_buffer = diff.read_with(cx, |diff, _| diff.base_text_buffer().clone());
4068
4069    let multibuffer = cx.new(|cx| {
4070        let mut multibuffer = MultiBuffer::singleton(base_text_buffer.clone(), cx);
4071        multibuffer.set_all_diff_hunks_expanded(cx);
4072        multibuffer.add_inverted_diff(diff.clone(), buffer.clone(), cx);
4073        multibuffer
4074    });
4075
4076    let (mut snapshot, mut subscription) = multibuffer.update(cx, |multibuffer, cx| {
4077        (multibuffer.snapshot(cx), multibuffer.subscribe())
4078    });
4079
4080    assert_eq!(snapshot.text(), base_text);
4081    assert_new_snapshot(
4082        &multibuffer,
4083        &mut snapshot,
4084        &mut subscription,
4085        cx,
4086        indoc!(
4087            "
4088            - aaa
4089            - bbb
4090            - ccc
4091            "
4092        ),
4093    );
4094
4095    let update = diff
4096        .update(cx, |diff, cx| {
4097            diff.update_diff(
4098                buffer.read(cx).text_snapshot(),
4099                Some("ddd\n".into()),
4100                Some(true),
4101                None,
4102                cx,
4103            )
4104        })
4105        .await;
4106    diff.update(cx, |diff, cx| {
4107        diff.set_snapshot(update, &buffer.read(cx).text_snapshot(), cx)
4108    })
4109    .detach();
4110
4111    let _hunks: Vec<_> = multibuffer
4112        .read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx))
4113        .diff_hunks()
4114        .collect();
4115}
4116
4117#[gpui::test]
4118async fn test_inverted_diff_secondary_version_mismatch(cx: &mut TestAppContext) {
4119    let base_text = "one\ntwo\nthree\nfour\nfive\n";
4120    let index_text = "one\nTWO\nthree\nfour\nfive\n";
4121    let buffer_text = "one\nTWO\nthree\nFOUR\nfive\n";
4122
4123    let buffer = cx.new(|cx| Buffer::local(buffer_text, cx));
4124
4125    let unstaged_diff = cx
4126        .new(|cx| BufferDiff::new_with_base_text(index_text, &buffer.read(cx).text_snapshot(), cx));
4127    cx.run_until_parked();
4128
4129    let uncommitted_diff = cx.new(|cx| {
4130        let mut diff =
4131            BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx);
4132        diff.set_secondary_diff(unstaged_diff.clone());
4133        diff
4134    });
4135    cx.run_until_parked();
4136
4137    buffer.update(cx, |buffer, cx| {
4138        buffer.edit([(0..0, "ZERO\n")], None, cx);
4139    });
4140
4141    let update = unstaged_diff
4142        .update(cx, |diff, cx| {
4143            diff.update_diff(
4144                buffer.read(cx).text_snapshot(),
4145                Some(index_text.into()),
4146                None,
4147                None,
4148                cx,
4149            )
4150        })
4151        .await;
4152    unstaged_diff
4153        .update(cx, |diff, cx| {
4154            diff.set_snapshot(update, &buffer.read(cx).text_snapshot(), cx)
4155        })
4156        .await;
4157
4158    let base_text_buffer =
4159        uncommitted_diff.read_with(cx, |diff, _| diff.base_text_buffer().clone());
4160
4161    let multibuffer = cx.new(|cx| {
4162        let mut multibuffer = MultiBuffer::singleton(base_text_buffer.clone(), cx);
4163        multibuffer.set_all_diff_hunks_expanded(cx);
4164        multibuffer.add_inverted_diff(uncommitted_diff.clone(), buffer.clone(), cx);
4165        multibuffer
4166    });
4167
4168    let _hunks: Vec<_> = multibuffer
4169        .read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx))
4170        .diff_hunks()
4171        .collect();
4172}
4173
4174#[track_caller]
4175fn assert_excerpts_match(
4176    multibuffer: &Entity<MultiBuffer>,
4177    cx: &mut TestAppContext,
4178    expected: &str,
4179) {
4180    let mut output = String::new();
4181    multibuffer.read_with(cx, |multibuffer, cx| {
4182        for (buffer, info) in multibuffer.snapshot(cx).excerpts() {
4183            output.push_str("-----\n");
4184            output.extend(buffer.text_for_range(info.range.context));
4185            if !output.ends_with('\n') {
4186                output.push('\n');
4187            }
4188        }
4189    });
4190    assert_eq!(output, expected);
4191}
4192
4193#[track_caller]
4194fn assert_new_snapshot(
4195    multibuffer: &Entity<MultiBuffer>,
4196    snapshot: &mut MultiBufferSnapshot,
4197    subscription: &mut Subscription<MultiBufferOffset>,
4198    cx: &mut TestAppContext,
4199    expected_diff: &str,
4200) {
4201    let new_snapshot = multibuffer.read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx));
4202    let actual_text = new_snapshot.text();
4203    let line_infos = new_snapshot
4204        .row_infos(MultiBufferRow(0))
4205        .collect::<Vec<_>>();
4206    let actual_diff = format_diff(&actual_text, &line_infos, &Default::default(), None);
4207    pretty_assertions::assert_eq!(actual_diff, expected_diff);
4208    check_edits(
4209        snapshot,
4210        &new_snapshot,
4211        &subscription.consume().into_inner(),
4212    );
4213    *snapshot = new_snapshot;
4214}
4215
4216#[track_caller]
4217fn check_edits(
4218    old_snapshot: &MultiBufferSnapshot,
4219    new_snapshot: &MultiBufferSnapshot,
4220    edits: &[Edit<MultiBufferOffset>],
4221) {
4222    let mut text = old_snapshot.text();
4223    let new_text = new_snapshot.text();
4224    for edit in edits.iter().rev() {
4225        if !text.is_char_boundary(edit.old.start.0)
4226            || !text.is_char_boundary(edit.old.end.0)
4227            || !new_text.is_char_boundary(edit.new.start.0)
4228            || !new_text.is_char_boundary(edit.new.end.0)
4229        {
4230            panic!(
4231                "invalid edits: {:?}\nold text: {:?}\nnew text: {:?}",
4232                edits, text, new_text
4233            );
4234        }
4235
4236        text.replace_range(
4237            edit.old.start.0..edit.old.end.0,
4238            &new_text[edit.new.start.0..edit.new.end.0],
4239        );
4240    }
4241
4242    pretty_assertions::assert_eq!(text, new_text, "invalid edits: {:?}", edits);
4243}
4244
4245#[track_caller]
4246fn assert_chunks_in_ranges(snapshot: &MultiBufferSnapshot) {
4247    let full_text = snapshot.text();
4248    for ix in 0..full_text.len() {
4249        let mut chunks = snapshot.chunks(MultiBufferOffset(0)..snapshot.len(), false);
4250        chunks.seek(MultiBufferOffset(ix)..snapshot.len());
4251        let tail = chunks.map(|chunk| chunk.text).collect::<String>();
4252        assert_eq!(tail, &full_text[ix..], "seek to range: {:?}", ix..);
4253    }
4254}
4255
4256#[track_caller]
4257fn assert_consistent_line_numbers(snapshot: &MultiBufferSnapshot) {
4258    let all_line_numbers = snapshot.row_infos(MultiBufferRow(0)).collect::<Vec<_>>();
4259    for start_row in 1..all_line_numbers.len() {
4260        let line_numbers = snapshot
4261            .row_infos(MultiBufferRow(start_row as u32))
4262            .collect::<Vec<_>>();
4263        assert_eq!(
4264            line_numbers,
4265            all_line_numbers[start_row..],
4266            "start_row: {start_row}"
4267        );
4268    }
4269}
4270
4271#[track_caller]
4272fn assert_position_translation(snapshot: &MultiBufferSnapshot) {
4273    let text = Rope::from(snapshot.text());
4274
4275    let mut left_anchors = Vec::new();
4276    let mut right_anchors = Vec::new();
4277    let mut offsets = Vec::new();
4278    let mut points = Vec::new();
4279    for offset in 0..=text.len() + 1 {
4280        let offset = MultiBufferOffset(offset);
4281        let clipped_left = snapshot.clip_offset(offset, Bias::Left);
4282        let clipped_right = snapshot.clip_offset(offset, Bias::Right);
4283        assert_eq!(
4284            clipped_left.0,
4285            text.clip_offset(offset.0, Bias::Left),
4286            "clip_offset({offset:?}, Left)"
4287        );
4288        assert_eq!(
4289            clipped_right.0,
4290            text.clip_offset(offset.0, Bias::Right),
4291            "clip_offset({offset:?}, Right)"
4292        );
4293        assert_eq!(
4294            snapshot.offset_to_point(clipped_left),
4295            text.offset_to_point(clipped_left.0),
4296            "offset_to_point({})",
4297            clipped_left.0
4298        );
4299        assert_eq!(
4300            snapshot.offset_to_point(clipped_right),
4301            text.offset_to_point(clipped_right.0),
4302            "offset_to_point({})",
4303            clipped_right.0
4304        );
4305        let anchor_after = snapshot.anchor_after(clipped_left);
4306        assert_eq!(
4307            anchor_after.to_offset(snapshot),
4308            clipped_left,
4309            "anchor_after({}).to_offset {anchor_after:?}",
4310            clipped_left.0
4311        );
4312        let anchor_before = snapshot.anchor_before(clipped_left);
4313        assert_eq!(
4314            anchor_before.to_offset(snapshot),
4315            clipped_left,
4316            "anchor_before({}).to_offset",
4317            clipped_left.0
4318        );
4319        left_anchors.push(anchor_before);
4320        right_anchors.push(anchor_after);
4321        offsets.push(clipped_left);
4322        points.push(text.offset_to_point(clipped_left.0));
4323    }
4324
4325    for row in 0..text.max_point().row {
4326        for column in 0..text.line_len(row) + 1 {
4327            let point = Point { row, column };
4328            let clipped_left = snapshot.clip_point(point, Bias::Left);
4329            let clipped_right = snapshot.clip_point(point, Bias::Right);
4330            assert_eq!(
4331                clipped_left,
4332                text.clip_point(point, Bias::Left),
4333                "clip_point({point:?}, Left)"
4334            );
4335            assert_eq!(
4336                clipped_right,
4337                text.clip_point(point, Bias::Right),
4338                "clip_point({point:?}, Right)"
4339            );
4340            assert_eq!(
4341                snapshot.point_to_offset(clipped_left).0,
4342                text.point_to_offset(clipped_left),
4343                "point_to_offset({clipped_left:?})"
4344            );
4345            assert_eq!(
4346                snapshot.point_to_offset(clipped_right).0,
4347                text.point_to_offset(clipped_right),
4348                "point_to_offset({clipped_right:?})"
4349            );
4350        }
4351    }
4352
4353    assert_eq!(
4354        snapshot.summaries_for_anchors::<MultiBufferOffset, _>(&left_anchors),
4355        offsets,
4356        "left_anchors <-> offsets"
4357    );
4358    assert_eq!(
4359        snapshot.summaries_for_anchors::<Point, _>(&left_anchors),
4360        points,
4361        "left_anchors <-> points"
4362    );
4363    assert_eq!(
4364        snapshot.summaries_for_anchors::<MultiBufferOffset, _>(&right_anchors),
4365        offsets,
4366        "right_anchors <-> offsets"
4367    );
4368    assert_eq!(
4369        snapshot.summaries_for_anchors::<Point, _>(&right_anchors),
4370        points,
4371        "right_anchors <-> points"
4372    );
4373
4374    for (anchors, bias) in [(&left_anchors, Bias::Left), (&right_anchors, Bias::Right)] {
4375        for (ix, (offset, anchor)) in offsets.iter().zip(anchors).enumerate() {
4376            if ix > 0 && *offset == MultiBufferOffset(252) && offset > &offsets[ix - 1] {
4377                let prev_anchor = left_anchors[ix - 1];
4378                assert!(
4379                    anchor.cmp(&prev_anchor, snapshot).is_gt(),
4380                    "anchor({}, {bias:?}).cmp(&anchor({}, {bias:?}).is_gt()",
4381                    offsets[ix],
4382                    offsets[ix - 1],
4383                );
4384                assert!(
4385                    prev_anchor.cmp(anchor, snapshot).is_lt(),
4386                    "anchor({}, {bias:?}).cmp(&anchor({}, {bias:?}).is_lt()",
4387                    offsets[ix - 1],
4388                    offsets[ix],
4389                );
4390            }
4391        }
4392    }
4393
4394    if let Some((buffer, offset)) = snapshot.point_to_buffer_offset(snapshot.max_point()) {
4395        assert!(offset.0 <= buffer.len());
4396    }
4397    if let Some((buffer, point)) = snapshot.point_to_buffer_point(snapshot.max_point()) {
4398        assert!(point <= buffer.max_point());
4399    }
4400}
4401
4402fn assert_line_indents(snapshot: &MultiBufferSnapshot) {
4403    let max_row = snapshot.max_point().row;
4404    let buffer_id = snapshot.excerpts().next().unwrap().1.buffer_id;
4405    let text = text::Buffer::new(ReplicaId::LOCAL, buffer_id, snapshot.text());
4406    let mut line_indents = text
4407        .line_indents_in_row_range(0..max_row + 1)
4408        .collect::<Vec<_>>();
4409    for start_row in 0..snapshot.max_point().row {
4410        pretty_assertions::assert_eq!(
4411            snapshot
4412                .line_indents(MultiBufferRow(start_row), |_| true)
4413                .map(|(row, indent, _)| (row.0, indent))
4414                .collect::<Vec<_>>(),
4415            &line_indents[(start_row as usize)..],
4416            "line_indents({start_row})"
4417        );
4418    }
4419
4420    line_indents.reverse();
4421    pretty_assertions::assert_eq!(
4422        snapshot
4423            .reversed_line_indents(MultiBufferRow(max_row), |_| true)
4424            .map(|(row, indent, _)| (row.0, indent))
4425            .collect::<Vec<_>>(),
4426        &line_indents[..],
4427        "reversed_line_indents({max_row})"
4428    );
4429}
4430
4431#[gpui::test]
4432fn test_new_empty_buffer_uses_untitled_title(cx: &mut App) {
4433    let buffer = cx.new(|cx| Buffer::local("", cx));
4434    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4435
4436    assert_eq!(multibuffer.read(cx).title(cx), "untitled");
4437}
4438
4439#[gpui::test]
4440fn test_new_empty_buffer_uses_untitled_title_when_only_contains_whitespace(cx: &mut App) {
4441    let buffer = cx.new(|cx| Buffer::local("\n ", cx));
4442    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4443
4444    assert_eq!(multibuffer.read(cx).title(cx), "untitled");
4445}
4446
4447#[gpui::test]
4448fn test_new_empty_buffer_takes_first_line_for_title(cx: &mut App) {
4449    let buffer = cx.new(|cx| Buffer::local("Hello World\nSecond line", cx));
4450    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4451
4452    assert_eq!(multibuffer.read(cx).title(cx), "Hello World");
4453}
4454
4455#[gpui::test]
4456fn test_new_empty_buffer_takes_trimmed_first_line_for_title(cx: &mut App) {
4457    let buffer = cx.new(|cx| Buffer::local("\nHello, World ", cx));
4458    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4459
4460    assert_eq!(multibuffer.read(cx).title(cx), "Hello, World");
4461}
4462
4463#[gpui::test]
4464fn test_new_empty_buffer_uses_truncated_first_line_for_title(cx: &mut App) {
4465    let title = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee";
4466    let title_after = "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd";
4467    let buffer = cx.new(|cx| Buffer::local(title, cx));
4468    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4469
4470    assert_eq!(multibuffer.read(cx).title(cx), title_after);
4471}
4472
4473#[gpui::test]
4474fn test_new_empty_buffer_uses_truncated_first_line_for_title_after_merging_adjacent_spaces(
4475    cx: &mut App,
4476) {
4477    let title = "aaaaaaaaaabbbbbbbbbb    ccccccccccddddddddddeeeeeeeeee";
4478    let title_after = "aaaaaaaaaabbbbbbbbbb ccccccccccddddddddd";
4479    let buffer = cx.new(|cx| Buffer::local(title, cx));
4480    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4481
4482    assert_eq!(multibuffer.read(cx).title(cx), title_after);
4483}
4484
4485#[gpui::test]
4486fn test_new_empty_buffers_title_can_be_set(cx: &mut App) {
4487    let buffer = cx.new(|cx| Buffer::local("Hello World", cx));
4488    let multibuffer = cx.new(|cx| MultiBuffer::singleton(buffer.clone(), cx));
4489    assert_eq!(multibuffer.read(cx).title(cx), "Hello World");
4490
4491    multibuffer.update(cx, |multibuffer, cx| {
4492        multibuffer.set_title("Hey".into(), cx)
4493    });
4494    assert_eq!(multibuffer.read(cx).title(cx), "Hey");
4495}
4496
4497#[gpui::test(iterations = 100)]
4498fn test_random_chunk_bitmaps(cx: &mut App, mut rng: StdRng) {
4499    let multibuffer = if rng.random() {
4500        let len = rng.random_range(0..10000);
4501        let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
4502        let buffer = cx.new(|cx| Buffer::local(text, cx));
4503        cx.new(|cx| MultiBuffer::singleton(buffer, cx))
4504    } else {
4505        MultiBuffer::build_random(&mut rng, cx)
4506    };
4507
4508    let snapshot = multibuffer.read(cx).snapshot(cx);
4509
4510    let chunks = snapshot.chunks(MultiBufferOffset(0)..snapshot.len(), false);
4511
4512    for chunk in chunks {
4513        let chunk_text = chunk.text;
4514        let chars_bitmap = chunk.chars;
4515        let tabs_bitmap = chunk.tabs;
4516
4517        if chunk_text.is_empty() {
4518            assert_eq!(
4519                chars_bitmap, 0,
4520                "Empty chunk should have empty chars bitmap"
4521            );
4522            assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
4523            continue;
4524        }
4525
4526        assert!(
4527            chunk_text.len() <= 128,
4528            "Chunk text length {} exceeds 128 bytes",
4529            chunk_text.len()
4530        );
4531
4532        // Verify chars bitmap
4533        let char_indices = chunk_text
4534            .char_indices()
4535            .map(|(i, _)| i)
4536            .collect::<Vec<_>>();
4537
4538        for byte_idx in 0..chunk_text.len() {
4539            let should_have_bit = char_indices.contains(&byte_idx);
4540            let has_bit = chars_bitmap & (1 << byte_idx) != 0;
4541
4542            if has_bit != should_have_bit {
4543                eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
4544                eprintln!("Char indices: {:?}", char_indices);
4545                eprintln!("Chars bitmap: {:#b}", chars_bitmap);
4546            }
4547
4548            assert_eq!(
4549                has_bit, should_have_bit,
4550                "Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
4551                byte_idx, chunk_text, should_have_bit, has_bit
4552            );
4553        }
4554
4555        for (byte_idx, byte) in chunk_text.bytes().enumerate() {
4556            let is_tab = byte == b'\t';
4557            let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
4558
4559            if has_bit != is_tab {
4560                eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
4561                eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
4562                assert_eq!(
4563                    has_bit, is_tab,
4564                    "Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
4565                    byte_idx, chunk_text, byte as char, is_tab, has_bit
4566                );
4567            }
4568        }
4569    }
4570}
4571
4572#[gpui::test(iterations = 10)]
4573fn test_random_chunk_bitmaps_with_diffs(cx: &mut App, mut rng: StdRng) {
4574    let settings_store = SettingsStore::test(cx);
4575    cx.set_global(settings_store);
4576    use buffer_diff::BufferDiff;
4577    use util::RandomCharIter;
4578
4579    let multibuffer = if rng.random() {
4580        let len = rng.random_range(100..10000);
4581        let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
4582        let buffer = cx.new(|cx| Buffer::local(text, cx));
4583        cx.new(|cx| MultiBuffer::singleton(buffer, cx))
4584    } else {
4585        MultiBuffer::build_random(&mut rng, cx)
4586    };
4587
4588    let _diff_count = rng.random_range(1..5);
4589    let mut diffs = Vec::new();
4590
4591    multibuffer.update(cx, |multibuffer, cx| {
4592        for buffer_id in multibuffer.all_buffer_ids() {
4593            if rng.random_bool(0.7) {
4594                if let Some(buffer_handle) = multibuffer.buffer(buffer_id) {
4595                    let buffer_text = buffer_handle.read(cx).text();
4596                    let mut base_text = String::new();
4597
4598                    for line in buffer_text.lines() {
4599                        if rng.random_bool(0.3) {
4600                            continue;
4601                        } else if rng.random_bool(0.3) {
4602                            let line_len = rng.random_range(0..50);
4603                            let modified_line = RandomCharIter::new(&mut rng)
4604                                .take(line_len)
4605                                .collect::<String>();
4606                            base_text.push_str(&modified_line);
4607                            base_text.push('\n');
4608                        } else {
4609                            base_text.push_str(line);
4610                            base_text.push('\n');
4611                        }
4612                    }
4613
4614                    if rng.random_bool(0.5) {
4615                        let extra_lines = rng.random_range(1..5);
4616                        for _ in 0..extra_lines {
4617                            let line_len = rng.random_range(0..50);
4618                            let extra_line = RandomCharIter::new(&mut rng)
4619                                .take(line_len)
4620                                .collect::<String>();
4621                            base_text.push_str(&extra_line);
4622                            base_text.push('\n');
4623                        }
4624                    }
4625
4626                    let diff = cx.new(|cx| {
4627                        BufferDiff::new_with_base_text(
4628                            &base_text,
4629                            &buffer_handle.read(cx).text_snapshot(),
4630                            cx,
4631                        )
4632                    });
4633                    diffs.push(diff.clone());
4634                    multibuffer.add_diff(diff, cx);
4635                }
4636            }
4637        }
4638    });
4639
4640    multibuffer.update(cx, |multibuffer, cx| {
4641        if rng.random_bool(0.5) {
4642            multibuffer.set_all_diff_hunks_expanded(cx);
4643        } else {
4644            let snapshot = multibuffer.snapshot(cx);
4645            let text = snapshot.text();
4646
4647            let mut ranges = Vec::new();
4648            for _ in 0..rng.random_range(1..5) {
4649                if snapshot.len().0 == 0 {
4650                    break;
4651                }
4652
4653                let diff_size = rng.random_range(5..1000);
4654                let mut start = rng.random_range(0..snapshot.len().0);
4655
4656                while !text.is_char_boundary(start) {
4657                    start = start.saturating_sub(1);
4658                }
4659
4660                let mut end = rng.random_range(start..snapshot.len().0.min(start + diff_size));
4661
4662                while !text.is_char_boundary(end) {
4663                    end = end.saturating_add(1);
4664                }
4665                let start_anchor = snapshot.anchor_after(MultiBufferOffset(start));
4666                let end_anchor = snapshot.anchor_before(MultiBufferOffset(end));
4667                ranges.push(start_anchor..end_anchor);
4668            }
4669            multibuffer.expand_diff_hunks(ranges, cx);
4670        }
4671    });
4672
4673    let snapshot = multibuffer.read(cx).snapshot(cx);
4674
4675    let chunks = snapshot.chunks(MultiBufferOffset(0)..snapshot.len(), false);
4676
4677    for chunk in chunks {
4678        let chunk_text = chunk.text;
4679        let chars_bitmap = chunk.chars;
4680        let tabs_bitmap = chunk.tabs;
4681
4682        if chunk_text.is_empty() {
4683            assert_eq!(
4684                chars_bitmap, 0,
4685                "Empty chunk should have empty chars bitmap"
4686            );
4687            assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
4688            continue;
4689        }
4690
4691        assert!(
4692            chunk_text.len() <= 128,
4693            "Chunk text length {} exceeds 128 bytes",
4694            chunk_text.len()
4695        );
4696
4697        let char_indices = chunk_text
4698            .char_indices()
4699            .map(|(i, _)| i)
4700            .collect::<Vec<_>>();
4701
4702        for byte_idx in 0..chunk_text.len() {
4703            let should_have_bit = char_indices.contains(&byte_idx);
4704            let has_bit = chars_bitmap & (1 << byte_idx) != 0;
4705
4706            if has_bit != should_have_bit {
4707                eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
4708                eprintln!("Char indices: {:?}", char_indices);
4709                eprintln!("Chars bitmap: {:#b}", chars_bitmap);
4710            }
4711
4712            assert_eq!(
4713                has_bit, should_have_bit,
4714                "Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
4715                byte_idx, chunk_text, should_have_bit, has_bit
4716            );
4717        }
4718
4719        for (byte_idx, byte) in chunk_text.bytes().enumerate() {
4720            let is_tab = byte == b'\t';
4721            let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
4722
4723            if has_bit != is_tab {
4724                eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
4725                eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
4726                assert_eq!(
4727                    has_bit, is_tab,
4728                    "Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
4729                    byte_idx, chunk_text, byte as char, is_tab, has_bit
4730                );
4731            }
4732        }
4733    }
4734}
4735
4736fn collect_word_diffs(
4737    base_text: &str,
4738    modified_text: &str,
4739    cx: &mut TestAppContext,
4740) -> Vec<String> {
4741    let buffer = cx.new(|cx| Buffer::local(modified_text, cx));
4742    let diff = cx
4743        .new(|cx| BufferDiff::new_with_base_text(base_text, &buffer.read(cx).text_snapshot(), cx));
4744    cx.run_until_parked();
4745
4746    let multibuffer = cx.new(|cx| {
4747        let mut multibuffer = MultiBuffer::singleton(buffer.clone(), cx);
4748        multibuffer.add_diff(diff.clone(), cx);
4749        multibuffer
4750    });
4751
4752    multibuffer.update(cx, |multibuffer, cx| {
4753        multibuffer.expand_diff_hunks(vec![Anchor::min()..Anchor::max()], cx);
4754    });
4755
4756    let snapshot = multibuffer.read_with(cx, |multibuffer, cx| multibuffer.snapshot(cx));
4757    let text = snapshot.text();
4758
4759    snapshot
4760        .diff_hunks()
4761        .flat_map(|hunk| hunk.word_diffs)
4762        .map(|range| text[range.start.0..range.end.0].to_string())
4763        .collect()
4764}
4765
4766#[gpui::test]
4767async fn test_word_diff_simple_replacement(cx: &mut TestAppContext) {
4768    let settings_store = cx.update(|cx| SettingsStore::test(cx));
4769    cx.set_global(settings_store);
4770
4771    let base_text = "hello world foo bar\n";
4772    let modified_text = "hello WORLD foo BAR\n";
4773
4774    let word_diffs = collect_word_diffs(base_text, modified_text, cx);
4775
4776    assert_eq!(word_diffs, vec!["world", "bar", "WORLD", "BAR"]);
4777}
4778
4779#[gpui::test]
4780async fn test_word_diff_white_space(cx: &mut TestAppContext) {
4781    let settings_store = cx.update(|cx| SettingsStore::test(cx));
4782    cx.set_global(settings_store);
4783
4784    let base_text = "hello world foo bar\n";
4785    let modified_text = "    hello world foo bar\n";
4786
4787    let word_diffs = collect_word_diffs(base_text, modified_text, cx);
4788
4789    assert_eq!(word_diffs, vec!["    "]);
4790}
4791
4792#[gpui::test]
4793async fn test_word_diff_consecutive_modified_lines(cx: &mut TestAppContext) {
4794    let settings_store = cx.update(|cx| SettingsStore::test(cx));
4795    cx.set_global(settings_store);
4796
4797    let base_text = "aaa bbb\nccc ddd\n";
4798    let modified_text = "aaa BBB\nccc DDD\n";
4799
4800    let word_diffs = collect_word_diffs(base_text, modified_text, cx);
4801
4802    assert_eq!(
4803        word_diffs,
4804        vec!["bbb", "ddd", "BBB", "DDD"],
4805        "consecutive modified lines should produce word diffs when line counts match"
4806    );
4807}
4808
4809#[gpui::test]
4810async fn test_word_diff_modified_lines_with_deletion_between(cx: &mut TestAppContext) {
4811    let settings_store = cx.update(|cx| SettingsStore::test(cx));
4812    cx.set_global(settings_store);
4813
4814    let base_text = "aaa bbb\ndeleted line\nccc ddd\n";
4815    let modified_text = "aaa BBB\nccc DDD\n";
4816
4817    let word_diffs = collect_word_diffs(base_text, modified_text, cx);
4818
4819    assert_eq!(
4820        word_diffs,
4821        Vec::<String>::new(),
4822        "modified lines with a deleted line between should not produce word diffs"
4823    );
4824}
4825
4826#[gpui::test]
4827async fn test_word_diff_disabled(cx: &mut TestAppContext) {
4828    let settings_store = cx.update(|cx| {
4829        let mut settings_store = SettingsStore::test(cx);
4830        settings_store.update_user_settings(cx, |settings| {
4831            settings.project.all_languages.defaults.word_diff_enabled = Some(false);
4832        });
4833        settings_store
4834    });
4835    cx.set_global(settings_store);
4836
4837    let base_text = "hello world\n";
4838    let modified_text = "hello WORLD\n";
4839
4840    let word_diffs = collect_word_diffs(base_text, modified_text, cx);
4841
4842    assert_eq!(
4843        word_diffs,
4844        Vec::<String>::new(),
4845        "word diffs should be empty when disabled"
4846    );
4847}
4848
4849/// Tests `excerpt_containing` and `excerpts_for_range` (functions mapping multi-buffer text-coordinates to excerpts)
4850#[gpui::test]
4851fn test_excerpts_containment_functions(cx: &mut App) {
4852    // Multibuffer content for these tests:
4853    //    0123
4854    // 0: aa0
4855    // 1: aa1
4856    //    -----
4857    // 2: bb0
4858    // 3: bb1
4859    //    -----MultiBufferOffset(0)..
4860    // 4: cc0
4861
4862    let buffer_1 = cx.new(|cx| Buffer::local("aa0\naa1", cx));
4863    let buffer_2 = cx.new(|cx| Buffer::local("bb0\nbb1", cx));
4864    let buffer_3 = cx.new(|cx| Buffer::local("cc0", cx));
4865
4866    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
4867
4868    let (excerpt_1_info, excerpt_2_info, excerpt_3_info) =
4869        multibuffer.update(cx, |multibuffer, cx| {
4870            multibuffer.set_excerpts_for_path(
4871                PathKey::sorted(0),
4872                buffer_1.clone(),
4873                [Point::new(0, 0)..Point::new(1, 3)],
4874                0,
4875                cx,
4876            );
4877
4878            multibuffer.set_excerpts_for_path(
4879                PathKey::sorted(1),
4880                buffer_2.clone(),
4881                [Point::new(0, 0)..Point::new(1, 3)],
4882                0,
4883                cx,
4884            );
4885
4886            multibuffer.set_excerpts_for_path(
4887                PathKey::sorted(2),
4888                buffer_3.clone(),
4889                [Point::new(0, 0)..Point::new(0, 3)],
4890                0,
4891                cx,
4892            );
4893
4894            let snapshot = multibuffer.snapshot(cx);
4895            let mut infos = snapshot.excerpts().map(|(_, info)| info);
4896            (
4897                infos.next().unwrap(),
4898                infos.next().unwrap(),
4899                infos.next().unwrap(),
4900            )
4901        });
4902
4903    let snapshot = multibuffer.read(cx).snapshot(cx);
4904
4905    assert_eq!(snapshot.text(), "aa0\naa1\nbb0\nbb1\ncc0");
4906
4907    //// Test `excerpts_for_range`
4908
4909    let p00 = snapshot.point_to_offset(Point::new(0, 0));
4910    let p10 = snapshot.point_to_offset(Point::new(1, 0));
4911    let p20 = snapshot.point_to_offset(Point::new(2, 0));
4912    let p23 = snapshot.point_to_offset(Point::new(2, 3));
4913    let p13 = snapshot.point_to_offset(Point::new(1, 3));
4914    let p40 = snapshot.point_to_offset(Point::new(4, 0));
4915    let p43 = snapshot.point_to_offset(Point::new(4, 3));
4916
4917    let excerpts: Vec<_> = snapshot.excerpts_for_range(p00..p00).collect();
4918    assert_eq!(excerpts.len(), 1);
4919    assert_eq!(excerpts[0].info(), excerpt_1_info);
4920
4921    // Cursor at very end of excerpt 3
4922    let excerpts: Vec<_> = snapshot.excerpts_for_range(p43..p43).collect();
4923    assert_eq!(excerpts.len(), 1);
4924    assert_eq!(excerpts[0].info(), excerpt_3_info);
4925
4926    let excerpts: Vec<_> = snapshot.excerpts_for_range(p00..p23).collect();
4927    assert_eq!(excerpts.len(), 2);
4928    assert_eq!(excerpts[0].info(), excerpt_1_info);
4929    assert_eq!(excerpts[1].info(), excerpt_2_info);
4930
4931    // This range represent an selection with end-point just inside excerpt_2
4932    // Today we only expand the first excerpt, but another interpretation that
4933    // we could consider is expanding both here
4934    let excerpts: Vec<_> = snapshot.excerpts_for_range(p10..p20).collect();
4935    assert_eq!(excerpts.len(), 1);
4936    assert_eq!(excerpts[0].info(), excerpt_1_info);
4937
4938    //// Test that `excerpts_for_range` and `excerpt_containing` agree for all single offsets (cursor positions)
4939    for offset in 0..=snapshot.len().0 {
4940        let offset = MultiBufferOffset(offset);
4941        let excerpts_for_range: Vec<_> = snapshot.excerpts_for_range(offset..offset).collect();
4942        assert_eq!(
4943            excerpts_for_range.len(),
4944            1,
4945            "Expected exactly one excerpt for offset {offset}",
4946        );
4947
4948        let excerpt_containing = snapshot.excerpt_containing(offset..offset);
4949        assert!(
4950            excerpt_containing.is_some(),
4951            "Expected excerpt_containing to find excerpt for offset {offset}",
4952        );
4953
4954        assert_eq!(
4955            excerpts_for_range[0].info(),
4956            excerpt_containing.unwrap().excerpt.info(),
4957            "excerpts_for_range and excerpt_containing should agree for offset {offset}",
4958        );
4959    }
4960
4961    //// Test `excerpt_containing` behavior with ranges:
4962
4963    // Ranges intersecting a single-excerpt
4964    let containing = snapshot.excerpt_containing(p00..p13);
4965    assert!(containing.is_some());
4966    assert_eq!(containing.unwrap().excerpt.info(), excerpt_1_info);
4967
4968    // Ranges intersecting multiple excerpts (should return None)
4969    let containing = snapshot.excerpt_containing(p20..p40);
4970    assert!(
4971        containing.is_none(),
4972        "excerpt_containing should return None for ranges spanning multiple excerpts"
4973    );
4974}
4975
4976#[gpui::test]
4977fn test_range_to_buffer_ranges_with_range_bounds(cx: &mut App) {
4978    use std::ops::Bound;
4979
4980    let buffer_1 = cx.new(|cx| Buffer::local("aaa\nbbb", cx));
4981    let buffer_2 = cx.new(|cx| Buffer::local("ccc", cx));
4982
4983    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
4984    multibuffer.update(cx, |multibuffer, cx| {
4985        multibuffer.set_excerpts_for_path(
4986            PathKey::sorted(0),
4987            buffer_1.clone(),
4988            [Point::new(0, 0)..Point::new(1, 3)],
4989            0,
4990            cx,
4991        );
4992
4993        multibuffer.set_excerpts_for_path(
4994            PathKey::sorted(1),
4995            buffer_2.clone(),
4996            [Point::new(0, 0)..Point::new(0, 3)],
4997            0,
4998            cx,
4999        );
5000    });
5001
5002    let snapshot = multibuffer.read(cx).snapshot(cx);
5003    assert_eq!(snapshot.text(), "aaa\nbbb\nccc");
5004
5005    let excerpt_2_start = Point::new(2, 0);
5006
5007    let ranges_half_open = snapshot.range_to_buffer_ranges(Point::zero()..excerpt_2_start);
5008    assert_eq!(
5009        ranges_half_open.len(),
5010        1,
5011        "Half-open range ending at excerpt start should EXCLUDE that excerpt"
5012    );
5013    assert_eq!(ranges_half_open[0].1, BufferOffset(0)..BufferOffset(7));
5014
5015    let ranges_inclusive = snapshot.range_to_buffer_ranges(Point::zero()..=excerpt_2_start);
5016    assert_eq!(
5017        ranges_inclusive.len(),
5018        2,
5019        "Inclusive range ending at excerpt start should INCLUDE that excerpt"
5020    );
5021    assert_eq!(ranges_half_open[0].1, BufferOffset(0)..BufferOffset(7));
5022    assert_eq!(
5023        ranges_half_open[0].0.remote_id(),
5024        buffer_1.read(cx).remote_id()
5025    );
5026    assert_eq!(ranges_half_open[1].1, BufferOffset(0)..BufferOffset(0));
5027    assert_eq!(
5028        ranges_half_open[1].0.remote_id(),
5029        buffer_2.read(cx).remote_id()
5030    );
5031
5032    let ranges_unbounded =
5033        snapshot.range_to_buffer_ranges((Bound::Included(Point::zero()), Bound::Unbounded));
5034    assert_eq!(
5035        ranges_unbounded.len(),
5036        2,
5037        "Unbounded end should include all excerpts"
5038    );
5039    assert_eq!(ranges_half_open[0].1, BufferOffset(0)..BufferOffset(7));
5040    assert_eq!(
5041        ranges_half_open[0].0.remote_id(),
5042        buffer_1.read(cx).remote_id()
5043    );
5044    assert_eq!(ranges_half_open[1].1, BufferOffset(0)..BufferOffset(3));
5045    assert_eq!(
5046        ranges_half_open[1].0.remote_id(),
5047        buffer_2.read(cx).remote_id()
5048    );
5049
5050    let ranges_excluded_end = snapshot.range_to_buffer_ranges((
5051        Bound::Included(Point::zero()),
5052        Bound::Excluded(excerpt_2_start),
5053    ));
5054    assert_eq!(
5055        ranges_excluded_end.len(),
5056        1,
5057        "Excluded end bound should exclude excerpt starting at that point"
5058    );
5059    assert_eq!(ranges_half_open[0].1, BufferOffset(0)..BufferOffset(7));
5060    assert_eq!(
5061        ranges_half_open[0].0.remote_id(),
5062        buffer_1.read(cx).remote_id()
5063    );
5064
5065    let buffer_empty = cx.new(|cx| Buffer::local("", cx));
5066    let multibuffer_trailing_empty = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
5067    let (te_excerpt_1_info, te_excerpt_2_info) =
5068        multibuffer_trailing_empty.update(cx, |multibuffer, cx| {
5069            multibuffer.set_excerpts_for_path(
5070                PathKey::sorted(0),
5071                buffer_1.clone(),
5072                [Point::new(0, 0)..Point::new(1, 3)],
5073                0,
5074                cx,
5075            );
5076
5077            multibuffer.set_excerpts_for_path(
5078                PathKey::sorted(1),
5079                buffer_empty.clone(),
5080                [Point::new(0, 0)..Point::new(0, 0)],
5081                0,
5082                cx,
5083            );
5084
5085            let snapshot = multibuffer.snapshot(cx);
5086            let mut infos = snapshot.excerpts().map(|(_, info)| info);
5087            (infos.next().unwrap(), infos.next().unwrap())
5088        });
5089
5090    let snapshot_trailing = multibuffer_trailing_empty.read(cx).snapshot(cx);
5091    assert_eq!(snapshot_trailing.text(), "aaa\nbbb\n");
5092
5093    let max_point = snapshot_trailing.max_point();
5094
5095    let ranges_half_open_max =
5096        snapshot_trailing.range_to_buffer_ranges_with_context(Point::zero()..max_point);
5097    assert_eq!(
5098        ranges_half_open_max.len(),
5099        1,
5100        "Half-open range to max_point should EXCLUDE trailing empty excerpt at max_point"
5101    );
5102    assert_eq!(ranges_half_open_max[0].2, te_excerpt_1_info.range.context);
5103
5104    let ranges_inclusive_max =
5105        snapshot_trailing.range_to_buffer_ranges_with_context(Point::zero()..=max_point);
5106    assert_eq!(
5107        ranges_inclusive_max.len(),
5108        2,
5109        "Inclusive range to max_point should INCLUDE trailing empty excerpt"
5110    );
5111    assert_eq!(ranges_inclusive_max[0].2, te_excerpt_1_info.range.context);
5112    assert_eq!(ranges_inclusive_max[1].2, te_excerpt_2_info.range.context);
5113
5114    let ranges_unbounded_trailing = snapshot_trailing
5115        .range_to_buffer_ranges_with_context((Bound::Included(Point::zero()), Bound::Unbounded));
5116    assert_eq!(
5117        ranges_unbounded_trailing.len(),
5118        2,
5119        "Unbounded end should include trailing empty excerpt"
5120    );
5121    assert_eq!(
5122        ranges_unbounded_trailing[0].2,
5123        te_excerpt_1_info.range.context
5124    );
5125    assert_eq!(
5126        ranges_unbounded_trailing[1].2,
5127        te_excerpt_2_info.range.context
5128    );
5129}
5130
5131#[gpui::test]
5132fn test_cannot_seek_backward_after_excerpt_replacement(cx: &mut TestAppContext) {
5133    let buffer_b_text: String = (0..50).map(|i| format!("line_b {i}\n")).collect();
5134    let buffer_b = cx.new(|cx| Buffer::local(buffer_b_text, cx));
5135
5136    let buffer_c_text: String = (0..10).map(|i| format!("line_c {i}\n")).collect();
5137    let buffer_c = cx.new(|cx| Buffer::local(buffer_c_text, cx));
5138
5139    let buffer_d_text: String = (0..10).map(|i| format!("line_d {i}\n")).collect();
5140    let buffer_d = cx.new(|cx| Buffer::local(buffer_d_text, cx));
5141
5142    let path_b = PathKey::with_sort_prefix(0, rel_path("bbb.rs").into_arc());
5143    let path_c = PathKey::with_sort_prefix(0, rel_path("ddd.rs").into_arc());
5144    let path_d = PathKey::with_sort_prefix(0, rel_path("ccc.rs").into_arc());
5145
5146    let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
5147
5148    multibuffer.update(cx, |multibuffer, cx| {
5149        multibuffer.set_excerpts_for_path(
5150            path_b.clone(),
5151            buffer_b.clone(),
5152            vec![
5153                Point::row_range(0..3),
5154                Point::row_range(15..18),
5155                Point::row_range(30..33),
5156            ],
5157            0,
5158            cx,
5159        );
5160    });
5161
5162    multibuffer.update(cx, |multibuffer, cx| {
5163        multibuffer.set_excerpts_for_path(
5164            path_c.clone(),
5165            buffer_c.clone(),
5166            vec![Point::row_range(0..3)],
5167            0,
5168            cx,
5169        );
5170    });
5171
5172    let (anchor_in_e_b2, anchor_in_e_b3) = multibuffer.read_with(cx, |multibuffer, cx| {
5173        let snapshot = multibuffer.snapshot(cx);
5174        let excerpt_infos: Vec<ExcerptInfo> = snapshot.excerpts().map(|(_, info)| info).collect();
5175        assert_eq!(excerpt_infos.len(), 4, "expected 4 excerpts (3×B + 1×C)");
5176
5177        let e_b2_info = excerpt_infos[1].clone();
5178        let e_b3_info = excerpt_infos[2].clone();
5179
5180        let anchor_b2 = Anchor::in_buffer(e_b2_info.path_key_index, e_b2_info.range.context.start);
5181        let anchor_b3 = Anchor::in_buffer(e_b3_info.path_key_index, e_b3_info.range.context.start);
5182        (anchor_b2, anchor_b3)
5183    });
5184
5185    multibuffer.update(cx, |multibuffer, cx| {
5186        multibuffer.set_excerpts_for_path(
5187            path_b.clone(),
5188            buffer_b.clone(),
5189            vec![Point::row_range(0..3), Point::row_range(28..36)],
5190            0,
5191            cx,
5192        );
5193    });
5194
5195    multibuffer.update(cx, |multibuffer, cx| {
5196        multibuffer.set_excerpts_for_path(
5197            path_d.clone(),
5198            buffer_d.clone(),
5199            vec![Point::row_range(0..3)],
5200            0,
5201            cx,
5202        );
5203    });
5204
5205    multibuffer.read_with(cx, |multibuffer, cx| {
5206        let snapshot = multibuffer.snapshot(cx);
5207        snapshot.summaries_for_anchors::<Point, _>(&[anchor_in_e_b2, anchor_in_e_b3]);
5208    });
5209}