From a3ce8d5e88e35697b2ba1cf9cb3c6816089b8281 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Thu, 26 Nov 2026 00:21:02 +0000 Subject: [PATCH] search: Replace from the editor while buffer search is open (#67470) ## Summary `search::ReplaceNext` and `search::ReplaceAll` only work while focus is in the buffer search bar. Bound in the `buffer_search_deployed` context, they do nothing from the editor, unlike `search::SelectNextMatch`. Both actions are now registered for the pane, like the other search actions. Inside and outside the search bar, they run only while the replace input is visible, so a hidden replacement can't overwrite matches. Workspace-level search actions now go to the focused pane instead of the center pane, so search actions in the terminal panel no longer act on the editor's search bar. `Workspace::focused_pane` now also finds zoomed dock panels, which render outside their dock. The debug panel has no `Panel::pane`, so its Terminal tab still sends them to the editor's search bar, as on `main`. With vim mode on, `vim::SearchSubmit` now propagates when the focused pane has no vim editor or does not contain the focus, so `enter` selects the next match in the search bars of the terminal panel and the debugger's Terminal tab. Closes #66518 ## Testing - To reproduce, bind `ctrl-r` to `search::ReplaceNext` in the `buffer_search_deployed` context, open the search bar in replace mode, fill in both fields, focus the editor, and press `ctrl-r`. Nothing happens. - Tested on Linux. On `main`, neither action changes the buffer. On this branch, `search::ReplaceNext` replaces one match per press and `search::ReplaceAll` replaces the rest. - The test dispatches both actions from the editor, from the search bar with the replace input hidden, and from a docked and a zoomed panel with its own pane and a panel without one, then checks the buffer text and the replace toggle. - Each part of `test_replace_with_editor_focused` fails without its fix. - `test_search_submit_without_vim_editor` presses `enter` in the search bar of an editor without vim and checks that the next match is selected. It fails without the fix. - `test_search_submit_outside_pane` does the same in a search bar outside the panes, with a vim editor in the center pane. It fails without the focus check. - `cargo test -p search -p workspace -p file_finder -p terminal_view -p vim --lib` and `./script/clippy -p search -p workspace -p vim` pass. ## 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 `search::ReplaceNext` and `search::ReplaceAll` doing nothing when the editor is focused while the buffer search bar is open, search actions in the terminal panel acting on the editor's search bar, and `enter` not selecting the next match in terminal search bars with Vim mode on https://github.com/user-attachments/assets/69be3dc7-9fb7-4c45-9aea-e6799db9725d --------- Co-authored-by: Kirill Bulatov --- crates/search/src/buffer_search.rs | 163 ++++++++++++++++++- crates/search/src/buffer_search/registrar.rs | 4 +- crates/vim/src/normal/search.rs | 74 ++++++++- crates/vim/src/vim.rs | 20 ++- crates/workspace/src/dock.rs | 6 + crates/workspace/src/workspace.rs | 9 +- 6 files changed, 254 insertions(+), 22 deletions(-) diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index cfcfe203834..72f63de9ba5 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -501,7 +501,9 @@ impl Render for BufferSearchBar { })) .when(replacement, |this| { this.on_action(cx.listener(Self::toggle_replace)) - .on_action(cx.listener(Self::replace_next)) + }) + .when(should_show_replace_input, |this| { + this.on_action(cx.listener(Self::replace_next)) .on_action(cx.listener(Self::replace_all)) }) .when(case, |this| { @@ -683,6 +685,32 @@ impl BufferSearchBar { cx.propagate(); } })); + registrar.register_handler(ForDeployed(|this, action: &ReplaceNext, window, cx| { + if this.supported_options(cx).replacement + && this.replace_enabled + && this + .active_searchable_item + .as_ref() + .is_some_and(|item| item.item_focus_handle(cx).contains_focused(window, cx)) + { + this.replace_next(action, window, cx); + } else { + cx.propagate(); + } + })); + registrar.register_handler(ForDeployed(|this, action: &ReplaceAll, window, cx| { + if this.supported_options(cx).replacement + && this.replace_enabled + && this + .active_searchable_item + .as_ref() + .is_some_and(|item| item.item_focus_handle(cx).contains_focused(window, cx)) + { + this.replace_all(action, window, cx); + } else { + cx.propagate(); + } + })); registrar.register_handler(WithResultsOrExternalQuery( |this, action: &SelectNextMatch, window, cx| { if this.supported_options(cx).find_in_results { @@ -1890,16 +1918,18 @@ mod tests { display_map::DisplayRow, test::editor_test_context::EditorTestContext, }; use futures::stream::StreamExt as _; - use gpui::{FocusHandle, Hsla, TestAppContext, UpdateGlobal, VisualTestContext}; + use gpui::{FocusHandle, Hsla, NoAction, TestAppContext, UpdateGlobal, VisualTestContext}; use language::{Buffer, Point}; - #[cfg(target_os = "macos")] - use project::Project; + use project::{FakeFs, Project}; use settings::{SearchSettingsContent, SettingsStore}; use unindent::Unindent as _; use util_macros::perf; #[cfg(target_os = "macos")] - use workspace::{AppState, MultiWorkspace, Workspace}; - use workspace::{item::Item, searchable::SearchableItem}; + use workspace::{AppState, Workspace}; + use workspace::{ + MultiWorkspace, Pane, dock::DockPosition, dock::PanelEvent, dock::test::TestPanel, + item::Item, searchable::SearchableItem, + }; fn init_globals(cx: &mut TestAppContext) { cx.update(|cx| { @@ -3026,6 +3056,127 @@ mod tests { ); } + #[gpui::test] + async fn test_replace_with_editor_focused(cx: &mut TestAppContext) { + init_globals(cx); + let project = Project::test(FakeFs::new(cx.executor()), [], cx).await; + let (multi_workspace, cx) = + cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx)); + let workspace = multi_workspace.read_with(cx, |multi, _| multi.workspace().clone()); + let search_bar = workspace.update_in(cx, |workspace, window, cx| { + let search_bar = cx.new(|cx| BufferSearchBar::new(None, window, cx)); + workspace.active_pane().update(cx, |pane, cx| { + pane.toolbar().update(cx, |toolbar, cx| { + toolbar.add_item(search_bar.clone(), window, cx); + }); + }); + search_bar + }); + let editor = workspace.update_in(cx, |workspace, window, cx| { + let editor = cx.new(|cx| Editor::single_line(window, cx)); + editor.update(cx, |editor, cx| editor.set_text("a a a a", window, cx)); + workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, window, cx); + editor + }); + search_bar.update_in(cx, |search_bar, window, cx| { + search_bar.deploy(&Deploy::replace(), None, window, cx); + search_bar.replacement_editor.update(cx, |editor, cx| { + editor.set_text("b", window, cx); + }); + }); + search_bar + .update_in(cx, |search_bar, window, cx| { + search_bar.search("a", None, true, window, cx) + }) + .await + .unwrap(); + editor.update_in(cx, |editor, window, cx| { + editor.focus_handle(cx).focus(window, cx) + }); + + cx.dispatch_action(ReplaceNext); + assert_eq!( + editor.read_with(cx, |editor, cx| editor.text(cx)), + "a a a b" + ); + cx.dispatch_action(ReplaceAll); + assert_eq!( + editor.read_with(cx, |editor, cx| editor.text(cx)), + "b b b b" + ); + + search_bar.update_in(cx, |search_bar, window, cx| { + search_bar.toggle_replace(&ToggleReplace, window, cx); + search_bar.replacement_editor.update(cx, |editor, cx| { + editor.set_text("a", window, cx); + }); + }); + search_bar + .update_in(cx, |search_bar, window, cx| { + search_bar.search("b", None, true, window, cx) + }) + .await + .unwrap(); + cx.dispatch_action(ReplaceNext); + cx.dispatch_action(ReplaceAll); + editor.update_in(cx, |editor, window, cx| { + editor.focus_handle(cx).focus(window, cx) + }); + cx.dispatch_action(ReplaceNext); + cx.dispatch_action(ReplaceAll); + assert_eq!( + editor.read_with(cx, |editor, cx| editor.text(cx)), + "b b b b" + ); + + search_bar.update_in(cx, |search_bar, window, cx| { + search_bar.toggle_replace(&ToggleReplace, window, cx) + }); + let panel = workspace.update_in(cx, |workspace, window, cx| { + let pane = cx.new(|cx| { + Pane::new( + workspace.weak_handle(), + workspace.project().clone(), + Default::default(), + None, + Box::new(NoAction), + false, + window, + cx, + ) + }); + let panel = cx.new(|cx| TestPanel { + pane: Some(pane), + ..TestPanel::new(DockPosition::Bottom, 100, cx) + }); + workspace.add_panel(panel.clone(), window, cx); + workspace.focus_panel::(window, cx); + panel + }); + cx.dispatch_action(ReplaceAll); + cx.dispatch_action(ToggleReplace); + assert!(search_bar.read_with(cx, |search_bar, _| search_bar.replace_enabled)); + panel.update(cx, |_, cx| cx.emit(PanelEvent::ZoomIn)); + cx.dispatch_action(ReplaceAll); + cx.dispatch_action(ToggleReplace); + assert!(search_bar.read_with(cx, |search_bar, _| search_bar.replace_enabled)); + assert_eq!( + editor.read_with(cx, |editor, cx| editor.text(cx)), + "b b b b" + ); + + workspace.update_in(cx, |workspace, window, cx| { + let panel = cx.new(|cx| TestPanel::new(DockPosition::Left, 100, cx)); + workspace.add_panel(panel, window, cx); + workspace.focus_panel::(window, cx); + }); + cx.dispatch_action(ReplaceAll); + assert_eq!( + editor.read_with(cx, |editor, cx| editor.text(cx)), + "b b b b" + ); + } + #[gpui::test] async fn test_replace_focus(cx: &mut TestAppContext) { let (editor, search_bar, cx) = init_test(cx); diff --git a/crates/search/src/buffer_search/registrar.rs b/crates/search/src/buffer_search/registrar.rs index 618f52aa107..c3303fa11f5 100644 --- a/crates/search/src/buffer_search/registrar.rs +++ b/crates/search/src/buffer_search/registrar.rs @@ -109,7 +109,7 @@ pub fn register_pane_search_actions(div: Div, pane: Entity) -> Div { registrar.into_div() } -/// Register actions for an active pane. +/// Register actions for the focused pane. impl SearchActionsRegistrar for Workspace { fn register_handler(&mut self, callback: impl ActionExecutor) { self.register_action(move |workspace, action: &A, window, cx| { @@ -118,7 +118,7 @@ impl SearchActionsRegistrar for Workspace { return; } - let pane = workspace.active_pane(); + let pane = workspace.focused_pane(window, cx); let callback = callback.clone(); pane.update(cx, |this, cx| { this.toolbar().update(cx, move |this, cx| { diff --git a/crates/vim/src/normal/search.rs b/crates/vim/src/normal/search.rs index af8404cca91..0bd50498df1 100644 --- a/crates/vim/src/normal/search.rs +++ b/crates/vim/src/normal/search.rs @@ -859,12 +859,17 @@ mod test { state::Mode, test::{NeovimBackedTestContext, VimTestContext}, }; - use editor::{DisplayPoint, HighlightKey, display_map::DisplayRow}; + use editor::{DisplayPoint, Editor, HighlightKey, MultiBufferOffset, display_map::DisplayRow}; + use gpui::{ + App, AppContext as _, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, + IntoElement, Render, Window, + }; use indoc::indoc; use itertools::Itertools; use search::BufferSearchBar; use settings::SettingsStore; + use workspace::{ModalView, ToolbarItemView as _}; #[test] fn test_replacement_parse_escaped_dollar() { @@ -1175,6 +1180,55 @@ mod test { cx.assert_state("one two Λ‡one two one", Mode::Normal); } + #[gpui::test] + async fn test_search_submit_without_vim_editor(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + let editor = cx.workspace(|workspace, window, cx| { + let editor = cx.new(|cx| Editor::single_line(window, cx)); + editor.update(cx, |editor, cx| editor.set_text("one one one", window, cx)); + workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, window, cx); + editor + }); + cx.dispatch_action(zed_actions::buffer_search::Deploy::find()); + cx.simulate_keystrokes("o n e"); + cx.run_until_parked(); + cx.simulate_keystrokes("enter"); + cx.run_until_parked(); + let selection = cx.update_entity(editor, |editor, _, cx| { + editor + .selections + .newest::(&editor.display_snapshot(cx)) + .range() + }); + assert_eq!(selection, MultiBufferOffset(0)..MultiBufferOffset(3)); + } + + #[gpui::test] + async fn test_search_submit_outside_pane(cx: &mut gpui::TestAppContext) { + let mut cx = VimTestContext::new(cx, true).await; + let editor = cx.workspace(|workspace, window, cx| { + let editor = cx.new(|cx| Editor::single_line(window, cx)); + editor.update(cx, |editor, cx| editor.set_text("one one one", window, cx)); + let search_bar = cx.new(|cx| { + let mut search_bar = BufferSearchBar::new(None, window, cx); + search_bar.set_active_pane_item(Some(&editor), window, cx); + search_bar.show(window, cx); + search_bar + }); + workspace.toggle_modal(window, cx, |_, _| SearchBarModal(search_bar)); + editor + }); + cx.simulate_keystrokes("o n e"); + cx.simulate_keystrokes("enter"); + let selection = cx.update_entity(editor, |editor, _, cx| { + editor + .selections + .newest::(&editor.display_snapshot(cx)) + .range() + }); + assert_eq!(selection, MultiBufferOffset(0)..MultiBufferOffset(3)); + } + #[gpui::test] async fn test_visual_star_hash(cx: &mut gpui::TestAppContext) { let mut cx = NeovimBackedTestContext::new(cx).await; @@ -1805,4 +1859,22 @@ mod test { ); }); } + + struct SearchBarModal(Entity); + + impl EventEmitter for SearchBarModal {} + + impl ModalView for SearchBarModal {} + + impl Focusable for SearchBarModal { + fn focus_handle(&self, cx: &App) -> FocusHandle { + self.0.focus_handle(cx) + } + } + + impl Render for SearchBarModal { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + self.0.clone() + } + } } diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs index 60e29cdec36..6fa6cdc86f7 100644 --- a/crates/vim/src/vim.rs +++ b/crates/vim/src/vim.rs @@ -426,13 +426,14 @@ pub fn init(cx: &mut App) { }); workspace.register_action(|workspace, _: &SearchSubmit, window, cx| { - let vim = workspace - .focused_pane(window, cx) - .read(cx) - .active_item() + let vim = Vim::pane_with_focus(workspace, window, cx) + .and_then(|pane| pane.read(cx).active_item()) .and_then(|item| item.act_as::(cx)) .and_then(|editor| editor.read(cx).addon::().cloned()); - let Some(vim) = vim else { return }; + let Some(vim) = vim else { + cx.propagate(); + return; + }; vim.entity.update(cx, |vim, cx| { if !vim.search.cmd_f_search { cx.defer_in(window, |vim, window, cx| vim.search_submit(window, cx)) @@ -1057,9 +1058,12 @@ impl Vim { } pub fn pane(&self, window: &Window, cx: &Context) -> Option> { - let pane = self - .workspace(window, cx) - .map(|workspace| workspace.read(cx).focused_pane(window, cx))?; + let workspace = self.workspace(window, cx)?; + Self::pane_with_focus(workspace.read(cx), window, cx) + } + + fn pane_with_focus(workspace: &Workspace, window: &Window, cx: &App) -> Option> { + let pane = workspace.focused_pane(window, cx); // `focused_pane` falls back to the center pane when a dock panel // without its own pane (e.g. the Agent panel) has focus. Guard // against that so vim search/match commands don't steal focus. diff --git a/crates/workspace/src/dock.rs b/crates/workspace/src/dock.rs index f6b96adad5f..aba52db7a23 100644 --- a/crates/workspace/src/dock.rs +++ b/crates/workspace/src/dock.rs @@ -1610,6 +1610,7 @@ pub mod test { pub default_size: Pixels, pub flexible: bool, pub activation_priority: u32, + pub pane: Option>, } actions!(test_only, [ToggleTestPanel]); @@ -1627,6 +1628,7 @@ pub mod test { default_size: px(300.), flexible: false, activation_priority, + pane: None, } } @@ -1748,6 +1750,10 @@ pub mod test { fn activation_priority(&self) -> u32 { self.activation_priority } + + fn pane(&self) -> Option> { + self.pane.clone() + } } impl Focusable for TestPanel { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index d59891d3b3f..07517c27a66 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -6637,11 +6637,10 @@ impl Workspace { pub fn focused_pane(&self, window: &Window, cx: &App) -> Entity { for dock in self.all_docks() { - if dock.focus_handle(cx).contains_focused(window, cx) - && let Some(pane) = dock - .read(cx) - .active_panel() - .and_then(|panel| panel.pane(cx)) + // A zoomed panel renders outside its dock. + if let Some(panel) = dock.read(cx).active_panel() + && panel.panel_focus_handle(cx).contains_focused(window, cx) + && let Some(pane) = panel.pane(cx) { return pane; }