search.rs

  1use bitflags::bitflags;
  2pub use buffer_search::BufferSearchBar;
  3use editor::SearchSettings;
  4use gpui::{actions, Action, App, FocusHandle, IntoElement};
  5use project::search::SearchQuery;
  6pub use project_search::ProjectSearchView;
  7use ui::{prelude::*, Tooltip};
  8use ui::{ButtonStyle, IconButton, IconButtonShape};
  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 App) {
 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        focus_handle: FocusHandle,
110        action: impl Fn(&gpui::ClickEvent, &mut Window, &mut App) + 'static,
111    ) -> impl IntoElement {
112        IconButton::new(self.label(), self.icon())
113            .on_click(action)
114            .style(ButtonStyle::Subtle)
115            .shape(IconButtonShape::Square)
116            .toggle_state(active)
117            .tooltip({
118                let action = self.to_toggle_action();
119                let label = self.label();
120                move |window, cx| Tooltip::for_action_in(label, &*action, &focus_handle, window, cx)
121            })
122    }
123}
124
125pub(crate) fn show_no_more_matches(window: &mut Window, cx: &mut App) {
126    window.defer(cx, |window, cx| {
127        struct NotifType();
128        let notification_id = NotificationId::unique::<NotifType>();
129
130        let Some(workspace) = window.root::<Workspace>().flatten() else {
131            return;
132        };
133        workspace.update(cx, |workspace, cx| {
134            workspace.show_toast(
135                Toast::new(notification_id.clone(), "No more matches").autohide(),
136                cx,
137            );
138        })
139    });
140}