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 can_save(&self, _: &gpui::AppContext) -> bool {
254 true
255 }
256
257 fn is_dirty(&self, cx: &AppContext) -> bool {
258 self.results_editor.read(cx).is_dirty(cx)
259 }
260
261 fn has_conflict(&self, cx: &AppContext) -> bool {
262 self.results_editor.read(cx).has_conflict(cx)
263 }
264
265 fn save(
266 &mut self,
267 project: ModelHandle<Project>,
268 cx: &mut ViewContext<Self>,
269 ) -> Task<anyhow::Result<()>> {
270 self.results_editor
271 .update(cx, |editor, cx| editor.save(project, cx))
272 }
273
274 fn can_save_as(&self, _: &gpui::AppContext) -> bool {
275 false
276 }
277
278 fn save_as(
279 &mut self,
280 _: ModelHandle<Project>,
281 _: PathBuf,
282 _: &mut ViewContext<Self>,
283 ) -> Task<anyhow::Result<()>> {
284 unreachable!("save_as should not have been called")
285 }
286
287 fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
288 where
289 Self: Sized,
290 {
291 let model = self.model.update(cx, |model, cx| model.clone(cx));
292 Some(Self::new(model, cx))
293 }
294
295 fn set_nav_history(&mut self, nav_history: ItemNavHistory, cx: &mut ViewContext<Self>) {
296 self.results_editor.update(cx, |editor, _| {
297 editor.set_nav_history(Some(nav_history));
298 });
299 }
300
301 fn navigate(&mut self, data: Box<dyn Any>, cx: &mut ViewContext<Self>) {
302 self.results_editor
303 .update(cx, |editor, cx| editor.navigate(data, cx));
304 }
305
306 fn should_update_tab_on_event(event: &ViewEvent) -> bool {
307 matches!(event, ViewEvent::UpdateTab)
308 }
309}
310
311impl ProjectSearchView {
312 fn new(model: ModelHandle<ProjectSearch>, cx: &mut ViewContext<Self>) -> Self {
313 let project;
314 let excerpts;
315 let mut query_text = String::new();
316 let mut regex = false;
317 let mut case_sensitive = false;
318 let mut whole_word = false;
319
320 {
321 let model = model.read(cx);
322 project = model.project.clone();
323 excerpts = model.excerpts.clone();
324 if let Some(active_query) = model.active_query.as_ref() {
325 query_text = active_query.as_str().to_string();
326 regex = active_query.is_regex();
327 case_sensitive = active_query.case_sensitive();
328 whole_word = active_query.whole_word();
329 }
330 }
331 cx.observe(&model, |this, _, cx| this.model_changed(true, cx))
332 .detach();
333
334 let query_editor = cx.add_view(|cx| {
335 let mut editor =
336 Editor::single_line(Some(|theme| theme.search.editor.input.clone()), cx);
337 editor.set_text(query_text, cx);
338 editor
339 });
340
341 let results_editor = cx.add_view(|cx| {
342 let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
343 editor.set_searchable(false);
344 editor
345 });
346 cx.observe(&results_editor, |_, _, cx| cx.emit(ViewEvent::UpdateTab))
347 .detach();
348 cx.subscribe(&results_editor, |this, _, event, cx| {
349 if matches!(event, editor::Event::SelectionsChanged) {
350 this.update_match_index(cx);
351 }
352 })
353 .detach();
354
355 let mut this = ProjectSearchView {
356 model,
357 query_editor,
358 results_editor,
359 case_sensitive,
360 whole_word,
361 regex,
362 query_contains_error: false,
363 active_match_index: None,
364 };
365 this.model_changed(false, cx);
366 this
367 }
368
369 // Re-activate the most recently activated search or the most recent if it has been closed.
370 // If no search exists in the workspace, create a new one.
371 fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext<Workspace>) {
372 // Clean up entries for dropped projects
373 cx.update_global(|state: &mut ActiveSearches, cx| {
374 state.0.retain(|project, _| project.is_upgradable(cx))
375 });
376
377 let active_search = cx
378 .global::<ActiveSearches>()
379 .0
380 .get(&workspace.project().downgrade());
381
382 let existing = active_search
383 .and_then(|active_search| {
384 workspace
385 .items_of_type::<ProjectSearchView>(cx)
386 .find(|search| search == active_search)
387 })
388 .or_else(|| workspace.item_of_type::<ProjectSearchView>(cx));
389
390 if let Some(existing) = existing {
391 workspace.activate_item(&existing, cx);
392 } else {
393 let model = cx.add_model(|cx| ProjectSearch::new(workspace.project().clone(), cx));
394 workspace.add_item(
395 Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
396 cx,
397 );
398 }
399 }
400
401 fn search(&mut self, _: &Search, cx: &mut ViewContext<Self>) {
402 if let Some(query) = self.build_search_query(cx) {
403 self.model.update(cx, |model, cx| model.search(query, cx));
404 }
405 }
406
407 fn search_in_new(workspace: &mut Workspace, _: &SearchInNew, cx: &mut ViewContext<Workspace>) {
408 if let Some(search_view) = workspace
409 .active_item(cx)
410 .and_then(|item| item.downcast::<ProjectSearchView>())
411 {
412 let new_query = search_view.update(cx, |search_view, cx| {
413 let new_query = search_view.build_search_query(cx);
414 if new_query.is_some() {
415 if let Some(old_query) = search_view.model.read(cx).active_query.clone() {
416 search_view.query_editor.update(cx, |editor, cx| {
417 editor.set_text(old_query.as_str(), cx);
418 });
419 search_view.regex = old_query.is_regex();
420 search_view.whole_word = old_query.whole_word();
421 search_view.case_sensitive = old_query.case_sensitive();
422 }
423 }
424 new_query
425 });
426 if let Some(new_query) = new_query {
427 let model = cx.add_model(|cx| {
428 let mut model = ProjectSearch::new(workspace.project().clone(), cx);
429 model.search(new_query, cx);
430 model
431 });
432 workspace.add_item(
433 Box::new(cx.add_view(|cx| ProjectSearchView::new(model, cx))),
434 cx,
435 );
436 }
437 }
438 }
439
440 fn build_search_query(&mut self, cx: &mut ViewContext<Self>) -> Option<SearchQuery> {
441 let text = self.query_editor.read(cx).text(cx);
442 if self.regex {
443 match SearchQuery::regex(text, self.whole_word, self.case_sensitive) {
444 Ok(query) => Some(query),
445 Err(_) => {
446 self.query_contains_error = true;
447 cx.notify();
448 None
449 }
450 }
451 } else {
452 Some(SearchQuery::text(
453 text,
454 self.whole_word,
455 self.case_sensitive,
456 ))
457 }
458 }
459
460 fn toggle_search_option(
461 &mut self,
462 ToggleSearchOption(option): &ToggleSearchOption,
463 cx: &mut ViewContext<Self>,
464 ) {
465 let value = match option {
466 SearchOption::WholeWord => &mut self.whole_word,
467 SearchOption::CaseSensitive => &mut self.case_sensitive,
468 SearchOption::Regex => &mut self.regex,
469 };
470 *value = !*value;
471 self.search(&Search, cx);
472 cx.notify();
473 }
474
475 fn select_match(&mut self, &SelectMatch(direction): &SelectMatch, cx: &mut ViewContext<Self>) {
476 if let Some(index) = self.active_match_index {
477 let model = self.model.read(cx);
478 let results_editor = self.results_editor.read(cx);
479 let new_index = match_index_for_direction(
480 &model.match_ranges,
481 &results_editor.newest_anchor_selection().head(),
482 index,
483 direction,
484 &results_editor.buffer().read(cx).read(cx),
485 );
486 let range_to_select = model.match_ranges[new_index].clone();
487 self.results_editor.update(cx, |editor, cx| {
488 editor.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
489 });
490 }
491 }
492
493 fn toggle_focus(&mut self, _: &ToggleFocus, cx: &mut ViewContext<Self>) {
494 if self.query_editor.is_focused(cx) {
495 if !self.model.read(cx).match_ranges.is_empty() {
496 self.focus_results_editor(cx);
497 }
498 } else {
499 self.focus_query_editor(cx);
500 }
501 }
502
503 fn tab(&mut self, _: &editor::Tab, cx: &mut ViewContext<Self>) {
504 if self.query_editor.is_focused(cx) {
505 if !self.model.read(cx).match_ranges.is_empty() {
506 self.focus_results_editor(cx);
507 }
508 } else {
509 cx.propagate_action()
510 }
511 }
512
513 fn focus_query_editor(&self, cx: &mut ViewContext<Self>) {
514 self.query_editor.update(cx, |query_editor, cx| {
515 query_editor.select_all(&SelectAll, cx);
516 });
517 cx.focus(&self.query_editor);
518 }
519
520 fn focus_results_editor(&self, cx: &mut ViewContext<Self>) {
521 self.query_editor.update(cx, |query_editor, cx| {
522 let cursor = query_editor.newest_anchor_selection().head();
523 query_editor.select_ranges([cursor.clone()..cursor], None, cx);
524 });
525 cx.focus(&self.results_editor);
526 }
527
528 fn model_changed(&mut self, reset_selections: bool, cx: &mut ViewContext<Self>) {
529 let match_ranges = self.model.read(cx).match_ranges.clone();
530 if match_ranges.is_empty() {
531 self.active_match_index = None;
532 } else {
533 self.results_editor.update(cx, |editor, cx| {
534 if reset_selections {
535 editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx);
536 }
537 let theme = &cx.global::<Settings>().theme.search;
538 editor.highlight_background::<Self>(match_ranges, theme.match_background, cx);
539 });
540 if self.query_editor.is_focused(cx) {
541 self.focus_results_editor(cx);
542 }
543 }
544
545 cx.emit(ViewEvent::UpdateTab);
546 cx.notify();
547 }
548
549 fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
550 let results_editor = self.results_editor.read(cx);
551 let new_index = active_match_index(
552 &self.model.read(cx).match_ranges,
553 &results_editor.newest_anchor_selection().head(),
554 &results_editor.buffer().read(cx).read(cx),
555 );
556 if self.active_match_index != new_index {
557 self.active_match_index = new_index;
558 cx.notify();
559 }
560 }
561
562 fn render_query_editor(&self, cx: &mut RenderContext<Self>) -> ElementBox {
563 let theme = cx.global::<Settings>().theme.clone();
564 let editor_container = if self.query_contains_error {
565 theme.search.invalid_editor
566 } else {
567 theme.search.editor.input.container
568 };
569 Flex::row()
570 .with_child(
571 ChildView::new(&self.query_editor)
572 .contained()
573 .with_style(editor_container)
574 .aligned()
575 .constrained()
576 .with_max_width(theme.search.editor.max_width)
577 .boxed(),
578 )
579 .with_child(
580 Flex::row()
581 .with_child(self.render_option_button("Case", SearchOption::CaseSensitive, cx))
582 .with_child(self.render_option_button("Word", SearchOption::WholeWord, cx))
583 .with_child(self.render_option_button("Regex", SearchOption::Regex, cx))
584 .contained()
585 .with_style(theme.search.option_button_group)
586 .aligned()
587 .boxed(),
588 )
589 .with_children({
590 self.active_match_index.into_iter().flat_map(|match_ix| {
591 [
592 Flex::row()
593 .with_child(self.render_nav_button("<", Direction::Prev, cx))
594 .with_child(self.render_nav_button(">", Direction::Next, cx))
595 .aligned()
596 .boxed(),
597 Label::new(
598 format!(
599 "{}/{}",
600 match_ix + 1,
601 self.model.read(cx).match_ranges.len()
602 ),
603 theme.search.match_index.text.clone(),
604 )
605 .contained()
606 .with_style(theme.search.match_index.container)
607 .aligned()
608 .boxed(),
609 ]
610 })
611 })
612 .contained()
613 .with_style(theme.search.container)
614 .constrained()
615 .with_height(theme.workspace.toolbar.height)
616 .named("project search")
617 }
618
619 fn render_option_button(
620 &self,
621 icon: &str,
622 option: SearchOption,
623 cx: &mut RenderContext<Self>,
624 ) -> ElementBox {
625 let is_active = self.is_option_enabled(option);
626 MouseEventHandler::new::<Self, _, _>(option as usize, cx, |state, cx| {
627 let theme = &cx.global::<Settings>().theme.search;
628 let style = match (is_active, state.hovered) {
629 (false, false) => &theme.option_button,
630 (false, true) => &theme.hovered_option_button,
631 (true, false) => &theme.active_option_button,
632 (true, true) => &theme.active_hovered_option_button,
633 };
634 Label::new(icon.to_string(), style.text.clone())
635 .contained()
636 .with_style(style.container)
637 .boxed()
638 })
639 .on_click(move |cx| cx.dispatch_action(ToggleSearchOption(option)))
640 .with_cursor_style(CursorStyle::PointingHand)
641 .boxed()
642 }
643
644 fn is_option_enabled(&self, option: SearchOption) -> bool {
645 match option {
646 SearchOption::WholeWord => self.whole_word,
647 SearchOption::CaseSensitive => self.case_sensitive,
648 SearchOption::Regex => self.regex,
649 }
650 }
651
652 fn render_nav_button(
653 &self,
654 icon: &str,
655 direction: Direction,
656 cx: &mut RenderContext<Self>,
657 ) -> ElementBox {
658 enum NavButton {}
659 MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, cx| {
660 let theme = &cx.global::<Settings>().theme.search;
661 let style = if state.hovered {
662 &theme.hovered_option_button
663 } else {
664 &theme.option_button
665 };
666 Label::new(icon.to_string(), style.text.clone())
667 .contained()
668 .with_style(style.container)
669 .boxed()
670 })
671 .on_click(move |cx| cx.dispatch_action(SelectMatch(direction)))
672 .with_cursor_style(CursorStyle::PointingHand)
673 .boxed()
674 }
675}
676
677#[cfg(test)]
678mod tests {
679 use super::*;
680 use editor::DisplayPoint;
681 use gpui::{color::Color, TestAppContext};
682 use project::FakeFs;
683 use serde_json::json;
684 use std::sync::Arc;
685
686 #[gpui::test]
687 async fn test_project_search(cx: &mut TestAppContext) {
688 let fonts = cx.font_cache();
689 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
690 theme.search.match_background = Color::red();
691 let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
692 cx.update(|cx| cx.set_global(settings));
693
694 let fs = FakeFs::new(cx.background());
695 fs.insert_tree(
696 "/dir",
697 json!({
698 "one.rs": "const ONE: usize = 1;",
699 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
700 "three.rs": "const THREE: usize = one::ONE + two::TWO;",
701 "four.rs": "const FOUR: usize = one::ONE + three::THREE;",
702 }),
703 )
704 .await;
705 let project = Project::test(fs.clone(), cx);
706 let (tree, _) = project
707 .update(cx, |project, cx| {
708 project.find_or_create_local_worktree("/dir", true, cx)
709 })
710 .await
711 .unwrap();
712 cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
713 .await;
714
715 let search = cx.add_model(|cx| ProjectSearch::new(project, cx));
716 let search_view = cx.add_view(Default::default(), |cx| {
717 ProjectSearchView::new(search.clone(), cx)
718 });
719
720 search_view.update(cx, |search_view, cx| {
721 search_view
722 .query_editor
723 .update(cx, |query_editor, cx| query_editor.set_text("TWO", cx));
724 search_view.search(&Search, cx);
725 });
726 search_view.next_notification(&cx).await;
727 search_view.update(cx, |search_view, cx| {
728 assert_eq!(
729 search_view
730 .results_editor
731 .update(cx, |editor, cx| editor.display_text(cx)),
732 "\n\nconst THREE: usize = one::ONE + two::TWO;\n\n\nconst TWO: usize = one::ONE + one::ONE;"
733 );
734 assert_eq!(
735 search_view
736 .results_editor
737 .update(cx, |editor, cx| editor.all_background_highlights(cx)),
738 &[
739 (
740 DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35),
741 Color::red()
742 ),
743 (
744 DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40),
745 Color::red()
746 ),
747 (
748 DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9),
749 Color::red()
750 )
751 ]
752 );
753 assert_eq!(search_view.active_match_index, Some(0));
754 assert_eq!(
755 search_view
756 .results_editor
757 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
758 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
759 );
760
761 search_view.select_match(&SelectMatch(Direction::Next), cx);
762 });
763
764 search_view.update(cx, |search_view, cx| {
765 assert_eq!(search_view.active_match_index, Some(1));
766 assert_eq!(
767 search_view
768 .results_editor
769 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
770 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
771 );
772 search_view.select_match(&SelectMatch(Direction::Next), cx);
773 });
774
775 search_view.update(cx, |search_view, cx| {
776 assert_eq!(search_view.active_match_index, Some(2));
777 assert_eq!(
778 search_view
779 .results_editor
780 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
781 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
782 );
783 search_view.select_match(&SelectMatch(Direction::Next), cx);
784 });
785
786 search_view.update(cx, |search_view, cx| {
787 assert_eq!(search_view.active_match_index, Some(0));
788 assert_eq!(
789 search_view
790 .results_editor
791 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
792 [DisplayPoint::new(2, 32)..DisplayPoint::new(2, 35)]
793 );
794 search_view.select_match(&SelectMatch(Direction::Prev), cx);
795 });
796
797 search_view.update(cx, |search_view, cx| {
798 assert_eq!(search_view.active_match_index, Some(2));
799 assert_eq!(
800 search_view
801 .results_editor
802 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
803 [DisplayPoint::new(5, 6)..DisplayPoint::new(5, 9)]
804 );
805 search_view.select_match(&SelectMatch(Direction::Prev), cx);
806 });
807
808 search_view.update(cx, |search_view, cx| {
809 assert_eq!(search_view.active_match_index, Some(1));
810 assert_eq!(
811 search_view
812 .results_editor
813 .update(cx, |editor, cx| editor.selected_display_ranges(cx)),
814 [DisplayPoint::new(2, 37)..DisplayPoint::new(2, 40)]
815 );
816 });
817 }
818}