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        /// Focuses on the search input field.
 27        FocusSearch,
 28        /// Toggles whole word matching.
 29        ToggleWholeWord,
 30        /// Toggles case-sensitive search.
 31        ToggleCaseSensitive,
 32        /// Toggles searching in ignored files.
 33        ToggleIncludeIgnored,
 34        /// Toggles regular expression mode.
 35        ToggleRegex,
 36        /// Toggles the replace interface.
 37        ToggleReplace,
 38        /// Toggles searching within selection only.
 39        ToggleSelection,
 40        /// Selects the next search match.
 41        SelectNextMatch,
 42        /// Selects the previous search match.
 43        SelectPreviousMatch,
 44        /// Selects all search matches.
 45        SelectAllMatches,
 46        /// Cycles through search modes.
 47        CycleMode,
 48        /// Navigates to the next query in search history.
 49        NextHistoryQuery,
 50        /// Navigates to the previous query in search history.
 51        PreviousHistoryQuery,
 52        /// Replaces all matches.
 53        ReplaceAll,
 54        /// Replaces the next match.
 55        ReplaceNext,
 56    ]
 57);
 58
 59bitflags! {
 60    #[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
 61    pub struct SearchOptions: u8 {
 62        const NONE = 0b000;
 63        const WHOLE_WORD = 0b001;
 64        const CASE_SENSITIVE = 0b010;
 65        const INCLUDE_IGNORED = 0b100;
 66        const REGEX = 0b1000;
 67        const ONE_MATCH_PER_LINE = 0b100000;
 68        /// If set, reverse direction when finding the active match
 69        const BACKWARDS = 0b10000;
 70    }
 71}
 72
 73impl SearchOptions {
 74    pub fn label(&self) -> &'static str {
 75        match *self {
 76            SearchOptions::WHOLE_WORD => "Match Whole Words",
 77            SearchOptions::CASE_SENSITIVE => "Match Case Sensitively",
 78            SearchOptions::INCLUDE_IGNORED => "Also search files ignored by configuration",
 79            SearchOptions::REGEX => "Use Regular Expressions",
 80            _ => panic!("{:?} is not a named SearchOption", self),
 81        }
 82    }
 83
 84    pub fn icon(&self) -> ui::IconName {
 85        match *self {
 86            SearchOptions::WHOLE_WORD => ui::IconName::WholeWord,
 87            SearchOptions::CASE_SENSITIVE => ui::IconName::CaseSensitive,
 88            SearchOptions::INCLUDE_IGNORED => ui::IconName::Sliders,
 89            SearchOptions::REGEX => ui::IconName::Regex,
 90            _ => panic!("{:?} is not a named SearchOption", self),
 91        }
 92    }
 93
 94    pub fn to_toggle_action(&self) -> Box<dyn Action + Sync + Send + 'static> {
 95        match *self {
 96            SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord),
 97            SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive),
 98            SearchOptions::INCLUDE_IGNORED => Box::new(ToggleIncludeIgnored),
 99            SearchOptions::REGEX => Box::new(ToggleRegex),
100            _ => panic!("{:?} is not a named SearchOption", self),
101        }
102    }
103
104    pub fn none() -> SearchOptions {
105        SearchOptions::NONE
106    }
107
108    pub fn from_query(query: &SearchQuery) -> SearchOptions {
109        let mut options = SearchOptions::NONE;
110        options.set(SearchOptions::WHOLE_WORD, query.whole_word());
111        options.set(SearchOptions::CASE_SENSITIVE, query.case_sensitive());
112        options.set(SearchOptions::INCLUDE_IGNORED, query.include_ignored());
113        options.set(SearchOptions::REGEX, query.is_regex());
114        options
115    }
116
117    pub fn from_settings(settings: &SearchSettings) -> SearchOptions {
118        let mut options = SearchOptions::NONE;
119        options.set(SearchOptions::WHOLE_WORD, settings.whole_word);
120        options.set(SearchOptions::CASE_SENSITIVE, settings.case_sensitive);
121        options.set(SearchOptions::INCLUDE_IGNORED, settings.include_ignored);
122        options.set(SearchOptions::REGEX, settings.regex);
123        options
124    }
125
126    pub fn as_button<Action: Fn(&gpui::ClickEvent, &mut Window, &mut App) + 'static>(
127        &self,
128        active: bool,
129        focus_handle: FocusHandle,
130        action: Action,
131    ) -> impl IntoElement + use<Action> {
132        IconButton::new(self.label(), self.icon())
133            .on_click(action)
134            .style(ButtonStyle::Subtle)
135            .shape(IconButtonShape::Square)
136            .toggle_state(active)
137            .tooltip({
138                let action = self.to_toggle_action();
139                let label = self.label();
140                move |window, cx| Tooltip::for_action_in(label, &*action, &focus_handle, window, cx)
141            })
142    }
143}
144
145pub(crate) fn show_no_more_matches(window: &mut Window, cx: &mut App) {
146    window.defer(cx, |window, cx| {
147        struct NotifType();
148        let notification_id = NotificationId::unique::<NotifType>();
149
150        let Some(workspace) = window.root::<Workspace>().flatten() else {
151            return;
152        };
153        workspace.update(cx, |workspace, cx| {
154            workspace.show_toast(
155                Toast::new(notification_id.clone(), "No more matches").autohide(),
156                cx,
157            );
158        })
159    });
160}