search.rs

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