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        const ONE_MATCH_PER_LINE = 0b100000;
 52        /// If set, reverse direction when finding the active match
 53        const BACKWARDS = 0b10000;
 54    }
 55}
 56
 57impl SearchOptions {
 58    pub fn label(&self) -> &'static str {
 59        match *self {
 60            SearchOptions::WHOLE_WORD => "Match Whole Words",
 61            SearchOptions::CASE_SENSITIVE => "Match Case Sensitively",
 62            SearchOptions::INCLUDE_IGNORED => "Also search files ignored by configuration",
 63            SearchOptions::REGEX => "Use Regular Expressions",
 64            _ => panic!("{:?} is not a named SearchOption", self),
 65        }
 66    }
 67
 68    pub fn icon(&self) -> ui::IconName {
 69        match *self {
 70            SearchOptions::WHOLE_WORD => ui::IconName::WholeWord,
 71            SearchOptions::CASE_SENSITIVE => ui::IconName::CaseSensitive,
 72            SearchOptions::INCLUDE_IGNORED => ui::IconName::Sliders,
 73            SearchOptions::REGEX => ui::IconName::Regex,
 74            _ => panic!("{:?} is not a named SearchOption", self),
 75        }
 76    }
 77
 78    pub fn to_toggle_action(&self) -> Box<dyn Action + Sync + Send + 'static> {
 79        match *self {
 80            SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord),
 81            SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive),
 82            SearchOptions::INCLUDE_IGNORED => Box::new(ToggleIncludeIgnored),
 83            SearchOptions::REGEX => Box::new(ToggleRegex),
 84            _ => panic!("{:?} is not a named SearchOption", self),
 85        }
 86    }
 87
 88    pub fn none() -> SearchOptions {
 89        SearchOptions::NONE
 90    }
 91
 92    pub fn from_query(query: &SearchQuery) -> SearchOptions {
 93        let mut options = SearchOptions::NONE;
 94        options.set(SearchOptions::WHOLE_WORD, query.whole_word());
 95        options.set(SearchOptions::CASE_SENSITIVE, query.case_sensitive());
 96        options.set(SearchOptions::INCLUDE_IGNORED, query.include_ignored());
 97        options.set(SearchOptions::REGEX, query.is_regex());
 98        options
 99    }
100
101    pub fn from_settings(settings: &SearchSettings) -> SearchOptions {
102        let mut options = SearchOptions::NONE;
103        options.set(SearchOptions::WHOLE_WORD, settings.whole_word);
104        options.set(SearchOptions::CASE_SENSITIVE, settings.case_sensitive);
105        options.set(SearchOptions::INCLUDE_IGNORED, settings.include_ignored);
106        options.set(SearchOptions::REGEX, settings.regex);
107        options
108    }
109
110    pub fn as_button<Action: Fn(&gpui::ClickEvent, &mut Window, &mut App) + 'static>(
111        &self,
112        active: bool,
113        focus_handle: FocusHandle,
114        action: Action,
115    ) -> impl IntoElement + use<Action> {
116        IconButton::new(self.label(), self.icon())
117            .on_click(action)
118            .style(ButtonStyle::Subtle)
119            .shape(IconButtonShape::Square)
120            .toggle_state(active)
121            .tooltip({
122                let action = self.to_toggle_action();
123                let label = self.label();
124                move |window, cx| Tooltip::for_action_in(label, &*action, &focus_handle, window, cx)
125            })
126    }
127}
128
129pub(crate) fn show_no_more_matches(window: &mut Window, cx: &mut App) {
130    window.defer(cx, |window, cx| {
131        struct NotifType();
132        let notification_id = NotificationId::unique::<NotifType>();
133
134        let Some(workspace) = window.root::<Workspace>().flatten() else {
135            return;
136        };
137        workspace.update(cx, |workspace, cx| {
138            workspace.show_toast(
139                Toast::new(notification_id.clone(), "No more matches").autohide(),
140                cx,
141            );
142        })
143    });
144}