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