search: Simplify search options handling (#36233)

Release Notes:

- N/A
This commit is contained in:
Lukas Wirth 2025-08-15 12:34:54 +02:00 committed by GitHub
parent 4f0b00b0d9
commit d891348442
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 115 additions and 185 deletions

View file

@ -1,12 +1,10 @@
mod registrar; mod registrar;
use crate::{ use crate::{
FocusSearch, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext, SearchOptions, FocusSearch, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext, SearchOption,
SelectAllMatches, SelectNextMatch, SelectPreviousMatch, ToggleCaseSensitive, ToggleRegex, SearchOptions, SelectAllMatches, SelectNextMatch, SelectPreviousMatch, ToggleCaseSensitive,
ToggleReplace, ToggleSelection, ToggleWholeWord, ToggleRegex, ToggleReplace, ToggleSelection, ToggleWholeWord,
search_bar::{ search_bar::{input_base_styles, render_action_button, render_text_input},
input_base_styles, render_action_button, render_text_input, toggle_replace_button,
},
}; };
use any_vec::AnyVec; use any_vec::AnyVec;
use anyhow::Context as _; use anyhow::Context as _;
@ -215,31 +213,22 @@ impl Render for BufferSearchBar {
h_flex() h_flex()
.gap_1() .gap_1()
.when(case, |div| { .when(case, |div| {
div.child(SearchOptions::CASE_SENSITIVE.as_button( div.child(
self.search_options.contains(SearchOptions::CASE_SENSITIVE), SearchOption::CaseSensitive
focus_handle.clone(), .as_button(self.search_options, focus_handle.clone()),
cx.listener(|this, _, window, cx| { )
this.toggle_case_sensitive(&ToggleCaseSensitive, window, cx)
}),
))
}) })
.when(word, |div| { .when(word, |div| {
div.child(SearchOptions::WHOLE_WORD.as_button( div.child(
self.search_options.contains(SearchOptions::WHOLE_WORD), SearchOption::WholeWord
focus_handle.clone(), .as_button(self.search_options, focus_handle.clone()),
cx.listener(|this, _, window, cx| { )
this.toggle_whole_word(&ToggleWholeWord, window, cx)
}),
))
}) })
.when(regex, |div| { .when(regex, |div| {
div.child(SearchOptions::REGEX.as_button( div.child(
self.search_options.contains(SearchOptions::REGEX), SearchOption::Regex
focus_handle.clone(), .as_button(self.search_options, focus_handle.clone()),
cx.listener(|this, _, window, cx| { )
this.toggle_regex(&ToggleRegex, window, cx)
}),
))
}), }),
) )
}); });
@ -248,13 +237,13 @@ impl Render for BufferSearchBar {
.gap_1() .gap_1()
.min_w_64() .min_w_64()
.when(replacement, |this| { .when(replacement, |this| {
this.child(toggle_replace_button( this.child(render_action_button(
"buffer-search-bar-toggle-replace-button", "buffer-search-bar-toggle",
focus_handle.clone(), IconName::Replace,
self.replace_enabled, self.replace_enabled,
cx.listener(|this, _: &ClickEvent, window, cx| { "Toggle Replace",
this.toggle_replace(&ToggleReplace, window, cx); &ToggleReplace,
}), focus_handle.clone(),
)) ))
}) })
.when(selection, |this| { .when(selection, |this| {

View file

@ -1,36 +0,0 @@
use gpui::{Action, SharedString};
use crate::{ActivateRegexMode, ActivateTextMode};
// TODO: Update the default search mode to get from config
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum SearchMode {
#[default]
Text,
Regex,
}
impl SearchMode {
pub(crate) fn label(&self) -> &'static str {
match self {
SearchMode::Text => "Text",
SearchMode::Regex => "Regex",
}
}
pub(crate) fn tooltip(&self) -> SharedString {
format!("Activate {} Mode", self.label()).into()
}
pub(crate) fn action(&self) -> Box<dyn Action> {
match self {
SearchMode::Text => ActivateTextMode.boxed_clone(),
SearchMode::Regex => ActivateRegexMode.boxed_clone(),
}
}
}
pub(crate) fn next_mode(mode: &SearchMode) -> SearchMode {
match mode {
SearchMode::Text => SearchMode::Regex,
SearchMode::Regex => SearchMode::Text,
}
}

View file

@ -1,11 +1,9 @@
use crate::{ use crate::{
BufferSearchBar, FocusSearch, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext, BufferSearchBar, FocusSearch, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll, ReplaceNext,
SearchOptions, SelectNextMatch, SelectPreviousMatch, ToggleCaseSensitive, ToggleIncludeIgnored, SearchOption, SearchOptions, SelectNextMatch, SelectPreviousMatch, ToggleCaseSensitive,
ToggleRegex, ToggleReplace, ToggleWholeWord, ToggleIncludeIgnored, ToggleRegex, ToggleReplace, ToggleWholeWord,
buffer_search::Deploy, buffer_search::Deploy,
search_bar::{ search_bar::{input_base_styles, render_action_button, render_text_input},
input_base_styles, render_action_button, render_text_input, toggle_replace_button,
},
}; };
use anyhow::Context as _; use anyhow::Context as _;
use collections::HashMap; use collections::HashMap;
@ -1784,14 +1782,6 @@ impl ProjectSearchBar {
} }
} }
fn is_option_enabled(&self, option: SearchOptions, cx: &App) -> bool {
if let Some(search) = self.active_project_search.as_ref() {
search.read(cx).search_options.contains(option)
} else {
false
}
}
fn next_history_query( fn next_history_query(
&mut self, &mut self,
_: &NextHistoryQuery, _: &NextHistoryQuery,
@ -1972,27 +1962,17 @@ impl Render for ProjectSearchBar {
.child( .child(
h_flex() h_flex()
.gap_1() .gap_1()
.child(SearchOptions::CASE_SENSITIVE.as_button( .child(
self.is_option_enabled(SearchOptions::CASE_SENSITIVE, cx), SearchOption::CaseSensitive
focus_handle.clone(), .as_button(search.search_options, focus_handle.clone()),
cx.listener(|this, _, window, cx| { )
this.toggle_search_option(SearchOptions::CASE_SENSITIVE, window, cx); .child(
}), SearchOption::WholeWord
)) .as_button(search.search_options, focus_handle.clone()),
.child(SearchOptions::WHOLE_WORD.as_button( )
self.is_option_enabled(SearchOptions::WHOLE_WORD, cx), .child(
focus_handle.clone(), SearchOption::Regex.as_button(search.search_options, focus_handle.clone()),
cx.listener(|this, _, window, cx| { ),
this.toggle_search_option(SearchOptions::WHOLE_WORD, window, cx);
}),
))
.child(SearchOptions::REGEX.as_button(
self.is_option_enabled(SearchOptions::REGEX, cx),
focus_handle.clone(),
cx.listener(|this, _, window, cx| {
this.toggle_search_option(SearchOptions::REGEX, window, cx);
}),
)),
); );
let mode_column = h_flex() let mode_column = h_flex()
@ -2026,16 +2006,16 @@ impl Render for ProjectSearchBar {
} }
}), }),
) )
.child(toggle_replace_button( .child(render_action_button(
"project-search-toggle-replace", "project-search",
focus_handle.clone(), IconName::Replace,
self.active_project_search self.active_project_search
.as_ref() .as_ref()
.map(|search| search.read(cx).replace_enabled) .map(|search| search.read(cx).replace_enabled)
.unwrap_or_default(), .unwrap_or_default(),
cx.listener(|this, _, window, cx| { "Toggle Replace",
this.toggle_replace(&ToggleReplace, window, cx); &ToggleReplace,
}), focus_handle.clone(),
)); ));
let query_focus = search.query_editor.focus_handle(cx); let query_focus = search.query_editor.focus_handle(cx);
@ -2149,15 +2129,8 @@ impl Render for ProjectSearchBar {
})), })),
) )
.child( .child(
SearchOptions::INCLUDE_IGNORED.as_button( SearchOption::IncludeIgnored
search .as_button(search.search_options, focus_handle.clone()),
.search_options
.contains(SearchOptions::INCLUDE_IGNORED),
focus_handle.clone(),
cx.listener(|this, _, window, cx| {
this.toggle_search_option(SearchOptions::INCLUDE_IGNORED, window, cx);
}),
),
); );
h_flex() h_flex()
.w_full() .w_full()

View file

@ -9,6 +9,8 @@ use ui::{Tooltip, prelude::*};
use workspace::notifications::NotificationId; use workspace::notifications::NotificationId;
use workspace::{Toast, Workspace}; use workspace::{Toast, Workspace};
pub use search_status_button::SEARCH_ICON;
pub mod buffer_search; pub mod buffer_search;
pub mod project_search; pub mod project_search;
pub(crate) mod search_bar; pub(crate) mod search_bar;
@ -59,48 +61,87 @@ actions!(
bitflags! { bitflags! {
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)] #[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct SearchOptions: u8 { pub struct SearchOptions: u8 {
const NONE = 0b000; const NONE = 0;
const WHOLE_WORD = 0b001; const WHOLE_WORD = 1 << SearchOption::WholeWord as u8;
const CASE_SENSITIVE = 0b010; const CASE_SENSITIVE = 1 << SearchOption::CaseSensitive as u8;
const INCLUDE_IGNORED = 0b100; const INCLUDE_IGNORED = 1 << SearchOption::IncludeIgnored as u8;
const REGEX = 0b1000; const REGEX = 1 << SearchOption::Regex as u8;
const ONE_MATCH_PER_LINE = 0b100000; const ONE_MATCH_PER_LINE = 1 << SearchOption::OneMatchPerLine as u8;
/// If set, reverse direction when finding the active match /// If set, reverse direction when finding the active match
const BACKWARDS = 0b10000; const BACKWARDS = 1 << SearchOption::Backwards as u8;
} }
} }
impl SearchOptions { #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum SearchOption {
WholeWord = 0,
CaseSensitive,
IncludeIgnored,
Regex,
OneMatchPerLine,
Backwards,
}
impl SearchOption {
pub fn as_options(self) -> SearchOptions {
SearchOptions::from_bits(1 << self as u8).unwrap()
}
pub fn label(&self) -> &'static str { pub fn label(&self) -> &'static str {
match *self { match self {
SearchOptions::WHOLE_WORD => "Match Whole Words", SearchOption::WholeWord => "Match Whole Words",
SearchOptions::CASE_SENSITIVE => "Match Case Sensitively", SearchOption::CaseSensitive => "Match Case Sensitively",
SearchOptions::INCLUDE_IGNORED => "Also search files ignored by configuration", SearchOption::IncludeIgnored => "Also search files ignored by configuration",
SearchOptions::REGEX => "Use Regular Expressions", SearchOption::Regex => "Use Regular Expressions",
_ => panic!("{:?} is not a named SearchOption", self), SearchOption::OneMatchPerLine => "One Match Per Line",
SearchOption::Backwards => "Search Backwards",
} }
} }
pub fn icon(&self) -> ui::IconName { pub fn icon(&self) -> ui::IconName {
match *self { match self {
SearchOptions::WHOLE_WORD => ui::IconName::WholeWord, SearchOption::WholeWord => ui::IconName::WholeWord,
SearchOptions::CASE_SENSITIVE => ui::IconName::CaseSensitive, SearchOption::CaseSensitive => ui::IconName::CaseSensitive,
SearchOptions::INCLUDE_IGNORED => ui::IconName::Sliders, SearchOption::IncludeIgnored => ui::IconName::Sliders,
SearchOptions::REGEX => ui::IconName::Regex, SearchOption::Regex => ui::IconName::Regex,
_ => panic!("{:?} is not a named SearchOption", self), _ => panic!("{self:?} is not a named SearchOption"),
} }
} }
pub fn to_toggle_action(&self) -> Box<dyn Action + Sync + Send + 'static> { pub fn to_toggle_action(&self) -> &'static dyn Action {
match *self { match *self {
SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord), SearchOption::WholeWord => &ToggleWholeWord,
SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive), SearchOption::CaseSensitive => &ToggleCaseSensitive,
SearchOptions::INCLUDE_IGNORED => Box::new(ToggleIncludeIgnored), SearchOption::IncludeIgnored => &ToggleIncludeIgnored,
SearchOptions::REGEX => Box::new(ToggleRegex), SearchOption::Regex => &ToggleRegex,
_ => panic!("{:?} is not a named SearchOption", self), _ => panic!("{self:?} is not a toggle action"),
} }
} }
pub fn as_button(&self, active: SearchOptions, focus_handle: FocusHandle) -> impl IntoElement {
let action = self.to_toggle_action();
let label = self.label();
IconButton::new(label, self.icon())
.on_click({
let focus_handle = focus_handle.clone();
move |_, window, cx| {
if !focus_handle.is_focused(&window) {
window.focus(&focus_handle);
}
window.dispatch_action(action.boxed_clone(), cx)
}
})
.style(ButtonStyle::Subtle)
.shape(IconButtonShape::Square)
.toggle_state(active.contains(self.as_options()))
.tooltip({
move |window, cx| Tooltip::for_action_in(label, action, &focus_handle, window, cx)
})
}
}
impl SearchOptions {
pub fn none() -> SearchOptions { pub fn none() -> SearchOptions {
SearchOptions::NONE SearchOptions::NONE
} }
@ -122,24 +163,6 @@ impl SearchOptions {
options.set(SearchOptions::REGEX, settings.regex); options.set(SearchOptions::REGEX, settings.regex);
options options
} }
pub fn as_button<Action: Fn(&gpui::ClickEvent, &mut Window, &mut App) + 'static>(
&self,
active: bool,
focus_handle: FocusHandle,
action: Action,
) -> impl IntoElement + use<Action> {
IconButton::new(self.label(), self.icon())
.on_click(action)
.style(ButtonStyle::Subtle)
.shape(IconButtonShape::Square)
.toggle_state(active)
.tooltip({
let action = self.to_toggle_action();
let label = self.label();
move |window, cx| Tooltip::for_action_in(label, &*action, &focus_handle, window, cx)
})
}
} }
pub(crate) fn show_no_more_matches(window: &mut Window, cx: &mut App) { pub(crate) fn show_no_more_matches(window: &mut Window, cx: &mut App) {

View file

@ -5,8 +5,6 @@ use theme::ThemeSettings;
use ui::{IconButton, IconButtonShape}; use ui::{IconButton, IconButtonShape};
use ui::{Tooltip, prelude::*}; use ui::{Tooltip, prelude::*};
use crate::ToggleReplace;
pub(super) fn render_action_button( pub(super) fn render_action_button(
id_prefix: &'static str, id_prefix: &'static str,
icon: ui::IconName, icon: ui::IconName,
@ -46,25 +44,6 @@ pub(crate) fn input_base_styles(border_color: Hsla, map: impl FnOnce(Div) -> Div
.rounded_lg() .rounded_lg()
} }
pub(crate) fn toggle_replace_button(
id: &'static str,
focus_handle: FocusHandle,
replace_enabled: bool,
on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut App) + 'static,
) -> IconButton {
IconButton::new(id, IconName::Replace)
.shape(IconButtonShape::Square)
.style(ButtonStyle::Subtle)
.when(replace_enabled, |button| button.style(ButtonStyle::Filled))
.on_click(on_click)
.toggle_state(replace_enabled)
.tooltip({
move |window, cx| {
Tooltip::for_action_in("Toggle Replace", &ToggleReplace, &focus_handle, window, cx)
}
})
}
pub(crate) fn render_text_input( pub(crate) fn render_text_input(
editor: &Entity<Editor>, editor: &Entity<Editor>,
color_override: Option<Color>, color_override: Option<Color>,

View file

@ -3,6 +3,8 @@ use settings::Settings as _;
use ui::{ButtonCommon, Clickable, Context, Render, Tooltip, Window, prelude::*}; use ui::{ButtonCommon, Clickable, Context, Render, Tooltip, Window, prelude::*};
use workspace::{ItemHandle, StatusItemView}; use workspace::{ItemHandle, StatusItemView};
pub const SEARCH_ICON: IconName = IconName::MagnifyingGlass;
pub struct SearchButton; pub struct SearchButton;
impl SearchButton { impl SearchButton {
@ -20,7 +22,7 @@ impl Render for SearchButton {
} }
button.child( button.child(
IconButton::new("project-search-indicator", IconName::MagnifyingGlass) IconButton::new("project-search-indicator", SEARCH_ICON)
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
.tooltip(|window, cx| { .tooltip(|window, cx| {
Tooltip::for_action( Tooltip::for_action(

View file

@ -140,7 +140,7 @@ impl Render for QuickActionBar {
let search_button = editor.is_singleton(cx).then(|| { let search_button = editor.is_singleton(cx).then(|| {
QuickActionBarButton::new( QuickActionBarButton::new(
"toggle buffer search", "toggle buffer search",
IconName::MagnifyingGlass, search::SEARCH_ICON,
!self.buffer_search_bar.read(cx).is_dismissed(), !self.buffer_search_bar.read(cx).is_dismissed(),
Box::new(buffer_search::Deploy::find()), Box::new(buffer_search::Deploy::find()),
focus_handle.clone(), focus_handle.clone(),