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