From 7c4f6ddad1dc8e39f5ef807399bce900184a2add Mon Sep 17 00:00:00 2001 From: Kim Wittenburg Date: Mon, 5 Oct 2026 21:02:44 +0000 Subject: [PATCH] keymap: Fix `shift-enter` in search bar for JetBrains keymap (#65129) ## Summary This PR fixes https://github.com/zed-industries/zed/issues/62696. The current JetBrains keymap binds `shift-enter` to `editor::NewlineBelow`. While that is correct for most contexts, in the search bar it is not. In the search bar JetBrains products use `shift-enter` to search backwards. The current keymap already tries to rectify this by adding an explicit `search::SelectPreviousMatch` mapping. However the precedence of this mapping is shadowed by an `Editor && mode == auto_height` match. This PR fixes the issue by giving the `search::SelectPreviousMatch` a higher precedence by making it more specific to the search bar. ## Testing (instructions for macOS) 1. Run a build with the modified keymap 2. Open a file and search via Cmd+F 3. Enter some text that occurs multiple times in the file 4. Press Shift-Enter. Notice that this searches in the document and rotates through the matches in reverse order. A note for test code: At first I didn't add a test for this change but then I saw that a test for a similar change exists (`test_thread_search_shift_enter_navigates_with_jetbrains_keymap`). The test is AI generated. As far as I can tell the test makes sense, but I'm not really familiar with the UI code. If you prefer to have the test removed/changed, please tell me. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - Fixed default Shift-Enter behavior in the JetBrains keymap to select the previous search match --------- Co-authored-by: MrSubidubi --- assets/keymaps/linux/jetbrains.json | 2 +- assets/keymaps/macos/jetbrains.json | 2 +- crates/search/src/buffer_search.rs | 95 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/assets/keymaps/linux/jetbrains.json b/assets/keymaps/linux/jetbrains.json index 40a66abadf1..183fade8a18 100644 --- a/assets/keymaps/linux/jetbrains.json +++ b/assets/keymaps/linux/jetbrains.json @@ -94,7 +94,7 @@ }, }, { - "context": "BufferSearchBar", + "context": "BufferSearchBar && !in_replace > Editor", "bindings": { "shift-enter": "search::SelectPreviousMatch", }, diff --git a/assets/keymaps/macos/jetbrains.json b/assets/keymaps/macos/jetbrains.json index 8eadc806451..176663ff2b3 100644 --- a/assets/keymaps/macos/jetbrains.json +++ b/assets/keymaps/macos/jetbrains.json @@ -93,7 +93,7 @@ }, }, { - "context": "BufferSearchBar", + "context": "BufferSearchBar && !in_replace > Editor", "bindings": { "shift-enter": "search::SelectPreviousMatch", }, diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index 8726f97e2c6..f68d5874d61 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -4509,6 +4509,101 @@ mod tests { }); } + #[gpui::test] + async fn test_shift_enter_selects_previous_match_with_jetbrains_keymap( + cx: &mut TestAppContext, + ) { + init_globals(cx); + let buffer = cx.new(|cx| Buffer::local("zed\nzed\nzed\n", cx)); + let mut editor = None; + let window = cx.add_window(|window, cx| { + // Load the keymaps in the same order (and with the same sources) as + // `load_default_keymap` does, so precedence matches a real session + // with `base_keymap: JetBrains`. + let mut default_bindings = settings::KeymapFile::load_asset_allow_partial_failure( + settings::DEFAULT_KEYMAP_PATH, + cx, + ) + .unwrap(); + for binding in &mut default_bindings { + binding.set_meta(settings::KeybindSource::Default.meta()); + } + cx.bind_keys(default_bindings); + + let jetbrains_keymap_path = settings::BaseKeymap::JetBrains + .asset_path() + .expect("JetBrains base keymap should have an asset path"); + let mut jetbrains_bindings = + settings::KeymapFile::load_asset_allow_partial_failure(jetbrains_keymap_path, cx) + .unwrap(); + for binding in &mut jetbrains_bindings { + binding.set_meta(settings::KeybindSource::Base.meta()); + } + cx.bind_keys(jetbrains_bindings); + + editor = Some(cx.new(|cx| Editor::for_buffer(buffer.clone(), None, window, cx))); + let mut search_bar = BufferSearchBar::new(None, window, cx); + search_bar.set_active_pane_item(Some(&editor.clone().unwrap()), window, cx); + search_bar.show(window, cx); + search_bar + }); + let search_bar = window.root(cx).unwrap(); + let cx = VisualTestContext::from_window(*window, cx).into_mut(); + + search_bar + .update_in(cx, |search_bar, window, cx| { + search_bar.search("zed", None, true, window, cx) + }) + .await + .unwrap(); + + let match_count = search_bar + .read_with(cx, |search_bar, _| { + search_bar + .searchable_items_with_matches + .values() + .next() + .map(|(matches, _)| matches.len()) + }) + .expect("search should have populated matches"); + assert!( + match_count >= 2, + "test precondition: need at least 2 matches, got {match_count}" + ); + assert_eq!( + search_bar.read_with(cx, |search_bar, _| search_bar.active_match_index), + Some(0), + "the first match should be active after searching" + ); + + let query_focus = search_bar.read_with(cx, |search_bar, cx| { + search_bar.query_editor.focus_handle(cx) + }); + cx.update(|window, cx| window.focus(&query_focus, cx)); + cx.update(|window, cx| { + assert!( + query_focus.contains_focused(window, cx), + "query editor must be focused before simulating shift-enter" + ); + }); + + cx.simulate_keystrokes("shift-enter"); + cx.run_until_parked(); + + let query_text = search_bar.read_with(cx, |search_bar, cx| { + search_bar.query_editor.read(cx).text(cx) + }); + assert!( + !query_text.contains('\n'), + "shift-enter must not insert a newline into the query; got {query_text:?}" + ); + assert_eq!( + search_bar.read_with(cx, |search_bar, _| search_bar.active_match_index), + Some(match_count - 1), + "shift-enter should wrap from the first to the last match" + ); + } + fn update_search_settings(search_settings: SearchSettings, cx: &mut TestAppContext) { cx.update(|cx| { SettingsStore::update_global(cx, |store, cx| {