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