1use crate::{
2 SearchOption, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleRegex,
3 ToggleWholeWord,
4};
5use anyhow::Result;
6use collections::HashMap;
7use editor::{
8 items::active_match_index, scroll::autoscroll::Autoscroll, Anchor, Editor, MultiBuffer,
9 SelectAll, MAX_TAB_TITLE_LEN,
10};
11use futures::StreamExt;
12use globset::{Glob, GlobMatcher};
13use gpui::{
14 actions,
15 elements::*,
16 platform::{CursorStyle, MouseButton},
17 Action, AnyElement, AnyViewHandle, AppContext, Entity, ModelContext, ModelHandle, Subscription,
18 Task, View, ViewContext, ViewHandle, WeakModelHandle, WeakViewHandle,
19};
20use menu::Confirm;
21use project::{search::SearchQuery, Project};
22use smallvec::SmallVec;
23use std::{
24 any::{Any, TypeId},
25 borrow::Cow,
26 collections::HashSet,
27 mem,
28 ops::Range,
29 path::PathBuf,
30 sync::Arc,
31};
32use util::ResultExt as _;
33use workspace::{
34 item::{BreadcrumbText, Item, ItemEvent, ItemHandle},
35 searchable::{Direction, SearchableItem, SearchableItemHandle},
36 ItemNavHistory, Pane, ToolbarItemLocation, ToolbarItemView, Workspace, WorkspaceId,
37};
38
39actions!(project_search, [SearchInNew, ToggleFocus, NextField]);
40
41#[derive(Default)]
42struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
43
44pub fn init(cx: &mut AppContext) {
45 cx.set_global(ActiveSearches::default());
46 cx.add_action(ProjectSearchView::deploy);
47 cx.add_action(ProjectSearchBar::search);
48 cx.add_action(ProjectSearchBar::search_in_new);
49 cx.add_action(ProjectSearchBar::select_next_match);
50 cx.add_action(ProjectSearchBar::select_prev_match);
51 cx.add_action(ProjectSearchBar::toggle_focus);
52 cx.capture_action(ProjectSearchBar::tab);
53 cx.capture_action(ProjectSearchBar::tab_previous);
54 add_toggle_option_action::<ToggleCaseSensitive>(SearchOption::CaseSensitive, cx);
55 add_toggle_option_action::<ToggleWholeWord>(SearchOption::WholeWord, cx);
56 add_toggle_option_action::<ToggleRegex>(SearchOption::Regex, cx);
57}
58
59fn add_toggle_option_action<A: Action>(option: SearchOption, cx: &mut AppContext) {
60 cx.add_action(move |pane: &mut Pane, _: &A, cx: &mut ViewContext<Pane>| {
61 if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<ProjectSearchBar>() {
62 if search_bar.update(cx, |search_bar, cx| {
63 search_bar.toggle_search_option(option, cx)
64 }) {
65 return;
66 }
67 }
68 cx.propagate_action();
69 });
70}
71
72struct ProjectSearch {
73 project: ModelHandle<Project>,
74 excerpts: ModelHandle<MultiBuffer>,
75 pending_search: Option<Task<Option<()>>>,
76 match_ranges: Vec<Range<Anchor>>,
77 active_query: Option<SearchQuery>,
78 search_id: usize,
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82enum InputPanel {
83 Query,
84 Exclude,
85 Include,
86}
87
88pub struct ProjectSearchView {
89 model: ModelHandle<ProjectSearch>,
90 query_editor: ViewHandle<Editor>,
91 results_editor: ViewHandle<Editor>,
92 case_sensitive: bool,
93 whole_word: bool,
94 regex: bool,
95 panels_with_errors: HashSet<InputPanel>,
96 active_match_index: Option<usize>,
97 search_id: usize,
98 query_editor_was_focused: bool,
99 included_files_editor: ViewHandle<Editor>,
100 excluded_files_editor: ViewHandle<Editor>,
101}
102
103pub struct ProjectSearchBar {
104 active_project_search: Option<ViewHandle<ProjectSearchView>>,
105 subscription: Option<Subscription>,
106}
107
108impl Entity for ProjectSearch {
109 type Event = ();
110}
111
112impl ProjectSearch {
113 fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
114 let replica_id = project.read(cx).replica_id();
115 Self {
116 project,
117 excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
118 pending_search: Default::default(),
119 match_ranges: Default::default(),
120 active_query: None,
121 search_id: 0,
122 }
123 }
124
125 fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
126 cx.add_model(|cx| Self {
127 project: self.project.clone(),
128 excerpts: self
129 .excerpts
130 .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
131 pending_search: Default::default(),
132 match_ranges: self.match_ranges.clone(),
133 active_query: self.active_query.clone(),
134 search_id: self.search_id,
135 })
136 }
137
138 fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
139 let search = self
140 .project
141 .update(cx, |project, cx| project.search(query.clone(), cx));
142 self.search_id += 1;
143 self.active_query = Some(query);
144 self.match_ranges.clear();
145 self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
146 let matches = search.await.log_err()?;
147 let this = this.upgrade(&cx)?;
148 let mut matches = matches.into_iter().collect::<Vec<_>>();
149 let (_task, mut match_ranges) = this.update(&mut cx, |this, cx| {
150 this.match_ranges.clear();
151 matches.sort_by_key(|(buffer, _)| buffer.read(cx).file().map(|file| file.path()));
152 this.excerpts.update(cx, |excerpts, cx| {
153 excerpts.clear(cx);
154 excerpts.stream_excerpts_with_context_lines(matches, 1, cx)
155 })
156 });
157
158 while let Some(match_range) = match_ranges.next().await {
159 this.update(&mut cx, |this, cx| {
160 this.match_ranges.push(match_range);
161 while let Ok(Some(match_range)) = match_ranges.try_next() {
162 this.match_ranges.push(match_range);
163 }
164 cx.notify();
165 });
166 }
167
168 this.update(&mut cx, |this, cx| {
169 this.pending_search.take();
170 cx.notify();
171 });
172
173 None
174 }));
175 cx.notify();
176 }
177}
178
179pub enum ViewEvent {
180 UpdateTab,
181 Activate,
182 EditorEvent(editor::Event),
183}
184
185impl Entity for ProjectSearchView {
186 type Event = ViewEvent;
187}
188
189impl View for ProjectSearchView {
190 fn ui_name() -> &'static str {
191 "ProjectSearchView"
192 }
193
194 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
195 let model = &self.model.read(cx);
196 if model.match_ranges.is_empty() {
197 enum Status {}
198
199 let theme = theme::current(cx).clone();
200 let text = if self.query_editor.read(cx).text(cx).is_empty() {
201 ""
202 } else if model.pending_search.is_some() {
203 "Searching..."
204 } else {
205 "No results"
206 };
207 MouseEventHandler::<Status, _>::new(0, cx, |_, _| {
208 Label::new(text, theme.search.results_status.clone())
209 .aligned()
210 .contained()
211 .with_background_color(theme.editor.background)
212 .flex(1., true)
213 })
214 .on_down(MouseButton::Left, |_, _, cx| {
215 cx.focus_parent();
216 })
217 .into_any_named("project search view")
218 } else {
219 ChildView::new(&self.results_editor, cx)
220 .flex(1., true)
221 .into_any_named("project search view")
222 }
223 }
224
225 fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
226 let handle = cx.weak_handle();
227 cx.update_global(|state: &mut ActiveSearches, cx| {
228 state
229 .0
230 .insert(self.model.read(cx).project.downgrade(), handle)
231 });
232
233 if cx.is_self_focused() {
234 if self.query_editor_was_focused {
235 cx.focus(&self.query_editor);
236 } else {
237 cx.focus(&self.results_editor);
238 }
239 }
240 }
241}
242
243impl Item for ProjectSearchView {
244 fn tab_tooltip_text(&self, cx: &AppContext) -> Option<Cow<str>> {
245 Some(self.query_editor.read(cx).text(cx).into())
246 }
247
248 fn act_as_type<'a>(
249 &'a self,
250 type_id: TypeId,
251 self_handle: &'a ViewHandle<Self>,
252 _: &'a AppContext,
253 ) -> Option<&'a AnyViewHandle> {
254 if type_id == TypeId::of::<Self>() {
255 Some(self_handle)
256 } else if type_id == TypeId::of::<Editor>() {
257 Some(&self.results_editor)
258 } else {
259 None
260 }
261 }
262
263 fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
264 self.results_editor
265 .update(cx, |editor, cx| editor.deactivated(cx));
266 }
267
268 fn tab_content<T: View>(
269 &self,
270 _detail: Option<usize>,
271 tab_theme: &theme::Tab,
272 cx: &AppContext,
273 ) -> AnyElement<T> {
274 Flex::row()
275 .with_child(
276 Svg::new("icons/magnifying_glass_12.svg")
277 .with_color(tab_theme.label.text.color)
278 .constrained()
279 .with_width(tab_theme.type_icon_width)
280 .aligned()
281 .contained()
282 .with_margin_right(tab_theme.spacing),
283 )
284 .with_children(self.model.read(cx).active_query.as_ref().map(|query| {
285 let query_text = util::truncate_and_trailoff(query.as_str(), MAX_TAB_TITLE_LEN);
286
287 Label::new(query_text, tab_theme.label.clone()).aligned()
288 }))
289 .into_any()
290 }
291
292 fn for_each_project_item(&self, cx: &AppContext, f: &mut dyn FnMut(usize, &dyn project::Item)) {
293 self.results_editor.for_each_project_item(cx, f)
294 }
295
296 fn is_singleton(&self, _: &AppContext) -> bool {
297 false
298 }
299
300 fn can_save(&self, _: &AppContext) -> bool {
301 true
302 }
303
304 fn is_dirty(&self, cx: &AppContext) -> bool {
305 self.results_editor.read(cx).is_dirty(cx)
306 }
307
308 fn has_conflict(&self, cx: &AppContext) -> bool {
309 self.results_editor.read(cx).has_conflict(cx)
310 }
311
312 fn save(
313 &mut self,
314 project: ModelHandle<Project>,
315 cx: &mut ViewContext<Self>,
316 ) -> Task<anyhow::Result<()>> {
317 self.results_editor
318 .update(cx, |editor, cx| editor.save(project, cx))
319 }
320
321 fn save_as(
322 &mut self,
323 _: ModelHandle<Project>,
324 _: PathBuf,
325 _: &mut ViewContext<Self>,
326 ) -> Task<anyhow::Result<()>> {
327 unreachable!("save_as should not have been called")
328 }
329
330 fn reload(
331 &mut self,
332 project: ModelHandle<Project>,
333 cx: &mut ViewContext<Self>,
334 ) -> Task<anyhow::Result<()>> {
335 self.results_editor
336 .update(cx, |editor, cx| editor.reload(project, cx))
337 }
338
339 fn clone_on_split(&self, _workspace_id: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self>
340 where
341 Self: Sized,
342 {
343 let model = self.model.update(cx, |model, cx| model.clone(cx));
344 Some(Self::new(model, cx))
345 }
346
347 fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
348 self.results_editor
349 .update(cx, |editor, cx| editor.added_to_workspace(workspace, cx));
350 }
351
352 fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
353 self.results_editor.update(cx, |editor, _| {
354 editor.set_nav_history(Some(nav_history));
355 });
356 }
357
358 fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) -> bool {
359 self.results_editor
360 .update(cx, |editor, cx| editor.navigate(data, cx))
361 }
362
363 fn git_diff_recalc(
364 &mut self,
365 project: ModelHandle<Project>,
366 cx: &mut ViewContext<Self>,
367 ) -> Task<anyhow::Result<()>> {
368 self.results_editor
369 .update(cx, |editor, cx| editor.git_diff_recalc(project, cx))
370 }
371
372 fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
373 match event {
374 ViewEvent::UpdateTab => {
375 smallvec::smallvec![ItemEvent::UpdateBreadcrumbs, ItemEvent::UpdateTab]
376 }
377 ViewEvent::EditorEvent(editor_event) => Editor::to_item_events(editor_event),
378 _ => SmallVec::new(),
379 }
380 }
381
382 fn breadcrumb_location(&self) -> ToolbarItemLocation {
383 if self.has_matches() {
384 ToolbarItemLocation::Secondary
385 } else {
386 ToolbarItemLocation::Hidden
387 }
388 }
389
390 fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<BreadcrumbText>> {
391 self.results_editor.breadcrumbs(theme, cx)
392 }
393
394 fn serialized_item_kind() -> Option<&'static str> {
395 None
396 }
397
398 fn deserialize(
399 _project: ModelHandle<Project>,
400 _workspace: WeakViewHandle<Workspace>,
401 _workspace_id: workspace::WorkspaceId,
402 _item_id: workspace::ItemId,
403 _cx: &mut ViewContext<Pane>,
404 ) -> Task<anyhow::Result<ViewHandle<Self>>> {
405 unimplemented!()
406 }
407}
408
409impl ProjectSearchView {
410 fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
411 let project;
412 let excerpts;
413 let mut query_text = String::new();
414 let mut regex = false;
415 let mut case_sensitive = false;
416 let mut whole_word = false;
417
418 {
419 let model = model.read(cx);
420 project = model.project.clone();
421 excerpts = model.excerpts.clone();
422 if let Some(active_query) = model.active_query.as_ref() {
423 query_text = active_query.as_str().to_string();
424 regex = active_query.is_regex();
425 case_sensitive = active_query.case_sensitive();
426 whole_word = active_query.whole_word();
427 }
428 }
429 cx.observe(&model, |this, _, cx| this.model_changed(cx))
430 .detach();
431
432 let query_editor = cx.add_view(|cx| {
433 let mut editor = Editor::single_line(
434 Some(Arc::new(|theme| theme.search.editor.input.clone())),
435 cx,
436 );
437 editor.set_text(query_text, cx);
438 editor
439 });
440 // Subscribe to query_editor in order to reraise editor events for workspace item activation purposes
441 cx.subscribe(&query_editor, |_, _, event, cx| {
442 cx.emit(ViewEvent::EditorEvent(event.clone()))
443 })
444 .detach();
445
446 let results_editor = cx.add_view(|cx| {
447 let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
448 editor.set_searchable(false);
449 editor
450 });
451 cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
452 .detach();
453
454 cx.subscribe(&results_editor, |this, _, event, cx| {
455 if matches!(event, editor::Event::SelectionsChanged { .. }) {
456 this.update_match_index(cx);
457 }
458 // Reraise editor events for workspace item activation purposes
459 cx.emit(ViewEvent::EditorEvent(event.clone()));
460 })
461 .detach();
462
463 let included_files_editor = cx.add_view(|cx| {
464 let mut editor = Editor::single_line(
465 Some(Arc::new(|theme| {
466 theme.search.include_exclude_editor.input.clone()
467 })),
468 cx,
469 );
470 editor.set_placeholder_text("Include: crates/**/*.toml", cx);
471
472 editor
473 });
474 // Subscribe to include_files_editor in order to reraise editor events for workspace item activation purposes
475 cx.subscribe(&included_files_editor, |_, _, event, cx| {
476 cx.emit(ViewEvent::EditorEvent(event.clone()))
477 })
478 .detach();
479
480 let excluded_files_editor = cx.add_view(|cx| {
481 let mut editor = Editor::single_line(
482 Some(Arc::new(|theme| {
483 theme.search.include_exclude_editor.input.clone()
484 })),
485 cx,
486 );
487 editor.set_placeholder_text("Exclude: vendor/*, *.lock", cx);
488
489 editor
490 });
491 // Subscribe to excluded_files_editor in order to reraise editor events for workspace item activation purposes
492 cx.subscribe(&excluded_files_editor, |_, _, event, cx| {
493 cx.emit(ViewEvent::EditorEvent(event.clone()))
494 })
495 .detach();
496
497 let mut this = ProjectSearchView {
498 search_id: model.read(cx).search_id,
499 model,
500 query_editor,
501 results_editor,
502 case_sensitive,
503 whole_word,
504 regex,
505 panels_with_errors: HashSet::new(),
506 active_match_index: None,
507 query_editor_was_focused: false,
508 included_files_editor,
509 excluded_files_editor,
510 };
511 this.model_changed(cx);
512 this
513 }
514
515 // Re-activate the most recently activated search or the most recent if it has been closed.
516 // If no search exists in the workspace, create a new one.
517 fn deploy(
518 workspace: &mut Workspace,
519 _: &workspace::NewSearch,
520 cx: &mut ViewContext<Workspace>,
521 ) {
522 // Clean up entries for dropped projects
523 cx.update_global(|state: &mut ActiveSearches, cx| {
524 state.0.retain(|project, _| project.is_upgradable(cx))
525 });
526
527 let active_search = cx
528 .global::<ActiveSearches>()
529 .0
530 .get(&workspace.project().downgrade());
531
532 let existing = active_search
533 .and_then(|active_search| {
534 workspace
535 .items_of_type::<ProjectSearchView>(cx)
536 .find(|search| search == active_search)
537 })
538 .or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
539
540 let query = workspace.active_item(cx).and_then(|item| {
541 let editor = item.act_as::<Editor>(cx)?;
542 let query = editor.query_suggestion(cx);
543 if query.is_empty() {
544 None
545 } else {
546 Some(query)
547 }
548 });
549
550 let search = if let Some(existing) = existing {
551 workspace.activate_item(&existing, cx);
552 existing
553 } else {
554 let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
555 let view = cx.add_view(|cx| ProjectSearchView::new(model, cx));
556 workspace.add_item(Box::new(view.clone()), cx);
557 view
558 };
559
560 search.update(cx, |search, cx| {
561 if let Some(query) = query {
562 search.set_query(&query, cx);
563 }
564 search.focus_query_editor(cx)
565 });
566 }
567
568 fn search(&mut self, cx: &mut ViewContext<Self>) {
569 if let Some(query) = self.build_search_query(cx) {
570 self.model.update(cx, |model, cx| model.search(query, cx));
571 }
572 }
573
574 fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
575 let text = self.query_editor.read(cx).text(cx);
576 let included_files =
577 match Self::load_glob_set(&self.included_files_editor.read(cx).text(cx)) {
578 Ok(included_files) => {
579 self.panels_with_errors.remove(&InputPanel::Include);
580 included_files
581 }
582 Err(_e) => {
583 self.panels_with_errors.insert(InputPanel::Include);
584 cx.notify();
585 return None;
586 }
587 };
588 let excluded_files =
589 match Self::load_glob_set(&self.excluded_files_editor.read(cx).text(cx)) {
590 Ok(excluded_files) => {
591 self.panels_with_errors.remove(&InputPanel::Exclude);
592 excluded_files
593 }
594 Err(_e) => {
595 self.panels_with_errors.insert(InputPanel::Exclude);
596 cx.notify();
597 return None;
598 }
599 };
600 if self.regex {
601 match SearchQuery::regex(
602 text,
603 self.whole_word,
604 self.case_sensitive,
605 included_files,
606 excluded_files,
607 ) {
608 Ok(query) => {
609 self.panels_with_errors.remove(&InputPanel::Query);
610 Some(query)
611 }
612 Err(_e) => {
613 self.panels_with_errors.insert(InputPanel::Query);
614 cx.notify();
615 None
616 }
617 }
618 } else {
619 Some(SearchQuery::text(
620 text,
621 self.whole_word,
622 self.case_sensitive,
623 included_files,
624 excluded_files,
625 ))
626 }
627 }
628
629 fn load_glob_set(text: &str) -> Result<Vec<GlobMatcher>> {
630 text.split(',')
631 .map(str::trim)
632 .filter(|glob_str| !glob_str.is_empty())
633 .map(|glob_str| anyhow::Ok(Glob::new(glob_str)?.compile_matcher()))
634 .collect()
635 }
636
637 fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
638 if let Some(index) = self.active_match_index {
639 let match_ranges = self.model.read(cx).match_ranges.clone();
640 let new_index = self.results_editor.update(cx, |editor, cx| {
641 editor.match_index_for_direction(&match_ranges, index, direction, cx)
642 });
643
644 let range_to_select = match_ranges[new_index].clone();
645 self.results_editor.update(cx, |editor, cx| {
646 editor.unfold_ranges([range_to_select.clone()], false, true, cx);
647 editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
648 s.select_ranges([range_to_select])
649 });
650 });
651 }
652 }
653
654 fn focus_query_editor(&mut self, cx: &mut ViewContext<Self>) {
655 self.query_editor.update(cx, |query_editor, cx| {
656 query_editor.select_all(&SelectAll, cx);
657 });
658 self.query_editor_was_focused = true;
659 cx.focus(&self.query_editor);
660 }
661
662 fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
663 self.query_editor
664 .update(cx, |query_editor, cx| query_editor.set_text(query, cx));
665 }
666
667 fn focus_results_editor(&mut self, cx: &mut ViewContext<Self>) {
668 self.query_editor.update(cx, |query_editor, cx| {
669 let cursor = query_editor.selections.newest_anchor().head();
670 query_editor.change_selections(None, cx, |s| s.select_ranges([cursor.clone()..cursor]));
671 });
672 self.query_editor_was_focused = false;
673 cx.focus(&self.results_editor);
674 }
675
676 fn model_changed(&mut self, cx: &mut ViewContext<Self>) {
677 let match_ranges = self.model.read(cx).match_ranges.clone();
678 if match_ranges.is_empty() {
679 self.active_match_index = None;
680 } else {
681 let prev_search_id = mem::replace(&mut self.search_id, self.model.read(cx).search_id);
682 let is_new_search = self.search_id != prev_search_id;
683 self.results_editor.update(cx, |editor, cx| {
684 if is_new_search {
685 editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
686 s.select_ranges(match_ranges.first().cloned())
687 });
688 }
689 editor.highlight_background::<Self>(
690 match_ranges,
691 |theme| theme.search.match_background,
692 cx,
693 );
694 });
695 if is_new_search && self.query_editor.is_focused(cx) {
696 self.focus_results_editor(cx);
697 }
698 }
699
700 cx.emit(ViewEvent::UpdateTab);
701 cx.notify();
702 }
703
704 fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
705 let results_editor = self.results_editor.read(cx);
706 let new_index = active_match_index(
707 &self.model.read(cx).match_ranges,
708 &results_editor.selections.newest_anchor().head(),
709 &results_editor.buffer().read(cx).snapshot(cx),
710 );
711 if self.active_match_index != new_index {
712 self.active_match_index = new_index;
713 cx.notify();
714 }
715 }
716
717 pub fn has_matches(&self) -> bool {
718 self.active_match_index.is_some()
719 }
720}
721
722impl Default for ProjectSearchBar {
723 fn default() -> Self {
724 Self::new()
725 }
726}
727
728impl ProjectSearchBar {
729 pub fn new() -> Self {
730 Self {
731 active_project_search: Default::default(),
732 subscription: Default::default(),
733 }
734 }
735
736 fn search(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
737 if let Some(search_view) = self.active_project_search.as_ref() {
738 search_view.update(cx, |search_view, cx| search_view.search(cx));
739 }
740 }
741
742 fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
743 if let Some(search_view) = workspace
744 .active_item(cx)
745 .and_then(|item| item.downcast::<ProjectSearchView>())
746 {
747 let new_query = search_view.update(cx, |search_view, cx| {
748 let new_query = search_view.build_search_query(cx);
749 if new_query.is_some() {
750 if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
751 search_view.query_editor.update(cx, |editor, cx| {
752 editor.set_text(old_query.as_str(), cx);
753 });
754 search_view.regex = old_query.is_regex();
755 search_view.whole_word = old_query.whole_word();
756 search_view.case_sensitive = old_query.case_sensitive();
757 }
758 }
759 new_query
760 });
761 if let Some(new_query) = new_query {
762 let model = cx.add_model(|cx| {
763 let mut model = ProjectSearch::new(workspace.project().clone(), cx);
764 model.search(new_query, cx);
765 model
766 });
767 workspace.add_item(
768 Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
769 cx,
770 );
771 }
772 }
773 }
774
775 fn select_next_match(pane: &mut Pane, _: &SelectNextMatch, cx: &mut ViewContext<Pane>) {
776 if let Some(search_view) = pane
777 .active_item()
778 .and_then(|item| item.downcast::<ProjectSearchView>())
779 {
780 search_view.update(cx, |view, cx| view.select_match(Direction::Next, cx));
781 } else {
782 cx.propagate_action();
783 }
784 }
785
786 fn select_prev_match(pane: &mut Pane, _: &SelectPrevMatch, cx: &mut ViewContext<Pane>) {
787 if let Some(search_view) = pane
788 .active_item()
789 .and_then(|item| item.downcast::<ProjectSearchView>())
790 {
791 search_view.update(cx, |view, cx| view.select_match(Direction::Prev, cx));
792 } else {
793 cx.propagate_action();
794 }
795 }
796
797 fn toggle_focus(pane: &mut Pane, _: &ToggleFocus, cx: &mut ViewContext<Pane>) {
798 if let Some(search_view) = pane
799 .active_item()
800 .and_then(|item| item.downcast::<ProjectSearchView>())
801 {
802 search_view.update(cx, |search_view, cx| {
803 if search_view.query_editor.is_focused(cx) {
804 if !search_view.model.read(cx).match_ranges.is_empty() {
805 search_view.focus_results_editor(cx);
806 }
807 } else {
808 search_view.focus_query_editor(cx);
809 }
810 });
811 } else {
812 cx.propagate_action();
813 }
814 }
815
816 fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
817 self.cycle_field(Direction::Next, cx);
818 }
819
820 fn tab_previous(&mut self, _: &editor::TabPrev, cx: &mut ViewContext<Self>) {
821 self.cycle_field(Direction::Prev, cx);
822 }
823
824 fn cycle_field(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
825 let active_project_search = match &self.active_project_search {
826 Some(active_project_search) => active_project_search,
827
828 None => {
829 cx.propagate_action();
830 return;
831 }
832 };
833
834 active_project_search.update(cx, |project_view, cx| {
835 let views = &[
836 &project_view.query_editor,
837 &project_view.included_files_editor,
838 &project_view.excluded_files_editor,
839 ];
840
841 let current_index = match views
842 .iter()
843 .enumerate()
844 .find(|(_, view)| view.is_focused(cx))
845 {
846 Some((index, _)) => index,
847
848 None => {
849 cx.propagate_action();
850 return;
851 }
852 };
853
854 let new_index = match direction {
855 Direction::Next => (current_index + 1) % views.len(),
856 Direction::Prev if current_index == 0 => views.len() - 1,
857 Direction::Prev => (current_index - 1) % views.len(),
858 };
859 cx.focus(views[new_index]);
860 });
861 }
862
863 fn toggle_search_option(&mut self, option: SearchOption, cx: &mut ViewContext<Self>) -> bool {
864 if let Some(search_view) = self.active_project_search.as_ref() {
865 search_view.update(cx, |search_view, cx| {
866 let value = match option {
867 SearchOption::WholeWord => &mut search_view.whole_word,
868 SearchOption::CaseSensitive => &mut search_view.case_sensitive,
869 SearchOption::Regex => &mut search_view.regex,
870 };
871 *value = !*value;
872 search_view.search(cx);
873 });
874 cx.notify();
875 true
876 } else {
877 false
878 }
879 }
880
881 fn render_nav_button(
882 &self,
883 icon: &'static str,
884 direction: Direction,
885 cx: &mut ViewContext<Self>,
886 ) -> AnyElement<Self> {
887 let action: Box<dyn Action>;
888 let tooltip;
889 match direction {
890 Direction::Prev => {
891 action = Box::new(SelectPrevMatch);
892 tooltip = "Select Previous Match";
893 }
894 Direction::Next => {
895 action = Box::new(SelectNextMatch);
896 tooltip = "Select Next Match";
897 }
898 };
899 let tooltip_style = theme::current(cx).tooltip.clone();
900
901 enum NavButton {}
902 MouseEventHandler::<NavButton, _>::new(direction as usize, cx, |state, cx| {
903 let theme = theme::current(cx);
904 let style = theme.search.option_button.style_for(state, false);
905 Label::new(icon, style.text.clone())
906 .contained()
907 .with_style(style.container)
908 })
909 .on_click(MouseButton::Left, move |_, this, cx| {
910 if let Some(search) = this.active_project_search.as_ref() {
911 search.update(cx, |search, cx| search.select_match(direction, cx));
912 }
913 })
914 .with_cursor_style(CursorStyle::PointingHand)
915 .with_tooltip::<NavButton>(
916 direction as usize,
917 tooltip.to_string(),
918 Some(action),
919 tooltip_style,
920 cx,
921 )
922 .into_any()
923 }
924
925 fn render_option_button(
926 &self,
927 icon: &'static str,
928 option: SearchOption,
929 cx: &mut ViewContext<Self>,
930 ) -> AnyElement<Self> {
931 let tooltip_style = theme::current(cx).tooltip.clone();
932 let is_active = self.is_option_enabled(option, cx);
933 MouseEventHandler::<Self, _>::new(option as usize, cx, |state, cx| {
934 let theme = theme::current(cx);
935 let style = theme.search.option_button.style_for(state, is_active);
936 Label::new(icon, style.text.clone())
937 .contained()
938 .with_style(style.container)
939 })
940 .on_click(MouseButton::Left, move |_, this, cx| {
941 this.toggle_search_option(option, cx);
942 })
943 .with_cursor_style(CursorStyle::PointingHand)
944 .with_tooltip::<Self>(
945 option as usize,
946 format!("Toggle {}", option.label()),
947 Some(option.to_toggle_action()),
948 tooltip_style,
949 cx,
950 )
951 .into_any()
952 }
953
954 fn is_option_enabled(&self, option: SearchOption, cx: &AppContext) -> bool {
955 if let Some(search) = self.active_project_search.as_ref() {
956 let search = search.read(cx);
957 match option {
958 SearchOption::WholeWord => search.whole_word,
959 SearchOption::CaseSensitive => search.case_sensitive,
960 SearchOption::Regex => search.regex,
961 }
962 } else {
963 false
964 }
965 }
966}
967
968impl Entity for ProjectSearchBar {
969 type Event = ();
970}
971
972impl View for ProjectSearchBar {
973 fn ui_name() -> &'static str {
974 "ProjectSearchBar"
975 }
976
977 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
978 if let Some(search) = self.active_project_search.as_ref() {
979 let search = search.read(cx);
980 let theme = theme::current(cx).clone();
981 let query_container_style = if search.panels_with_errors.contains(&InputPanel::Query) {
982 theme.search.invalid_editor
983 } else {
984 theme.search.editor.input.container
985 };
986 let include_container_style =
987 if search.panels_with_errors.contains(&InputPanel::Include) {
988 theme.search.invalid_include_exclude_editor
989 } else {
990 theme.search.include_exclude_editor.input.container
991 };
992 let exclude_container_style =
993 if search.panels_with_errors.contains(&InputPanel::Exclude) {
994 theme.search.invalid_include_exclude_editor
995 } else {
996 theme.search.include_exclude_editor.input.container
997 };
998
999 let included_files_view = ChildView::new(&search.included_files_editor, cx)
1000 .aligned()
1001 .left()
1002 .flex(1.0, true);
1003 let excluded_files_view = ChildView::new(&search.excluded_files_editor, cx)
1004 .aligned()
1005 .right()
1006 .flex(1.0, true);
1007
1008 let row_spacing = theme.workspace.toolbar.container.padding.bottom;
1009
1010 Flex::column()
1011 .with_child(
1012 Flex::row()
1013 .with_child(
1014 Flex::row()
1015 .with_child(
1016 ChildView::new(&search.query_editor, cx)
1017 .aligned()
1018 .left()
1019 .flex(1., true),
1020 )
1021 .with_children(search.active_match_index.map(|match_ix| {
1022 Label::new(
1023 format!(
1024 "{}/{}",
1025 match_ix + 1,
1026 search.model.read(cx).match_ranges.len()
1027 ),
1028 theme.search.match_index.text.clone(),
1029 )
1030 .contained()
1031 .with_style(theme.search.match_index.container)
1032 .aligned()
1033 }))
1034 .contained()
1035 .with_style(query_container_style)
1036 .aligned()
1037 .constrained()
1038 .with_min_width(theme.search.editor.min_width)
1039 .with_max_width(theme.search.editor.max_width)
1040 .flex(1., false),
1041 )
1042 .with_child(
1043 Flex::row()
1044 .with_child(self.render_nav_button("<", Direction::Prev, cx))
1045 .with_child(self.render_nav_button(">", Direction::Next, cx))
1046 .aligned(),
1047 )
1048 .with_child(
1049 Flex::row()
1050 .with_child(self.render_option_button(
1051 "Case",
1052 SearchOption::CaseSensitive,
1053 cx,
1054 ))
1055 .with_child(self.render_option_button(
1056 "Word",
1057 SearchOption::WholeWord,
1058 cx,
1059 ))
1060 .with_child(self.render_option_button(
1061 "Regex",
1062 SearchOption::Regex,
1063 cx,
1064 ))
1065 .contained()
1066 .with_style(theme.search.option_button_group)
1067 .aligned(),
1068 )
1069 .contained()
1070 .with_margin_bottom(row_spacing),
1071 )
1072 .with_child(
1073 Flex::row()
1074 .with_child(
1075 Flex::row()
1076 .with_child(included_files_view)
1077 .contained()
1078 .with_style(include_container_style)
1079 .aligned()
1080 .constrained()
1081 .with_min_width(theme.search.include_exclude_editor.min_width)
1082 .with_max_width(theme.search.include_exclude_editor.max_width)
1083 .flex(1., false),
1084 )
1085 .with_child(
1086 Flex::row()
1087 .with_child(excluded_files_view)
1088 .contained()
1089 .with_style(exclude_container_style)
1090 .aligned()
1091 .constrained()
1092 .with_min_width(theme.search.include_exclude_editor.min_width)
1093 .with_max_width(theme.search.include_exclude_editor.max_width)
1094 .flex(1., false),
1095 ),
1096 )
1097 .contained()
1098 .with_style(theme.search.container)
1099 .aligned()
1100 .left()
1101 .into_any_named("project search")
1102 } else {
1103 Empty::new().into_any()
1104 }
1105 }
1106}
1107
1108impl ToolbarItemView for ProjectSearchBar {
1109 fn set_active_pane_item(
1110 &mut self,
1111 active_pane_item: Option<&dyn ItemHandle>,
1112 cx: &mut ViewContext<Self>,
1113 ) -> ToolbarItemLocation {
1114 cx.notify();
1115 self.subscription = None;
1116 self.active_project_search = None;
1117 if let Some(search) = active_pane_item.and_then(|i| i.downcast::<ProjectSearchView>()) {
1118 self.subscription = Some(cx.observe(&search, |_, _, cx| cx.notify()));
1119 self.active_project_search = Some(search);
1120 ToolbarItemLocation::PrimaryLeft {
1121 flex: Some((1., false)),
1122 }
1123 } else {
1124 ToolbarItemLocation::Hidden
1125 }
1126 }
1127
1128 fn row_count(&self) -> usize {
1129 2
1130 }
1131}
1132
1133#[cfg(test)]
1134pub mod tests {
1135 use super::*;
1136 use editor::DisplayPoint;
1137 use gpui::{color::Color, executor::Deterministic, TestAppContext};
1138 use project::FakeFs;
1139 use serde_json::json;
1140 use settings::SettingsStore;
1141 use std::sync::Arc;
1142 use theme::ThemeSettings;
1143
1144 #[gpui::test]
1145 async fn test_project_search(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
1146 init_test(cx);
1147
1148 let fs = FakeFs::new(cx.background());
1149 fs.insert_tree(
1150 "/dir",
1151 json!({
1152 "one.rs": "const ONE: usize = 1;",
1153 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
1154 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
1155 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
1156 }),
1157 )
1158 .await;
1159 let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
1160 let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
1161 let (_, search_view) = cx.add_window(|cx| ProjectSearchView::new(search.clone(), cx));
1162
1163 search_view.update(cx, |search_view, cx| {
1164 search_view
1165 .query_editor
1166 .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
1167 search_view.search(cx);
1168 });
1169 deterministic.run_until_parked();
1170 search_view.update(cx, |search_view, cx| {
1171 assert_eq!(
1172 search_view
1173 .results_editor
1174 .update(cx, |editor, cx| editor.display_text(cx)),
1175 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
1176 );
1177 assert_eq!(
1178 search_view
1179 .results_editor
1180 .update(cx, |editor, cx| editor.all_background_highlights(cx)),
1181 &[
1182 (
1183 DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
1184 Color::red()
1185 ),
1186 (
1187 DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
1188 Color::red()
1189 ),
1190 (
1191 DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
1192 Color::red()
1193 )
1194 ]
1195 );
1196 assert_eq!(search_view.active_match_index, Some(0));
1197 assert_eq!(
1198 search_view
1199 .results_editor
1200 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1201 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1202 );
1203
1204 search_view.select_match(Direction::Next, cx);
1205 });
1206
1207 search_view.update(cx, |search_view, cx| {
1208 assert_eq!(search_view.active_match_index, Some(1));
1209 assert_eq!(
1210 search_view
1211 .results_editor
1212 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1213 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1214 );
1215 search_view.select_match(Direction::Next, cx);
1216 });
1217
1218 search_view.update(cx, |search_view, cx| {
1219 assert_eq!(search_view.active_match_index, Some(2));
1220 assert_eq!(
1221 search_view
1222 .results_editor
1223 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1224 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1225 );
1226 search_view.select_match(Direction::Next, cx);
1227 });
1228
1229 search_view.update(cx, |search_view, cx| {
1230 assert_eq!(search_view.active_match_index, Some(0));
1231 assert_eq!(
1232 search_view
1233 .results_editor
1234 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1235 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
1236 );
1237 search_view.select_match(Direction::Prev, cx);
1238 });
1239
1240 search_view.update(cx, |search_view, cx| {
1241 assert_eq!(search_view.active_match_index, Some(2));
1242 assert_eq!(
1243 search_view
1244 .results_editor
1245 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1246 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
1247 );
1248 search_view.select_match(Direction::Prev, cx);
1249 });
1250
1251 search_view.update(cx, |search_view, cx| {
1252 assert_eq!(search_view.active_match_index, Some(1));
1253 assert_eq!(
1254 search_view
1255 .results_editor
1256 .update(cx, |editor, cx| editor.selections.display_ranges(cx)),
1257 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
1258 );
1259 });
1260 }
1261
1262 pub fn init_test(cx: &mut TestAppContext) {
1263 let fonts = cx.font_cache();
1264 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), theme::Theme::default);
1265 theme.search.match_background = Color::red();
1266
1267 cx.update(|cx| {
1268 cx.set_global(SettingsStore::test(cx));
1269 cx.set_global(ActiveSearches::default());
1270
1271 theme::init((), cx);
1272 cx.update_global::<SettingsStore, _, _>(|store, _| {
1273 let mut settings = store.get::<ThemeSettings>(None).clone();
1274 settings.theme = Arc::new(theme);
1275 store.override_global(settings)
1276 });
1277
1278 language::init(cx);
1279 client::init_settings(cx);
1280 editor::init_settings(cx);
1281 workspace::init_settings(cx);
1282 });
1283 }
1284}