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, Task, View, ViewContext,
 10    ViewHandle, WeakModelHandle, WeakViewHandle,
 11};
 12use project::{search::SearchQuery, Project};
 13use std::{
 14    any::{Any, TypeId},
 15    ops::Range,
 16    path::PathBuf,
 17};
 18use util::ResultExt as _;
 19use workspace::{Item, ItemNavHistory, Settings, Workspace};
 20
 21action!(Deploy);
 22action!(Search);
 23action!(SearchInNew);
 24action!(ToggleFocus);
 25
 26const MAX_TAB_TITLE_LEN: usize = 24;
 27
 28#[derive(Default)]
 29struct ActiveSearches(HashMap<WeakModelHandle<Project>, WeakViewHandle<ProjectSearchView>>);
 30
 31pub fn init(cx: &mut MutableAppContext) {
 32    cx.set_global(ActiveSearches::default());
 33    cx.add_bindings([
 34        Binding::new("cmd-shift-F", ToggleFocus, Some("ProjectSearchView")),
 35        Binding::new("cmd-f", ToggleFocus, Some("ProjectSearchView")),
 36        Binding::new("cmd-shift-F", Deploy, Some("Workspace")),
 37        Binding::new("enter", Search, Some("ProjectSearchView")),
 38        Binding::new("cmd-enter", SearchInNew, Some("ProjectSearchView")),
 39        Binding::new(
 40            "cmd-g",
 41            SelectMatch(Direction::Next),
 42            Some("ProjectSearchView"),
 43        ),
 44        Binding::new(
 45            "cmd-shift-G",
 46            SelectMatch(Direction::Prev),
 47            Some("ProjectSearchView"),
 48        ),
 49    ]);
 50    cx.add_action(ProjectSearchView::deploy);
 51    cx.add_action(ProjectSearchView::search);
 52    cx.add_action(ProjectSearchView::search_in_new);
 53    cx.add_action(ProjectSearchView::toggle_search_option);
 54    cx.add_action(ProjectSearchView::select_match);
 55    cx.add_action(ProjectSearchView::toggle_focus);
 56    cx.capture_action(ProjectSearchView::tab);
 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
 67struct 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}
 77
 78impl Entity for ProjectSearch {
 79    type Event = ();
 80}
 81
 82impl ProjectSearch {
 83    fn new(project: ModelHandle<Project>, cx: &mut ModelContext<Self>) -> Self {
 84        let replica_id = project.read(cx).replica_id();
 85        Self {
 86            project,
 87            excerpts: cx.add_model(|_| MultiBuffer::new(replica_id)),
 88            pending_search: Default::default(),
 89            match_ranges: Default::default(),
 90            active_query: None,
 91        }
 92    }
 93
 94    fn clone(&self, cx: &mut ModelContext<Self>) -> ModelHandle<Self> {
 95        cx.add_model(|cx| Self {
 96            project: self.project.clone(),
 97            excerpts: self
 98                .excerpts
 99                .update(cx, |excerpts, cx| cx.add_model(|cx| excerpts.clone(cx))),
100            pending_search: Default::default(),
101            match_ranges: self.match_ranges.clone(),
102            active_query: self.active_query.clone(),
103        })
104    }
105
106    fn search(&mut self, query: SearchQuery, cx: &mut ModelContext<Self>) {
107        let search = self
108            .project
109            .update(cx, |project, cx| project.search(query.clone(), cx));
110        self.active_query = Some(query);
111        self.match_ranges.clear();
112        self.pending_search = Some(cx.spawn_weak(|this, mut cx| async move {
113            let matches = search.await.log_err()?;
114            if let Some(this) = this.upgrade(&cx) {
115                this.update(&mut cx, |this, cx| {
116                    this.match_ranges.clear();
117                    let mut matches = matches.into_iter().collect::<Vec<_>>();
118                    matches
119                        .sort_by_key(|(buffer, _)| buffer.read(cx).file().map(|file| file.path()));
120                    this.excerpts.update(cx, |excerpts, cx| {
121                        excerpts.clear(cx);
122                        for (buffer, buffer_matches) in matches {
123                            let ranges_to_highlight = excerpts.push_excerpts_with_context_lines(
124                                buffer,
125                                buffer_matches.clone(),
126                                1,
127                                cx,
128                            );
129                            this.match_ranges.extend(ranges_to_highlight);
130                        }
131                    });
132                    this.pending_search.take();
133                    cx.notify();
134                });
135            }
136            None
137        }));
138        cx.notify();
139    }
140}
141
142enum ViewEvent {
143    UpdateTab,
144}
145
146impl Entity for ProjectSearchView {
147    type Event = ViewEvent;
148}
149
150impl View for ProjectSearchView {
151    fn ui_name() -> &'static str {
152        "ProjectSearchView"
153    }
154
155    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
156        let model = &self.model.read(cx);
157        let results = if model.match_ranges.is_empty() {
158            let theme = &cx.global::<Settings>().theme;
159            let text = if self.query_editor.read(cx).text(cx).is_empty() {
160                ""
161            } else if model.pending_search.is_some() {
162                "Searching..."
163            } else {
164                "No results"
165            };
166            Label::new(text.to_string(), theme.search.results_status.clone())
167                .aligned()
168                .contained()
169                .with_background_color(theme.editor.background)
170                .flexible(1., true)
171                .boxed()
172        } else {
173            ChildView::new(&self.results_editor)
174                .flexible(1., true)
175                .boxed()
176        };
177
178        Flex::column()
179            .with_child(self.render_query_editor(cx))
180            .with_child(results)
181            .boxed()
182    }
183
184    fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
185        let handle = cx.weak_handle();
186        cx.update_global(|state: &mut ActiveSearches, cx| {
187            state
188                .0
189                .insert(self.model.read(cx).project.downgrade(), handle)
190        });
191
192        if self.model.read(cx).match_ranges.is_empty() {
193            cx.focus(&self.query_editor);
194        } else {
195            self.focus_results_editor(cx);
196        }
197    }
198}
199
200impl Item for ProjectSearchView {
201    fn act_as_type(
202        &self,
203        type_id: TypeId,
204        self_handle: &ViewHandle<Self>,
205        _: &gpui::AppContext,
206    ) -> Option<gpui::AnyViewHandle> {
207        if type_id == TypeId::of::<Self>() {
208            Some(self_handle.into())
209        } else if type_id == TypeId::of::<Editor>() {
210            Some((&self.results_editor).into())
211        } else {
212            None
213        }
214    }
215
216    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
217        self.results_editor
218            .update(cx, |editor, cx| editor.deactivated(cx));
219    }
220
221    fn tab_content(&self, tab_theme: &theme::Tab, cx: &gpui::AppContext) -> ElementBox {
222        let settings = cx.global::<Settings>();
223        let search_theme = &settings.theme.search;
224        Flex::row()
225            .with_child(
226                Svg::new("icons/magnifier.svg")
227                    .with_color(tab_theme.label.text.color)
228                    .constrained()
229                    .with_width(search_theme.tab_icon_width)
230                    .aligned()
231                    .boxed(),
232            )
233            .with_children(self.model.read(cx).active_query.as_ref().map(|query| {
234                let query_text = if query.as_str().len() > MAX_TAB_TITLE_LEN {
235                    query.as_str()[..MAX_TAB_TITLE_LEN].to_string() + ""
236                } else {
237                    query.as_str().to_string()
238                };
239
240                Label::new(query_text, tab_theme.label.clone())
241                    .aligned()
242                    .contained()
243                    .with_margin_left(search_theme.tab_icon_spacing)
244                    .boxed()
245            }))
246            .boxed()
247    }
248
249    fn project_path(&self, _: &gpui::AppContext) -> Option<project::ProjectPath> {
250        None
251    }
252
253    fn project_entry_id(&self, _: &AppContext) -> Option<project::ProjectEntryId> {
254        None
255    }
256
257    fn can_save(&self, _: &gpui::AppContext) -> bool {
258        true
259    }
260
261    fn is_dirty(&self, cx: &AppContext) -> bool {
262        self.results_editor.read(cx).is_dirty(cx)
263    }
264
265    fn has_conflict(&self, cx: &AppContext) -> bool {
266        self.results_editor.read(cx).has_conflict(cx)
267    }
268
269    fn save(
270        &mut self,
271        project: ModelHandle<Project>,
272        cx: &mut ViewContext<Self>,
273    ) -> Task<anyhow::Result<()>> {
274        self.results_editor
275            .update(cx, |editor, cx| editor.save(project, cx))
276    }
277
278    fn can_save_as(&self, _: &gpui::AppContext) -> bool {
279        false
280    }
281
282    fn save_as(
283        &mut self,
284        _: ModelHandle<Project>,
285        _: PathBuf,
286        _: &mut ViewContext<Self>,
287    ) -> Task<anyhow::Result<()>> {
288        unreachable!("save_as should not have been called")
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, _: &Search, 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 search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
412        if let Some(search_view) = workspace
413            .active_item(cx)
414            .and_then(|item| item.downcast::<ProjectSearchView>())
415        {
416            let new_query = search_view.update(cx, |search_view, cx| {
417                let new_query = search_view.build_search_query(cx);
418                if new_query.is_some() {
419                    if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
420                        search_view.query_editor.update(cx, |editor, cx| {
421                            editor.set_text(old_query.as_str(), cx);
422                        });
423                        search_view.regex = old_query.is_regex();
424                        search_view.whole_word = old_query.whole_word();
425                        search_view.case_sensitive = old_query.case_sensitive();
426                    }
427                }
428                new_query
429            });
430            if let Some(new_query) = new_query {
431                let model = cx.add_model(|cx| {
432                    let mut model = ProjectSearch::new(workspace.project().clone(), cx);
433                    model.search(new_query, cx);
434                    model
435                });
436                workspace.add_item(
437                    Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
438                    cx,
439                );
440            }
441        }
442    }
443
444    fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
445        let text = self.query_editor.read(cx).text(cx);
446        if self.regex {
447            match SearchQuery::regex(text, self.whole_word, self.case_sensitive) {
448                Ok(query) => Some(query),
449                Err(_) => {
450                    self.query_contains_error = true;
451                    cx.notify();
452                    None
453                }
454            }
455        } else {
456            Some(SearchQuery::text(
457                text,
458                self.whole_word,
459                self.case_sensitive,
460            ))
461        }
462    }
463
464    fn toggle_search_option(
465        &mut self,
466        ToggleSearchOption(option): &ToggleSearchOption,
467        cx: &mut ViewContext<Self>,
468    ) {
469        let value = match option {
470            SearchOption::WholeWord => &mut self.whole_word,
471            SearchOption::CaseSensitive => &mut self.case_sensitive,
472            SearchOption::Regex => &mut self.regex,
473        };
474        *value = !*value;
475        self.search(&Search, cx);
476        cx.notify();
477    }
478
479    fn select_match(&mut self, &SelectMatch(direction): &SelectMatch, cx: &mut ViewContext<Self>) {
480        if let Some(index) = self.active_match_index {
481            let model = self.model.read(cx);
482            let results_editor = self.results_editor.read(cx);
483            let new_index = match_index_for_direction(
484                &model.match_ranges,
485                &results_editor.newest_anchor_selection().head(),
486                index,
487                direction,
488                &results_editor.buffer().read(cx).read(cx),
489            );
490            let range_to_select = model.match_ranges[new_index].clone();
491            self.results_editor.update(cx, |editor, cx| {
492                editor.unfold_ranges([range_to_select.clone()], false, cx);
493                editor.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
494            });
495        }
496    }
497
498    fn toggle_focus(&mut self, _: &ToggleFocus, cx: &mut ViewContext<Self>) {
499        if self.query_editor.is_focused(cx) {
500            if !self.model.read(cx).match_ranges.is_empty() {
501                self.focus_results_editor(cx);
502            }
503        } else {
504            self.focus_query_editor(cx);
505        }
506    }
507
508    fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
509        if self.query_editor.is_focused(cx) {
510            if !self.model.read(cx).match_ranges.is_empty() {
511                self.focus_results_editor(cx);
512            }
513        } else {
514            cx.propagate_action()
515        }
516    }
517
518    fn focus_query_editor(&self, cx: &mut ViewContext<Self>) {
519        self.query_editor.update(cx, |query_editor, cx| {
520            query_editor.select_all(&SelectAll, cx);
521        });
522        cx.focus(&self.query_editor);
523    }
524
525    fn focus_results_editor(&self, cx: &mut ViewContext<Self>) {
526        self.query_editor.update(cx, |query_editor, cx| {
527            let cursor = query_editor.newest_anchor_selection().head();
528            query_editor.select_ranges([cursor.clone()..cursor], None, cx);
529        });
530        cx.focus(&self.results_editor);
531    }
532
533    fn model_changed(&mut self, reset_selections: bool, cx: &mut ViewContext<Self>) {
534        let match_ranges = self.model.read(cx).match_ranges.clone();
535        if match_ranges.is_empty() {
536            self.active_match_index = None;
537        } else {
538            self.results_editor.update(cx, |editor, cx| {
539                if reset_selections {
540                    editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx);
541                }
542                let theme = &cx.global::<Settings>().theme.search;
543                editor.highlight_background::<Self>(match_ranges, theme.match_background, cx);
544            });
545            if self.query_editor.is_focused(cx) {
546                self.focus_results_editor(cx);
547            }
548        }
549
550        cx.emit(ViewEvent::UpdateTab);
551        cx.notify();
552    }
553
554    fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
555        let results_editor = self.results_editor.read(cx);
556        let new_index = active_match_index(
557            &self.model.read(cx).match_ranges,
558            &results_editor.newest_anchor_selection().head(),
559            &results_editor.buffer().read(cx).read(cx),
560        );
561        if self.active_match_index != new_index {
562            self.active_match_index = new_index;
563            cx.notify();
564        }
565    }
566
567    fn render_query_editor(&self, cx: &mut RenderContext<Self>) -> ElementBox {
568        let theme = cx.global::<Settings>().theme.clone();
569        let editor_container = if self.query_contains_error {
570            theme.search.invalid_editor
571        } else {
572            theme.search.editor.input.container
573        };
574        Flex::row()
575            .with_child(
576                ChildView::new(&self.query_editor)
577                    .contained()
578                    .with_style(editor_container)
579                    .aligned()
580                    .constrained()
581                    .with_max_width(theme.search.editor.max_width)
582                    .boxed(),
583            )
584            .with_child(
585                Flex::row()
586                    .with_child(self.render_option_button("Case", SearchOption::CaseSensitive, cx))
587                    .with_child(self.render_option_button("Word", SearchOption::WholeWord, cx))
588                    .with_child(self.render_option_button("Regex", SearchOption::Regex, cx))
589                    .contained()
590                    .with_style(theme.search.option_button_group)
591                    .aligned()
592                    .boxed(),
593            )
594            .with_children({
595                self.active_match_index.into_iter().flat_map(|match_ix| {
596                    [
597                        Flex::row()
598                            .with_child(self.render_nav_button("<", Direction::Prev, cx))
599                            .with_child(self.render_nav_button(">", Direction::Next, cx))
600                            .aligned()
601                            .boxed(),
602                        Label::new(
603                            format!(
604                                "{}/{}",
605                                match_ix + 1,
606                                self.model.read(cx).match_ranges.len()
607                            ),
608                            theme.search.match_index.text.clone(),
609                        )
610                        .contained()
611                        .with_style(theme.search.match_index.container)
612                        .aligned()
613                        .boxed(),
614                    ]
615                })
616            })
617            .contained()
618            .with_style(theme.search.container)
619            .constrained()
620            .with_height(theme.workspace.toolbar.height)
621            .named("project search")
622    }
623
624    fn render_option_button(
625        &self,
626        icon: &str,
627        option: SearchOption,
628        cx: &mut RenderContext<Self>,
629    ) -> ElementBox {
630        let is_active = self.is_option_enabled(option);
631        MouseEventHandler::new::<Self, _, _>(option as usize, cx, |state, cx| {
632            let theme = &cx.global::<Settings>().theme.search;
633            let style = match (is_active, state.hovered) {
634                (false, false) => &theme.option_button,
635                (false, true) => &theme.hovered_option_button,
636                (true, false) => &theme.active_option_button,
637                (true, true) => &theme.active_hovered_option_button,
638            };
639            Label::new(icon.to_string(), style.text.clone())
640                .contained()
641                .with_style(style.container)
642                .boxed()
643        })
644        .on_click(move |cx| cx.dispatch_action(ToggleSearchOption(option)))
645        .with_cursor_style(CursorStyle::PointingHand)
646        .boxed()
647    }
648
649    fn is_option_enabled(&self, option: SearchOption) -> bool {
650        match option {
651            SearchOption::WholeWord => self.whole_word,
652            SearchOption::CaseSensitive => self.case_sensitive,
653            SearchOption::Regex => self.regex,
654        }
655    }
656
657    fn render_nav_button(
658        &self,
659        icon: &str,
660        direction: Direction,
661        cx: &mut RenderContext<Self>,
662    ) -> ElementBox {
663        enum NavButton {}
664        MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, cx| {
665            let theme = &cx.global::<Settings>().theme.search;
666            let style = if state.hovered {
667                &theme.hovered_option_button
668            } else {
669                &theme.option_button
670            };
671            Label::new(icon.to_string(), style.text.clone())
672                .contained()
673                .with_style(style.container)
674                .boxed()
675        })
676        .on_click(move |cx| cx.dispatch_action(SelectMatch(direction)))
677        .with_cursor_style(CursorStyle::PointingHand)
678        .boxed()
679    }
680}
681
682#[cfg(test)]
683mod tests {
684    use super::*;
685    use editor::DisplayPoint;
686    use gpui::{color::Color, TestAppContext};
687    use project::FakeFs;
688    use serde_json::json;
689    use std::sync::Arc;
690
691    #[gpui::test]
692    async fn test_project_search(cx: &mut TestAppContext) {
693        let fonts = cx.font_cache();
694        let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
695        theme.search.match_background = Color::red();
696        let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
697        cx.update(|cx| cx.set_global(settings));
698
699        let fs = FakeFs::new(cx.background());
700        fs.insert_tree(
701            "/dir",
702            json!({
703                "one.rs": "const ONE: usize = 1;",
704                "two.rs": "const TWO: usize = one::ONE + one::ONE;",
705                "three.rs": "const THREE: usize = one::ONE + two::TWO;",
706                "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
707            }),
708        )
709        .await;
710        let project = Project::test(fs.clone(), cx);
711        let (tree, _) = project
712            .update(cx, |project, cx| {
713                project.find_or_create_local_worktree("/dir", true, cx)
714            })
715            .await
716            .unwrap();
717        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
718            .await;
719
720        let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
721        let search_view = cx.add_view(Default::default(), |cx| {
722            ProjectSearchView::new(search.clone(), cx)
723        });
724
725        search_view.update(cx, |search_view, cx| {
726            search_view
727                .query_editor
728                .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
729            search_view.search(&Search, cx);
730        });
731        search_view.next_notification(&cx).await;
732        search_view.update(cx, |search_view, cx| {
733            assert_eq!(
734                search_view
735                    .results_editor
736                    .update(cx, |editor, cx| editor.display_text(cx)),
737                "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
738            );
739            assert_eq!(
740                search_view
741                    .results_editor
742                    .update(cx, |editor, cx| editor.all_background_highlights(cx)),
743                &[
744                    (
745                        DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
746                        Color::red()
747                    ),
748                    (
749                        DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
750                        Color::red()
751                    ),
752                    (
753                        DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
754                        Color::red()
755                    )
756                ]
757            );
758            assert_eq!(search_view.active_match_index, Some(0));
759            assert_eq!(
760                search_view
761                    .results_editor
762                    .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
763                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
764            );
765
766            search_view.select_match(&SelectMatch(Direction::Next), cx);
767        });
768
769        search_view.update(cx, |search_view, cx| {
770            assert_eq!(search_view.active_match_index, Some(1));
771            assert_eq!(
772                search_view
773                    .results_editor
774                    .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
775                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
776            );
777            search_view.select_match(&SelectMatch(Direction::Next), cx);
778        });
779
780        search_view.update(cx, |search_view, cx| {
781            assert_eq!(search_view.active_match_index, Some(2));
782            assert_eq!(
783                search_view
784                    .results_editor
785                    .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
786                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
787            );
788            search_view.select_match(&SelectMatch(Direction::Next), cx);
789        });
790
791        search_view.update(cx, |search_view, cx| {
792            assert_eq!(search_view.active_match_index, Some(0));
793            assert_eq!(
794                search_view
795                    .results_editor
796                    .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
797                [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
798            );
799            search_view.select_match(&SelectMatch(Direction::Prev), cx);
800        });
801
802        search_view.update(cx, |search_view, cx| {
803            assert_eq!(search_view.active_match_index, Some(2));
804            assert_eq!(
805                search_view
806                    .results_editor
807                    .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
808                [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
809            );
810            search_view.select_match(&SelectMatch(Direction::Prev), cx);
811        });
812
813        search_view.update(cx, |search_view, cx| {
814            assert_eq!(search_view.active_match_index, Some(1));
815            assert_eq!(
816                search_view
817                    .results_editor
818                    .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
819                [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
820            );
821        });
822    }
823}