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.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
493 });
494 }
495 }
496
497 fn toggle_focus(&mut self, _: &ToggleFocus, cx: &mut ViewContext<Self>) {
498 if self.query_editor.is_focused(cx) {
499 if !self.model.read(cx).match_ranges.is_empty() {
500 self.focus_results_editor(cx);
501 }
502 } else {
503 self.focus_query_editor(cx);
504 }
505 }
506
507 fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
508 if self.query_editor.is_focused(cx) {
509 if !self.model.read(cx).match_ranges.is_empty() {
510 self.focus_results_editor(cx);
511 }
512 } else {
513 cx.propagate_action()
514 }
515 }
516
517 fn focus_query_editor(&self, cx: &mut ViewContext<Self>) {
518 self.query_editor.update(cx, |query_editor, cx| {
519 query_editor.select_all(&SelectAll, cx);
520 });
521 cx.focus(&self.query_editor);
522 }
523
524 fn focus_results_editor(&self, cx: &mut ViewContext<Self>) {
525 self.query_editor.update(cx, |query_editor, cx| {
526 let cursor = query_editor.newest_anchor_selection().head();
527 query_editor.select_ranges([cursor.clone()..cursor], None, cx);
528 });
529 cx.focus(&self.results_editor);
530 }
531
532 fn model_changed(&mut self, reset_selections: bool, cx: &mut ViewContext<Self>) {
533 let match_ranges = self.model.read(cx).match_ranges.clone();
534 if match_ranges.is_empty() {
535 self.active_match_index = None;
536 } else {
537 self.results_editor.update(cx, |editor, cx| {
538 if reset_selections {
539 editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx);
540 }
541 let theme = &cx.global::<Settings>().theme.search;
542 editor.highlight_background::<Self>(match_ranges, theme.match_background, cx);
543 });
544 if self.query_editor.is_focused(cx) {
545 self.focus_results_editor(cx);
546 }
547 }
548
549 cx.emit(ViewEvent::UpdateTab);
550 cx.notify();
551 }
552
553 fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
554 let results_editor = self.results_editor.read(cx);
555 let new_index = active_match_index(
556 &self.model.read(cx).match_ranges,
557 &results_editor.newest_anchor_selection().head(),
558 &results_editor.buffer().read(cx).read(cx),
559 );
560 if self.active_match_index != new_index {
561 self.active_match_index = new_index;
562 cx.notify();
563 }
564 }
565
566 fn render_query_editor(&self, cx: &mut RenderContext<Self>) -> ElementBox {
567 let theme = cx.global::<Settings>().theme.clone();
568 let editor_container = if self.query_contains_error {
569 theme.search.invalid_editor
570 } else {
571 theme.search.editor.input.container
572 };
573 Flex::row()
574 .with_child(
575 ChildView::new(&self.query_editor)
576 .contained()
577 .with_style(editor_container)
578 .aligned()
579 .constrained()
580 .with_max_width(theme.search.editor.max_width)
581 .boxed(),
582 )
583 .with_child(
584 Flex::row()
585 .with_child(self.render_option_button("Case", SearchOption::CaseSensitive, cx))
586 .with_child(self.render_option_button("Word", SearchOption::WholeWord, cx))
587 .with_child(self.render_option_button("Regex", SearchOption::Regex, cx))
588 .contained()
589 .with_style(theme.search.option_button_group)
590 .aligned()
591 .boxed(),
592 )
593 .with_children({
594 self.active_match_index.into_iter().flat_map(|match_ix| {
595 [
596 Flex::row()
597 .with_child(self.render_nav_button("<", Direction::Prev, cx))
598 .with_child(self.render_nav_button(">", Direction::Next, cx))
599 .aligned()
600 .boxed(),
601 Label::new(
602 format!(
603 "{}/{}",
604 match_ix + 1,
605 self.model.read(cx).match_ranges.len()
606 ),
607 theme.search.match_index.text.clone(),
608 )
609 .contained()
610 .with_style(theme.search.match_index.container)
611 .aligned()
612 .boxed(),
613 ]
614 })
615 })
616 .contained()
617 .with_style(theme.search.container)
618 .constrained()
619 .with_height(theme.workspace.toolbar.height)
620 .named("project search")
621 }
622
623 fn render_option_button(
624 &self,
625 icon: &str,
626 option: SearchOption,
627 cx: &mut RenderContext<Self>,
628 ) -> ElementBox {
629 let is_active = self.is_option_enabled(option);
630 MouseEventHandler::new::<Self, _, _>(option as usize, cx, |state, cx| {
631 let theme = &cx.global::<Settings>().theme.search;
632 let style = match (is_active, state.hovered) {
633 (false, false) => &theme.option_button,
634 (false, true) => &theme.hovered_option_button,
635 (true, false) => &theme.active_option_button,
636 (true, true) => &theme.active_hovered_option_button,
637 };
638 Label::new(icon.to_string(), style.text.clone())
639 .contained()
640 .with_style(style.container)
641 .boxed()
642 })
643 .on_click(move |cx| cx.dispatch_action(ToggleSearchOption(option)))
644 .with_cursor_style(CursorStyle::PointingHand)
645 .boxed()
646 }
647
648 fn is_option_enabled(&self, option: SearchOption) -> bool {
649 match option {
650 SearchOption::WholeWord => self.whole_word,
651 SearchOption::CaseSensitive => self.case_sensitive,
652 SearchOption::Regex => self.regex,
653 }
654 }
655
656 fn render_nav_button(
657 &self,
658 icon: &str,
659 direction: Direction,
660 cx: &mut RenderContext<Self>,
661 ) -> ElementBox {
662 enum NavButton {}
663 MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, cx| {
664 let theme = &cx.global::<Settings>().theme.search;
665 let style = if state.hovered {
666 &theme.hovered_option_button
667 } else {
668 &theme.option_button
669 };
670 Label::new(icon.to_string(), style.text.clone())
671 .contained()
672 .with_style(style.container)
673 .boxed()
674 })
675 .on_click(move |cx| cx.dispatch_action(SelectMatch(direction)))
676 .with_cursor_style(CursorStyle::PointingHand)
677 .boxed()
678 }
679}
680
681#[cfg(test)]
682mod tests {
683 use super::*;
684 use editor::DisplayPoint;
685 use gpui::{color::Color, TestAppContext};
686 use project::FakeFs;
687 use serde_json::json;
688 use std::sync::Arc;
689
690 #[gpui::test]
691 async fn test_project_search(cx: &mut TestAppContext) {
692 let fonts = cx.font_cache();
693 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
694 theme.search.match_background = Color::red();
695 let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
696 cx.update(|cx| cx.set_global(settings));
697
698 let fs = FakeFs::new(cx.background());
699 fs.insert_tree(
700 "/dir",
701 json!({
702 "one.rs": "const ONE: usize = 1;",
703 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
704 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
705 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
706 }),
707 )
708 .await;
709 let project = Project::test(fs.clone(), cx);
710 let (tree, _) = project
711 .update(cx, |project, cx| {
712 project.find_or_create_local_worktree("/dir", true, cx)
713 })
714 .await
715 .unwrap();
716 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
717 .await;
718
719 let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
720 let search_view = cx.add_view(Default::default(), |cx| {
721 ProjectSearchView::new(search.clone(), cx)
722 });
723
724 search_view.update(cx, |search_view, cx| {
725 search_view
726 .query_editor
727 .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
728 search_view.search(&Search, cx);
729 });
730 search_view.next_notification(&cx).await;
731 search_view.update(cx, |search_view, cx| {
732 assert_eq!(
733 search_view
734 .results_editor
735 .update(cx, |editor, cx| editor.display_text(cx)),
736 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
737 );
738 assert_eq!(
739 search_view
740 .results_editor
741 .update(cx, |editor, cx| editor.all_background_highlights(cx)),
742 &[
743 (
744 DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
745 Color::red()
746 ),
747 (
748 DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
749 Color::red()
750 ),
751 (
752 DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
753 Color::red()
754 )
755 ]
756 );
757 assert_eq!(search_view.active_match_index, Some(0));
758 assert_eq!(
759 search_view
760 .results_editor
761 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
762 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
763 );
764
765 search_view.select_match(&SelectMatch(Direction::Next), cx);
766 });
767
768 search_view.update(cx, |search_view, cx| {
769 assert_eq!(search_view.active_match_index, Some(1));
770 assert_eq!(
771 search_view
772 .results_editor
773 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
774 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
775 );
776 search_view.select_match(&SelectMatch(Direction::Next), cx);
777 });
778
779 search_view.update(cx, |search_view, cx| {
780 assert_eq!(search_view.active_match_index, Some(2));
781 assert_eq!(
782 search_view
783 .results_editor
784 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
785 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
786 );
787 search_view.select_match(&SelectMatch(Direction::Next), cx);
788 });
789
790 search_view.update(cx, |search_view, cx| {
791 assert_eq!(search_view.active_match_index, Some(0));
792 assert_eq!(
793 search_view
794 .results_editor
795 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
796 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
797 );
798 search_view.select_match(&SelectMatch(Direction::Prev), cx);
799 });
800
801 search_view.update(cx, |search_view, cx| {
802 assert_eq!(search_view.active_match_index, Some(2));
803 assert_eq!(
804 search_view
805 .results_editor
806 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
807 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
808 );
809 search_view.select_match(&SelectMatch(Direction::Prev), cx);
810 });
811
812 search_view.update(cx, |search_view, cx| {
813 assert_eq!(search_view.active_match_index, Some(1));
814 assert_eq!(
815 search_view
816 .results_editor
817 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
818 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
819 );
820 });
821 }
822}