Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

search: Fix project search focus/action context (#50882)

allison70b1b91b73ee
diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs
index d9de17ac831..ff893cc107a 100644
--- a/crates/search/src/buffer_search.rs
+++ b/crates/search/src/buffer_search.rs
@@ -7,8 +7,8 @@ use crate::{
buffer_search::registrar::WithResultsOrExternalQuery,
search_bar::{
ActionButtonState, HistoryNavigationDirection, alignment_element,
- filter_search_results_input, input_base_styles, render_action_button, render_text_input,
- should_navigate_history,
+ filter_search_results_input, input_base_styles, render_local_action_button,
+ render_text_input, should_navigate_history,
},
};
use any_vec::AnyVec;
@@ -242,21 +242,21 @@ impl Render for BufferSearchBar {
.when(case, |div| {
div.child(SearchOption::CaseSensitive.as_button(
self.search_options,
- SearchSource::Buffer,
+ SearchSource::BufferSearchBar(cx),
focus_handle.clone(),
))
})
.when(word, |div| {
div.child(SearchOption::WholeWord.as_button(
self.search_options,
- SearchSource::Buffer,
+ SearchSource::BufferSearchBar(cx),
focus_handle.clone(),
))
})
.when(regex, |div| {
div.child(SearchOption::Regex.as_button(
self.search_options,
- SearchSource::Buffer,
+ SearchSource::BufferSearchBar(cx),
focus_handle.clone(),
))
}),
@@ -266,13 +266,15 @@ impl Render for BufferSearchBar {
.gap_1()
.min_w_64()
.when(replacement, |this| {
- this.child(render_action_button(
+ this.child(render_local_action_button(
"buffer-search-bar-toggle",
IconName::Replace,
self.replace_enabled.then_some(ActionButtonState::Toggled),
"Toggle Replace",
&ToggleReplace,
focus_handle.clone(),
+ cx,
+ Self::toggle_replace,
))
})
.when(selection, |this| {
@@ -310,7 +312,7 @@ impl Render for BufferSearchBar {
.ml_2()
.border_l_1()
.border_color(theme_colors.border_variant)
- .child(render_action_button(
+ .child(render_local_action_button(
"buffer-search-nav-button",
ui::IconName::ChevronLeft,
self.active_match_index
@@ -319,8 +321,10 @@ impl Render for BufferSearchBar {
"Select Previous Match",
&SelectPreviousMatch,
query_focus.clone(),
+ cx,
+ Self::select_prev_match,
))
- .child(render_action_button(
+ .child(render_local_action_button(
"buffer-search-nav-button",
ui::IconName::ChevronRight,
self.active_match_index
@@ -329,6 +333,8 @@ impl Render for BufferSearchBar {
"Select Next Match",
&SelectNextMatch,
query_focus.clone(),
+ cx,
+ Self::select_next_match,
))
.when(!narrow_mode, |this| {
this.child(div().ml_2().min_w(rems_from_px(40_f32)).child(
@@ -343,25 +349,29 @@ impl Render for BufferSearchBar {
});
el.when(select_all, |el| {
- el.child(render_action_button(
+ el.child(render_local_action_button(
"buffer-search-nav-button",
IconName::SelectAll,
Default::default(),
"Select All Matches",
&SelectAllMatches,
query_focus.clone(),
+ cx,
+ Self::select_all_matches,
))
})
.child(matches_column)
})
.when(find_in_results, |el| {
- el.child(render_action_button(
+ el.child(render_local_action_button(
"buffer-search",
IconName::Close,
Default::default(),
"Close Search Bar",
&Dismiss,
focus_handle.clone(),
+ cx,
+ Self::dismiss,
))
});
@@ -389,21 +399,25 @@ impl Render for BufferSearchBar {
let replace_actions = h_flex()
.min_w_64()
.gap_1()
- .child(render_action_button(
+ .child(render_local_action_button(
"buffer-search-replace-button",
IconName::ReplaceNext,
Default::default(),
"Replace Next Match",
&ReplaceNext,
focus_handle.clone(),
+ cx,
+ Self::replace_next,
))
- .child(render_action_button(
+ .child(render_local_action_button(
"buffer-search-replace-button",
IconName::ReplaceAll,
Default::default(),
"Replace All Matches",
&ReplaceAll,
focus_handle,
+ cx,
+ Self::replace_all,
));
h_flex()
@@ -442,13 +456,15 @@ impl Render for BufferSearchBar {
.border_r_1()
.border_color(cx.theme().colors().border_variant)
})
- .child(render_action_button(
+ .child(render_local_action_button(
"buffer-search",
IconName::Close,
Default::default(),
"Close Search Bar",
&Dismiss,
focus_handle.clone(),
+ cx,
+ Self::dismiss,
)),
)
});
diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs
index 904516f0743..25202647fa7 100644
--- a/crates/search/src/project_search.rs
+++ b/crates/search/src/project_search.rs
@@ -6,7 +6,7 @@ use crate::{
buffer_search::Deploy,
search_bar::{
ActionButtonState, HistoryNavigationDirection, alignment_element, input_base_styles,
- render_action_button, render_text_input, should_navigate_history,
+ render_local_action_button, render_text_input, should_navigate_history,
},
text_finder::TextFinder,
};
@@ -22,10 +22,10 @@ use editor::{
};
use futures::{StreamExt, stream::FuturesOrdered};
use gpui::{
- Action, AnyElement, App, AsyncApp, Context, Entity, EntityId, EventEmitter, FocusHandle,
- Focusable, Global, Hsla, InteractiveElement, IntoElement, KeyContext, ParentElement, Point,
- Render, SharedString, Styled, Subscription, Task, TaskExt, UpdateGlobal, WeakEntity, Window,
- actions, div,
+ Action, AnyElement, App, AsyncApp, ClickEvent, Context, Entity, EntityId, EventEmitter,
+ FocusHandle, Focusable, Global, Hsla, InteractiveElement, IntoElement, KeyContext,
+ ParentElement, Point, Render, SharedString, Styled, Subscription, Task, TaskExt, UpdateGlobal,
+ WeakEntity, Window, actions, div,
};
use itertools::Itertools;
use language::{Buffer, Language};
@@ -1029,6 +1029,29 @@ pub enum ViewEvent {
impl EventEmitter<ViewEvent> for ProjectSearchView {}
+fn render_landing_action_button<A: Action>(
+ id: &'static str,
+ label: &'static str,
+ icon: IconName,
+ action: &'static A,
+ focus_handle: FocusHandle,
+ cx: &Context<ProjectSearchView>,
+ handler: fn(&mut ProjectSearchView, &A, &mut Window, &mut Context<ProjectSearchView>),
+) -> impl IntoElement {
+ Button::new(id, label)
+ .start_icon(Icon::new(icon).size(IconSize::Small))
+ .key_binding(KeyBinding::for_action_in(action, &focus_handle, cx))
+ .on_click({
+ let focus_handle = focus_handle.clone();
+ cx.listener(move |this, _: &ClickEvent, window, cx| {
+ if !focus_handle.is_focused(window) {
+ window.focus(&focus_handle, cx);
+ }
+ handler(this, action, window, cx);
+ })
+ })
+}
+
impl Render for ProjectSearchView {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let mut key_context = KeyContext::default();
@@ -1338,6 +1361,41 @@ impl ProjectSearchView {
self.adjust_query_regex_language(cx);
}
+ fn toggle_replace(&mut self, _: &ToggleReplace, window: &mut Window, cx: &mut Context<Self>) {
+ self.replace_enabled = !self.replace_enabled;
+ let editor_to_focus = if self.replace_enabled {
+ self.replacement_editor.focus_handle(cx)
+ } else {
+ self.query_editor.focus_handle(cx)
+ };
+ window.focus(&editor_to_focus, cx);
+ cx.notify();
+ }
+
+ fn toggle_filters_ui(
+ &mut self,
+ _: &ToggleFilters,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ self.toggle_filters(cx);
+ self.included_files_editor.update(cx, |_, cx| cx.notify());
+ self.excluded_files_editor.update(cx, |_, cx| cx.notify());
+ if EditorSettings::get_global(cx).search.search_on_type
+ && self.entity.read(cx).active_query.is_some()
+ && !self.is_dirty(cx)
+ {
+ self.search(SearchMode::Refresh, cx);
+ }
+ window.refresh();
+ cx.notify();
+ }
+
+ fn toggle_search_option_ui(&mut self, option: SearchOptions, cx: &mut Context<Self>) {
+ self.toggle_search_option(option, cx);
+ cx.notify();
+ }
+
fn toggle_opened_only(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {
self.included_opened_only = !self.included_opened_only;
}
@@ -2456,7 +2514,7 @@ impl ProjectSearchView {
self.active_match_index.is_some()
}
- fn landing_text_minor(&self, cx: &App) -> impl IntoElement {
+ fn landing_text_minor(&self, cx: &mut Context<Self>) -> impl IntoElement {
let focus_handle = self.focus_handle.clone();
v_flex()
.gap_1()
@@ -2469,54 +2527,51 @@ impl ProjectSearchView {
.color(Color::Muted)
.mb_2(),
)
- .child(
- Button::new("filter-paths", "Include/exclude specific paths")
- .start_icon(Icon::new(IconName::Filter).size(IconSize::Small))
- .key_binding(KeyBinding::for_action_in(&ToggleFilters, &focus_handle, cx))
- .on_click(|_event, window, cx| {
- window.dispatch_action(ToggleFilters.boxed_clone(), cx)
- }),
- )
- .child(
- Button::new("find-replace", "Find and replace")
- .start_icon(Icon::new(IconName::Replace).size(IconSize::Small))
- .key_binding(KeyBinding::for_action_in(&ToggleReplace, &focus_handle, cx))
- .on_click(|_event, window, cx| {
- window.dispatch_action(ToggleReplace.boxed_clone(), cx)
- }),
- )
- .child(
- Button::new("regex", "Match with regex")
- .start_icon(Icon::new(IconName::Regex).size(IconSize::Small))
- .key_binding(KeyBinding::for_action_in(&ToggleRegex, &focus_handle, cx))
- .on_click(|_event, window, cx| {
- window.dispatch_action(ToggleRegex.boxed_clone(), cx)
- }),
- )
- .child(
- Button::new("match-case", "Match case")
- .start_icon(Icon::new(IconName::CaseSensitive).size(IconSize::Small))
- .key_binding(KeyBinding::for_action_in(
- &ToggleCaseSensitive,
- &focus_handle,
- cx,
- ))
- .on_click(|_event, window, cx| {
- window.dispatch_action(ToggleCaseSensitive.boxed_clone(), cx)
- }),
- )
- .child(
- Button::new("match-whole-words", "Match whole words")
- .start_icon(Icon::new(IconName::WholeWord).size(IconSize::Small))
- .key_binding(KeyBinding::for_action_in(
- &ToggleWholeWord,
- &focus_handle,
- cx,
- ))
- .on_click(|_event, window, cx| {
- window.dispatch_action(ToggleWholeWord.boxed_clone(), cx)
- }),
- )
+ .child(render_landing_action_button(
+ "filter-paths",
+ "Include/exclude specific paths",
+ IconName::Filter,
+ &ToggleFilters,
+ focus_handle.clone(),
+ cx,
+ Self::toggle_filters_ui,
+ ))
+ .child(render_landing_action_button(
+ "find-replace",
+ "Find and replace",
+ IconName::Replace,
+ &ToggleReplace,
+ focus_handle.clone(),
+ cx,
+ Self::toggle_replace,
+ ))
+ .child(render_landing_action_button(
+ "regex",
+ "Match with regex",
+ IconName::Regex,
+ &ToggleRegex,
+ focus_handle.clone(),
+ cx,
+ |this, _, _, cx| this.toggle_search_option_ui(SearchOptions::REGEX, cx),
+ ))
+ .child(render_landing_action_button(
+ "match-case",
+ "Match case",
+ IconName::CaseSensitive,
+ &ToggleCaseSensitive,
+ focus_handle.clone(),
+ cx,
+ |this, _, _, cx| this.toggle_search_option_ui(SearchOptions::CASE_SENSITIVE, cx),
+ ))
+ .child(render_landing_action_button(
+ "match-whole-words",
+ "Match whole words",
+ IconName::WholeWord,
+ &ToggleWholeWord,
+ focus_handle,
+ cx,
+ |this, _, _, cx| this.toggle_search_option_ui(SearchOptions::WHOLE_WORD, cx),
+ ))
}
fn border_color_for(&self, panel: InputPanel, cx: &App) -> Hsla {
@@ -2743,39 +2798,33 @@ impl ProjectSearchBar {
true
}
- fn toggle_replace(&mut self, _: &ToggleReplace, window: &mut Window, cx: &mut Context<Self>) {
+ fn toggle_replace(
+ &mut self,
+ action: &ToggleReplace,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
if let Some(search) = &self.active_project_search {
- search.update(cx, |this, cx| {
- this.replace_enabled = !this.replace_enabled;
- let editor_to_focus = if this.replace_enabled {
- this.replacement_editor.focus_handle(cx)
- } else {
- this.query_editor.focus_handle(cx)
- };
- window.focus(&editor_to_focus, cx);
- cx.notify();
- });
+ search.update(cx, |this, cx| this.toggle_replace(action, window, cx));
+ }
+ }
+
+ fn replace_next(&mut self, action: &ReplaceNext, window: &mut Window, cx: &mut Context<Self>) {
+ if let Some(search) = &self.active_project_search {
+ search.update(cx, |this, cx| this.replace_next(action, window, cx));
+ }
+ }
+
+ fn replace_all(&mut self, action: &ReplaceAll, window: &mut Window, cx: &mut Context<Self>) {
+ if let Some(search) = &self.active_project_search {
+ search.update(cx, |this, cx| this.replace_all(action, window, cx));
}
}
fn toggle_filters(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
if let Some(search_view) = self.active_project_search.as_ref() {
search_view.update(cx, |search_view, cx| {
- search_view.toggle_filters(cx);
- search_view
- .included_files_editor
- .update(cx, |_, cx| cx.notify());
- search_view
- .excluded_files_editor
- .update(cx, |_, cx| cx.notify());
- if EditorSettings::get_global(cx).search.search_on_type
- && search_view.entity.read(cx).active_query.is_some()
- && !search_view.is_dirty(cx)
- {
- search_view.search(SearchMode::Refresh, cx);
- }
- window.refresh();
- cx.notify();
+ search_view.toggle_filters_ui(&ToggleFilters, window, cx)
});
cx.notify();
true
@@ -3080,7 +3129,7 @@ impl Render for ProjectSearchBar {
.pl_1p5()
.border_l_1()
.border_color(theme_colors.border_variant)
- .child(render_action_button(
+ .child(render_local_action_button(
"project-search-nav-button",
IconName::ChevronLeft,
search
@@ -3090,8 +3139,10 @@ impl Render for ProjectSearchBar {
"Select Previous Match",
&SelectPreviousMatch,
query_focus.clone(),
+ cx,
+ Self::select_prev_match,
))
- .child(render_action_button(
+ .child(render_local_action_button(
"project-search-nav-button",
IconName::ChevronRight,
search
@@ -3101,6 +3152,8 @@ impl Render for ProjectSearchBar {
"Select Next Match",
&SelectNextMatch,
query_focus.clone(),
+ cx,
+ Self::select_next_match,
))
.child(
div()
@@ -3164,7 +3217,7 @@ impl Render for ProjectSearchBar {
}
}),
)
- .child(render_action_button(
+ .child(render_local_action_button(
"project-search",
IconName::Replace,
self.active_project_search
@@ -3174,6 +3227,8 @@ impl Render for ProjectSearchBar {
"Toggle Replace",
&ToggleReplace,
focus_handle.clone(),
+ cx,
+ Self::toggle_replace,
))
.child(matches_column);
@@ -3224,21 +3279,25 @@ impl Render for ProjectSearchBar {
let replace_actions = h_flex()
.min_w_64()
.gap_1()
- .child(render_action_button(
+ .child(render_local_action_button(
"project-search-replace-button",
IconName::ReplaceNext,
is_search_underway.then_some(ActionButtonState::Disabled),
"Replace Next Match",
&ReplaceNext,
focus_handle.clone(),
+ cx,
+ Self::replace_next,
))
- .child(render_action_button(
+ .child(render_local_action_button(
"project-search-replace-button",
IconName::ReplaceAll,
Default::default(),
"Replace All Matches",
&ReplaceAll,
focus_handle,
+ cx,
+ Self::replace_all,
));
h_flex()
@@ -3354,18 +3413,10 @@ impl Render for ProjectSearchBar {
this.toggle_search_option(SearchOptions::CASE_SENSITIVE, window, cx);
}))
.on_action(cx.listener(|this, action, window, cx| {
- if let Some(search) = this.active_project_search.as_ref() {
- search.update(cx, |this, cx| {
- this.replace_next(action, window, cx);
- })
- }
+ this.replace_next(action, window, cx);
}))
.on_action(cx.listener(|this, action, window, cx| {
- if let Some(search) = this.active_project_search.as_ref() {
- search.update(cx, |this, cx| {
- this.replace_all(action, window, cx);
- })
- }
+ this.replace_all(action, window, cx);
}))
.when(search.filters_enabled, |this| {
this.on_action(cx.listener(|this, _: &ToggleIncludeIgnored, window, cx| {
diff --git a/crates/search/src/search.rs b/crates/search/src/search.rs
index 63702a22578..817172ee551 100644
--- a/crates/search/src/search.rs
+++ b/crates/search/src/search.rs
@@ -86,10 +86,24 @@ const INCLUDE_PLACEHOLDER: &str = "Include: e.g. src/**/*.rs";
const EXCLUDE_PLACEHOLDER: &str = "Exclude: e.g. vendor/*, *.lock";
pub enum SearchSource<'a, 'b> {
+ /// Dispatches the toggle action from the button's focus handle. Only use this when the
+ /// action handlers are on that focus handle's dispatch path, as workspace-level handlers
+ /// resolve the active pane before a click in another pane has activated it.
Buffer,
+ BufferSearchBar(&'a Context<'b, BufferSearchBar>),
Project(&'a Context<'b, ProjectSearchBar>),
}
+impl SearchSource<'_, '_> {
+ pub fn discriminant(&self) -> u32 {
+ match self {
+ SearchSource::Buffer => 0,
+ SearchSource::BufferSearchBar(_) => 1,
+ SearchSource::Project(_) => 2,
+ }
+ }
+}
+
impl SearchOption {
pub fn as_options(&self) -> SearchOptions {
SearchOptions::from_bits(1 << *self as u8).unwrap()
@@ -135,30 +149,37 @@ impl SearchOption {
let action = self.to_toggle_action();
let label = self.label();
- IconButton::new(
- (label, matches!(search_source, SearchSource::Buffer) as u32),
- self.icon(),
- )
- .map(|button| match search_source {
- SearchSource::Buffer => {
- let focus_handle = focus_handle.clone();
- button.on_click(move |_: &ClickEvent, window, cx| {
- if !focus_handle.is_focused(window) {
- window.focus(&focus_handle, cx);
- }
- window.dispatch_action(action.boxed_clone(), cx);
- })
- }
- SearchSource::Project(cx) => {
- let options = self.as_options();
- button.on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
- this.toggle_search_option(options, window, cx);
- }))
- }
- })
- .shape(IconButtonShape::Square)
- .toggle_state(active.contains(self.as_options()))
- .tooltip(move |_window, cx| Tooltip::for_action_in(label, action, &focus_handle, cx))
+ IconButton::new((label, search_source.discriminant()), self.icon())
+ .map(|button| match search_source {
+ SearchSource::Buffer => {
+ let focus_handle = focus_handle.clone();
+ button.on_click(move |_: &ClickEvent, window, cx| {
+ if !focus_handle.is_focused(window) {
+ window.focus(&focus_handle, cx);
+ }
+ window.dispatch_action(action.boxed_clone(), cx);
+ })
+ }
+ SearchSource::BufferSearchBar(cx) => {
+ let focus_handle = focus_handle.clone();
+ let options = self.as_options();
+ button.on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
+ if !focus_handle.is_focused(window) {
+ window.focus(&focus_handle, cx);
+ }
+ this.toggle_search_option(options, window, cx);
+ }))
+ }
+ SearchSource::Project(cx) => {
+ let options = self.as_options();
+ button.on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
+ this.toggle_search_option(options, window, cx);
+ }))
+ }
+ })
+ .shape(IconButtonShape::Square)
+ .toggle_state(active.contains(self.as_options()))
+ .tooltip(move |_window, cx| Tooltip::for_action_in(label, action, &focus_handle, cx))
}
}
diff --git a/crates/search/src/search_bar.rs b/crates/search/src/search_bar.rs
index a4757631a18..fb4c9de6494 100644
--- a/crates/search/src/search_bar.rs
+++ b/crates/search/src/search_bar.rs
@@ -1,5 +1,7 @@
use editor::{Editor, EditorElement, EditorStyle, MultiBufferOffset, ToOffset};
-use gpui::{Action, App, Entity, FocusHandle, Hsla, IntoElement, TextStyle};
+use gpui::{
+ Action, App, ClickEvent, Context, Entity, FocusHandle, Hsla, IntoElement, TextStyle, Window,
+};
use settings::Settings;
use theme_settings::ThemeSettings;
use ui::{IconButton, IconButtonShape};
@@ -37,28 +39,30 @@ pub(super) enum ActionButtonState {
Toggled,
}
-pub(super) fn render_action_button(
+pub(super) fn render_local_action_button<T: 'static, A: Action>(
id_prefix: &'static str,
icon: ui::IconName,
button_state: Option<ActionButtonState>,
tooltip: &'static str,
- action: &'static dyn Action,
+ action: &'static A,
focus_handle: FocusHandle,
+ cx: &Context<T>,
+ handler: fn(&mut T, &A, &mut Window, &mut Context<T>),
) -> impl IntoElement {
IconButton::new(
SharedString::from(format!("{id_prefix}-{}", action.name())),
icon,
)
.shape(IconButtonShape::Square)
- .on_click({
+ .on_click(cx.listener({
let focus_handle = focus_handle.clone();
- move |_, window, cx| {
+ move |this, _: &ClickEvent, window, cx| {
if !focus_handle.is_focused(window) {
window.focus(&focus_handle, cx);
}
- window.dispatch_action(action.boxed_clone(), cx);
+ handler(this, action, window, cx);
}
- })
+ }))
.tooltip(move |_window, cx| Tooltip::for_action_in(tooltip, action, &focus_handle, cx))
.when_some(button_state, |this, state| match state {
ActionButtonState::Toggled => this.toggle_state(true),