test.rs

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