project_search.rs

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