test.rs

   1mod neovim_backed_test_context;
   2mod neovim_connection;
   3mod vim_test_context;
   4
   5use std::{sync::Arc, time::Duration};
   6
   7use collections::HashMap;
   8use command_palette::CommandPalette;
   9use editor::{
  10    AnchorRangeExt, DisplayPoint, Editor, EditorMode, MultiBuffer,
  11    actions::{DeleteLine, WrapSelectionsInTag},
  12    code_context_menus::CodeContextMenu,
  13    display_map::DisplayRow,
  14    test::editor_test_context::EditorTestContext,
  15};
  16use futures::StreamExt;
  17use gpui::{KeyBinding, Modifiers, MouseButton, TestAppContext, px};
  18use itertools::Itertools;
  19use language::{Language, LanguageConfig, Point};
  20pub use neovim_backed_test_context::*;
  21use settings::SettingsStore;
  22use ui::Pixels;
  23use util::test::marked_text_ranges;
  24pub use vim_test_context::*;
  25
  26use indoc::indoc;
  27use search::BufferSearchBar;
  28
  29use crate::{PushSneak, PushSneakBackward, insert::NormalBefore, motion, state::Mode};
  30
  31use util_macros::perf;
  32
  33#[perf]
  34#[gpui::test]
  35async fn test_initially_disabled(cx: &mut gpui::TestAppContext) {
  36    let mut cx = VimTestContext::new(cx, false).await;
  37    cx.simulate_keystrokes("h j k l");
  38    cx.assert_editor_state("hjklˇ");
  39}
  40
  41#[perf]
  42#[gpui::test]
  43async fn test_neovim(cx: &mut gpui::TestAppContext) {
  44    let mut cx = NeovimBackedTestContext::new(cx).await;
  45
  46    cx.simulate_shared_keystrokes("i").await;
  47    cx.shared_state().await.assert_matches();
  48    cx.simulate_shared_keystrokes("shift-t e s t space t e s t escape 0 d w")
  49        .await;
  50    cx.shared_state().await.assert_matches();
  51    cx.assert_editor_state("ˇtest");
  52}
  53
  54#[perf]
  55#[gpui::test]
  56async fn test_toggle_through_settings(cx: &mut gpui::TestAppContext) {
  57    let mut cx = VimTestContext::new(cx, true).await;
  58
  59    cx.simulate_keystrokes("i");
  60    assert_eq!(cx.mode(), Mode::Insert);
  61
  62    // Editor acts as though vim is disabled
  63    cx.disable_vim();
  64    cx.simulate_keystrokes("h j k l");
  65    cx.assert_editor_state("hjklˇ");
  66
  67    // Selections aren't changed if editor is blurred but vim-mode is still disabled.
  68    cx.cx.set_state("«hjklˇ»");
  69    cx.assert_editor_state("«hjklˇ»");
  70    cx.update_editor(|_, window, _cx| window.blur());
  71    cx.assert_editor_state("«hjklˇ»");
  72    cx.update_editor(|_, window, cx| cx.focus_self(window));
  73    cx.assert_editor_state("«hjklˇ»");
  74
  75    // Enabling dynamically sets vim mode again and restores normal mode
  76    cx.enable_vim();
  77    assert_eq!(cx.mode(), Mode::Normal);
  78    cx.simulate_keystrokes("h h h l");
  79    assert_eq!(cx.buffer_text(), "hjkl".to_owned());
  80    cx.assert_editor_state("hˇjkl");
  81    cx.simulate_keystrokes("i T e s t");
  82    cx.assert_editor_state("hTestˇjkl");
  83
  84    // Disabling and enabling resets to normal mode
  85    assert_eq!(cx.mode(), Mode::Insert);
  86    cx.disable_vim();
  87    cx.enable_vim();
  88    assert_eq!(cx.mode(), Mode::Normal);
  89}
  90
  91#[perf]
  92#[gpui::test]
  93async fn test_cancel_selection(cx: &mut gpui::TestAppContext) {
  94    let mut cx = VimTestContext::new(cx, true).await;
  95
  96    cx.set_state(
  97        indoc! {"The quick brown fox juˇmps over the lazy dog"},
  98        Mode::Normal,
  99    );
 100    // jumps
 101    cx.simulate_keystrokes("v l l");
 102    cx.assert_editor_state("The quick brown fox ju«mpsˇ» over the lazy dog");
 103
 104    cx.simulate_keystrokes("escape");
 105    cx.assert_editor_state("The quick brown fox jumpˇs over the lazy dog");
 106
 107    // go back to the same selection state
 108    cx.simulate_keystrokes("v h h");
 109    cx.assert_editor_state("The quick brown fox ju«ˇmps» over the lazy dog");
 110
 111    // Ctrl-[ should behave like Esc
 112    cx.simulate_keystrokes("ctrl-[");
 113    cx.assert_editor_state("The quick brown fox juˇmps over the lazy dog");
 114}
 115
 116#[perf]
 117#[gpui::test]
 118async fn test_buffer_search(cx: &mut gpui::TestAppContext) {
 119    let mut cx = VimTestContext::new(cx, true).await;
 120
 121    cx.set_state(
 122        indoc! {"
 123            The quick brown
 124            fox juˇmps over
 125            the lazy dog"},
 126        Mode::Normal,
 127    );
 128    cx.simulate_keystrokes("/");
 129
 130    let search_bar = cx.workspace(|workspace, _, cx| {
 131        workspace
 132            .active_pane()
 133            .read(cx)
 134            .toolbar()
 135            .read(cx)
 136            .item_of_type::<BufferSearchBar>()
 137            .expect("Buffer search bar should be deployed")
 138    });
 139
 140    cx.update_entity(search_bar, |bar, _, cx| {
 141        assert_eq!(bar.query(cx), "");
 142    })
 143}
 144
 145#[perf]
 146#[gpui::test]
 147async fn test_count_down(cx: &mut gpui::TestAppContext) {
 148    let mut cx = VimTestContext::new(cx, true).await;
 149
 150    cx.set_state(indoc! {"aˇa\nbb\ncc\ndd\nee"}, Mode::Normal);
 151    cx.simulate_keystrokes("2 down");
 152    cx.assert_editor_state("aa\nbb\ncˇc\ndd\nee");
 153    cx.simulate_keystrokes("9 down");
 154    cx.assert_editor_state("aa\nbb\ncc\ndd\neˇe");
 155}
 156
 157#[perf]
 158#[gpui::test]
 159async fn test_end_of_document_710(cx: &mut gpui::TestAppContext) {
 160    let mut cx = VimTestContext::new(cx, true).await;
 161
 162    // goes to end by default
 163    cx.set_state(indoc! {"aˇa\nbb\ncc"}, Mode::Normal);
 164    cx.simulate_keystrokes("shift-g");
 165    cx.assert_editor_state("aa\nbb\ncˇc");
 166
 167    // can go to line 1 (https://github.com/zed-industries/zed/issues/5812)
 168    cx.simulate_keystrokes("1 shift-g");
 169    cx.assert_editor_state("aˇa\nbb\ncc");
 170}
 171
 172#[perf]
 173#[gpui::test]
 174async fn test_end_of_line_with_times(cx: &mut gpui::TestAppContext) {
 175    let mut cx = VimTestContext::new(cx, true).await;
 176
 177    // goes to current line end
 178    cx.set_state(indoc! {"ˇaa\nbb\ncc"}, Mode::Normal);
 179    cx.simulate_keystrokes("$");
 180    cx.assert_editor_state("aˇa\nbb\ncc");
 181
 182    // goes to next line end
 183    cx.simulate_keystrokes("2 $");
 184    cx.assert_editor_state("aa\nbˇb\ncc");
 185
 186    // try to exceed the final line.
 187    cx.simulate_keystrokes("4 $");
 188    cx.assert_editor_state("aa\nbb\ncˇc");
 189}
 190
 191#[perf]
 192#[gpui::test]
 193async fn test_indent_outdent(cx: &mut gpui::TestAppContext) {
 194    let mut cx = VimTestContext::new(cx, true).await;
 195
 196    // works in normal mode
 197    cx.set_state(indoc! {"aa\nbˇb\ncc"}, Mode::Normal);
 198    cx.simulate_keystrokes("> >");
 199    cx.assert_editor_state("aa\n    bˇb\ncc");
 200    cx.simulate_keystrokes("< <");
 201    cx.assert_editor_state("aa\nbˇb\ncc");
 202
 203    // works in visual mode
 204    cx.simulate_keystrokes("shift-v down >");
 205    cx.assert_editor_state("aa\n    bˇb\n    cc");
 206
 207    // works as operator
 208    cx.set_state("aa\nbˇb\ncc\n", Mode::Normal);
 209    cx.simulate_keystrokes("> j");
 210    cx.assert_editor_state("aa\n    bˇb\n    cc\n");
 211    cx.simulate_keystrokes("< k");
 212    cx.assert_editor_state("aa\nbˇb\n    cc\n");
 213    cx.simulate_keystrokes("> i p");
 214    cx.assert_editor_state("    aa\n    bˇb\n        cc\n");
 215    cx.simulate_keystrokes("< i p");
 216    cx.assert_editor_state("aa\nbˇb\n    cc\n");
 217    cx.simulate_keystrokes("< i p");
 218    cx.assert_editor_state("aa\nbˇb\ncc\n");
 219
 220    cx.set_state("ˇaa\nbb\ncc\n", Mode::Normal);
 221    cx.simulate_keystrokes("> 2 j");
 222    cx.assert_editor_state("    ˇaa\n    bb\n    cc\n");
 223
 224    cx.set_state("aa\nbb\nˇcc\n", Mode::Normal);
 225    cx.simulate_keystrokes("> 2 k");
 226    cx.assert_editor_state("    aa\n    bb\n    ˇcc\n");
 227
 228    // works with repeat
 229    cx.set_state("a\nb\nccˇc\n", Mode::Normal);
 230    cx.simulate_keystrokes("> 2 k");
 231    cx.assert_editor_state("    a\n    b\n    ccˇc\n");
 232    cx.simulate_keystrokes(".");
 233    cx.assert_editor_state("        a\n        b\n        ccˇc\n");
 234    cx.simulate_keystrokes("v k <");
 235    cx.assert_editor_state("        a\n\n    ccc\n");
 236    cx.simulate_keystrokes(".");
 237    cx.assert_editor_state("        a\n\nccc\n");
 238}
 239
 240#[perf]
 241#[gpui::test]
 242async fn test_escape_command_palette(cx: &mut gpui::TestAppContext) {
 243    let mut cx = VimTestContext::new(cx, true).await;
 244
 245    cx.set_state("aˇbc\n", Mode::Normal);
 246    cx.simulate_keystrokes("i cmd-shift-p");
 247
 248    assert!(
 249        cx.workspace(|workspace, _, cx| workspace.active_modal::<CommandPalette>(cx).is_some())
 250    );
 251    cx.simulate_keystrokes("escape");
 252    cx.run_until_parked();
 253    assert!(
 254        !cx.workspace(|workspace, _, cx| workspace.active_modal::<CommandPalette>(cx).is_some())
 255    );
 256    cx.assert_state("aˇbc\n", Mode::Insert);
 257}
 258
 259#[perf]
 260#[gpui::test]
 261async fn test_escape_cancels(cx: &mut gpui::TestAppContext) {
 262    let mut cx = VimTestContext::new(cx, true).await;
 263
 264    cx.set_state("aˇbˇc", Mode::Normal);
 265    cx.simulate_keystrokes("escape");
 266
 267    cx.assert_state("aˇbc", Mode::Normal);
 268}
 269
 270#[perf]
 271#[gpui::test]
 272async fn test_selection_on_search(cx: &mut gpui::TestAppContext) {
 273    let mut cx = VimTestContext::new(cx, true).await;
 274
 275    cx.set_state(indoc! {"aa\nbˇb\ncc\ncc\ncc\n"}, Mode::Normal);
 276    cx.simulate_keystrokes("/ c c");
 277
 278    let search_bar = cx.workspace(|workspace, _, cx| {
 279        workspace
 280            .active_pane()
 281            .read(cx)
 282            .toolbar()
 283            .read(cx)
 284            .item_of_type::<BufferSearchBar>()
 285            .expect("Buffer search bar should be deployed")
 286    });
 287
 288    cx.update_entity(search_bar, |bar, _, cx| {
 289        assert_eq!(bar.query(cx), "cc");
 290    });
 291
 292    cx.update_editor(|editor, window, cx| {
 293        let highlights = editor.all_text_background_highlights(window, cx);
 294        assert_eq!(3, highlights.len());
 295        assert_eq!(
 296            DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2),
 297            highlights[0].0
 298        )
 299    });
 300    cx.simulate_keystrokes("enter");
 301
 302    cx.assert_state(indoc! {"aa\nbb\nˇcc\ncc\ncc\n"}, Mode::Normal);
 303    cx.simulate_keystrokes("n");
 304    cx.assert_state(indoc! {"aa\nbb\ncc\nˇcc\ncc\n"}, Mode::Normal);
 305    cx.simulate_keystrokes("shift-n");
 306    cx.assert_state(indoc! {"aa\nbb\nˇcc\ncc\ncc\n"}, Mode::Normal);
 307}
 308
 309#[perf]
 310#[gpui::test]
 311async fn test_word_characters(cx: &mut gpui::TestAppContext) {
 312    let mut cx = VimTestContext::new_typescript(cx).await;
 313    cx.set_state(
 314        indoc! { "
 315        class A {
 316            #ˇgoop = 99;
 317            $ˇgoop () { return this.#gˇoop };
 318        };
 319        console.log(new A().$gooˇp())
 320    "},
 321        Mode::Normal,
 322    );
 323    cx.simulate_keystrokes("v i w");
 324    cx.assert_state(
 325        indoc! {"
 326        class A {
 327            «#goopˇ» = 99;
 328            «$goopˇ» () { return this.«#goopˇ» };
 329        };
 330        console.log(new A().«$goopˇ»())
 331    "},
 332        Mode::Visual,
 333    )
 334}
 335
 336#[perf]
 337#[gpui::test]
 338async fn test_kebab_case(cx: &mut gpui::TestAppContext) {
 339    let mut cx = VimTestContext::new_html(cx).await;
 340    cx.set_state(
 341        indoc! { r#"
 342            <div><a class="bg-rˇed"></a></div>
 343            "#},
 344        Mode::Normal,
 345    );
 346    cx.simulate_keystrokes("v i w");
 347    cx.assert_state(
 348        indoc! { r#"
 349        <div><a class="bg-«redˇ»"></a></div>
 350        "#
 351        },
 352        Mode::Visual,
 353    )
 354}
 355
 356#[perf]
 357#[gpui::test]
 358async fn test_join_lines(cx: &mut gpui::TestAppContext) {
 359    let mut cx = NeovimBackedTestContext::new(cx).await;
 360
 361    cx.set_shared_state(indoc! {"
 362      ˇone
 363      two
 364      three
 365      four
 366      five
 367      six
 368      "})
 369        .await;
 370    cx.simulate_shared_keystrokes("shift-j").await;
 371    cx.shared_state().await.assert_eq(indoc! {"
 372          oneˇ two
 373          three
 374          four
 375          five
 376          six
 377          "});
 378    cx.simulate_shared_keystrokes("3 shift-j").await;
 379    cx.shared_state().await.assert_eq(indoc! {"
 380          one two threeˇ four
 381          five
 382          six
 383          "});
 384
 385    cx.set_shared_state(indoc! {"
 386      ˇone
 387      two
 388      three
 389      four
 390      five
 391      six
 392      "})
 393        .await;
 394    cx.simulate_shared_keystrokes("j v 3 j shift-j").await;
 395    cx.shared_state().await.assert_eq(indoc! {"
 396      one
 397      two three fourˇ five
 398      six
 399      "});
 400
 401    cx.set_shared_state(indoc! {"
 402      ˇone
 403      two
 404      three
 405      four
 406      five
 407      six
 408      "})
 409        .await;
 410    cx.simulate_shared_keystrokes("g shift-j").await;
 411    cx.shared_state().await.assert_eq(indoc! {"
 412          oneˇtwo
 413          three
 414          four
 415          five
 416          six
 417          "});
 418    cx.simulate_shared_keystrokes("3 g shift-j").await;
 419    cx.shared_state().await.assert_eq(indoc! {"
 420          onetwothreeˇfour
 421          five
 422          six
 423          "});
 424
 425    cx.set_shared_state(indoc! {"
 426      ˇone
 427      two
 428      three
 429      four
 430      five
 431      six
 432      "})
 433        .await;
 434    cx.simulate_shared_keystrokes("j v 3 j g shift-j").await;
 435    cx.shared_state().await.assert_eq(indoc! {"
 436      one
 437      twothreefourˇfive
 438      six
 439      "});
 440}
 441
 442#[cfg(target_os = "macos")]
 443#[perf]
 444#[gpui::test]
 445async fn test_wrapped_lines(cx: &mut gpui::TestAppContext) {
 446    let mut cx = NeovimBackedTestContext::new(cx).await;
 447
 448    cx.set_shared_wrap(12).await;
 449    // tests line wrap as follows:
 450    //  1: twelve char
 451    //     twelve char
 452    //  2: twelve char
 453    cx.set_shared_state(indoc! { "
 454        tˇwelve char twelve char
 455        twelve char
 456    "})
 457        .await;
 458    cx.simulate_shared_keystrokes("j").await;
 459    cx.shared_state().await.assert_eq(indoc! {"
 460        twelve char twelve char
 461        tˇwelve char
 462    "});
 463    cx.simulate_shared_keystrokes("k").await;
 464    cx.shared_state().await.assert_eq(indoc! {"
 465        tˇwelve char twelve char
 466        twelve char
 467    "});
 468    cx.simulate_shared_keystrokes("g j").await;
 469    cx.shared_state().await.assert_eq(indoc! {"
 470        twelve char tˇwelve char
 471        twelve char
 472    "});
 473    cx.simulate_shared_keystrokes("g j").await;
 474    cx.shared_state().await.assert_eq(indoc! {"
 475        twelve char twelve char
 476        tˇwelve char
 477    "});
 478
 479    cx.simulate_shared_keystrokes("g k").await;
 480    cx.shared_state().await.assert_eq(indoc! {"
 481        twelve char tˇwelve char
 482        twelve char
 483    "});
 484
 485    cx.simulate_shared_keystrokes("g ^").await;
 486    cx.shared_state().await.assert_eq(indoc! {"
 487        twelve char ˇtwelve char
 488        twelve char
 489    "});
 490
 491    cx.simulate_shared_keystrokes("^").await;
 492    cx.shared_state().await.assert_eq(indoc! {"
 493        ˇtwelve char twelve char
 494        twelve char
 495    "});
 496
 497    cx.simulate_shared_keystrokes("g $").await;
 498    cx.shared_state().await.assert_eq(indoc! {"
 499        twelve charˇ twelve char
 500        twelve char
 501    "});
 502    cx.simulate_shared_keystrokes("$").await;
 503    cx.shared_state().await.assert_eq(indoc! {"
 504        twelve char twelve chaˇr
 505        twelve char
 506    "});
 507
 508    cx.set_shared_state(indoc! { "
 509        tˇwelve char twelve char
 510        twelve char
 511    "})
 512        .await;
 513    cx.simulate_shared_keystrokes("enter").await;
 514    cx.shared_state().await.assert_eq(indoc! {"
 515            twelve char twelve char
 516            ˇtwelve char
 517        "});
 518
 519    cx.set_shared_state(indoc! { "
 520        twelve char
 521        tˇwelve char twelve char
 522        twelve char
 523    "})
 524        .await;
 525    cx.simulate_shared_keystrokes("o o escape").await;
 526    cx.shared_state().await.assert_eq(indoc! {"
 527        twelve char
 528        twelve char twelve char
 529        ˇo
 530        twelve char
 531    "});
 532
 533    cx.set_shared_state(indoc! { "
 534        twelve char
 535        tˇwelve char twelve char
 536        twelve char
 537    "})
 538        .await;
 539    cx.simulate_shared_keystrokes("shift-a a escape").await;
 540    cx.shared_state().await.assert_eq(indoc! {"
 541        twelve char
 542        twelve char twelve charˇa
 543        twelve char
 544    "});
 545    cx.simulate_shared_keystrokes("shift-i i escape").await;
 546    cx.shared_state().await.assert_eq(indoc! {"
 547        twelve char
 548        ˇitwelve char twelve chara
 549        twelve char
 550    "});
 551    cx.simulate_shared_keystrokes("shift-d").await;
 552    cx.shared_state().await.assert_eq(indoc! {"
 553        twelve char
 554        ˇ
 555        twelve char
 556    "});
 557
 558    cx.set_shared_state(indoc! { "
 559        twelve char
 560        twelve char tˇwelve char
 561        twelve char
 562    "})
 563        .await;
 564    cx.simulate_shared_keystrokes("shift-o o escape").await;
 565    cx.shared_state().await.assert_eq(indoc! {"
 566        twelve char
 567        ˇo
 568        twelve char twelve char
 569        twelve char
 570    "});
 571
 572    // line wraps as:
 573    // fourteen ch
 574    // ar
 575    // fourteen ch
 576    // ar
 577    cx.set_shared_state(indoc! { "
 578        fourteen chaˇr
 579        fourteen char
 580    "})
 581        .await;
 582
 583    cx.simulate_shared_keystrokes("d i w").await;
 584    cx.shared_state().await.assert_eq(indoc! {"
 585        fourteenˇ•
 586        fourteen char
 587    "});
 588    cx.simulate_shared_keystrokes("j shift-f e f r").await;
 589    cx.shared_state().await.assert_eq(indoc! {"
 590        fourteen•
 591        fourteen chaˇr
 592    "});
 593}
 594
 595#[perf]
 596#[gpui::test]
 597async fn test_folds(cx: &mut gpui::TestAppContext) {
 598    let mut cx = NeovimBackedTestContext::new(cx).await;
 599    cx.set_neovim_option("foldmethod=manual").await;
 600
 601    cx.set_shared_state(indoc! { "
 602        fn boop() {
 603          ˇbarp()
 604          bazp()
 605        }
 606    "})
 607        .await;
 608    cx.simulate_shared_keystrokes("shift-v j z f").await;
 609
 610    // visual display is now:
 611    // fn boop () {
 612    //  [FOLDED]
 613    // }
 614
 615    // TODO: this should not be needed but currently zf does not
 616    // return to normal mode.
 617    cx.simulate_shared_keystrokes("escape").await;
 618
 619    // skip over fold downward
 620    cx.simulate_shared_keystrokes("g g").await;
 621    cx.shared_state().await.assert_eq(indoc! {"
 622        ˇfn boop() {
 623          barp()
 624          bazp()
 625        }
 626    "});
 627
 628    cx.simulate_shared_keystrokes("j j").await;
 629    cx.shared_state().await.assert_eq(indoc! {"
 630        fn boop() {
 631          barp()
 632          bazp()
 633        ˇ}
 634    "});
 635
 636    // skip over fold upward
 637    cx.simulate_shared_keystrokes("2 k").await;
 638    cx.shared_state().await.assert_eq(indoc! {"
 639        ˇfn boop() {
 640          barp()
 641          bazp()
 642        }
 643    "});
 644
 645    // yank the fold
 646    cx.simulate_shared_keystrokes("down y y").await;
 647    cx.shared_clipboard()
 648        .await
 649        .assert_eq("  barp()\n  bazp()\n");
 650
 651    // re-open
 652    cx.simulate_shared_keystrokes("z o").await;
 653    cx.shared_state().await.assert_eq(indoc! {"
 654        fn boop() {
 655        ˇ  barp()
 656          bazp()
 657        }
 658    "});
 659}
 660
 661#[perf]
 662#[gpui::test]
 663async fn test_folds_panic(cx: &mut gpui::TestAppContext) {
 664    let mut cx = NeovimBackedTestContext::new(cx).await;
 665    cx.set_neovim_option("foldmethod=manual").await;
 666
 667    cx.set_shared_state(indoc! { "
 668        fn boop() {
 669          ˇbarp()
 670          bazp()
 671        }
 672    "})
 673        .await;
 674    cx.simulate_shared_keystrokes("shift-v j z f").await;
 675    cx.simulate_shared_keystrokes("escape").await;
 676    cx.simulate_shared_keystrokes("g g").await;
 677    cx.simulate_shared_keystrokes("5 d j").await;
 678    cx.shared_state().await.assert_eq("ˇ");
 679    cx.set_shared_state(indoc! {"
 680        fn boop() {
 681          ˇbarp()
 682          bazp()
 683        }
 684    "})
 685        .await;
 686    cx.simulate_shared_keystrokes("shift-v j j z f").await;
 687    cx.simulate_shared_keystrokes("escape").await;
 688    cx.simulate_shared_keystrokes("shift-g shift-v").await;
 689    cx.shared_state().await.assert_eq(indoc! {"
 690        fn boop() {
 691          barp()
 692          bazp()
 693        }
 694        ˇ"});
 695}
 696
 697#[perf]
 698#[gpui::test]
 699async fn test_clear_counts(cx: &mut gpui::TestAppContext) {
 700    let mut cx = NeovimBackedTestContext::new(cx).await;
 701
 702    cx.set_shared_state(indoc! {"
 703        The quick brown
 704        fox juˇmps over
 705        the lazy dog"})
 706        .await;
 707
 708    cx.simulate_shared_keystrokes("4 escape 3 d l").await;
 709    cx.shared_state().await.assert_eq(indoc! {"
 710        The quick brown
 711        fox juˇ over
 712        the lazy dog"});
 713}
 714
 715#[perf]
 716#[gpui::test]
 717async fn test_zero(cx: &mut gpui::TestAppContext) {
 718    let mut cx = NeovimBackedTestContext::new(cx).await;
 719
 720    cx.set_shared_state(indoc! {"
 721        The quˇick brown
 722        fox jumps over
 723        the lazy dog"})
 724        .await;
 725
 726    cx.simulate_shared_keystrokes("0").await;
 727    cx.shared_state().await.assert_eq(indoc! {"
 728        ˇThe quick brown
 729        fox jumps over
 730        the lazy dog"});
 731
 732    cx.simulate_shared_keystrokes("1 0 l").await;
 733    cx.shared_state().await.assert_eq(indoc! {"
 734        The quick ˇbrown
 735        fox jumps over
 736        the lazy dog"});
 737}
 738
 739#[perf]
 740#[gpui::test]
 741async fn test_selection_goal(cx: &mut gpui::TestAppContext) {
 742    let mut cx = NeovimBackedTestContext::new(cx).await;
 743
 744    cx.set_shared_state(indoc! {"
 745        ;;ˇ;
 746        Lorem Ipsum"})
 747        .await;
 748
 749    cx.simulate_shared_keystrokes("a down up ; down up").await;
 750    cx.shared_state().await.assert_eq(indoc! {"
 751        ;;;;ˇ
 752        Lorem Ipsum"});
 753}
 754
 755#[cfg(target_os = "macos")]
 756#[perf]
 757#[gpui::test]
 758async fn test_wrapped_motions(cx: &mut gpui::TestAppContext) {
 759    let mut cx = NeovimBackedTestContext::new(cx).await;
 760
 761    cx.set_shared_wrap(12).await;
 762
 763    cx.set_shared_state(indoc! {"
 764                aaˇaa
 765                😃😃"
 766    })
 767    .await;
 768    cx.simulate_shared_keystrokes("j").await;
 769    cx.shared_state().await.assert_eq(indoc! {"
 770                aaaa
 771                😃ˇ😃"
 772    });
 773
 774    cx.set_shared_state(indoc! {"
 775                123456789012aaˇaa
 776                123456789012😃😃"
 777    })
 778    .await;
 779    cx.simulate_shared_keystrokes("j").await;
 780    cx.shared_state().await.assert_eq(indoc! {"
 781        123456789012aaaa
 782        123456789012😃ˇ😃"
 783    });
 784
 785    cx.set_shared_state(indoc! {"
 786                123456789012aaˇaa
 787                123456789012😃😃"
 788    })
 789    .await;
 790    cx.simulate_shared_keystrokes("j").await;
 791    cx.shared_state().await.assert_eq(indoc! {"
 792        123456789012aaaa
 793        123456789012😃ˇ😃"
 794    });
 795
 796    cx.set_shared_state(indoc! {"
 797        123456789012aaaaˇaaaaaaaa123456789012
 798        wow
 799        123456789012😃😃😃😃😃😃123456789012"
 800    })
 801    .await;
 802    cx.simulate_shared_keystrokes("j j").await;
 803    cx.shared_state().await.assert_eq(indoc! {"
 804        123456789012aaaaaaaaaaaa123456789012
 805        wow
 806        123456789012😃😃ˇ😃😃😃😃123456789012"
 807    });
 808}
 809
 810#[perf]
 811#[gpui::test]
 812async fn test_wrapped_delete_end_document(cx: &mut gpui::TestAppContext) {
 813    let mut cx = NeovimBackedTestContext::new(cx).await;
 814
 815    cx.set_shared_wrap(12).await;
 816
 817    cx.set_shared_state(indoc! {"
 818                aaˇaaaaaaaaaaaaaaaaaa
 819                bbbbbbbbbbbbbbbbbbbb
 820                cccccccccccccccccccc"
 821    })
 822    .await;
 823    cx.simulate_shared_keystrokes("d shift-g i z z z").await;
 824    cx.shared_state().await.assert_eq(indoc! {"
 825                zzzˇ"
 826    });
 827}
 828
 829#[perf]
 830#[gpui::test]
 831async fn test_paragraphs_dont_wrap(cx: &mut gpui::TestAppContext) {
 832    let mut cx = NeovimBackedTestContext::new(cx).await;
 833
 834    cx.set_shared_state(indoc! {"
 835        one
 836        ˇ
 837        two"})
 838        .await;
 839
 840    cx.simulate_shared_keystrokes("} }").await;
 841    cx.shared_state().await.assert_eq(indoc! {"
 842        one
 843
 844        twˇo"});
 845
 846    cx.simulate_shared_keystrokes("{ { {").await;
 847    cx.shared_state().await.assert_eq(indoc! {"
 848        ˇone
 849
 850        two"});
 851}
 852
 853#[perf]
 854#[gpui::test]
 855async fn test_select_all_issue_2170(cx: &mut gpui::TestAppContext) {
 856    let mut cx = VimTestContext::new(cx, true).await;
 857
 858    cx.set_state(
 859        indoc! {"
 860        defmodule Test do
 861            def test(a, ˇ[_, _] = b), do: IO.puts('hi')
 862        end
 863    "},
 864        Mode::Normal,
 865    );
 866    cx.simulate_keystrokes("g a");
 867    cx.assert_state(
 868        indoc! {"
 869        defmodule Test do
 870            def test(a, «[ˇ»_, _] = b), do: IO.puts('hi')
 871        end
 872    "},
 873        Mode::Visual,
 874    );
 875}
 876
 877#[perf]
 878#[gpui::test]
 879async fn test_jk(cx: &mut gpui::TestAppContext) {
 880    let mut cx = NeovimBackedTestContext::new(cx).await;
 881
 882    cx.update(|_, cx| {
 883        cx.bind_keys([KeyBinding::new(
 884            "j k",
 885            NormalBefore,
 886            Some("vim_mode == insert"),
 887        )])
 888    });
 889    cx.neovim.exec("imap jk <esc>").await;
 890
 891    cx.set_shared_state("ˇhello").await;
 892    cx.simulate_shared_keystrokes("i j o j k").await;
 893    cx.shared_state().await.assert_eq("jˇohello");
 894}
 895
 896fn assert_pending_input(cx: &mut VimTestContext, expected: &str) {
 897    cx.update_editor(|editor, window, cx| {
 898        let snapshot = editor.snapshot(window, cx);
 899        let highlights = editor
 900            .text_highlights::<editor::PendingInput>(cx)
 901            .unwrap()
 902            .1;
 903        let (_, ranges) = marked_text_ranges(expected, false);
 904
 905        assert_eq!(
 906            highlights
 907                .iter()
 908                .map(|highlight| highlight.to_offset(&snapshot.buffer_snapshot()))
 909                .collect::<Vec<_>>(),
 910            ranges
 911        )
 912    });
 913}
 914
 915#[perf]
 916#[gpui::test]
 917async fn test_jk_multi(cx: &mut gpui::TestAppContext) {
 918    let mut cx = VimTestContext::new(cx, true).await;
 919
 920    cx.update(|_, cx| {
 921        cx.bind_keys([KeyBinding::new(
 922            "j k l",
 923            NormalBefore,
 924            Some("vim_mode == insert"),
 925        )])
 926    });
 927
 928    cx.set_state("ˇone ˇone ˇone", Mode::Normal);
 929    cx.simulate_keystrokes("i j");
 930    cx.simulate_keystrokes("k");
 931    cx.assert_state("ˇjkone ˇjkone ˇjkone", Mode::Insert);
 932    assert_pending_input(&mut cx, "«jk»one «jk»one «jk»one");
 933    cx.simulate_keystrokes("o j k");
 934    cx.assert_state("jkoˇjkone jkoˇjkone jkoˇjkone", Mode::Insert);
 935    assert_pending_input(&mut cx, "jko«jk»one jko«jk»one jko«jk»one");
 936    cx.simulate_keystrokes("l");
 937    cx.assert_state("jkˇoone jkˇoone jkˇoone", Mode::Normal);
 938}
 939
 940#[perf]
 941#[gpui::test]
 942async fn test_jk_delay(cx: &mut gpui::TestAppContext) {
 943    let mut cx = VimTestContext::new(cx, true).await;
 944
 945    cx.update(|_, cx| {
 946        cx.bind_keys([KeyBinding::new(
 947            "j k",
 948            NormalBefore,
 949            Some("vim_mode == insert"),
 950        )])
 951    });
 952
 953    cx.set_state("ˇhello", Mode::Normal);
 954    cx.simulate_keystrokes("i j");
 955    cx.executor().advance_clock(Duration::from_millis(500));
 956    cx.run_until_parked();
 957    cx.assert_state("ˇjhello", Mode::Insert);
 958    cx.update_editor(|editor, window, cx| {
 959        let snapshot = editor.snapshot(window, cx);
 960        let highlights = editor
 961            .text_highlights::<editor::PendingInput>(cx)
 962            .unwrap()
 963            .1;
 964
 965        assert_eq!(
 966            highlights
 967                .iter()
 968                .map(|highlight| highlight.to_offset(&snapshot.buffer_snapshot()))
 969                .collect::<Vec<_>>(),
 970            vec![0..1]
 971        )
 972    });
 973    cx.executor().advance_clock(Duration::from_millis(500));
 974    cx.run_until_parked();
 975    cx.assert_state("jˇhello", Mode::Insert);
 976    cx.simulate_keystrokes("k j k");
 977    cx.assert_state("jˇkhello", Mode::Normal);
 978}
 979
 980#[perf]
 981#[gpui::test]
 982async fn test_jk_max_count(cx: &mut gpui::TestAppContext) {
 983    let mut cx = NeovimBackedTestContext::new(cx).await;
 984
 985    cx.set_shared_state("1\nˇ2\n3").await;
 986    cx.simulate_shared_keystrokes("9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 j")
 987        .await;
 988    cx.shared_state().await.assert_eq("1\n2\nˇ3");
 989
 990    let number: String = usize::MAX.to_string().split("").join(" ");
 991    cx.simulate_shared_keystrokes(&format!("{number} k")).await;
 992    cx.shared_state().await.assert_eq("ˇ1\n2\n3");
 993}
 994
 995#[perf]
 996#[gpui::test]
 997async fn test_comma_w(cx: &mut gpui::TestAppContext) {
 998    let mut cx = NeovimBackedTestContext::new(cx).await;
 999
1000    cx.update(|_, cx| {
1001        cx.bind_keys([KeyBinding::new(
1002            ", w",
1003            motion::Down {
1004                display_lines: false,
1005            },
1006            Some("vim_mode == normal"),
1007        )])
1008    });
1009    cx.neovim.exec("map ,w j").await;
1010
1011    cx.set_shared_state("ˇhello hello\nhello hello").await;
1012    cx.simulate_shared_keystrokes("f o ; , w").await;
1013    cx.shared_state()
1014        .await
1015        .assert_eq("hello hello\nhello hellˇo");
1016
1017    cx.set_shared_state("ˇhello hello\nhello hello").await;
1018    cx.simulate_shared_keystrokes("f o ; , i").await;
1019    cx.shared_state()
1020        .await
1021        .assert_eq("hellˇo hello\nhello hello");
1022}
1023
1024#[perf]
1025#[gpui::test]
1026async fn test_completion_menu_scroll_aside(cx: &mut TestAppContext) {
1027    let mut cx = VimTestContext::new_typescript(cx).await;
1028
1029    cx.lsp
1030        .set_request_handler::<lsp::request::Completion, _, _>(move |_, _| async move {
1031            Ok(Some(lsp::CompletionResponse::Array(vec![
1032                lsp::CompletionItem {
1033                    label: "Test Item".to_string(),
1034                    documentation: Some(lsp::Documentation::String(
1035                        "This is some very long documentation content that will be displayed in the aside panel for scrolling.\n".repeat(50)
1036                    )),
1037                    ..Default::default()
1038                },
1039            ])))
1040        });
1041
1042    cx.set_state("variableˇ", Mode::Insert);
1043    cx.simulate_keystroke(".");
1044    cx.executor().run_until_parked();
1045
1046    let mut initial_offset: Pixels = px(0.0);
1047
1048    cx.update_editor(|editor, _, _| {
1049        let binding = editor.context_menu().borrow();
1050        let Some(CodeContextMenu::Completions(menu)) = binding.as_ref() else {
1051            panic!("Should have completions menu open");
1052        };
1053
1054        initial_offset = menu.scroll_handle_aside.offset().y;
1055    });
1056
1057    // The `ctrl-e` shortcut should scroll the completion menu's aside content
1058    // down, so the updated offset should be lower than the initial offset.
1059    cx.simulate_keystroke("ctrl-e");
1060    cx.update_editor(|editor, _, _| {
1061        let binding = editor.context_menu().borrow();
1062        let Some(CodeContextMenu::Completions(menu)) = binding.as_ref() else {
1063            panic!("Should have completions menu open");
1064        };
1065
1066        assert!(menu.scroll_handle_aside.offset().y < initial_offset);
1067    });
1068
1069    // The `ctrl-y` shortcut should do the inverse scrolling as `ctrl-e`, so the
1070    // offset should now be the same as the initial offset.
1071    cx.simulate_keystroke("ctrl-y");
1072    cx.update_editor(|editor, _, _| {
1073        let binding = editor.context_menu().borrow();
1074        let Some(CodeContextMenu::Completions(menu)) = binding.as_ref() else {
1075            panic!("Should have completions menu open");
1076        };
1077
1078        assert_eq!(menu.scroll_handle_aside.offset().y, initial_offset);
1079    });
1080
1081    // The `ctrl-d` shortcut should scroll the completion menu's aside content
1082    // down, so the updated offset should be lower than the initial offset.
1083    cx.simulate_keystroke("ctrl-d");
1084    cx.update_editor(|editor, _, _| {
1085        let binding = editor.context_menu().borrow();
1086        let Some(CodeContextMenu::Completions(menu)) = binding.as_ref() else {
1087            panic!("Should have completions menu open");
1088        };
1089
1090        assert!(menu.scroll_handle_aside.offset().y < initial_offset);
1091    });
1092
1093    // The `ctrl-u` shortcut should do the inverse scrolling as `ctrl-u`, so the
1094    // offset should now be the same as the initial offset.
1095    cx.simulate_keystroke("ctrl-u");
1096    cx.update_editor(|editor, _, _| {
1097        let binding = editor.context_menu().borrow();
1098        let Some(CodeContextMenu::Completions(menu)) = binding.as_ref() else {
1099            panic!("Should have completions menu open");
1100        };
1101
1102        assert_eq!(menu.scroll_handle_aside.offset().y, initial_offset);
1103    });
1104}
1105
1106#[perf]
1107#[gpui::test]
1108async fn test_rename(cx: &mut gpui::TestAppContext) {
1109    let mut cx = VimTestContext::new_typescript(cx).await;
1110
1111    cx.set_state("const beˇfore = 2; console.log(before)", Mode::Normal);
1112    let def_range = cx.lsp_range("const «beforeˇ» = 2; console.log(before)");
1113    let tgt_range = cx.lsp_range("const before = 2; console.log(«beforeˇ»)");
1114    let mut prepare_request = cx.set_request_handler::<lsp::request::PrepareRenameRequest, _, _>(
1115        move |_, _, _| async move { Ok(Some(lsp::PrepareRenameResponse::Range(def_range))) },
1116    );
1117    let mut rename_request =
1118        cx.set_request_handler::<lsp::request::Rename, _, _>(move |url, params, _| async move {
1119            Ok(Some(lsp::WorkspaceEdit {
1120                changes: Some(
1121                    [(
1122                        url.clone(),
1123                        vec![
1124                            lsp::TextEdit::new(def_range, params.new_name.clone()),
1125                            lsp::TextEdit::new(tgt_range, params.new_name),
1126                        ],
1127                    )]
1128                    .into(),
1129                ),
1130                ..Default::default()
1131            }))
1132        });
1133
1134    cx.simulate_keystrokes("c d");
1135    prepare_request.next().await.unwrap();
1136    cx.simulate_input("after");
1137    cx.simulate_keystrokes("enter");
1138    rename_request.next().await.unwrap();
1139    cx.assert_state("const afterˇ = 2; console.log(after)", Mode::Normal)
1140}
1141
1142#[gpui::test]
1143async fn test_go_to_definition(cx: &mut gpui::TestAppContext) {
1144    let mut cx = VimTestContext::new_typescript(cx).await;
1145
1146    cx.set_state("const before = 2; console.log(beforˇe)", Mode::Normal);
1147    let def_range = cx.lsp_range("const «beforeˇ» = 2; console.log(before)");
1148    let mut go_to_request =
1149        cx.set_request_handler::<lsp::request::GotoDefinition, _, _>(move |url, _, _| async move {
1150            Ok(Some(lsp::GotoDefinitionResponse::Scalar(
1151                lsp::Location::new(url.clone(), def_range),
1152            )))
1153        });
1154
1155    cx.simulate_keystrokes("g d");
1156    go_to_request.next().await.unwrap();
1157    cx.run_until_parked();
1158
1159    cx.assert_state("const ˇbefore = 2; console.log(before)", Mode::Normal);
1160}
1161
1162#[perf]
1163#[gpui::test]
1164async fn test_remap(cx: &mut gpui::TestAppContext) {
1165    let mut cx = VimTestContext::new(cx, true).await;
1166
1167    // test moving the cursor
1168    cx.update(|_, cx| {
1169        cx.bind_keys([KeyBinding::new(
1170            "g z",
1171            workspace::SendKeystrokes("l l l l".to_string()),
1172            None,
1173        )])
1174    });
1175    cx.set_state("ˇ123456789", Mode::Normal);
1176    cx.simulate_keystrokes("g z");
1177    cx.assert_state("1234ˇ56789", Mode::Normal);
1178
1179    // test switching modes
1180    cx.update(|_, cx| {
1181        cx.bind_keys([KeyBinding::new(
1182            "g y",
1183            workspace::SendKeystrokes("i f o o escape l".to_string()),
1184            None,
1185        )])
1186    });
1187    cx.set_state("ˇ123456789", Mode::Normal);
1188    cx.simulate_keystrokes("g y");
1189    cx.assert_state("fooˇ123456789", Mode::Normal);
1190
1191    // test recursion
1192    cx.update(|_, cx| {
1193        cx.bind_keys([KeyBinding::new(
1194            "g x",
1195            workspace::SendKeystrokes("g z g y".to_string()),
1196            None,
1197        )])
1198    });
1199    cx.set_state("ˇ123456789", Mode::Normal);
1200    cx.simulate_keystrokes("g x");
1201    cx.assert_state("1234fooˇ56789", Mode::Normal);
1202
1203    // test command
1204    cx.update(|_, cx| {
1205        cx.bind_keys([KeyBinding::new(
1206            "g w",
1207            workspace::SendKeystrokes(": j enter".to_string()),
1208            None,
1209        )])
1210    });
1211    cx.set_state("ˇ1234\n56789", Mode::Normal);
1212    cx.simulate_keystrokes("g w");
1213    cx.assert_state("1234ˇ 56789", Mode::Normal);
1214
1215    // test leaving command
1216    cx.update(|_, cx| {
1217        cx.bind_keys([KeyBinding::new(
1218            "g u",
1219            workspace::SendKeystrokes("g w g z".to_string()),
1220            None,
1221        )])
1222    });
1223    cx.set_state("ˇ1234\n56789", Mode::Normal);
1224    cx.simulate_keystrokes("g u");
1225    cx.assert_state("1234 567ˇ89", Mode::Normal);
1226
1227    // test leaving command
1228    cx.update(|_, cx| {
1229        cx.bind_keys([KeyBinding::new(
1230            "g t",
1231            workspace::SendKeystrokes("i space escape".to_string()),
1232            None,
1233        )])
1234    });
1235    cx.set_state("12ˇ34", Mode::Normal);
1236    cx.simulate_keystrokes("g t");
1237    cx.assert_state("12ˇ 34", Mode::Normal);
1238}
1239
1240#[perf]
1241#[gpui::test]
1242async fn test_undo(cx: &mut gpui::TestAppContext) {
1243    let mut cx = NeovimBackedTestContext::new(cx).await;
1244
1245    cx.set_shared_state("hello quˇoel world").await;
1246    cx.simulate_shared_keystrokes("v i w s c o escape u").await;
1247    cx.shared_state().await.assert_eq("hello ˇquoel world");
1248    cx.simulate_shared_keystrokes("ctrl-r").await;
1249    cx.shared_state().await.assert_eq("hello ˇco world");
1250    cx.simulate_shared_keystrokes("a o right l escape").await;
1251    cx.shared_state().await.assert_eq("hello cooˇl world");
1252    cx.simulate_shared_keystrokes("u").await;
1253    cx.shared_state().await.assert_eq("hello cooˇ world");
1254    cx.simulate_shared_keystrokes("u").await;
1255    cx.shared_state().await.assert_eq("hello cˇo world");
1256    cx.simulate_shared_keystrokes("u").await;
1257    cx.shared_state().await.assert_eq("hello ˇquoel world");
1258
1259    cx.set_shared_state("hello quˇoel world").await;
1260    cx.simulate_shared_keystrokes("v i w ~ u").await;
1261    cx.shared_state().await.assert_eq("hello ˇquoel world");
1262
1263    cx.set_shared_state("\nhello quˇoel world\n").await;
1264    cx.simulate_shared_keystrokes("shift-v s c escape u").await;
1265    cx.shared_state().await.assert_eq("\nˇhello quoel world\n");
1266
1267    cx.set_shared_state(indoc! {"
1268        ˇ1
1269        2
1270        3"})
1271        .await;
1272
1273    cx.simulate_shared_keystrokes("ctrl-v shift-g ctrl-a").await;
1274    cx.shared_state().await.assert_eq(indoc! {"
1275        ˇ2
1276        3
1277        4"});
1278
1279    cx.simulate_shared_keystrokes("u").await;
1280    cx.shared_state().await.assert_eq(indoc! {"
1281        ˇ1
1282        2
1283        3"});
1284}
1285
1286#[perf]
1287#[gpui::test]
1288async fn test_mouse_selection(cx: &mut TestAppContext) {
1289    let mut cx = VimTestContext::new(cx, true).await;
1290
1291    cx.set_state("ˇone two three", Mode::Normal);
1292
1293    let start_point = cx.pixel_position("one twˇo three");
1294    let end_point = cx.pixel_position("one ˇtwo three");
1295
1296    cx.simulate_mouse_down(start_point, MouseButton::Left, Modifiers::none());
1297    cx.simulate_mouse_move(end_point, MouseButton::Left, Modifiers::none());
1298    cx.simulate_mouse_up(end_point, MouseButton::Left, Modifiers::none());
1299
1300    cx.assert_state("one «ˇtwo» three", Mode::Visual)
1301}
1302
1303#[perf]
1304#[gpui::test]
1305async fn test_lowercase_marks(cx: &mut TestAppContext) {
1306    let mut cx = NeovimBackedTestContext::new(cx).await;
1307
1308    cx.set_shared_state("line one\nline ˇtwo\nline three").await;
1309    cx.simulate_shared_keystrokes("m a l ' a").await;
1310    cx.shared_state()
1311        .await
1312        .assert_eq("line one\nˇline two\nline three");
1313    cx.simulate_shared_keystrokes("` a").await;
1314    cx.shared_state()
1315        .await
1316        .assert_eq("line one\nline ˇtwo\nline three");
1317
1318    cx.simulate_shared_keystrokes("^ d ` a").await;
1319    cx.shared_state()
1320        .await
1321        .assert_eq("line one\nˇtwo\nline three");
1322}
1323
1324#[perf]
1325#[gpui::test]
1326async fn test_lt_gt_marks(cx: &mut TestAppContext) {
1327    let mut cx = NeovimBackedTestContext::new(cx).await;
1328
1329    cx.set_shared_state(indoc!(
1330        "
1331        Line one
1332        Line two
1333        Line ˇthree
1334        Line four
1335        Line five
1336    "
1337    ))
1338    .await;
1339
1340    cx.simulate_shared_keystrokes("v j escape k k").await;
1341
1342    cx.simulate_shared_keystrokes("' <").await;
1343    cx.shared_state().await.assert_eq(indoc! {"
1344        Line one
1345        Line two
1346        ˇLine three
1347        Line four
1348        Line five
1349    "});
1350
1351    cx.simulate_shared_keystrokes("` <").await;
1352    cx.shared_state().await.assert_eq(indoc! {"
1353        Line one
1354        Line two
1355        Line ˇthree
1356        Line four
1357        Line five
1358    "});
1359
1360    cx.simulate_shared_keystrokes("' >").await;
1361    cx.shared_state().await.assert_eq(indoc! {"
1362        Line one
1363        Line two
1364        Line three
1365        ˇLine four
1366        Line five
1367    "
1368    });
1369
1370    cx.simulate_shared_keystrokes("` >").await;
1371    cx.shared_state().await.assert_eq(indoc! {"
1372        Line one
1373        Line two
1374        Line three
1375        Line ˇfour
1376        Line five
1377    "
1378    });
1379
1380    cx.simulate_shared_keystrokes("v i w o escape").await;
1381    cx.simulate_shared_keystrokes("` >").await;
1382    cx.shared_state().await.assert_eq(indoc! {"
1383        Line one
1384        Line two
1385        Line three
1386        Line fouˇr
1387        Line five
1388    "
1389    });
1390    cx.simulate_shared_keystrokes("` <").await;
1391    cx.shared_state().await.assert_eq(indoc! {"
1392        Line one
1393        Line two
1394        Line three
1395        Line ˇfour
1396        Line five
1397    "
1398    });
1399}
1400
1401#[perf]
1402#[gpui::test]
1403async fn test_caret_mark(cx: &mut TestAppContext) {
1404    let mut cx = NeovimBackedTestContext::new(cx).await;
1405
1406    cx.set_shared_state(indoc!(
1407        "
1408        Line one
1409        Line two
1410        Line three
1411        ˇLine four
1412        Line five
1413    "
1414    ))
1415    .await;
1416
1417    cx.simulate_shared_keystrokes("c w shift-s t r a i g h t space t h i n g escape j j")
1418        .await;
1419
1420    cx.simulate_shared_keystrokes("' ^").await;
1421    cx.shared_state().await.assert_eq(indoc! {"
1422        Line one
1423        Line two
1424        Line three
1425        ˇStraight thing four
1426        Line five
1427    "
1428    });
1429
1430    cx.simulate_shared_keystrokes("` ^").await;
1431    cx.shared_state().await.assert_eq(indoc! {"
1432        Line one
1433        Line two
1434        Line three
1435        Straight thingˇ four
1436        Line five
1437    "
1438    });
1439
1440    cx.simulate_shared_keystrokes("k a ! escape k g i ?").await;
1441    cx.shared_state().await.assert_eq(indoc! {"
1442        Line one
1443        Line two
1444        Line three!?ˇ
1445        Straight thing four
1446        Line five
1447    "
1448    });
1449}
1450
1451#[cfg(target_os = "macos")]
1452#[perf]
1453#[gpui::test]
1454async fn test_dw_eol(cx: &mut gpui::TestAppContext) {
1455    let mut cx = NeovimBackedTestContext::new(cx).await;
1456
1457    cx.set_shared_wrap(12).await;
1458    cx.set_shared_state("twelve ˇchar twelve char\ntwelve char")
1459        .await;
1460    cx.simulate_shared_keystrokes("d w").await;
1461    cx.shared_state()
1462        .await
1463        .assert_eq("twelve ˇtwelve char\ntwelve char");
1464}
1465
1466#[perf]
1467#[gpui::test]
1468async fn test_toggle_comments(cx: &mut gpui::TestAppContext) {
1469    let mut cx = VimTestContext::new(cx, true).await;
1470
1471    let language = std::sync::Arc::new(language::Language::new(
1472        language::LanguageConfig {
1473            line_comments: vec!["// ".into(), "//! ".into(), "/// ".into()],
1474            ..Default::default()
1475        },
1476        Some(language::tree_sitter_rust::LANGUAGE.into()),
1477    ));
1478    cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
1479
1480    // works in normal model
1481    cx.set_state(
1482        indoc! {"
1483      ˇone
1484      two
1485      three
1486      "},
1487        Mode::Normal,
1488    );
1489    cx.simulate_keystrokes("g c c");
1490    cx.assert_state(
1491        indoc! {"
1492          // ˇone
1493          two
1494          three
1495          "},
1496        Mode::Normal,
1497    );
1498
1499    // works in visual mode
1500    cx.simulate_keystrokes("v j g c");
1501    cx.assert_state(
1502        indoc! {"
1503          // // ˇone
1504          // two
1505          three
1506          "},
1507        Mode::Normal,
1508    );
1509
1510    // works in visual line mode
1511    cx.simulate_keystrokes("shift-v j g c");
1512    cx.assert_state(
1513        indoc! {"
1514          // ˇone
1515          two
1516          three
1517          "},
1518        Mode::Normal,
1519    );
1520
1521    // works with count
1522    cx.simulate_keystrokes("g c 2 j");
1523    cx.assert_state(
1524        indoc! {"
1525            // // ˇone
1526            // two
1527            // three
1528            "},
1529        Mode::Normal,
1530    );
1531
1532    // works with motion object
1533    cx.simulate_keystrokes("shift-g");
1534    cx.simulate_keystrokes("g c g g");
1535    cx.assert_state(
1536        indoc! {"
1537            // one
1538            two
1539            three
1540            ˇ"},
1541        Mode::Normal,
1542    );
1543}
1544
1545#[perf]
1546#[gpui::test]
1547async fn test_find_multibyte(cx: &mut gpui::TestAppContext) {
1548    let mut cx = NeovimBackedTestContext::new(cx).await;
1549
1550    cx.set_shared_state(r#"<label for="guests">ˇPočet hostů</label>"#)
1551        .await;
1552
1553    cx.simulate_shared_keystrokes("c t < o escape").await;
1554    cx.shared_state()
1555        .await
1556        .assert_eq(r#"<label for="guests">ˇo</label>"#);
1557}
1558
1559#[perf]
1560#[gpui::test]
1561async fn test_sneak(cx: &mut gpui::TestAppContext) {
1562    let mut cx = VimTestContext::new(cx, true).await;
1563
1564    cx.update(|_window, cx| {
1565        cx.bind_keys([
1566            KeyBinding::new(
1567                "s",
1568                PushSneak { first_char: None },
1569                Some("vim_mode == normal"),
1570            ),
1571            KeyBinding::new(
1572                "shift-s",
1573                PushSneakBackward { first_char: None },
1574                Some("vim_mode == normal"),
1575            ),
1576            KeyBinding::new(
1577                "shift-s",
1578                PushSneakBackward { first_char: None },
1579                Some("vim_mode == visual"),
1580            ),
1581        ])
1582    });
1583
1584    // Sneak forwards multibyte & multiline
1585    cx.set_state(
1586        indoc! {
1587            r#"<labelˇ for="guests">
1588                    Počet hostů
1589                </label>"#
1590        },
1591        Mode::Normal,
1592    );
1593    cx.simulate_keystrokes("s t ů");
1594    cx.assert_state(
1595        indoc! {
1596            r#"<label for="guests">
1597                Počet hosˇtů
1598            </label>"#
1599        },
1600        Mode::Normal,
1601    );
1602
1603    // Visual sneak backwards multibyte & multiline
1604    cx.simulate_keystrokes("v S < l");
1605    cx.assert_state(
1606        indoc! {
1607            r#"«ˇ<label for="guests">
1608                Počet host»ů
1609            </label>"#
1610        },
1611        Mode::Visual,
1612    );
1613
1614    // Sneak backwards repeated
1615    cx.set_state(r#"11 12 13 ˇ14"#, Mode::Normal);
1616    cx.simulate_keystrokes("S space 1");
1617    cx.assert_state(r#"11 12ˇ 13 14"#, Mode::Normal);
1618    cx.simulate_keystrokes(";");
1619    cx.assert_state(r#"11ˇ 12 13 14"#, Mode::Normal);
1620}
1621
1622#[perf]
1623#[gpui::test]
1624async fn test_plus_minus(cx: &mut gpui::TestAppContext) {
1625    let mut cx = NeovimBackedTestContext::new(cx).await;
1626
1627    cx.set_shared_state(indoc! {
1628        "one
1629           two
1630        thrˇee
1631    "})
1632        .await;
1633
1634    cx.simulate_shared_keystrokes("-").await;
1635    cx.shared_state().await.assert_matches();
1636    cx.simulate_shared_keystrokes("-").await;
1637    cx.shared_state().await.assert_matches();
1638    cx.simulate_shared_keystrokes("+").await;
1639    cx.shared_state().await.assert_matches();
1640}
1641
1642#[perf]
1643#[gpui::test]
1644async fn test_command_alias(cx: &mut gpui::TestAppContext) {
1645    let mut cx = VimTestContext::new(cx, true).await;
1646    cx.update_global(|store: &mut SettingsStore, cx| {
1647        store.update_user_settings(cx, |s| {
1648            let mut aliases = HashMap::default();
1649            aliases.insert("Q".to_string(), "upper".to_string());
1650            s.workspace.command_aliases = aliases
1651        });
1652    });
1653
1654    cx.set_state("ˇhello world", Mode::Normal);
1655    cx.simulate_keystrokes(": Q");
1656    cx.set_state("ˇHello world", Mode::Normal);
1657}
1658
1659#[perf]
1660#[gpui::test]
1661async fn test_remap_adjacent_dog_cat(cx: &mut gpui::TestAppContext) {
1662    let mut cx = NeovimBackedTestContext::new(cx).await;
1663    cx.update(|_, cx| {
1664        cx.bind_keys([
1665            KeyBinding::new(
1666                "d o g",
1667                workspace::SendKeystrokes("🐶".to_string()),
1668                Some("vim_mode == insert"),
1669            ),
1670            KeyBinding::new(
1671                "c a t",
1672                workspace::SendKeystrokes("🐱".to_string()),
1673                Some("vim_mode == insert"),
1674            ),
1675        ])
1676    });
1677    cx.neovim.exec("imap dog 🐶").await;
1678    cx.neovim.exec("imap cat 🐱").await;
1679
1680    cx.set_shared_state("ˇ").await;
1681    cx.simulate_shared_keystrokes("i d o g").await;
1682    cx.shared_state().await.assert_eq("🐶ˇ");
1683
1684    cx.set_shared_state("ˇ").await;
1685    cx.simulate_shared_keystrokes("i d o d o g").await;
1686    cx.shared_state().await.assert_eq("do🐶ˇ");
1687
1688    cx.set_shared_state("ˇ").await;
1689    cx.simulate_shared_keystrokes("i d o c a t").await;
1690    cx.shared_state().await.assert_eq("do🐱ˇ");
1691}
1692
1693#[perf]
1694#[gpui::test]
1695async fn test_remap_nested_pineapple(cx: &mut gpui::TestAppContext) {
1696    let mut cx = NeovimBackedTestContext::new(cx).await;
1697    cx.update(|_, cx| {
1698        cx.bind_keys([
1699            KeyBinding::new(
1700                "p i n",
1701                workspace::SendKeystrokes("📌".to_string()),
1702                Some("vim_mode == insert"),
1703            ),
1704            KeyBinding::new(
1705                "p i n e",
1706                workspace::SendKeystrokes("🌲".to_string()),
1707                Some("vim_mode == insert"),
1708            ),
1709            KeyBinding::new(
1710                "p i n e a p p l e",
1711                workspace::SendKeystrokes("🍍".to_string()),
1712                Some("vim_mode == insert"),
1713            ),
1714        ])
1715    });
1716    cx.neovim.exec("imap pin 📌").await;
1717    cx.neovim.exec("imap pine 🌲").await;
1718    cx.neovim.exec("imap pineapple 🍍").await;
1719
1720    cx.set_shared_state("ˇ").await;
1721    cx.simulate_shared_keystrokes("i p i n").await;
1722    cx.executor().advance_clock(Duration::from_millis(1000));
1723    cx.run_until_parked();
1724    cx.shared_state().await.assert_eq("📌ˇ");
1725
1726    cx.set_shared_state("ˇ").await;
1727    cx.simulate_shared_keystrokes("i p i n e").await;
1728    cx.executor().advance_clock(Duration::from_millis(1000));
1729    cx.run_until_parked();
1730    cx.shared_state().await.assert_eq("🌲ˇ");
1731
1732    cx.set_shared_state("ˇ").await;
1733    cx.simulate_shared_keystrokes("i p i n e a p p l e").await;
1734    cx.shared_state().await.assert_eq("🍍ˇ");
1735}
1736
1737#[perf]
1738#[gpui::test]
1739async fn test_remap_recursion(cx: &mut gpui::TestAppContext) {
1740    let mut cx = NeovimBackedTestContext::new(cx).await;
1741    cx.update(|_, cx| {
1742        cx.bind_keys([KeyBinding::new(
1743            "x",
1744            workspace::SendKeystrokes("\" _ x".to_string()),
1745            Some("VimControl"),
1746        )]);
1747        cx.bind_keys([KeyBinding::new(
1748            "y",
1749            workspace::SendKeystrokes("2 x".to_string()),
1750            Some("VimControl"),
1751        )])
1752    });
1753    cx.neovim.exec("noremap x \"_x").await;
1754    cx.neovim.exec("map y 2x").await;
1755
1756    cx.set_shared_state("ˇhello").await;
1757    cx.simulate_shared_keystrokes("d l").await;
1758    cx.shared_clipboard().await.assert_eq("h");
1759    cx.simulate_shared_keystrokes("y").await;
1760    cx.shared_clipboard().await.assert_eq("h");
1761    cx.shared_state().await.assert_eq("ˇlo");
1762}
1763
1764#[perf]
1765#[gpui::test]
1766async fn test_escape_while_waiting(cx: &mut gpui::TestAppContext) {
1767    let mut cx = NeovimBackedTestContext::new(cx).await;
1768    cx.set_shared_state("ˇhi").await;
1769    cx.simulate_shared_keystrokes("\" + escape x").await;
1770    cx.shared_state().await.assert_eq("ˇi");
1771}
1772
1773#[perf]
1774#[gpui::test]
1775async fn test_ctrl_w_override(cx: &mut gpui::TestAppContext) {
1776    let mut cx = NeovimBackedTestContext::new(cx).await;
1777    cx.update(|_, cx| {
1778        cx.bind_keys([KeyBinding::new("ctrl-w", DeleteLine, None)]);
1779    });
1780    cx.neovim.exec("map <c-w> D").await;
1781    cx.set_shared_state("ˇhi").await;
1782    cx.simulate_shared_keystrokes("ctrl-w").await;
1783    cx.shared_state().await.assert_eq("ˇ");
1784}
1785
1786#[perf]
1787#[gpui::test]
1788async fn test_visual_indent_count(cx: &mut gpui::TestAppContext) {
1789    let mut cx = VimTestContext::new(cx, true).await;
1790    cx.set_state("ˇhi", Mode::Normal);
1791    cx.simulate_keystrokes("shift-v 3 >");
1792    cx.assert_state("            ˇhi", Mode::Normal);
1793    cx.simulate_keystrokes("shift-v 2 <");
1794    cx.assert_state("    ˇhi", Mode::Normal);
1795}
1796
1797#[perf]
1798#[gpui::test]
1799async fn test_record_replay_recursion(cx: &mut gpui::TestAppContext) {
1800    let mut cx = NeovimBackedTestContext::new(cx).await;
1801
1802    cx.set_shared_state("ˇhello world").await;
1803    cx.simulate_shared_keystrokes(">").await;
1804    cx.simulate_shared_keystrokes(".").await;
1805    cx.simulate_shared_keystrokes(".").await;
1806    cx.simulate_shared_keystrokes(".").await;
1807    cx.shared_state().await.assert_eq("ˇhello world");
1808}
1809
1810#[perf]
1811#[gpui::test]
1812async fn test_blackhole_register(cx: &mut gpui::TestAppContext) {
1813    let mut cx = NeovimBackedTestContext::new(cx).await;
1814
1815    cx.set_shared_state("ˇhello world").await;
1816    cx.simulate_shared_keystrokes("d i w \" _ d a w").await;
1817    cx.simulate_shared_keystrokes("p").await;
1818    cx.shared_state().await.assert_eq("hellˇo");
1819}
1820
1821#[perf]
1822#[gpui::test]
1823async fn test_sentence_backwards(cx: &mut gpui::TestAppContext) {
1824    let mut cx = NeovimBackedTestContext::new(cx).await;
1825
1826    cx.set_shared_state("one\n\ntwo\nthree\nˇ\nfour").await;
1827    cx.simulate_shared_keystrokes("(").await;
1828    cx.shared_state()
1829        .await
1830        .assert_eq("one\n\nˇtwo\nthree\n\nfour");
1831
1832    cx.set_shared_state("hello.\n\n\nworˇld.").await;
1833    cx.simulate_shared_keystrokes("(").await;
1834    cx.shared_state().await.assert_eq("hello.\n\n\nˇworld.");
1835    cx.simulate_shared_keystrokes("(").await;
1836    cx.shared_state().await.assert_eq("hello.\n\nˇ\nworld.");
1837    cx.simulate_shared_keystrokes("(").await;
1838    cx.shared_state().await.assert_eq("ˇhello.\n\n\nworld.");
1839
1840    cx.set_shared_state("hello. worlˇd.").await;
1841    cx.simulate_shared_keystrokes("(").await;
1842    cx.shared_state().await.assert_eq("hello. ˇworld.");
1843    cx.simulate_shared_keystrokes("(").await;
1844    cx.shared_state().await.assert_eq("ˇhello. world.");
1845
1846    cx.set_shared_state(". helˇlo.").await;
1847    cx.simulate_shared_keystrokes("(").await;
1848    cx.shared_state().await.assert_eq(". ˇhello.");
1849    cx.simulate_shared_keystrokes("(").await;
1850    cx.shared_state().await.assert_eq(". ˇhello.");
1851
1852    cx.set_shared_state(indoc! {
1853        "{
1854            hello_world();
1855        ˇ}"
1856    })
1857    .await;
1858    cx.simulate_shared_keystrokes("(").await;
1859    cx.shared_state().await.assert_eq(indoc! {
1860        "ˇ{
1861            hello_world();
1862        }"
1863    });
1864
1865    cx.set_shared_state(indoc! {
1866        "Hello! World..?
1867
1868        \tHello! World... ˇ"
1869    })
1870    .await;
1871    cx.simulate_shared_keystrokes("(").await;
1872    cx.shared_state().await.assert_eq(indoc! {
1873        "Hello! World..?
1874
1875        \tHello! ˇWorld... "
1876    });
1877    cx.simulate_shared_keystrokes("(").await;
1878    cx.shared_state().await.assert_eq(indoc! {
1879        "Hello! World..?
1880
1881        \tˇHello! World... "
1882    });
1883    cx.simulate_shared_keystrokes("(").await;
1884    cx.shared_state().await.assert_eq(indoc! {
1885        "Hello! World..?
1886        ˇ
1887        \tHello! World... "
1888    });
1889    cx.simulate_shared_keystrokes("(").await;
1890    cx.shared_state().await.assert_eq(indoc! {
1891        "Hello! ˇWorld..?
1892
1893        \tHello! World... "
1894    });
1895}
1896
1897#[perf]
1898#[gpui::test]
1899async fn test_sentence_forwards(cx: &mut gpui::TestAppContext) {
1900    let mut cx = NeovimBackedTestContext::new(cx).await;
1901
1902    cx.set_shared_state("helˇlo.\n\n\nworld.").await;
1903    cx.simulate_shared_keystrokes(")").await;
1904    cx.shared_state().await.assert_eq("hello.\nˇ\n\nworld.");
1905    cx.simulate_shared_keystrokes(")").await;
1906    cx.shared_state().await.assert_eq("hello.\n\n\nˇworld.");
1907    cx.simulate_shared_keystrokes(")").await;
1908    cx.shared_state().await.assert_eq("hello.\n\n\nworldˇ.");
1909
1910    cx.set_shared_state("helˇlo.\n\n\nworld.").await;
1911}
1912
1913#[perf]
1914#[gpui::test]
1915async fn test_ctrl_o_visual(cx: &mut gpui::TestAppContext) {
1916    let mut cx = NeovimBackedTestContext::new(cx).await;
1917
1918    cx.set_shared_state("helloˇ world.").await;
1919    cx.simulate_shared_keystrokes("i ctrl-o v b r l").await;
1920    cx.shared_state().await.assert_eq("ˇllllllworld.");
1921    cx.simulate_shared_keystrokes("ctrl-o v f w d").await;
1922    cx.shared_state().await.assert_eq("ˇorld.");
1923}
1924
1925#[perf]
1926#[gpui::test]
1927async fn test_ctrl_o_position(cx: &mut gpui::TestAppContext) {
1928    let mut cx = NeovimBackedTestContext::new(cx).await;
1929
1930    cx.set_shared_state("helˇlo world.").await;
1931    cx.simulate_shared_keystrokes("i ctrl-o d i w").await;
1932    cx.shared_state().await.assert_eq("ˇ world.");
1933    cx.simulate_shared_keystrokes("ctrl-o p").await;
1934    cx.shared_state().await.assert_eq(" helloˇworld.");
1935}
1936
1937#[perf]
1938#[gpui::test]
1939async fn test_ctrl_o_dot(cx: &mut gpui::TestAppContext) {
1940    let mut cx = NeovimBackedTestContext::new(cx).await;
1941
1942    cx.set_shared_state("heˇllo world.").await;
1943    cx.simulate_shared_keystrokes("x i ctrl-o .").await;
1944    cx.shared_state().await.assert_eq("heˇo world.");
1945    cx.simulate_shared_keystrokes("l l escape .").await;
1946    cx.shared_state().await.assert_eq("hellˇllo world.");
1947}
1948
1949#[perf(iterations = 1)]
1950#[gpui::test]
1951async fn test_folded_multibuffer_excerpts(cx: &mut gpui::TestAppContext) {
1952    VimTestContext::init(cx);
1953    cx.update(|cx| {
1954        VimTestContext::init_keybindings(true, cx);
1955    });
1956    let (editor, cx) = cx.add_window_view(|window, cx| {
1957        let multi_buffer = MultiBuffer::build_multi(
1958            [
1959                ("111\n222\n333\n444\n", vec![Point::row_range(0..2)]),
1960                ("aaa\nbbb\nccc\nddd\n", vec![Point::row_range(0..2)]),
1961                ("AAA\nBBB\nCCC\nDDD\n", vec![Point::row_range(0..2)]),
1962                ("one\ntwo\nthr\nfou\n", vec![Point::row_range(0..2)]),
1963            ],
1964            cx,
1965        );
1966        let mut editor = Editor::new(EditorMode::full(), multi_buffer.clone(), None, window, cx);
1967
1968        let buffer_ids = multi_buffer.read(cx).excerpt_buffer_ids();
1969        // fold all but the second buffer, so that we test navigating between two
1970        // adjacent folded buffers, as well as folded buffers at the start and
1971        // end the multibuffer
1972        editor.fold_buffer(buffer_ids[0], cx);
1973        editor.fold_buffer(buffer_ids[2], cx);
1974        editor.fold_buffer(buffer_ids[3], cx);
1975
1976        editor
1977    });
1978    let mut cx = EditorTestContext::for_editor_in(editor.clone(), cx).await;
1979
1980    cx.assert_excerpts_with_selections(indoc! {"
1981        [EXCERPT]
1982        ˇ[FOLDED]
1983        [EXCERPT]
1984        aaa
1985        bbb
1986        [EXCERPT]
1987        [FOLDED]
1988        [EXCERPT]
1989        [FOLDED]
1990        "
1991    });
1992    cx.simulate_keystroke("j");
1993    cx.assert_excerpts_with_selections(indoc! {"
1994        [EXCERPT]
1995        [FOLDED]
1996        [EXCERPT]
1997        ˇaaa
1998        bbb
1999        [EXCERPT]
2000        [FOLDED]
2001        [EXCERPT]
2002        [FOLDED]
2003        "
2004    });
2005    cx.simulate_keystroke("j");
2006    cx.simulate_keystroke("j");
2007    cx.assert_excerpts_with_selections(indoc! {"
2008        [EXCERPT]
2009        [FOLDED]
2010        [EXCERPT]
2011        aaa
2012        bbb
2013        ˇ[EXCERPT]
2014        [FOLDED]
2015        [EXCERPT]
2016        [FOLDED]
2017        "
2018    });
2019    cx.simulate_keystroke("j");
2020    cx.assert_excerpts_with_selections(indoc! {"
2021        [EXCERPT]
2022        [FOLDED]
2023        [EXCERPT]
2024        aaa
2025        bbb
2026        [EXCERPT]
2027        ˇ[FOLDED]
2028        [EXCERPT]
2029        [FOLDED]
2030        "
2031    });
2032    cx.simulate_keystroke("j");
2033    cx.assert_excerpts_with_selections(indoc! {"
2034        [EXCERPT]
2035        [FOLDED]
2036        [EXCERPT]
2037        aaa
2038        bbb
2039        [EXCERPT]
2040        [FOLDED]
2041        [EXCERPT]
2042        ˇ[FOLDED]
2043        "
2044    });
2045    cx.simulate_keystroke("k");
2046    cx.assert_excerpts_with_selections(indoc! {"
2047        [EXCERPT]
2048        [FOLDED]
2049        [EXCERPT]
2050        aaa
2051        bbb
2052        [EXCERPT]
2053        ˇ[FOLDED]
2054        [EXCERPT]
2055        [FOLDED]
2056        "
2057    });
2058    cx.simulate_keystroke("k");
2059    cx.simulate_keystroke("k");
2060    cx.simulate_keystroke("k");
2061    cx.assert_excerpts_with_selections(indoc! {"
2062        [EXCERPT]
2063        [FOLDED]
2064        [EXCERPT]
2065        ˇaaa
2066        bbb
2067        [EXCERPT]
2068        [FOLDED]
2069        [EXCERPT]
2070        [FOLDED]
2071        "
2072    });
2073    cx.simulate_keystroke("k");
2074    cx.assert_excerpts_with_selections(indoc! {"
2075        [EXCERPT]
2076        ˇ[FOLDED]
2077        [EXCERPT]
2078        aaa
2079        bbb
2080        [EXCERPT]
2081        [FOLDED]
2082        [EXCERPT]
2083        [FOLDED]
2084        "
2085    });
2086    cx.simulate_keystroke("shift-g");
2087    cx.assert_excerpts_with_selections(indoc! {"
2088        [EXCERPT]
2089        [FOLDED]
2090        [EXCERPT]
2091        aaa
2092        bbb
2093        [EXCERPT]
2094        [FOLDED]
2095        [EXCERPT]
2096        ˇ[FOLDED]
2097        "
2098    });
2099    cx.simulate_keystrokes("g g");
2100    cx.assert_excerpts_with_selections(indoc! {"
2101        [EXCERPT]
2102        ˇ[FOLDED]
2103        [EXCERPT]
2104        aaa
2105        bbb
2106        [EXCERPT]
2107        [FOLDED]
2108        [EXCERPT]
2109        [FOLDED]
2110        "
2111    });
2112    cx.update_editor(|editor, _, cx| {
2113        let buffer_ids = editor.buffer().read(cx).excerpt_buffer_ids();
2114        editor.fold_buffer(buffer_ids[1], cx);
2115    });
2116
2117    cx.assert_excerpts_with_selections(indoc! {"
2118        [EXCERPT]
2119        ˇ[FOLDED]
2120        [EXCERPT]
2121        [FOLDED]
2122        [EXCERPT]
2123        [FOLDED]
2124        [EXCERPT]
2125        [FOLDED]
2126        "
2127    });
2128    cx.simulate_keystrokes("2 j");
2129    cx.assert_excerpts_with_selections(indoc! {"
2130        [EXCERPT]
2131        [FOLDED]
2132        [EXCERPT]
2133        [FOLDED]
2134        [EXCERPT]
2135        ˇ[FOLDED]
2136        [EXCERPT]
2137        [FOLDED]
2138        "
2139    });
2140}
2141
2142#[perf]
2143#[gpui::test]
2144async fn test_delete_paragraph_motion(cx: &mut gpui::TestAppContext) {
2145    let mut cx = NeovimBackedTestContext::new(cx).await;
2146    cx.set_shared_state(indoc! {
2147        "ˇhello world.
2148
2149        hello world.
2150        "
2151    })
2152    .await;
2153    cx.simulate_shared_keystrokes("y }").await;
2154    cx.shared_clipboard().await.assert_eq("hello world.\n");
2155    cx.simulate_shared_keystrokes("d }").await;
2156    cx.shared_state().await.assert_eq("ˇ\nhello world.\n");
2157    cx.shared_clipboard().await.assert_eq("hello world.\n");
2158
2159    cx.set_shared_state(indoc! {
2160        "helˇlo world.
2161
2162            hello world.
2163            "
2164    })
2165    .await;
2166    cx.simulate_shared_keystrokes("y }").await;
2167    cx.shared_clipboard().await.assert_eq("lo world.");
2168    cx.simulate_shared_keystrokes("d }").await;
2169    cx.shared_state().await.assert_eq("heˇl\n\nhello world.\n");
2170    cx.shared_clipboard().await.assert_eq("lo world.");
2171}
2172
2173#[perf]
2174#[gpui::test]
2175async fn test_delete_unmatched_brace(cx: &mut gpui::TestAppContext) {
2176    let mut cx = NeovimBackedTestContext::new(cx).await;
2177    cx.set_shared_state(indoc! {
2178        "fn o(wow: i32) {
2179          othˇ(wow)
2180          oth(wow)
2181        }
2182        "
2183    })
2184    .await;
2185    cx.simulate_shared_keystrokes("d ] }").await;
2186    cx.shared_state().await.assert_eq(indoc! {
2187        "fn o(wow: i32) {
2188          otˇh
2189        }
2190        "
2191    });
2192    cx.shared_clipboard().await.assert_eq("(wow)\n  oth(wow)");
2193    cx.set_shared_state(indoc! {
2194        "fn o(wow: i32) {
2195          ˇoth(wow)
2196          oth(wow)
2197        }
2198        "
2199    })
2200    .await;
2201    cx.simulate_shared_keystrokes("d ] }").await;
2202    cx.shared_state().await.assert_eq(indoc! {
2203        "fn o(wow: i32) {
2204         ˇ}
2205        "
2206    });
2207    cx.shared_clipboard()
2208        .await
2209        .assert_eq("  oth(wow)\n  oth(wow)\n");
2210}
2211
2212#[perf]
2213#[gpui::test]
2214async fn test_paragraph_multi_delete(cx: &mut gpui::TestAppContext) {
2215    let mut cx = NeovimBackedTestContext::new(cx).await;
2216    cx.set_shared_state(indoc! {
2217        "
2218        Emacs is
2219        ˇa great
2220
2221        operating system
2222
2223        all it lacks
2224        is a
2225
2226        decent text editor
2227        "
2228    })
2229    .await;
2230
2231    cx.simulate_shared_keystrokes("2 d a p").await;
2232    cx.shared_state().await.assert_eq(indoc! {
2233        "
2234        ˇall it lacks
2235        is a
2236
2237        decent text editor
2238        "
2239    });
2240
2241    cx.simulate_shared_keystrokes("d a p").await;
2242    cx.shared_clipboard()
2243        .await
2244        .assert_eq("all it lacks\nis a\n\n");
2245
2246    //reset to initial state
2247    cx.simulate_shared_keystrokes("2 u").await;
2248
2249    cx.simulate_shared_keystrokes("4 d a p").await;
2250    cx.shared_state().await.assert_eq(indoc! {"ˇ"});
2251}
2252
2253#[perf]
2254#[gpui::test]
2255async fn test_multi_cursor_replay(cx: &mut gpui::TestAppContext) {
2256    let mut cx = VimTestContext::new(cx, true).await;
2257    cx.set_state(
2258        indoc! {
2259            "
2260        oˇne one one
2261
2262        two two two
2263        "
2264        },
2265        Mode::Normal,
2266    );
2267
2268    cx.simulate_keystrokes("3 g l s wow escape escape");
2269    cx.assert_state(
2270        indoc! {
2271            "
2272        woˇw wow wow
2273
2274        two two two
2275        "
2276        },
2277        Mode::Normal,
2278    );
2279
2280    cx.simulate_keystrokes("2 j 3 g l .");
2281    cx.assert_state(
2282        indoc! {
2283            "
2284        wow wow wow
2285
2286        woˇw woˇw woˇw
2287        "
2288        },
2289        Mode::Normal,
2290    );
2291}
2292
2293#[gpui::test]
2294async fn test_clipping_on_mode_change(cx: &mut gpui::TestAppContext) {
2295    let mut cx = VimTestContext::new(cx, true).await;
2296
2297    cx.set_state(
2298        indoc! {
2299        "
2300        ˇverylongline
2301        andsomelinebelow
2302        "
2303        },
2304        Mode::Normal,
2305    );
2306
2307    cx.simulate_keystrokes("v e");
2308    cx.assert_state(
2309        indoc! {
2310        "
2311        «verylonglineˇ»
2312        andsomelinebelow
2313        "
2314        },
2315        Mode::Visual,
2316    );
2317
2318    let mut pixel_position = cx.update_editor(|editor, window, cx| {
2319        let snapshot = editor.snapshot(window, cx);
2320        let current_head = editor
2321            .selections
2322            .newest_display(&snapshot.display_snapshot)
2323            .end;
2324        editor.last_bounds().unwrap().origin
2325            + editor
2326                .display_to_pixel_point(current_head, &snapshot, window)
2327                .unwrap()
2328    });
2329    pixel_position.x += px(100.);
2330    // click beyond end of the line
2331    cx.simulate_click(pixel_position, Modifiers::default());
2332    cx.run_until_parked();
2333
2334    cx.assert_state(
2335        indoc! {
2336        "
2337        verylonglinˇe
2338        andsomelinebelow
2339        "
2340        },
2341        Mode::Normal,
2342    );
2343}
2344
2345#[gpui::test]
2346async fn test_wrap_selections_in_tag_line_mode(cx: &mut gpui::TestAppContext) {
2347    let mut cx = VimTestContext::new(cx, true).await;
2348
2349    let js_language = Arc::new(Language::new(
2350        LanguageConfig {
2351            name: "JavaScript".into(),
2352            wrap_characters: Some(language::WrapCharactersConfig {
2353                start_prefix: "<".into(),
2354                start_suffix: ">".into(),
2355                end_prefix: "</".into(),
2356                end_suffix: ">".into(),
2357            }),
2358            ..LanguageConfig::default()
2359        },
2360        None,
2361    ));
2362
2363    cx.update_buffer(|buffer, cx| buffer.set_language(Some(js_language), cx));
2364
2365    cx.set_state(
2366        indoc! {
2367        "
2368        ˇaaaaa
2369        bbbbb
2370        "
2371        },
2372        Mode::Normal,
2373    );
2374
2375    cx.simulate_keystrokes("shift-v j");
2376    cx.dispatch_action(WrapSelectionsInTag);
2377
2378    cx.assert_state(
2379        indoc! {
2380            "
2381            <ˇ>aaaaa
2382            bbbbb</ˇ>
2383            "
2384        },
2385        Mode::VisualLine,
2386    );
2387}
2388
2389#[gpui::test]
2390async fn test_repeat_grouping_41735(cx: &mut gpui::TestAppContext) {
2391    let mut cx = NeovimBackedTestContext::new(cx).await;
2392
2393    // typically transaction gropuing is disabled in tests, but here we need to test it.
2394    cx.update_buffer(|buffer, _cx| buffer.set_group_interval(Duration::from_millis(300)));
2395
2396    cx.set_shared_state("ˇ").await;
2397
2398    cx.simulate_shared_keystrokes("i a escape").await;
2399    cx.simulate_shared_keystrokes(". . .").await;
2400    cx.shared_state().await.assert_eq("ˇaaaa");
2401    cx.simulate_shared_keystrokes("u").await;
2402    cx.shared_state().await.assert_eq("ˇaaa");
2403}