mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-03 14:01:30 +01:00
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 <mail4score@gmail.com>
This commit is contained in:
1 parent
8aeb64f744
commit
a3ce8d5e88
6 files changed
+254
-22
No files matched your search
@@ -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::<TestPanel>(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::<TestPanel>(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);
|
||||
|
||||
@@ -109,7 +109,7 @@ pub fn register_pane_search_actions(div: Div, pane: Entity<Pane>) -> Div {
|
||||
registrar.into_div()
|
||||
}
|
||||
|
||||
/// Register actions for an active pane.
|
||||
/// Register actions for the focused pane.
|
||||
impl SearchActionsRegistrar for Workspace {
|
||||
fn register_handler<A: Action>(&mut self, callback: impl ActionExecutor<A>) {
|
||||
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| {
|
||||
|
||||
@@ -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::<MultiBufferOffset>(&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::<MultiBufferOffset>(&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<BufferSearchBar>);
|
||||
|
||||
impl EventEmitter<DismissEvent> 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<Self>) -> impl IntoElement {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-8
@@ -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::<Editor>(cx))
|
||||
.and_then(|editor| editor.read(cx).addon::<VimAddon>().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<Self>) -> Option<Entity<Pane>> {
|
||||
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<Entity<Pane>> {
|
||||
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.
|
||||
|
||||
@@ -1610,6 +1610,7 @@ pub mod test {
|
||||
pub default_size: Pixels,
|
||||
pub flexible: bool,
|
||||
pub activation_priority: u32,
|
||||
pub pane: Option<Entity<Pane>>,
|
||||
}
|
||||
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<Entity<Pane>> {
|
||||
self.pane.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Focusable for TestPanel {
|
||||
|
||||
@@ -6637,11 +6637,10 @@ impl Workspace {
|
||||
|
||||
pub fn focused_pane(&self, window: &Window, cx: &App) -> Entity<Pane> {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user