1use crate::{
2 active_match_index, match_index_for_direction, query_suggestion_for_editor, Direction,
3 SearchOption, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex,
4 ToggleWholeWord,
5};
6use collections::HashMap;
7use editor::{Anchor, Autoscroll, Editor, MultiBuffer, SelectAll, MAX_TAB_TITLE_LEN};
8use gpui::{
9 actions, elements::*, platform::CursorStyle, Action, AppContext, ElementBox, Entity,
10 ModelContext, ModelHandle, MouseButton, MutableAppContext, RenderContext, Subscription, Task,
11 View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
12};
13use menu::Confirm;
14use project::{search::SearchQuery, Project};
15use settings::Settings;
16use smallvec::SmallVec;
17use std::{
18 any::{Any, TypeId},
19 ops::Range,
20 path::PathBuf,
21};
22use util::ResultExt as _;
23use workspace::{
24 Item, ItemHandle, ItemNavHistory, Pane, ToolbarItemLocation, ToolbarItemView, Workspace,
25};
26
27actions!(project_search, [SearchInNew, ToggleFocus]);
28
29#[derive(Default)]
30struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
31
32pub fn init(cx: &mut MutableAppContext) {
33 cx.set_global(ActiveSearches::default());
34 cx.add_action(ProjectSearchView::deploy);
35 cx.add_action(ProjectSearchBar::search);
36 cx.add_action(ProjectSearchBar::search_in_new);
37 cx.add_action(ProjectSearchBar::select_next_match);
38 cx.add_action(ProjectSearchBar::select_prev_match);
39 cx.add_action(ProjectSearchBar::toggle_focus);
40 cx.capture_action(ProjectSearchBar::tab);
41 add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);
42 add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);
43 add_toggle_option_action::<ToggleRegex>(SearchOption::Regex, cx);
44}
45
46fn add_toggle_option_action<A: Action>(option: SearchOption, cx: &mut MutableAppContext) {
47 cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext<Pane>| {
48 if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<ProjectSearchBar>() {
49 if search_bar.update(cx, |search_bar, cx| {
50 search_bar.toggle_search_option(option, cx)
51 }) {
52 return;
53 }
54 }
55 cx.propagate_action();
56 });
57}
58
59struct ProjectSearch {
60 project: ModelHandle<Project>,
61 excerpts: ModelHandle<MultiBuffer>,
62 pending_search: Option<Task<Option<()>>>,
63 match_ranges: Vec<Range<Anchor>>,
64 active_query: Option<SearchQuery>,
65}
66
67pub struct ProjectSearchView {
68 model: ModelHandle<ProjectSearch>,
69 query_editor: ViewHandle<Editor>,
70 results_editor: ViewHandle<Editor>,
71 case_sensitive: bool,
72 whole_word: bool,
73 regex: bool,
74 query_contains_error: bool,
75 active_match_index: Option<usize>,
76 results_editor_was_focused: bool,
77}
78
79pub struct ProjectSearchBar {
80 active_project_search: Option<ViewHandle<ProjectSearchView>>,
81 subscription: Option<Subscription>,
82}
83
84impl Entity for ProjectSearch {
85 type Event = ();
86}
87
88impl ProjectSearch {
89 fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
90 let replica_id = project.read(cx).replica_id();
91 Self {
92 project,
93 excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
94 pending_search: Default::default(),
95 match_ranges: Default::default(),
96 active_query: None,
97 }
98 }
99
100 fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
101 cx.add_model(|cx| Self {
102 project: self.project.clone(),
103 excerpts: self
104 .excerpts
105 .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
106 pending_search: Default::default(),
107 match_ranges: self.match_ranges.clone(),
108 active_query: self.active_query.clone(),
109 })
110 }
111
112 fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
113 let search = self
114 .project
115 .update(cx, |project, cx| project.search(query.clone(), cx));
116 self.active_query = Some(query);
117 self.match_ranges.clear();
118 self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
119 let matches = search.await.log_err()?;
120 if let Some(this) = this.upgrade(&cx) {
121 this.update(&mut cx, |this, cx| {
122 this.match_ranges.clear();
123 let mut matches = matches.into_iter().collect::<Vec<_>>();
124 matches
125 .sort_by_key(|(buffer, _)| buffer.read(cx).file().map(|file| file.path()));
126 this.excerpts.update(cx, |excerpts, cx| {
127 excerpts.clear(cx);
128 for (buffer, buffer_matches) in matches {
129 let ranges_to_highlight = excerpts.push_excerpts_with_context_lines(
130 buffer,
131 buffer_matches.clone(),
132 1,
133 cx,
134 );
135 this.match_ranges.extend(ranges_to_highlight);
136 }
137 });
138 this.pending_search.take();
139 cx.notify();
140 });
141 }
142 None
143 }));
144 cx.notify();
145 }
146}
147
148pub enum ViewEvent {
149 UpdateTab,
150 Activate,
151 EditorEvent(editor::Event),
152}
153
154impl Entity for ProjectSearchView {
155 type Event = ViewEvent;
156}
157
158impl View for ProjectSearchView {
159 fn ui_name() -> &'static str {
160 "ProjectSearchView"
161 }
162
163 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
164 let model = &self.model.read(cx);
165 if model.match_ranges.is_empty() {
166 enum Status {}
167
168 let theme = cx.global::<Settings>().theme.clone();
169 let text = if self.query_editor.read(cx).text(cx).is_empty() {
170 ""
171 } else if model.pending_search.is_some() {
172 "Searching..."
173 } else {
174 "No results"
175 };
176 MouseEventHandler::new::<Status, _, _>(0, cx, |_, _| {
177 Label::new(text.to_string(), theme.search.results_status.clone())
178 .aligned()
179 .contained()
180 .with_background_color(theme.editor.background)
181 .flex(1., true)
182 .boxed()
183 })
184 .on_down(MouseButton::Left, |_, cx| {
185 cx.focus_parent_view();
186 })
187 .boxed()
188 } else {
189 ChildView::new(&self.results_editor).flex(1., true).boxed()
190 }
191 }
192
193 fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
194 let handle = cx.weak_handle();
195 cx.update_global(|state: &mut ActiveSearches, cx| {
196 state
197 .0
198 .insert(self.model.read(cx).project.downgrade(), handle)
199 });
200
201 if self.results_editor_was_focused && !self.model.read(cx).match_ranges.is_empty() {
202 self.focus_results_editor(cx);
203 } else {
204 cx.focus(&self.query_editor);
205 }
206 }
207}
208
209impl Item for ProjectSearchView {
210 fn act_as_type(
211 &self,
212 type_id: TypeId,
213 self_handle: &ViewHandle<Self>,
214 _: &gpui::AppContext,
215 ) -> Option<gpui::AnyViewHandle> {
216 if type_id == TypeId::of::<Self>() {
217 Some(self_handle.into())
218 } else if type_id == TypeId::of::<Editor>() {
219 Some((&self.results_editor).into())
220 } else {
221 None
222 }
223 }
224
225 fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
226 self.results_editor
227 .update(cx, |editor, cx| editor.deactivated(cx));
228 }
229
230 fn tab_content(
231 &self,
232 _detail: Option<usize>,
233 tab_theme: &theme::Tab,
234 cx: &gpui::AppContext,
235 ) -> ElementBox {
236 let settings = cx.global::<Settings>();
237 let search_theme = &settings.theme.search;
238 Flex::row()
239 .with_child(
240 Svg::new("icons/magnifying_glass_12.svg")
241 .with_color(tab_theme.label.text.color)
242 .constrained()
243 .with_width(search_theme.tab_icon_width)
244 .aligned()
245 .boxed(),
246 )
247 .with_children(self.model.read(cx).active_query.as_ref().map(|query| {
248 let query_text = if query.as_str().len() > MAX_TAB_TITLE_LEN {
249 query.as_str()[..MAX_TAB_TITLE_LEN].to_string() + "…"
250 } else {
251 query.as_str().to_string()
252 };
253
254 Label::new(query_text, tab_theme.label.clone())
255 .aligned()
256 .contained()
257 .with_margin_left(search_theme.tab_icon_spacing)
258 .boxed()
259 }))
260 .boxed()
261 }
262
263 fn project_path(&self, _: &gpui::AppContext) -> Option<project::ProjectPath> {
264 None
265 }
266
267 fn project_entry_ids(&self, cx: &AppContext) -> SmallVec<[project::ProjectEntryId; 3]> {
268 self.results_editor.project_entry_ids(cx)
269 }
270
271 fn is_singleton(&self, _: &AppContext) -> bool {
272 false
273 }
274
275 fn can_save(&self, _: &gpui::AppContext) -> bool {
276 true
277 }
278
279 fn is_dirty(&self, cx: &AppContext) -> bool {
280 self.results_editor.read(cx).is_dirty(cx)
281 }
282
283 fn has_conflict(&self, cx: &AppContext) -> bool {
284 self.results_editor.read(cx).has_conflict(cx)
285 }
286
287 fn save(
288 &mut self,
289 project: ModelHandle<Project>,
290 cx: &mut ViewContext<Self>,
291 ) -> Task<anyhow::Result<()>> {
292 self.results_editor
293 .update(cx, |editor, cx| editor.save(project, cx))
294 }
295
296 fn save_as(
297 &mut self,
298 _: ModelHandle<Project>,
299 _: PathBuf,
300 _: &mut ViewContext<Self>,
301 ) -> Task<anyhow::Result<()>> {
302 unreachable!("save_as should not have been called")
303 }
304
305 fn reload(
306 &mut self,
307 project: ModelHandle<Project>,
308 cx: &mut ViewContext<Self>,
309 ) -> Task<anyhow::Result<()>> {
310 self.results_editor
311 .update(cx, |editor, cx| editor.reload(project, cx))
312 }
313
314 fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
315 where
316 Self: Sized,
317 {
318 let model = self.model.update(cx, |model, cx| model.clone(cx));
319 Some(Self::new(model, cx))
320 }
321
322 fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
323 self.results_editor.update(cx, |editor, _| {
324 editor.set_nav_history(Some(nav_history));
325 });
326 }
327
328 fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
329 self.results_editor
330 .update(cx, |editor, cx| editor.navigate(data, cx))
331 }
332
333 fn should_activate_item_on_event(event: &Self::Event) -> bool {
334 if let ViewEvent::EditorEvent(editor_event) = event {
335 Editor::should_activate_item_on_event(editor_event)
336 } else {
337 false
338 }
339 }
340
341 fn should_update_tab_on_event(event: &ViewEvent) -> bool {
342 matches!(event, ViewEvent::UpdateTab)
343 }
344
345 fn is_edit_event(event: &Self::Event) -> bool {
346 if let ViewEvent::EditorEvent(editor_event) = event {
347 Editor::is_edit_event(editor_event)
348 } else {
349 false
350 }
351 }
352}
353
354impl ProjectSearchView {
355 fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
356 let project;
357 let excerpts;
358 let mut query_text = String::new();
359 let mut regex = false;
360 let mut case_sensitive = false;
361 let mut whole_word = false;
362
363 {
364 let model = model.read(cx);
365 project = model.project.clone();
366 excerpts = model.excerpts.clone();
367 if let Some(active_query) = model.active_query.as_ref() {
368 query_text = active_query.as_str().to_string();
369 regex = active_query.is_regex();
370 case_sensitive = active_query.case_sensitive();
371 whole_word = active_query.whole_word();
372 }
373 }
374 cx.observe(&model, |this, _, cx| this.model_changed(true, cx))
375 .detach();
376
377 let query_editor = cx.add_view(|cx| {
378 let mut editor =
379 Editor::single_line(Some(|theme| theme.search.editor.input.clone()), cx);
380 editor.set_text(query_text, cx);
381 editor
382 });
383 // Subcribe to query_editor in order to reraise editor events for workspace item activation purposes
384 cx.subscribe(&query_editor, |_, _, event, cx| {
385 cx.emit(ViewEvent::EditorEvent(event.clone()))
386 })
387 .detach();
388 cx.observe_focus(&query_editor, |this, _, focused, _| {
389 if focused {
390 this.results_editor_was_focused = false;
391 }
392 })
393 .detach();
394
395 let results_editor = cx.add_view(|cx| {
396 let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
397 editor.set_searchable(false);
398 editor
399 });
400 cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
401 .detach();
402 cx.observe_focus(&results_editor, |this, _, focused, _| {
403 if focused {
404 this.results_editor_was_focused = true;
405 }
406 })
407 .detach();
408 cx.subscribe(&results_editor, |this, _, event, cx| {
409 if matches!(event, editor::Event::SelectionsChanged { .. }) {
410 this.update_match_index(cx);
411 }
412 // Reraise editor events for workspace item activation purposes
413 cx.emit(ViewEvent::EditorEvent(event.clone()));
414 })
415 .detach();
416
417 let mut this = ProjectSearchView {
418 model,
419 query_editor,
420 results_editor,
421 case_sensitive,
422 whole_word,
423 regex,
424 query_contains_error: false,
425 active_match_index: None,
426 results_editor_was_focused: false,
427 };
428 this.model_changed(false, cx);
429 this
430 }
431
432 // Re-activate the most recently activated search or the most recent if it has been closed.
433 // If no search exists in the workspace, create a new one.
434 fn deploy(
435 workspace: &mut Workspace,
436 _: &workspace::NewSearch,
437 cx: &mut ViewContext<Workspace>,
438 ) {
439 // Clean up entries for dropped projects
440 cx.update_global(|state: &mut ActiveSearches, cx| {
441 state.0.retain(|project, _| project.is_upgradable(cx))
442 });
443
444 let active_search = cx
445 .global::<ActiveSearches>()
446 .0
447 .get(&workspace.project().downgrade());
448
449 let existing = active_search
450 .and_then(|active_search| {
451 workspace
452 .items_of_type::<ProjectSearchView>(cx)
453 .find(|search| search == active_search)
454 })
455 .or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
456
457 let query = workspace.active_item(cx).and_then(|item| {
458 let editor = item.act_as::<Editor>(cx)?;
459 let query = query_suggestion_for_editor(&editor, cx);
460 if query.is_empty() {
461 None
462 } else {
463 Some(query)
464 }
465 });
466
467 let search = if let Some(existing) = existing {
468 workspace.activate_item(&existing, cx);
469 existing
470 } else {
471 let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
472 let view = cx.add_view(|cx| ProjectSearchView::new(model, cx));
473 workspace.add_item(Box::new(view.clone()), cx);
474 view
475 };
476
477 search.update(cx, |search, cx| {
478 if let Some(query) = query {
479 search.set_query(&query, cx);
480 }
481 search.focus_query_editor(cx)
482 });
483 }
484
485 fn search(&mut self, cx: &mut ViewContext<Self>) {
486 if let Some(query) = self.build_search_query(cx) {
487 self.model.update(cx, |model, cx| model.search(query, cx));
488 }
489 }
490
491 fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
492 let text = self.query_editor.read(cx).text(cx);
493 if self.regex {
494 match SearchQuery::regex(text, self.whole_word, self.case_sensitive) {
495 Ok(query) => Some(query),
496 Err(_) => {
497 self.query_contains_error = true;
498 cx.notify();
499 None
500 }
501 }
502 } else {
503 Some(SearchQuery::text(
504 text,
505 self.whole_word,
506 self.case_sensitive,
507 ))
508 }
509 }
510
511 fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
512 if let Some(index) = self.active_match_index {
513 let model = self.model.read(cx);
514 let results_editor = self.results_editor.read(cx);
515 let new_index = match_index_for_direction(
516 &model.match_ranges,
517 &results_editor.selections.newest_anchor().head(),
518 index,
519 direction,
520 &results_editor.buffer().read(cx).snapshot(cx),
521 );
522 let range_to_select = model.match_ranges[new_index].clone();
523 self.results_editor.update(cx, |editor, cx| {
524 editor.unfold_ranges([range_to_select.clone()], false, cx);
525 editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
526 s.select_ranges([range_to_select])
527 });
528 });
529 }
530 }
531
532 fn focus_query_editor(&self, cx: &mut ViewContext<Self>) {
533 self.query_editor.update(cx, |query_editor, cx| {
534 query_editor.select_all(&SelectAll, cx);
535 });
536 cx.focus(&self.query_editor);
537 }
538
539 fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
540 self.query_editor
541 .update(cx, |query_editor, cx| query_editor.set_text(query, cx));
542 }
543
544 fn focus_results_editor(&self, cx: &mut ViewContext<Self>) {
545 self.query_editor.update(cx, |query_editor, cx| {
546 let cursor = query_editor.selections.newest_anchor().head();
547 query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor]));
548 });
549 cx.focus(&self.results_editor);
550 }
551
552 fn model_changed(&mut self, reset_selections: bool, cx: &mut ViewContext<Self>) {
553 let match_ranges = self.model.read(cx).match_ranges.clone();
554 if match_ranges.is_empty() {
555 self.active_match_index = None;
556 } else {
557 self.results_editor.update(cx, |editor, cx| {
558 if reset_selections {
559 editor.change_selections(Some(Autoscroll::Fit), cx, |s| {
560 s.select_ranges(match_ranges.first().cloned())
561 });
562 }
563 editor.highlight_background::<Self>(
564 match_ranges,
565 |theme| theme.search.match_background,
566 cx,
567 );
568 });
569 if self.query_editor.is_focused(cx) {
570 self.focus_results_editor(cx);
571 }
572 }
573
574 cx.emit(ViewEvent::UpdateTab);
575 cx.notify();
576 }
577
578 fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
579 let results_editor = self.results_editor.read(cx);
580 let new_index = active_match_index(
581 &self.model.read(cx).match_ranges,
582 &results_editor.selections.newest_anchor().head(),
583 &results_editor.buffer().read(cx).snapshot(cx),
584 );
585 if self.active_match_index != new_index {
586 self.active_match_index = new_index;
587 cx.notify();
588 }
589 }
590
591 pub fn has_matches(&self) -> bool {
592 self.active_match_index.is_some()
593 }
594}
595
596impl ProjectSearchBar {
597 pub fn new() -> Self {
598 Self {
599 active_project_search: Default::default(),
600 subscription: Default::default(),
601 }
602 }
603
604 fn search(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
605 if let Some(search_view) = self.active_project_search.as_ref() {
606 search_view.update(cx, |search_view, cx| search_view.search(cx));
607 }
608 }
609
610 fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
611 if let Some(search_view) = workspace
612 .active_item(cx)
613 .and_then(|item| item.downcast::<ProjectSearchView>())
614 {
615 let new_query = search_view.update(cx, |search_view, cx| {
616 let new_query = search_view.build_search_query(cx);
617 if new_query.is_some() {
618 if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
619 search_view.query_editor.update(cx, |editor, cx| {
620 editor.set_text(old_query.as_str(), cx);
621 });
622 search_view.regex = old_query.is_regex();
623 search_view.whole_word = old_query.whole_word();
624 search_view.case_sensitive = old_query.case_sensitive();
625 }
626 }
627 new_query
628 });
629 if let Some(new_query) = new_query {
630 let model = cx.add_model(|cx| {
631 let mut model = ProjectSearch::new(workspace.project().clone(), cx);
632 model.search(new_query, cx);
633 model
634 });
635 workspace.add_item(
636 Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
637 cx,
638 );
639 }
640 }
641 }
642
643 fn select_next_match(pane: &mut Pane, _: &SelectNextMatch, cx: &mut ViewContext<Pane>) {
644 if let Some(search_view) = pane
645 .active_item()
646 .and_then(|item| item.downcast::<ProjectSearchView>())
647 {
648 search_view.update(cx, |view, cx| view.select_match(Direction::Next, cx));
649 } else {
650 cx.propagate_action();
651 }
652 }
653
654 fn select_prev_match(pane: &mut Pane, _: &SelectPrevMatch, cx: &mut ViewContext<Pane>) {
655 if let Some(search_view) = pane
656 .active_item()
657 .and_then(|item| item.downcast::<ProjectSearchView>())
658 {
659 search_view.update(cx, |view, cx| view.select_match(Direction::Prev, cx));
660 } else {
661 cx.propagate_action();
662 }
663 }
664
665 fn toggle_focus(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext<Pane>) {
666 if let Some(search_view) = pane
667 .active_item()
668 .and_then(|item| item.downcast::<ProjectSearchView>())
669 {
670 search_view.update(cx, |search_view, cx| {
671 if search_view.query_editor.is_focused(cx) {
672 if !search_view.model.read(cx).match_ranges.is_empty() {
673 search_view.focus_results_editor(cx);
674 }
675 } else {
676 search_view.focus_query_editor(cx);
677 }
678 });
679 } else {
680 cx.propagate_action();
681 }
682 }
683
684 fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
685 if let Some(search_view) = self.active_project_search.as_ref() {
686 search_view.update(cx, |search_view, cx| {
687 if search_view.query_editor.is_focused(cx) {
688 if !search_view.model.read(cx).match_ranges.is_empty() {
689 search_view.focus_results_editor(cx);
690 }
691 } else {
692 cx.propagate_action();
693 }
694 });
695 } else {
696 cx.propagate_action();
697 }
698 }
699
700 fn toggle_search_option(&mut self, option: SearchOption, cx: &mut ViewContext<Self>) -> bool {
701 if let Some(search_view) = self.active_project_search.as_ref() {
702 search_view.update(cx, |search_view, cx| {
703 let value = match option {
704 SearchOption::WholeWord => &mut search_view.whole_word,
705 SearchOption::CaseSensitive => &mut search_view.case_sensitive,
706 SearchOption::Regex => &mut search_view.regex,
707 };
708 *value = !*value;
709 search_view.search(cx);
710 });
711 cx.notify();
712 true
713 } else {
714 false
715 }
716 }
717
718 fn render_nav_button(
719 &self,
720 icon: &str,
721 direction: Direction,
722 cx: &mut RenderContext<Self>,
723 ) -> ElementBox {
724 let action: Box<dyn Action>;
725 let tooltip;
726 match direction {
727 Direction::Prev => {
728 action = Box::new(SelectPrevMatch);
729 tooltip = "Select Previous Match";
730 }
731 Direction::Next => {
732 action = Box::new(SelectNextMatch);
733 tooltip = "Select Next Match";
734 }
735 };
736 let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
737
738 enum NavButton {}
739 MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, cx| {
740 let style = &cx
741 .global::<Settings>()
742 .theme
743 .search
744 .option_button
745 .style_for(state, false);
746 Label::new(icon.to_string(), style.text.clone())
747 .contained()
748 .with_style(style.container)
749 .boxed()
750 })
751 .on_click(MouseButton::Left, {
752 let action = action.boxed_clone();
753 move |_, cx| cx.dispatch_any_action(action.boxed_clone())
754 })
755 .with_cursor_style(CursorStyle::PointingHand)
756 .with_tooltip::<NavButton, _>(
757 direction as usize,
758 tooltip.to_string(),
759 Some(action),
760 tooltip_style,
761 cx,
762 )
763 .boxed()
764 }
765
766 fn render_option_button(
767 &self,
768 icon: &str,
769 option: SearchOption,
770 cx: &mut RenderContext<Self>,
771 ) -> ElementBox {
772 let tooltip_style = cx.global::<Settings>().theme.tooltip.clone();
773 let is_active = self.is_option_enabled(option, cx);
774 MouseEventHandler::new::<Self, _, _>(option as usize, cx, |state, cx| {
775 let style = &cx
776 .global::<Settings>()
777 .theme
778 .search
779 .option_button
780 .style_for(state, is_active);
781 Label::new(icon.to_string(), style.text.clone())
782 .contained()
783 .with_style(style.container)
784 .boxed()
785 })
786 .on_click(MouseButton::Left, move |_, cx| {
787 cx.dispatch_any_action(option.to_toggle_action())
788 })
789 .with_cursor_style(CursorStyle::PointingHand)
790 .with_tooltip::<Self, _>(
791 option as usize,
792 format!("Toggle {}", option.label()),
793 Some(option.to_toggle_action()),
794 tooltip_style,
795 cx,
796 )
797 .boxed()
798 }
799
800 fn is_option_enabled(&self, option: SearchOption, cx: &AppContext) -> bool {
801 if let Some(search) = self.active_project_search.as_ref() {
802 let search = search.read(cx);
803 match option {
804 SearchOption::WholeWord => search.whole_word,
805 SearchOption::CaseSensitive => search.case_sensitive,
806 SearchOption::Regex => search.regex,
807 }
808 } else {
809 false
810 }
811 }
812}
813
814impl Entity for ProjectSearchBar {
815 type Event = ();
816}
817
818impl View for ProjectSearchBar {
819 fn ui_name() -> &'static str {
820 "ProjectSearchBar"
821 }
822
823 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
824 if let Some(search) = self.active_project_search.as_ref() {
825 let search = search.read(cx);
826 let theme = cx.global::<Settings>().theme.clone();
827 let editor_container = if search.query_contains_error {
828 theme.search.invalid_editor
829 } else {
830 theme.search.editor.input.container
831 };
832 Flex::row()
833 .with_child(
834 Flex::row()
835 .with_child(
836 ChildView::new(&search.query_editor)
837 .aligned()
838 .left()
839 .flex(1., true)
840 .boxed(),
841 )
842 .with_children(search.active_match_index.map(|match_ix| {
843 Label::new(
844 format!(
845 "{}/{}",
846 match_ix + 1,
847 search.model.read(cx).match_ranges.len()
848 ),
849 theme.search.match_index.text.clone(),
850 )
851 .contained()
852 .with_style(theme.search.match_index.container)
853 .aligned()
854 .boxed()
855 }))
856 .contained()
857 .with_style(editor_container)
858 .aligned()
859 .constrained()
860 .with_min_width(theme.search.editor.min_width)
861 .with_max_width(theme.search.editor.max_width)
862 .flex(1., false)
863 .boxed(),
864 )
865 .with_child(
866 Flex::row()
867 .with_child(self.render_nav_button("<", Direction::Prev, cx))
868 .with_child(self.render_nav_button(">", Direction::Next, cx))
869 .aligned()
870 .boxed(),
871 )
872 .with_child(
873 Flex::row()
874 .with_child(self.render_option_button(
875 "Case",
876 SearchOption::CaseSensitive,
877 cx,
878 ))
879 .with_child(self.render_option_button("Word", SearchOption::WholeWord, cx))
880 .with_child(self.render_option_button("Regex", SearchOption::Regex, cx))
881 .contained()
882 .with_style(theme.search.option_button_group)
883 .aligned()
884 .boxed(),
885 )
886 .contained()
887 .with_style(theme.search.container)
888 .aligned()
889 .left()
890 .named("project search")
891 } else {
892 Empty::new().boxed()
893 }
894 }
895}
896
897impl ToolbarItemView for ProjectSearchBar {
898 fn set_active_pane_item(
899 &mut self,
900 active_pane_item: Option<&dyn workspace::ItemHandle>,
901 cx: &mut ViewContext<Self>,
902 ) -> ToolbarItemLocation {
903 cx.notify();
904 self.subscription = None;
905 self.active_project_search = None;
906 if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
907 self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
908 self.active_project_search = Some(search);
909 ToolbarItemLocation::PrimaryLeft {
910 flex: Some((1., false)),
911 }
912 } else {
913 ToolbarItemLocation::Hidden
914 }
915 }
916}
917
918#[cfg(test)]
919mod tests {
920 use super::*;
921 use editor::DisplayPoint;
922 use gpui::{color::Color, TestAppContext};
923 use project::FakeFs;
924 use serde_json::json;
925 use std::sync::Arc;
926
927 #[gpui::test]
928 async fn test_project_search(cx: &mut TestAppContext) {
929 let fonts = cx.font_cache();
930 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
931 theme.search.match_background = Color::red();
932 cx.update(|cx| {
933 let mut settings = Settings::test(cx);
934 settings.theme = Arc::new(theme);
935 cx.set_global(settings)
936 });
937
938 let fs = FakeFs::new(cx.background());
939 fs.insert_tree(
940 "/dir",
941 json!({
942 "one.rs": "const ONE: usize = 1;",
943 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
944 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
945 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
946 }),
947 )
948 .await;
949 let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
950 let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
951 let search_view = cx.add_view(Default::default(), |cx| {
952 ProjectSearchView::new(search.clone(), cx)
953 });
954
955 search_view.update(cx, |search_view, cx| {
956 search_view
957 .query_editor
958 .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
959 search_view.search(cx);
960 });
961 search_view.next_notification(&cx).await;
962 search_view.update(cx, |search_view, cx| {
963 assert_eq!(
964 search_view
965 .results_editor
966 .update(cx, |editor, cx| editor.display_text(cx)),
967 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
968 );
969 assert_eq!(
970 search_view
971 .results_editor
972 .update(cx, |editor, cx| editor.all_background_highlights(cx)),
973 &[
974 (
975 DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
976 Color::red()
977 ),
978 (
979 DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
980 Color::red()
981 ),
982 (
983 DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
984 Color::red()
985 )
986 ]
987 );
988 assert_eq!(search_view.active_match_index, Some(0));
989 assert_eq!(
990 search_view
991 .results_editor
992 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
993 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
994 );
995
996 search_view.select_match(Direction::Next, cx);
997 });
998
999 search_view.update(cx, |search_view, cx| {
1000 assert_eq!(search_view.active_match_index, Some(1));
1001 assert_eq!(
1002 search_view
1003 .results_editor
1004 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1005 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1006 );
1007 search_view.select_match(Direction::Next, cx);
1008 });
1009
1010 search_view.update(cx, |search_view, cx| {
1011 assert_eq!(search_view.active_match_index, Some(2));
1012 assert_eq!(
1013 search_view
1014 .results_editor
1015 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1016 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1017 );
1018 search_view.select_match(Direction::Next, cx);
1019 });
1020
1021 search_view.update(cx, |search_view, cx| {
1022 assert_eq!(search_view.active_match_index, Some(0));
1023 assert_eq!(
1024 search_view
1025 .results_editor
1026 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1027 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1028 );
1029 search_view.select_match(Direction::Prev, cx);
1030 });
1031
1032 search_view.update(cx, |search_view, cx| {
1033 assert_eq!(search_view.active_match_index, Some(2));
1034 assert_eq!(
1035 search_view
1036 .results_editor
1037 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1038 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1039 );
1040 search_view.select_match(Direction::Prev, cx);
1041 });
1042
1043 search_view.update(cx, |search_view, cx| {
1044 assert_eq!(search_view.active_match_index, Some(1));
1045 assert_eq!(
1046 search_view
1047 .results_editor
1048 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1049 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1050 );
1051 });
1052 }
1053}