From 96ce0bb78338d185bf0e2c223696f748e3c319a1 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 28 Jun 2023 12:48:15 -0600 Subject: [PATCH] vim: Enter/n/N to navigate search results --- assets/keymaps/vim.json | 8 ++++++++ crates/vim/src/test.rs | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index 84f9689c2cbb346e7b89d03dea1e0389c5ca6632..72aa7194080f20a1215867822f4cba90f6129523 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -60,6 +60,8 @@ "ignorePunctuation": true } ], + "n": "search::SelectNextMatch", + "shift-n": "search::SelectPrevMatch", "%": "vim::Matching", "f": [ "vim::PushOperator", @@ -335,5 +337,11 @@ "Normal" ] } + }, + { + "context": "BufferSearchBar", + "bindings": { + "enter": "buffer_search::FocusEditor" + } } ] diff --git a/crates/vim/src/test.rs b/crates/vim/src/test.rs index 95962f85b892b1dfa376bb081dd5e1c22dd9d5fc..d9d24ec30eeb19577cadf743c7ba84db874210ff 100644 --- a/crates/vim/src/test.rs +++ b/crates/vim/src/test.rs @@ -5,6 +5,7 @@ mod vim_binding_test_context; mod vim_test_context; use command_palette::CommandPalette; +use editor::DisplayPoint; pub use neovim_backed_binding_test_context::*; pub use neovim_backed_test_context::*; pub use vim_binding_test_context::*; @@ -153,3 +154,44 @@ async fn test_escape_command_palette(cx: &mut gpui::TestAppContext) { assert!(!cx.workspace(|workspace, _| workspace.modal::().is_some())); cx.assert_state("aˇbc\n", Mode::Insert); } + +#[gpui::test] +async fn test_selection_on_search(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + + cx.set_state(indoc! {"aa\nbˇb\ncc\ncc\ncc\n"}, Mode::Normal); + cx.simulate_keystrokes(["/", "c", "c"]); + + let search_bar = cx.workspace(|workspace, cx| { + workspace + .active_pane() + .read(cx) + .toolbar() + .read(cx) + .item_of_type::() + .expect("Buffer search bar should be deployed") + }); + + search_bar.read_with(cx.cx, |bar, cx| { + assert_eq!(bar.query_editor.read(cx).text(cx), "cc"); + }); + + // wait for the query editor change event to fire. + search_bar.next_notification(&cx).await; + + cx.update_editor(|editor, cx| { + let highlights = editor.all_background_highlights(cx); + assert_eq!(3, highlights.len()); + assert_eq!( + DisplayPoint::new(2, 0)..DisplayPoint::new(2, 2), + highlights[0].0 + ) + }); + cx.simulate_keystrokes(["enter"]); + + cx.assert_state(indoc! {"aa\nbb\nˇcc\ncc\ncc\n"}, Mode::Normal); + cx.simulate_keystrokes(["n"]); + cx.assert_state(indoc! {"aa\nbb\ncc\nˇcc\ncc\n"}, Mode::Normal); + cx.simulate_keystrokes(["shift-n"]); + cx.assert_state(indoc! {"aa\nbb\nˇcc\ncc\ncc\n"}, Mode::Normal); +}