search.rs

  1use bitflags::bitflags;
  2pub use buffer_search::BufferSearchBar;
  3use editor::SearchSettings;
  4use gpui::{actions, Action, AppContext, IntoElement};
  5use project::search::SearchQuery;
  6pub use project_search::ProjectSearchView;
  7use ui::{prelude::*, Tooltip};
  8use ui::{ButtonStyle, IconButton};
  9use workspace::notifications::NotificationId;
 10use workspace::{Toast, Workspace};
 11
 12pub mod buffer_search;
 13pub mod project_search;
 14pub(crate) mod search_bar;
 15
 16pub fn init(cx: &mut AppContext) {
 17    menu::init();
 18    buffer_search::init(cx);
 19    project_search::init(cx);
 20}
 21
 22actions!(
 23    search,
 24    [
 25        FocusSearch,
 26        ToggleWholeWord,
 27        ToggleCaseSensitive,
 28        ToggleIncludeIgnored,
 29        ToggleRegex,
 30        ToggleReplace,
 31        ToggleSelection,
 32        SelectNextMatch,
 33        SelectPrevMatch,
 34        SelectAllMatches,
 35        NextHistoryQuery,
 36        PreviousHistoryQuery,
 37        ReplaceAll,
 38        ReplaceNext,
 39    ]
 40);
 41
 42bitflags! {
 43    #[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
 44    pub struct SearchOptions: u8 {
 45        const NONE = 0b000;
 46        const WHOLE_WORD = 0b001;
 47        const CASE_SENSITIVE = 0b010;
 48        const INCLUDE_IGNORED = 0b100;
 49        const REGEX = 0b1000;
 50    }
 51}
 52
 53impl SearchOptions {
 54    pub fn label(&self) -> &'static str {
 55        match *self {
 56            SearchOptions::WHOLE_WORD => "Match whole words",
 57            SearchOptions::CASE_SENSITIVE => "Match case sensitively",
 58            SearchOptions::INCLUDE_IGNORED => "Also search files ignored by configuration",
 59            SearchOptions::REGEX => "Use regular expressions",
 60            _ => panic!("{:?} is not a named SearchOption", self),
 61        }
 62    }
 63
 64    pub fn icon(&self) -> ui::IconName {
 65        match *self {
 66            SearchOptions::WHOLE_WORD => ui::IconName::WholeWord,
 67            SearchOptions::CASE_SENSITIVE => ui::IconName::CaseSensitive,
 68            SearchOptions::INCLUDE_IGNORED => ui::IconName::Sliders,
 69            SearchOptions::REGEX => ui::IconName::Regex,
 70            _ => panic!("{:?} is not a named SearchOption", self),
 71        }
 72    }
 73
 74    pub fn to_toggle_action(&self) -> Box<dyn Action + Sync + Send + 'static> {
 75        match *self {
 76            SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord),
 77            SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive),
 78            SearchOptions::INCLUDE_IGNORED => Box::new(ToggleIncludeIgnored),
 79            SearchOptions::REGEX => Box::new(ToggleRegex),
 80            _ => panic!("{:?} is not a named SearchOption", self),
 81        }
 82    }
 83
 84    pub fn none() -> SearchOptions {
 85        SearchOptions::NONE
 86    }
 87
 88    pub fn from_query(query: &SearchQuery) -> SearchOptions {
 89        let mut options = SearchOptions::NONE;
 90        options.set(SearchOptions::WHOLE_WORD, query.whole_word());
 91        options.set(SearchOptions::CASE_SENSITIVE, query.case_sensitive());
 92        options.set(SearchOptions::INCLUDE_IGNORED, query.include_ignored());
 93        options.set(SearchOptions::REGEX, query.is_regex());
 94        options
 95    }
 96
 97    pub fn from_settings(settings: &SearchSettings) -> SearchOptions {
 98        let mut options = SearchOptions::NONE;
 99        options.set(SearchOptions::WHOLE_WORD, settings.whole_word);
100        options.set(SearchOptions::CASE_SENSITIVE, settings.case_sensitive);
101        options.set(SearchOptions::INCLUDE_IGNORED, settings.include_ignored);
102        options.set(SearchOptions::REGEX, settings.regex);
103        options
104    }
105
106    pub fn as_button(
107        &self,
108        active: bool,
109        action: impl Fn(&gpui::ClickEvent, &mut WindowContext) + 'static,
110    ) -> impl IntoElement {
111        IconButton::new(self.label(), self.icon())
112            .on_click(action)
113            .style(ButtonStyle::Subtle)
114            .selected(active)
115            .tooltip({
116                let action = self.to_toggle_action();
117                let label = self.label();
118                move |cx| Tooltip::for_action(label, &*action, cx)
119            })
120    }
121}
122
123pub(crate) fn show_no_more_matches(cx: &mut WindowContext) {
124    cx.defer(|cx| {
125        struct NotifType();
126        let notification_id = NotificationId::unique::<NotifType>();
127        let Some(workspace) = cx.window_handle().downcast::<Workspace>() else {
128            return;
129        };
130        workspace
131            .update(cx, |workspace, cx| {
132                workspace.show_toast(
133                    Toast::new(notification_id.clone(), "No more matches").autohide(),
134                    cx,
135                );
136            })
137            .ok();
138    });
139}