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