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    bˇ\n    ccc\n");
 236    cx.simulate_keystrokes(".");
 237    cx.assert_editor_state("        a\nbˇ\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#[perf]
1143#[gpui::test]
1144async fn test_remap(cx: &mut gpui::TestAppContext) {
1145    let mut cx = VimTestContext::new(cx, true).await;
1146
1147    // test moving the cursor
1148    cx.update(|_, cx| {
1149        cx.bind_keys([KeyBinding::new(
1150            "g z",
1151            workspace::SendKeystrokes("l l l l".to_string()),
1152            None,
1153        )])
1154    });
1155    cx.set_state("ˇ123456789", Mode::Normal);
1156    cx.simulate_keystrokes("g z");
1157    cx.assert_state("1234ˇ56789", Mode::Normal);
1158
1159    // test switching modes
1160    cx.update(|_, cx| {
1161        cx.bind_keys([KeyBinding::new(
1162            "g y",
1163            workspace::SendKeystrokes("i f o o escape l".to_string()),
1164            None,
1165        )])
1166    });
1167    cx.set_state("ˇ123456789", Mode::Normal);
1168    cx.simulate_keystrokes("g y");
1169    cx.assert_state("fooˇ123456789", Mode::Normal);
1170
1171    // test recursion
1172    cx.update(|_, cx| {
1173        cx.bind_keys([KeyBinding::new(
1174            "g x",
1175            workspace::SendKeystrokes("g z g y".to_string()),
1176            None,
1177        )])
1178    });
1179    cx.set_state("ˇ123456789", Mode::Normal);
1180    cx.simulate_keystrokes("g x");
1181    cx.assert_state("1234fooˇ56789", Mode::Normal);
1182
1183    // test command
1184    cx.update(|_, cx| {
1185        cx.bind_keys([KeyBinding::new(
1186            "g w",
1187            workspace::SendKeystrokes(": j enter".to_string()),
1188            None,
1189        )])
1190    });
1191    cx.set_state("ˇ1234\n56789", Mode::Normal);
1192    cx.simulate_keystrokes("g w");
1193    cx.assert_state("1234ˇ 56789", Mode::Normal);
1194
1195    // test leaving command
1196    cx.update(|_, cx| {
1197        cx.bind_keys([KeyBinding::new(
1198            "g u",
1199            workspace::SendKeystrokes("g w g z".to_string()),
1200            None,
1201        )])
1202    });
1203    cx.set_state("ˇ1234\n56789", Mode::Normal);
1204    cx.simulate_keystrokes("g u");
1205    cx.assert_state("1234 567ˇ89", Mode::Normal);
1206
1207    // test leaving command
1208    cx.update(|_, cx| {
1209        cx.bind_keys([KeyBinding::new(
1210            "g t",
1211            workspace::SendKeystrokes("i space escape".to_string()),
1212            None,
1213        )])
1214    });
1215    cx.set_state("12ˇ34", Mode::Normal);
1216    cx.simulate_keystrokes("g t");
1217    cx.assert_state("12ˇ 34", Mode::Normal);
1218}
1219
1220#[perf]
1221#[gpui::test]
1222async fn test_undo(cx: &mut gpui::TestAppContext) {
1223    let mut cx = NeovimBackedTestContext::new(cx).await;
1224
1225    cx.set_shared_state("hello quˇoel world").await;
1226    cx.simulate_shared_keystrokes("v i w s c o escape u").await;
1227    cx.shared_state().await.assert_eq("hello ˇquoel world");
1228    cx.simulate_shared_keystrokes("ctrl-r").await;
1229    cx.shared_state().await.assert_eq("hello ˇco world");
1230    cx.simulate_shared_keystrokes("a o right l escape").await;
1231    cx.shared_state().await.assert_eq("hello cooˇl world");
1232    cx.simulate_shared_keystrokes("u").await;
1233    cx.shared_state().await.assert_eq("hello cooˇ world");
1234    cx.simulate_shared_keystrokes("u").await;
1235    cx.shared_state().await.assert_eq("hello cˇo world");
1236    cx.simulate_shared_keystrokes("u").await;
1237    cx.shared_state().await.assert_eq("hello ˇquoel world");
1238
1239    cx.set_shared_state("hello quˇoel world").await;
1240    cx.simulate_shared_keystrokes("v i w ~ u").await;
1241    cx.shared_state().await.assert_eq("hello ˇquoel world");
1242
1243    cx.set_shared_state("\nhello quˇoel world\n").await;
1244    cx.simulate_shared_keystrokes("shift-v s c escape u").await;
1245    cx.shared_state().await.assert_eq("\nˇhello quoel world\n");
1246
1247    cx.set_shared_state(indoc! {"
1248        ˇ1
1249        2
1250        3"})
1251        .await;
1252
1253    cx.simulate_shared_keystrokes("ctrl-v shift-g ctrl-a").await;
1254    cx.shared_state().await.assert_eq(indoc! {"
1255        ˇ2
1256        3
1257        4"});
1258
1259    cx.simulate_shared_keystrokes("u").await;
1260    cx.shared_state().await.assert_eq(indoc! {"
1261        ˇ1
1262        2
1263        3"});
1264}
1265
1266#[perf]
1267#[gpui::test]
1268async fn test_mouse_selection(cx: &mut TestAppContext) {
1269    let mut cx = VimTestContext::new(cx, true).await;
1270
1271    cx.set_state("ˇone two three", Mode::Normal);
1272
1273    let start_point = cx.pixel_position("one twˇo three");
1274    let end_point = cx.pixel_position("one ˇtwo three");
1275
1276    cx.simulate_mouse_down(start_point, MouseButton::Left, Modifiers::none());
1277    cx.simulate_mouse_move(end_point, MouseButton::Left, Modifiers::none());
1278    cx.simulate_mouse_up(end_point, MouseButton::Left, Modifiers::none());
1279
1280    cx.assert_state("one «ˇtwo» three", Mode::Visual)
1281}
1282
1283#[perf]
1284#[gpui::test]
1285async fn test_lowercase_marks(cx: &mut TestAppContext) {
1286    let mut cx = NeovimBackedTestContext::new(cx).await;
1287
1288    cx.set_shared_state("line one\nline ˇtwo\nline three").await;
1289    cx.simulate_shared_keystrokes("m a l ' a").await;
1290    cx.shared_state()
1291        .await
1292        .assert_eq("line one\nˇline two\nline three");
1293    cx.simulate_shared_keystrokes("` a").await;
1294    cx.shared_state()
1295        .await
1296        .assert_eq("line one\nline ˇtwo\nline three");
1297
1298    cx.simulate_shared_keystrokes("^ d ` a").await;
1299    cx.shared_state()
1300        .await
1301        .assert_eq("line one\nˇtwo\nline three");
1302}
1303
1304#[perf]
1305#[gpui::test]
1306async fn test_lt_gt_marks(cx: &mut TestAppContext) {
1307    let mut cx = NeovimBackedTestContext::new(cx).await;
1308
1309    cx.set_shared_state(indoc!(
1310        "
1311        Line one
1312        Line two
1313        Line ˇthree
1314        Line four
1315        Line five
1316    "
1317    ))
1318    .await;
1319
1320    cx.simulate_shared_keystrokes("v j escape k k").await;
1321
1322    cx.simulate_shared_keystrokes("' <").await;
1323    cx.shared_state().await.assert_eq(indoc! {"
1324        Line one
1325        Line two
1326        ˇLine three
1327        Line four
1328        Line five
1329    "});
1330
1331    cx.simulate_shared_keystrokes("` <").await;
1332    cx.shared_state().await.assert_eq(indoc! {"
1333        Line one
1334        Line two
1335        Line ˇthree
1336        Line four
1337        Line five
1338    "});
1339
1340    cx.simulate_shared_keystrokes("' >").await;
1341    cx.shared_state().await.assert_eq(indoc! {"
1342        Line one
1343        Line two
1344        Line three
1345        ˇLine four
1346        Line five
1347    "
1348    });
1349
1350    cx.simulate_shared_keystrokes("` >").await;
1351    cx.shared_state().await.assert_eq(indoc! {"
1352        Line one
1353        Line two
1354        Line three
1355        Line ˇfour
1356        Line five
1357    "
1358    });
1359
1360    cx.simulate_shared_keystrokes("v i w o escape").await;
1361    cx.simulate_shared_keystrokes("` >").await;
1362    cx.shared_state().await.assert_eq(indoc! {"
1363        Line one
1364        Line two
1365        Line three
1366        Line fouˇr
1367        Line five
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
1381#[perf]
1382#[gpui::test]
1383async fn test_caret_mark(cx: &mut TestAppContext) {
1384    let mut cx = NeovimBackedTestContext::new(cx).await;
1385
1386    cx.set_shared_state(indoc!(
1387        "
1388        Line one
1389        Line two
1390        Line three
1391        ˇLine four
1392        Line five
1393    "
1394    ))
1395    .await;
1396
1397    cx.simulate_shared_keystrokes("c w shift-s t r a i g h t space t h i n g escape j j")
1398        .await;
1399
1400    cx.simulate_shared_keystrokes("' ^").await;
1401    cx.shared_state().await.assert_eq(indoc! {"
1402        Line one
1403        Line two
1404        Line three
1405        ˇStraight thing four
1406        Line five
1407    "
1408    });
1409
1410    cx.simulate_shared_keystrokes("` ^").await;
1411    cx.shared_state().await.assert_eq(indoc! {"
1412        Line one
1413        Line two
1414        Line three
1415        Straight thingˇ four
1416        Line five
1417    "
1418    });
1419
1420    cx.simulate_shared_keystrokes("k a ! escape k g i ?").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
1431#[cfg(target_os = "macos")]
1432#[perf]
1433#[gpui::test]
1434async fn test_dw_eol(cx: &mut gpui::TestAppContext) {
1435    let mut cx = NeovimBackedTestContext::new(cx).await;
1436
1437    cx.set_shared_wrap(12).await;
1438    cx.set_shared_state("twelve ˇchar twelve char\ntwelve char")
1439        .await;
1440    cx.simulate_shared_keystrokes("d w").await;
1441    cx.shared_state()
1442        .await
1443        .assert_eq("twelve ˇtwelve char\ntwelve char");
1444}
1445
1446#[perf]
1447#[gpui::test]
1448async fn test_toggle_comments(cx: &mut gpui::TestAppContext) {
1449    let mut cx = VimTestContext::new(cx, true).await;
1450
1451    let language = std::sync::Arc::new(language::Language::new(
1452        language::LanguageConfig {
1453            line_comments: vec!["// ".into(), "//! ".into(), "/// ".into()],
1454            ..Default::default()
1455        },
1456        Some(language::tree_sitter_rust::LANGUAGE.into()),
1457    ));
1458    cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
1459
1460    // works in normal model
1461    cx.set_state(
1462        indoc! {"
1463      ˇone
1464      two
1465      three
1466      "},
1467        Mode::Normal,
1468    );
1469    cx.simulate_keystrokes("g c c");
1470    cx.assert_state(
1471        indoc! {"
1472          // ˇone
1473          two
1474          three
1475          "},
1476        Mode::Normal,
1477    );
1478
1479    // works in visual mode
1480    cx.simulate_keystrokes("v j g c");
1481    cx.assert_state(
1482        indoc! {"
1483          // // ˇone
1484          // two
1485          three
1486          "},
1487        Mode::Normal,
1488    );
1489
1490    // works in visual line mode
1491    cx.simulate_keystrokes("shift-v j g c");
1492    cx.assert_state(
1493        indoc! {"
1494          // ˇone
1495          two
1496          three
1497          "},
1498        Mode::Normal,
1499    );
1500
1501    // works with count
1502    cx.simulate_keystrokes("g c 2 j");
1503    cx.assert_state(
1504        indoc! {"
1505            // // ˇone
1506            // two
1507            // three
1508            "},
1509        Mode::Normal,
1510    );
1511
1512    // works with motion object
1513    cx.simulate_keystrokes("shift-g");
1514    cx.simulate_keystrokes("g c g g");
1515    cx.assert_state(
1516        indoc! {"
1517            // one
1518            two
1519            three
1520            ˇ"},
1521        Mode::Normal,
1522    );
1523}
1524
1525#[perf]
1526#[gpui::test]
1527async fn test_find_multibyte(cx: &mut gpui::TestAppContext) {
1528    let mut cx = NeovimBackedTestContext::new(cx).await;
1529
1530    cx.set_shared_state(r#"<label for="guests">ˇPočet hostů</label>"#)
1531        .await;
1532
1533    cx.simulate_shared_keystrokes("c t < o escape").await;
1534    cx.shared_state()
1535        .await
1536        .assert_eq(r#"<label for="guests">ˇo</label>"#);
1537}
1538
1539#[perf]
1540#[gpui::test]
1541async fn test_sneak(cx: &mut gpui::TestAppContext) {
1542    let mut cx = VimTestContext::new(cx, true).await;
1543
1544    cx.update(|_window, cx| {
1545        cx.bind_keys([
1546            KeyBinding::new(
1547                "s",
1548                PushSneak { first_char: None },
1549                Some("vim_mode == normal"),
1550            ),
1551            KeyBinding::new(
1552                "shift-s",
1553                PushSneakBackward { first_char: None },
1554                Some("vim_mode == normal"),
1555            ),
1556            KeyBinding::new(
1557                "shift-s",
1558                PushSneakBackward { first_char: None },
1559                Some("vim_mode == visual"),
1560            ),
1561        ])
1562    });
1563
1564    // Sneak forwards multibyte & multiline
1565    cx.set_state(
1566        indoc! {
1567            r#"<labelˇ for="guests">
1568                    Počet hostů
1569                </label>"#
1570        },
1571        Mode::Normal,
1572    );
1573    cx.simulate_keystrokes("s t ů");
1574    cx.assert_state(
1575        indoc! {
1576            r#"<label for="guests">
1577                Počet hosˇtů
1578            </label>"#
1579        },
1580        Mode::Normal,
1581    );
1582
1583    // Visual sneak backwards multibyte & multiline
1584    cx.simulate_keystrokes("v S < l");
1585    cx.assert_state(
1586        indoc! {
1587            r#"«ˇ<label for="guests">
1588                Počet host»ů
1589            </label>"#
1590        },
1591        Mode::Visual,
1592    );
1593
1594    // Sneak backwards repeated
1595    cx.set_state(r#"11 12 13 ˇ14"#, Mode::Normal);
1596    cx.simulate_keystrokes("S space 1");
1597    cx.assert_state(r#"11 12ˇ 13 14"#, Mode::Normal);
1598    cx.simulate_keystrokes(";");
1599    cx.assert_state(r#"11ˇ 12 13 14"#, Mode::Normal);
1600}
1601
1602#[perf]
1603#[gpui::test]
1604async fn test_plus_minus(cx: &mut gpui::TestAppContext) {
1605    let mut cx = NeovimBackedTestContext::new(cx).await;
1606
1607    cx.set_shared_state(indoc! {
1608        "one
1609           two
1610        thrˇee
1611    "})
1612        .await;
1613
1614    cx.simulate_shared_keystrokes("-").await;
1615    cx.shared_state().await.assert_matches();
1616    cx.simulate_shared_keystrokes("-").await;
1617    cx.shared_state().await.assert_matches();
1618    cx.simulate_shared_keystrokes("+").await;
1619    cx.shared_state().await.assert_matches();
1620}
1621
1622#[perf]
1623#[gpui::test]
1624async fn test_command_alias(cx: &mut gpui::TestAppContext) {
1625    let mut cx = VimTestContext::new(cx, true).await;
1626    cx.update_global(|store: &mut SettingsStore, cx| {
1627        store.update_user_settings(cx, |s| {
1628            let mut aliases = HashMap::default();
1629            aliases.insert("Q".to_string(), "upper".to_string());
1630            s.workspace.command_aliases = aliases
1631        });
1632    });
1633
1634    cx.set_state("ˇhello world", Mode::Normal);
1635    cx.simulate_keystrokes(": Q");
1636    cx.set_state("ˇHello world", Mode::Normal);
1637}
1638
1639#[perf]
1640#[gpui::test]
1641async fn test_remap_adjacent_dog_cat(cx: &mut gpui::TestAppContext) {
1642    let mut cx = NeovimBackedTestContext::new(cx).await;
1643    cx.update(|_, cx| {
1644        cx.bind_keys([
1645            KeyBinding::new(
1646                "d o g",
1647                workspace::SendKeystrokes("🐶".to_string()),
1648                Some("vim_mode == insert"),
1649            ),
1650            KeyBinding::new(
1651                "c a t",
1652                workspace::SendKeystrokes("🐱".to_string()),
1653                Some("vim_mode == insert"),
1654            ),
1655        ])
1656    });
1657    cx.neovim.exec("imap dog 🐶").await;
1658    cx.neovim.exec("imap cat 🐱").await;
1659
1660    cx.set_shared_state("ˇ").await;
1661    cx.simulate_shared_keystrokes("i d o g").await;
1662    cx.shared_state().await.assert_eq("🐶ˇ");
1663
1664    cx.set_shared_state("ˇ").await;
1665    cx.simulate_shared_keystrokes("i d o d o g").await;
1666    cx.shared_state().await.assert_eq("do🐶ˇ");
1667
1668    cx.set_shared_state("ˇ").await;
1669    cx.simulate_shared_keystrokes("i d o c a t").await;
1670    cx.shared_state().await.assert_eq("do🐱ˇ");
1671}
1672
1673#[perf]
1674#[gpui::test]
1675async fn test_remap_nested_pineapple(cx: &mut gpui::TestAppContext) {
1676    let mut cx = NeovimBackedTestContext::new(cx).await;
1677    cx.update(|_, cx| {
1678        cx.bind_keys([
1679            KeyBinding::new(
1680                "p i n",
1681                workspace::SendKeystrokes("📌".to_string()),
1682                Some("vim_mode == insert"),
1683            ),
1684            KeyBinding::new(
1685                "p i n e",
1686                workspace::SendKeystrokes("🌲".to_string()),
1687                Some("vim_mode == insert"),
1688            ),
1689            KeyBinding::new(
1690                "p i n e a p p l e",
1691                workspace::SendKeystrokes("🍍".to_string()),
1692                Some("vim_mode == insert"),
1693            ),
1694        ])
1695    });
1696    cx.neovim.exec("imap pin 📌").await;
1697    cx.neovim.exec("imap pine 🌲").await;
1698    cx.neovim.exec("imap pineapple 🍍").await;
1699
1700    cx.set_shared_state("ˇ").await;
1701    cx.simulate_shared_keystrokes("i p i n").await;
1702    cx.executor().advance_clock(Duration::from_millis(1000));
1703    cx.run_until_parked();
1704    cx.shared_state().await.assert_eq("📌ˇ");
1705
1706    cx.set_shared_state("ˇ").await;
1707    cx.simulate_shared_keystrokes("i p i n e").await;
1708    cx.executor().advance_clock(Duration::from_millis(1000));
1709    cx.run_until_parked();
1710    cx.shared_state().await.assert_eq("🌲ˇ");
1711
1712    cx.set_shared_state("ˇ").await;
1713    cx.simulate_shared_keystrokes("i p i n e a p p l e").await;
1714    cx.shared_state().await.assert_eq("🍍ˇ");
1715}
1716
1717#[perf]
1718#[gpui::test]
1719async fn test_remap_recursion(cx: &mut gpui::TestAppContext) {
1720    let mut cx = NeovimBackedTestContext::new(cx).await;
1721    cx.update(|_, cx| {
1722        cx.bind_keys([KeyBinding::new(
1723            "x",
1724            workspace::SendKeystrokes("\" _ x".to_string()),
1725            Some("VimControl"),
1726        )]);
1727        cx.bind_keys([KeyBinding::new(
1728            "y",
1729            workspace::SendKeystrokes("2 x".to_string()),
1730            Some("VimControl"),
1731        )])
1732    });
1733    cx.neovim.exec("noremap x \"_x").await;
1734    cx.neovim.exec("map y 2x").await;
1735
1736    cx.set_shared_state("ˇhello").await;
1737    cx.simulate_shared_keystrokes("d l").await;
1738    cx.shared_clipboard().await.assert_eq("h");
1739    cx.simulate_shared_keystrokes("y").await;
1740    cx.shared_clipboard().await.assert_eq("h");
1741    cx.shared_state().await.assert_eq("ˇlo");
1742}
1743
1744#[perf]
1745#[gpui::test]
1746async fn test_escape_while_waiting(cx: &mut gpui::TestAppContext) {
1747    let mut cx = NeovimBackedTestContext::new(cx).await;
1748    cx.set_shared_state("ˇhi").await;
1749    cx.simulate_shared_keystrokes("\" + escape x").await;
1750    cx.shared_state().await.assert_eq("ˇi");
1751}
1752
1753#[perf]
1754#[gpui::test]
1755async fn test_ctrl_w_override(cx: &mut gpui::TestAppContext) {
1756    let mut cx = NeovimBackedTestContext::new(cx).await;
1757    cx.update(|_, cx| {
1758        cx.bind_keys([KeyBinding::new("ctrl-w", DeleteLine, None)]);
1759    });
1760    cx.neovim.exec("map <c-w> D").await;
1761    cx.set_shared_state("ˇhi").await;
1762    cx.simulate_shared_keystrokes("ctrl-w").await;
1763    cx.shared_state().await.assert_eq("ˇ");
1764}
1765
1766#[perf]
1767#[gpui::test]
1768async fn test_visual_indent_count(cx: &mut gpui::TestAppContext) {
1769    let mut cx = VimTestContext::new(cx, true).await;
1770    cx.set_state("ˇhi", Mode::Normal);
1771    cx.simulate_keystrokes("shift-v 3 >");
1772    cx.assert_state("            ˇhi", Mode::Normal);
1773    cx.simulate_keystrokes("shift-v 2 <");
1774    cx.assert_state("    ˇhi", Mode::Normal);
1775}
1776
1777#[perf]
1778#[gpui::test]
1779async fn test_record_replay_recursion(cx: &mut gpui::TestAppContext) {
1780    let mut cx = NeovimBackedTestContext::new(cx).await;
1781
1782    cx.set_shared_state("ˇhello world").await;
1783    cx.simulate_shared_keystrokes(">").await;
1784    cx.simulate_shared_keystrokes(".").await;
1785    cx.simulate_shared_keystrokes(".").await;
1786    cx.simulate_shared_keystrokes(".").await;
1787    cx.shared_state().await.assert_eq("ˇhello world");
1788}
1789
1790#[perf]
1791#[gpui::test]
1792async fn test_blackhole_register(cx: &mut gpui::TestAppContext) {
1793    let mut cx = NeovimBackedTestContext::new(cx).await;
1794
1795    cx.set_shared_state("ˇhello world").await;
1796    cx.simulate_shared_keystrokes("d i w \" _ d a w").await;
1797    cx.simulate_shared_keystrokes("p").await;
1798    cx.shared_state().await.assert_eq("hellˇo");
1799}
1800
1801#[perf]
1802#[gpui::test]
1803async fn test_sentence_backwards(cx: &mut gpui::TestAppContext) {
1804    let mut cx = NeovimBackedTestContext::new(cx).await;
1805
1806    cx.set_shared_state("one\n\ntwo\nthree\nˇ\nfour").await;
1807    cx.simulate_shared_keystrokes("(").await;
1808    cx.shared_state()
1809        .await
1810        .assert_eq("one\n\nˇtwo\nthree\n\nfour");
1811
1812    cx.set_shared_state("hello.\n\n\nworˇld.").await;
1813    cx.simulate_shared_keystrokes("(").await;
1814    cx.shared_state().await.assert_eq("hello.\n\n\nˇworld.");
1815    cx.simulate_shared_keystrokes("(").await;
1816    cx.shared_state().await.assert_eq("hello.\n\nˇ\nworld.");
1817    cx.simulate_shared_keystrokes("(").await;
1818    cx.shared_state().await.assert_eq("ˇhello.\n\n\nworld.");
1819
1820    cx.set_shared_state("hello. worlˇd.").await;
1821    cx.simulate_shared_keystrokes("(").await;
1822    cx.shared_state().await.assert_eq("hello. ˇworld.");
1823    cx.simulate_shared_keystrokes("(").await;
1824    cx.shared_state().await.assert_eq("ˇhello. world.");
1825
1826    cx.set_shared_state(". helˇlo.").await;
1827    cx.simulate_shared_keystrokes("(").await;
1828    cx.shared_state().await.assert_eq(". ˇhello.");
1829    cx.simulate_shared_keystrokes("(").await;
1830    cx.shared_state().await.assert_eq(". ˇhello.");
1831
1832    cx.set_shared_state(indoc! {
1833        "{
1834            hello_world();
1835        ˇ}"
1836    })
1837    .await;
1838    cx.simulate_shared_keystrokes("(").await;
1839    cx.shared_state().await.assert_eq(indoc! {
1840        "ˇ{
1841            hello_world();
1842        }"
1843    });
1844
1845    cx.set_shared_state(indoc! {
1846        "Hello! World..?
1847
1848        \tHello! World... ˇ"
1849    })
1850    .await;
1851    cx.simulate_shared_keystrokes("(").await;
1852    cx.shared_state().await.assert_eq(indoc! {
1853        "Hello! World..?
1854
1855        \tHello! ˇWorld... "
1856    });
1857    cx.simulate_shared_keystrokes("(").await;
1858    cx.shared_state().await.assert_eq(indoc! {
1859        "Hello! World..?
1860
1861        \tˇHello! World... "
1862    });
1863    cx.simulate_shared_keystrokes("(").await;
1864    cx.shared_state().await.assert_eq(indoc! {
1865        "Hello! World..?
1866        ˇ
1867        \tHello! World... "
1868    });
1869    cx.simulate_shared_keystrokes("(").await;
1870    cx.shared_state().await.assert_eq(indoc! {
1871        "Hello! ˇWorld..?
1872
1873        \tHello! World... "
1874    });
1875}
1876
1877#[perf]
1878#[gpui::test]
1879async fn test_sentence_forwards(cx: &mut gpui::TestAppContext) {
1880    let mut cx = NeovimBackedTestContext::new(cx).await;
1881
1882    cx.set_shared_state("helˇlo.\n\n\nworld.").await;
1883    cx.simulate_shared_keystrokes(")").await;
1884    cx.shared_state().await.assert_eq("hello.\nˇ\n\nworld.");
1885    cx.simulate_shared_keystrokes(")").await;
1886    cx.shared_state().await.assert_eq("hello.\n\n\nˇworld.");
1887    cx.simulate_shared_keystrokes(")").await;
1888    cx.shared_state().await.assert_eq("hello.\n\n\nworldˇ.");
1889
1890    cx.set_shared_state("helˇlo.\n\n\nworld.").await;
1891}
1892
1893#[perf]
1894#[gpui::test]
1895async fn test_ctrl_o_visual(cx: &mut gpui::TestAppContext) {
1896    let mut cx = NeovimBackedTestContext::new(cx).await;
1897
1898    cx.set_shared_state("helloˇ world.").await;
1899    cx.simulate_shared_keystrokes("i ctrl-o v b r l").await;
1900    cx.shared_state().await.assert_eq("ˇllllllworld.");
1901    cx.simulate_shared_keystrokes("ctrl-o v f w d").await;
1902    cx.shared_state().await.assert_eq("ˇorld.");
1903}
1904
1905#[perf]
1906#[gpui::test]
1907async fn test_ctrl_o_position(cx: &mut gpui::TestAppContext) {
1908    let mut cx = NeovimBackedTestContext::new(cx).await;
1909
1910    cx.set_shared_state("helˇlo world.").await;
1911    cx.simulate_shared_keystrokes("i ctrl-o d i w").await;
1912    cx.shared_state().await.assert_eq("ˇ world.");
1913    cx.simulate_shared_keystrokes("ctrl-o p").await;
1914    cx.shared_state().await.assert_eq(" helloˇworld.");
1915}
1916
1917#[perf]
1918#[gpui::test]
1919async fn test_ctrl_o_dot(cx: &mut gpui::TestAppContext) {
1920    let mut cx = NeovimBackedTestContext::new(cx).await;
1921
1922    cx.set_shared_state("heˇllo world.").await;
1923    cx.simulate_shared_keystrokes("x i ctrl-o .").await;
1924    cx.shared_state().await.assert_eq("heˇo world.");
1925    cx.simulate_shared_keystrokes("l l escape .").await;
1926    cx.shared_state().await.assert_eq("hellˇllo world.");
1927}
1928
1929#[perf(iterations = 1)]
1930#[gpui::test]
1931async fn test_folded_multibuffer_excerpts(cx: &mut gpui::TestAppContext) {
1932    VimTestContext::init(cx);
1933    cx.update(|cx| {
1934        VimTestContext::init_keybindings(true, cx);
1935    });
1936    let (editor, cx) = cx.add_window_view(|window, cx| {
1937        let multi_buffer = MultiBuffer::build_multi(
1938            [
1939                ("111\n222\n333\n444\n", vec![Point::row_range(0..2)]),
1940                ("aaa\nbbb\nccc\nddd\n", vec![Point::row_range(0..2)]),
1941                ("AAA\nBBB\nCCC\nDDD\n", vec![Point::row_range(0..2)]),
1942                ("one\ntwo\nthr\nfou\n", vec![Point::row_range(0..2)]),
1943            ],
1944            cx,
1945        );
1946        let mut editor = Editor::new(EditorMode::full(), multi_buffer.clone(), None, window, cx);
1947
1948        let buffer_ids = multi_buffer.read(cx).excerpt_buffer_ids();
1949        // fold all but the second buffer, so that we test navigating between two
1950        // adjacent folded buffers, as well as folded buffers at the start and
1951        // end the multibuffer
1952        editor.fold_buffer(buffer_ids[0], cx);
1953        editor.fold_buffer(buffer_ids[2], cx);
1954        editor.fold_buffer(buffer_ids[3], cx);
1955
1956        editor
1957    });
1958    let mut cx = EditorTestContext::for_editor_in(editor.clone(), cx).await;
1959
1960    cx.assert_excerpts_with_selections(indoc! {"
1961        [EXCERPT]
1962        ˇ[FOLDED]
1963        [EXCERPT]
1964        aaa
1965        bbb
1966        [EXCERPT]
1967        [FOLDED]
1968        [EXCERPT]
1969        [FOLDED]
1970        "
1971    });
1972    cx.simulate_keystroke("j");
1973    cx.assert_excerpts_with_selections(indoc! {"
1974        [EXCERPT]
1975        [FOLDED]
1976        [EXCERPT]
1977        ˇaaa
1978        bbb
1979        [EXCERPT]
1980        [FOLDED]
1981        [EXCERPT]
1982        [FOLDED]
1983        "
1984    });
1985    cx.simulate_keystroke("j");
1986    cx.simulate_keystroke("j");
1987    cx.assert_excerpts_with_selections(indoc! {"
1988        [EXCERPT]
1989        [FOLDED]
1990        [EXCERPT]
1991        aaa
1992        bbb
1993        ˇ[EXCERPT]
1994        [FOLDED]
1995        [EXCERPT]
1996        [FOLDED]
1997        "
1998    });
1999    cx.simulate_keystroke("j");
2000    cx.assert_excerpts_with_selections(indoc! {"
2001        [EXCERPT]
2002        [FOLDED]
2003        [EXCERPT]
2004        aaa
2005        bbb
2006        [EXCERPT]
2007        ˇ[FOLDED]
2008        [EXCERPT]
2009        [FOLDED]
2010        "
2011    });
2012    cx.simulate_keystroke("j");
2013    cx.assert_excerpts_with_selections(indoc! {"
2014        [EXCERPT]
2015        [FOLDED]
2016        [EXCERPT]
2017        aaa
2018        bbb
2019        [EXCERPT]
2020        [FOLDED]
2021        [EXCERPT]
2022        ˇ[FOLDED]
2023        "
2024    });
2025    cx.simulate_keystroke("k");
2026    cx.assert_excerpts_with_selections(indoc! {"
2027        [EXCERPT]
2028        [FOLDED]
2029        [EXCERPT]
2030        aaa
2031        bbb
2032        [EXCERPT]
2033        ˇ[FOLDED]
2034        [EXCERPT]
2035        [FOLDED]
2036        "
2037    });
2038    cx.simulate_keystroke("k");
2039    cx.simulate_keystroke("k");
2040    cx.simulate_keystroke("k");
2041    cx.assert_excerpts_with_selections(indoc! {"
2042        [EXCERPT]
2043        [FOLDED]
2044        [EXCERPT]
2045        ˇaaa
2046        bbb
2047        [EXCERPT]
2048        [FOLDED]
2049        [EXCERPT]
2050        [FOLDED]
2051        "
2052    });
2053    cx.simulate_keystroke("k");
2054    cx.assert_excerpts_with_selections(indoc! {"
2055        [EXCERPT]
2056        ˇ[FOLDED]
2057        [EXCERPT]
2058        aaa
2059        bbb
2060        [EXCERPT]
2061        [FOLDED]
2062        [EXCERPT]
2063        [FOLDED]
2064        "
2065    });
2066    cx.simulate_keystroke("shift-g");
2067    cx.assert_excerpts_with_selections(indoc! {"
2068        [EXCERPT]
2069        [FOLDED]
2070        [EXCERPT]
2071        aaa
2072        bbb
2073        [EXCERPT]
2074        [FOLDED]
2075        [EXCERPT]
2076        ˇ[FOLDED]
2077        "
2078    });
2079    cx.simulate_keystrokes("g g");
2080    cx.assert_excerpts_with_selections(indoc! {"
2081        [EXCERPT]
2082        ˇ[FOLDED]
2083        [EXCERPT]
2084        aaa
2085        bbb
2086        [EXCERPT]
2087        [FOLDED]
2088        [EXCERPT]
2089        [FOLDED]
2090        "
2091    });
2092    cx.update_editor(|editor, _, cx| {
2093        let buffer_ids = editor.buffer().read(cx).excerpt_buffer_ids();
2094        editor.fold_buffer(buffer_ids[1], cx);
2095    });
2096
2097    cx.assert_excerpts_with_selections(indoc! {"
2098        [EXCERPT]
2099        ˇ[FOLDED]
2100        [EXCERPT]
2101        [FOLDED]
2102        [EXCERPT]
2103        [FOLDED]
2104        [EXCERPT]
2105        [FOLDED]
2106        "
2107    });
2108    cx.simulate_keystrokes("2 j");
2109    cx.assert_excerpts_with_selections(indoc! {"
2110        [EXCERPT]
2111        [FOLDED]
2112        [EXCERPT]
2113        [FOLDED]
2114        [EXCERPT]
2115        ˇ[FOLDED]
2116        [EXCERPT]
2117        [FOLDED]
2118        "
2119    });
2120}
2121
2122#[perf]
2123#[gpui::test]
2124async fn test_delete_paragraph_motion(cx: &mut gpui::TestAppContext) {
2125    let mut cx = NeovimBackedTestContext::new(cx).await;
2126    cx.set_shared_state(indoc! {
2127        "ˇhello world.
2128
2129        hello world.
2130        "
2131    })
2132    .await;
2133    cx.simulate_shared_keystrokes("y }").await;
2134    cx.shared_clipboard().await.assert_eq("hello world.\n");
2135    cx.simulate_shared_keystrokes("d }").await;
2136    cx.shared_state().await.assert_eq("ˇ\nhello world.\n");
2137    cx.shared_clipboard().await.assert_eq("hello world.\n");
2138
2139    cx.set_shared_state(indoc! {
2140        "helˇlo world.
2141
2142            hello world.
2143            "
2144    })
2145    .await;
2146    cx.simulate_shared_keystrokes("y }").await;
2147    cx.shared_clipboard().await.assert_eq("lo world.");
2148    cx.simulate_shared_keystrokes("d }").await;
2149    cx.shared_state().await.assert_eq("heˇl\n\nhello world.\n");
2150    cx.shared_clipboard().await.assert_eq("lo world.");
2151}
2152
2153#[perf]
2154#[gpui::test]
2155async fn test_delete_unmatched_brace(cx: &mut gpui::TestAppContext) {
2156    let mut cx = NeovimBackedTestContext::new(cx).await;
2157    cx.set_shared_state(indoc! {
2158        "fn o(wow: i32) {
2159          othˇ(wow)
2160          oth(wow)
2161        }
2162        "
2163    })
2164    .await;
2165    cx.simulate_shared_keystrokes("d ] }").await;
2166    cx.shared_state().await.assert_eq(indoc! {
2167        "fn o(wow: i32) {
2168          otˇh
2169        }
2170        "
2171    });
2172    cx.shared_clipboard().await.assert_eq("(wow)\n  oth(wow)");
2173    cx.set_shared_state(indoc! {
2174        "fn o(wow: i32) {
2175          ˇoth(wow)
2176          oth(wow)
2177        }
2178        "
2179    })
2180    .await;
2181    cx.simulate_shared_keystrokes("d ] }").await;
2182    cx.shared_state().await.assert_eq(indoc! {
2183        "fn o(wow: i32) {
2184         ˇ}
2185        "
2186    });
2187    cx.shared_clipboard()
2188        .await
2189        .assert_eq("  oth(wow)\n  oth(wow)\n");
2190}
2191
2192#[perf]
2193#[gpui::test]
2194async fn test_paragraph_multi_delete(cx: &mut gpui::TestAppContext) {
2195    let mut cx = NeovimBackedTestContext::new(cx).await;
2196    cx.set_shared_state(indoc! {
2197        "
2198        Emacs is
2199        ˇa great
2200
2201        operating system
2202
2203        all it lacks
2204        is a
2205
2206        decent text editor
2207        "
2208    })
2209    .await;
2210
2211    cx.simulate_shared_keystrokes("2 d a p").await;
2212    cx.shared_state().await.assert_eq(indoc! {
2213        "
2214        ˇall it lacks
2215        is a
2216
2217        decent text editor
2218        "
2219    });
2220
2221    cx.simulate_shared_keystrokes("d a p").await;
2222    cx.shared_clipboard()
2223        .await
2224        .assert_eq("all it lacks\nis a\n\n");
2225
2226    //reset to initial state
2227    cx.simulate_shared_keystrokes("2 u").await;
2228
2229    cx.simulate_shared_keystrokes("4 d a p").await;
2230    cx.shared_state().await.assert_eq(indoc! {"ˇ"});
2231}
2232
2233#[perf]
2234#[gpui::test]
2235async fn test_multi_cursor_replay(cx: &mut gpui::TestAppContext) {
2236    let mut cx = VimTestContext::new(cx, true).await;
2237    cx.set_state(
2238        indoc! {
2239            "
2240        oˇne one one
2241
2242        two two two
2243        "
2244        },
2245        Mode::Normal,
2246    );
2247
2248    cx.simulate_keystrokes("3 g l s wow escape escape");
2249    cx.assert_state(
2250        indoc! {
2251            "
2252        woˇw wow wow
2253
2254        two two two
2255        "
2256        },
2257        Mode::Normal,
2258    );
2259
2260    cx.simulate_keystrokes("2 j 3 g l .");
2261    cx.assert_state(
2262        indoc! {
2263            "
2264        wow wow wow
2265
2266        woˇw woˇw woˇw
2267        "
2268        },
2269        Mode::Normal,
2270    );
2271}
2272
2273#[gpui::test]
2274async fn test_clipping_on_mode_change(cx: &mut gpui::TestAppContext) {
2275    let mut cx = VimTestContext::new(cx, true).await;
2276
2277    cx.set_state(
2278        indoc! {
2279        "
2280        ˇverylongline
2281        andsomelinebelow
2282        "
2283        },
2284        Mode::Normal,
2285    );
2286
2287    cx.simulate_keystrokes("v e");
2288    cx.assert_state(
2289        indoc! {
2290        "
2291        «verylonglineˇ»
2292        andsomelinebelow
2293        "
2294        },
2295        Mode::Visual,
2296    );
2297
2298    let mut pixel_position = cx.update_editor(|editor, window, cx| {
2299        let snapshot = editor.snapshot(window, cx);
2300        let current_head = editor
2301            .selections
2302            .newest_display(&snapshot.display_snapshot)
2303            .end;
2304        editor.last_bounds().unwrap().origin
2305            + editor
2306                .display_to_pixel_point(current_head, &snapshot, window)
2307                .unwrap()
2308    });
2309    pixel_position.x += px(100.);
2310    // click beyond end of the line
2311    cx.simulate_click(pixel_position, Modifiers::default());
2312    cx.run_until_parked();
2313
2314    cx.assert_state(
2315        indoc! {
2316        "
2317        verylonglinˇe
2318        andsomelinebelow
2319        "
2320        },
2321        Mode::Normal,
2322    );
2323}
2324
2325#[gpui::test]
2326async fn test_wrap_selections_in_tag_line_mode(cx: &mut gpui::TestAppContext) {
2327    let mut cx = VimTestContext::new(cx, true).await;
2328
2329    let js_language = Arc::new(Language::new(
2330        LanguageConfig {
2331            name: "JavaScript".into(),
2332            wrap_characters: Some(language::WrapCharactersConfig {
2333                start_prefix: "<".into(),
2334                start_suffix: ">".into(),
2335                end_prefix: "</".into(),
2336                end_suffix: ">".into(),
2337            }),
2338            ..LanguageConfig::default()
2339        },
2340        None,
2341    ));
2342
2343    cx.update_buffer(|buffer, cx| buffer.set_language(Some(js_language), cx));
2344
2345    cx.set_state(
2346        indoc! {
2347        "
2348        ˇaaaaa
2349        bbbbb
2350        "
2351        },
2352        Mode::Normal,
2353    );
2354
2355    cx.simulate_keystrokes("shift-v j");
2356    cx.dispatch_action(WrapSelectionsInTag);
2357
2358    cx.assert_state(
2359        indoc! {
2360            "
2361            <ˇ>aaaaa
2362            bbbbb</ˇ>
2363            "
2364        },
2365        Mode::VisualLine,
2366    );
2367}