1use aho_corasick::AhoCorasickBuilder;
2use anyhow::Result;
3use collections::HashSet;
4use editor::{
5 char_kind, display_map::ToDisplayPoint, Anchor, Autoscroll, Bias, Editor, EditorSettings,
6 MultiBufferSnapshot,
7};
8use gpui::{
9 action, elements::*, keymap::Binding, platform::CursorStyle, Entity, MutableAppContext,
10 RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
11};
12use postage::watch;
13use regex::RegexBuilder;
14use smol::future::yield_now;
15use std::{
16 cmp::{self, Ordering},
17 ops::Range,
18 sync::Arc,
19};
20use workspace::{ItemViewHandle, Pane, Settings, Toolbar, Workspace};
21
22action!(Deploy, bool);
23action!(Dismiss);
24action!(FocusEditor);
25action!(ToggleMode, SearchMode);
26action!(GoToMatch, Direction);
27
28#[derive(Clone, Copy, PartialEq, Eq)]
29pub enum Direction {
30 Prev,
31 Next,
32}
33
34#[derive(Clone, Copy)]
35pub enum SearchMode {
36 WholeWord,
37 CaseSensitive,
38 Regex,
39}
40
41pub fn init(cx: &mut MutableAppContext) {
42 cx.add_bindings([
43 Binding::new("cmd-f", Deploy(true), Some("Editor && mode == full")),
44 Binding::new("cmd-e", Deploy(false), Some("Editor && mode == full")),
45 Binding::new("escape", Dismiss, Some("FindBar")),
46 Binding::new("cmd-f", FocusEditor, Some("FindBar")),
47 Binding::new("enter", GoToMatch(Direction::Next), Some("FindBar")),
48 Binding::new("shift-enter", GoToMatch(Direction::Prev), Some("FindBar")),
49 Binding::new("cmd-g", GoToMatch(Direction::Next), Some("Pane")),
50 Binding::new("cmd-shift-G", GoToMatch(Direction::Prev), Some("Pane")),
51 ]);
52 cx.add_action(FindBar::deploy);
53 cx.add_action(FindBar::dismiss);
54 cx.add_action(FindBar::focus_editor);
55 cx.add_action(FindBar::toggle_mode);
56 cx.add_action(FindBar::go_to_match);
57 cx.add_action(FindBar::go_to_match_on_pane);
58}
59
60struct FindBar {
61 settings: watch::Receiver<Settings>,
62 query_editor: ViewHandle<Editor>,
63 active_editor: Option<ViewHandle<Editor>>,
64 active_match_index: Option<usize>,
65 active_editor_subscription: Option<Subscription>,
66 highlighted_editors: HashSet<WeakViewHandle<Editor>>,
67 pending_search: Option<Task<()>>,
68 case_sensitive_mode: bool,
69 whole_word_mode: bool,
70 regex_mode: bool,
71 query_contains_error: bool,
72}
73
74impl Entity for FindBar {
75 type Event = ();
76}
77
78impl View for FindBar {
79 fn ui_name() -> &'static str {
80 "FindBar"
81 }
82
83 fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
84 cx.focus(&self.query_editor);
85 }
86
87 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
88 let theme = &self.settings.borrow().theme;
89 let editor_container = if self.query_contains_error {
90 theme.find.invalid_editor
91 } else {
92 theme.find.editor.input.container
93 };
94 Flex::row()
95 .with_child(
96 ChildView::new(&self.query_editor)
97 .contained()
98 .with_style(editor_container)
99 .aligned()
100 .constrained()
101 .with_max_width(theme.find.editor.max_width)
102 .boxed(),
103 )
104 .with_child(
105 Flex::row()
106 .with_child(self.render_mode_button("Case", SearchMode::CaseSensitive, cx))
107 .with_child(self.render_mode_button("Word", SearchMode::WholeWord, cx))
108 .with_child(self.render_mode_button("Regex", SearchMode::Regex, cx))
109 .contained()
110 .with_style(theme.find.mode_button_group)
111 .aligned()
112 .boxed(),
113 )
114 .with_child(
115 Flex::row()
116 .with_child(self.render_nav_button("<", Direction::Prev, cx))
117 .with_child(self.render_nav_button(">", Direction::Next, cx))
118 .aligned()
119 .boxed(),
120 )
121 .with_children(self.active_editor.as_ref().and_then(|editor| {
122 let (_, highlighted_ranges) =
123 editor.read(cx).highlighted_ranges_for_type::<Self>()?;
124 let message = if let Some(match_ix) = self.active_match_index {
125 format!("{}/{}", match_ix + 1, highlighted_ranges.len())
126 } else {
127 "No matches".to_string()
128 };
129
130 Some(
131 Label::new(message, theme.find.match_index.text.clone())
132 .contained()
133 .with_style(theme.find.match_index.container)
134 .aligned()
135 .boxed(),
136 )
137 }))
138 .contained()
139 .with_style(theme.find.container)
140 .constrained()
141 .with_height(theme.workspace.toolbar.height)
142 .named("find bar")
143 }
144}
145
146impl Toolbar for FindBar {
147 fn active_item_changed(
148 &mut self,
149 item: Option<Box<dyn ItemViewHandle>>,
150 cx: &mut ViewContext<Self>,
151 ) -> bool {
152 self.active_editor_subscription.take();
153 self.active_editor.take();
154 self.pending_search.take();
155
156 if let Some(editor) = item.and_then(|item| item.act_as::<Editor>(cx)) {
157 self.active_editor_subscription =
158 Some(cx.subscribe(&editor, Self::on_active_editor_event));
159 self.active_editor = Some(editor);
160 self.update_matches(cx);
161 true
162 } else {
163 false
164 }
165 }
166
167 fn on_dismiss(&mut self, cx: &mut ViewContext<Self>) {
168 self.active_editor.take();
169 self.active_editor_subscription.take();
170 self.active_match_index.take();
171 self.pending_search.take();
172 self.clear_matches(cx);
173 }
174}
175
176impl FindBar {
177 fn new(settings: watch::Receiver<Settings>, cx: &mut ViewContext<Self>) -> Self {
178 let query_editor = cx.add_view(|cx| {
179 Editor::auto_height(
180 2,
181 {
182 let settings = settings.clone();
183 Arc::new(move |_| {
184 let settings = settings.borrow();
185 EditorSettings {
186 style: settings.theme.find.editor.input.as_editor(),
187 tab_size: settings.tab_size,
188 soft_wrap: editor::SoftWrap::None,
189 }
190 })
191 },
192 cx,
193 )
194 });
195 cx.subscribe(&query_editor, Self::on_query_editor_event)
196 .detach();
197
198 Self {
199 query_editor,
200 active_editor: None,
201 active_editor_subscription: None,
202 active_match_index: None,
203 highlighted_editors: Default::default(),
204 case_sensitive_mode: false,
205 whole_word_mode: false,
206 regex_mode: false,
207 settings,
208 pending_search: None,
209 query_contains_error: false,
210 }
211 }
212
213 fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
214 self.query_editor.update(cx, |query_editor, cx| {
215 query_editor.buffer().update(cx, |query_buffer, cx| {
216 let len = query_buffer.read(cx).len();
217 query_buffer.edit([0..len], query, cx);
218 });
219 });
220 }
221
222 fn render_mode_button(
223 &self,
224 icon: &str,
225 mode: SearchMode,
226 cx: &mut RenderContext<Self>,
227 ) -> ElementBox {
228 let theme = &self.settings.borrow().theme.find;
229 let is_active = self.is_mode_enabled(mode);
230 MouseEventHandler::new::<Self, _, _>(mode as usize, cx, |state, _| {
231 let style = match (is_active, state.hovered) {
232 (false, false) => &theme.mode_button,
233 (false, true) => &theme.hovered_mode_button,
234 (true, false) => &theme.active_mode_button,
235 (true, true) => &theme.active_hovered_mode_button,
236 };
237 Label::new(icon.to_string(), style.text.clone())
238 .contained()
239 .with_style(style.container)
240 .boxed()
241 })
242 .on_click(move |cx| cx.dispatch_action(ToggleMode(mode)))
243 .with_cursor_style(CursorStyle::PointingHand)
244 .boxed()
245 }
246
247 fn render_nav_button(
248 &self,
249 icon: &str,
250 direction: Direction,
251 cx: &mut RenderContext<Self>,
252 ) -> ElementBox {
253 let theme = &self.settings.borrow().theme.find;
254 enum NavButton {}
255 MouseEventHandler::new::<NavButton, _, _>(direction as usize, cx, |state, _| {
256 let style = if state.hovered {
257 &theme.hovered_mode_button
258 } else {
259 &theme.mode_button
260 };
261 Label::new(icon.to_string(), style.text.clone())
262 .contained()
263 .with_style(style.container)
264 .boxed()
265 })
266 .on_click(move |cx| cx.dispatch_action(GoToMatch(direction)))
267 .with_cursor_style(CursorStyle::PointingHand)
268 .boxed()
269 }
270
271 fn deploy(workspace: &mut Workspace, Deploy(focus): &Deploy, cx: &mut ViewContext<Workspace>) {
272 let settings = workspace.settings();
273 workspace.active_pane().update(cx, |pane, cx| {
274 pane.show_toolbar(cx, |cx| FindBar::new(settings, cx));
275
276 if let Some(find_bar) = pane
277 .active_toolbar()
278 .and_then(|toolbar| toolbar.downcast::<Self>())
279 {
280 let editor = pane.active_item().unwrap().act_as::<Editor>(cx).unwrap();
281 let display_map = editor
282 .update(cx, |editor, cx| editor.snapshot(cx))
283 .display_snapshot;
284 let selection = editor
285 .read(cx)
286 .newest_selection::<usize>(&display_map.buffer_snapshot);
287
288 let mut text: String;
289 if selection.start == selection.end {
290 let point = selection.start.to_display_point(&display_map);
291 let range = editor::movement::surrounding_word(&display_map, point);
292 let range = range.start.to_offset(&display_map, Bias::Left)
293 ..range.end.to_offset(&display_map, Bias::Right);
294 text = display_map.buffer_snapshot.text_for_range(range).collect();
295 if text.trim().is_empty() {
296 text = String::new();
297 }
298 } else {
299 text = display_map
300 .buffer_snapshot
301 .text_for_range(selection.start..selection.end)
302 .collect();
303 }
304
305 if !text.is_empty() {
306 find_bar.update(cx, |find_bar, cx| find_bar.set_query(&text, cx));
307 }
308
309 if *focus {
310 let query_editor = find_bar.read(cx).query_editor.clone();
311 query_editor.update(cx, |query_editor, cx| {
312 query_editor.select_all(&editor::SelectAll, cx);
313 });
314 cx.focus(&find_bar);
315 }
316 }
317 });
318 }
319
320 fn dismiss(pane: &mut Pane, _: &Dismiss, cx: &mut ViewContext<Pane>) {
321 if pane.toolbar::<FindBar>().is_some() {
322 pane.dismiss_toolbar(cx);
323 }
324 }
325
326 fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext<Self>) {
327 if let Some(active_editor) = self.active_editor.as_ref() {
328 cx.focus(active_editor);
329 }
330 }
331
332 fn is_mode_enabled(&self, mode: SearchMode) -> bool {
333 match mode {
334 SearchMode::WholeWord => self.whole_word_mode,
335 SearchMode::CaseSensitive => self.case_sensitive_mode,
336 SearchMode::Regex => self.regex_mode,
337 }
338 }
339
340 fn toggle_mode(&mut self, ToggleMode(mode): &ToggleMode, cx: &mut ViewContext<Self>) {
341 let value = match mode {
342 SearchMode::WholeWord => &mut self.whole_word_mode,
343 SearchMode::CaseSensitive => &mut self.case_sensitive_mode,
344 SearchMode::Regex => &mut self.regex_mode,
345 };
346 *value = !*value;
347 self.update_matches(cx);
348 cx.notify();
349 }
350
351 fn go_to_match(&mut self, GoToMatch(direction): &GoToMatch, cx: &mut ViewContext<Self>) {
352 if let Some(mut index) = self.active_match_index {
353 if let Some(editor) = self.active_editor.as_ref() {
354 editor.update(cx, |editor, cx| {
355 let newest_selection = editor.newest_anchor_selection().clone();
356 if let Some((_, ranges)) = editor.highlighted_ranges_for_type::<Self>() {
357 let position = newest_selection.head();
358 let buffer = editor.buffer().read(cx).read(cx);
359 if ranges[index].start.cmp(&position, &buffer).unwrap().is_gt() {
360 if *direction == Direction::Prev {
361 if index == 0 {
362 index = ranges.len() - 1;
363 } else {
364 index -= 1;
365 }
366 }
367 } else if ranges[index].end.cmp(&position, &buffer).unwrap().is_lt() {
368 if *direction == Direction::Next {
369 index = 0;
370 }
371 } else if *direction == Direction::Prev {
372 if index == 0 {
373 index = ranges.len() - 1;
374 } else {
375 index -= 1;
376 }
377 } else if *direction == Direction::Next {
378 if index == ranges.len() - 1 {
379 index = 0
380 } else {
381 index += 1;
382 }
383 }
384
385 let range_to_select = ranges[index].clone();
386 drop(buffer);
387 editor.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
388 }
389 });
390 }
391 }
392 }
393
394 fn go_to_match_on_pane(pane: &mut Pane, action: &GoToMatch, cx: &mut ViewContext<Pane>) {
395 if let Some(find_bar) = pane.toolbar::<FindBar>() {
396 find_bar.update(cx, |find_bar, cx| find_bar.go_to_match(action, cx));
397 }
398 }
399
400 fn on_query_editor_event(
401 &mut self,
402 _: ViewHandle<Editor>,
403 event: &editor::Event,
404 cx: &mut ViewContext<Self>,
405 ) {
406 match event {
407 editor::Event::Edited => {
408 self.query_contains_error = false;
409 self.clear_matches(cx);
410 self.update_matches(cx);
411 cx.notify();
412 }
413 _ => {}
414 }
415 }
416
417 fn on_active_editor_event(
418 &mut self,
419 _: ViewHandle<Editor>,
420 event: &editor::Event,
421 cx: &mut ViewContext<Self>,
422 ) {
423 match event {
424 editor::Event::Edited => self.update_matches(cx),
425 editor::Event::SelectionsChanged => self.update_match_index(cx),
426 _ => {}
427 }
428 }
429
430 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
431 for editor in self.highlighted_editors.drain() {
432 if let Some(editor) = editor.upgrade(cx) {
433 if Some(&editor) != self.active_editor.as_ref() {
434 editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
435 }
436 }
437 }
438 }
439
440 fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
441 let query = self.query_editor.read(cx).text(cx);
442 self.pending_search.take();
443 if let Some(editor) = self.active_editor.as_ref() {
444 if query.is_empty() {
445 self.active_match_index.take();
446 editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
447 } else {
448 let buffer = editor.read(cx).buffer().read(cx).snapshot(cx);
449 let case_sensitive = self.case_sensitive_mode;
450 let whole_word = self.whole_word_mode;
451 let ranges = if self.regex_mode {
452 cx.background()
453 .spawn(regex_search(buffer, query, case_sensitive, whole_word))
454 } else {
455 cx.background().spawn(async move {
456 Ok(search(buffer, query, case_sensitive, whole_word).await)
457 })
458 };
459
460 let editor = editor.downgrade();
461 self.pending_search = Some(cx.spawn(|this, mut cx| async move {
462 match ranges.await {
463 Ok(ranges) => {
464 if let Some(editor) = editor.upgrade(&cx) {
465 this.update(&mut cx, |this, cx| {
466 this.highlighted_editors.insert(editor.downgrade());
467 editor.update(cx, |editor, cx| {
468 let theme = &this.settings.borrow().theme.find;
469 editor.highlight_ranges::<Self>(
470 ranges,
471 theme.match_background,
472 cx,
473 )
474 });
475 this.update_match_index(cx);
476 });
477 }
478 }
479 Err(_) => {
480 this.update(&mut cx, |this, cx| {
481 this.query_contains_error = true;
482 cx.notify();
483 });
484 }
485 }
486 }));
487 }
488 }
489 }
490
491 fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
492 self.active_match_index = self.active_match_index(cx);
493 cx.notify();
494 }
495
496 fn active_match_index(&mut self, cx: &mut ViewContext<Self>) -> Option<usize> {
497 let editor = self.active_editor.as_ref()?;
498 let editor = editor.read(cx);
499 let position = editor.newest_anchor_selection().head();
500 let ranges = editor.highlighted_ranges_for_type::<Self>()?.1;
501 if ranges.is_empty() {
502 None
503 } else {
504 let buffer = editor.buffer().read(cx).read(cx);
505 match ranges.binary_search_by(|probe| {
506 if probe.end.cmp(&position, &*buffer).unwrap().is_lt() {
507 Ordering::Less
508 } else if probe.start.cmp(&position, &*buffer).unwrap().is_gt() {
509 Ordering::Greater
510 } else {
511 Ordering::Equal
512 }
513 }) {
514 Ok(i) | Err(i) => Some(cmp::min(i, ranges.len() - 1)),
515 }
516 }
517 }
518}
519
520const YIELD_INTERVAL: usize = 20000;
521
522async fn search(
523 buffer: MultiBufferSnapshot,
524 query: String,
525 case_sensitive: bool,
526 whole_word: bool,
527) -> Vec<Range<Anchor>> {
528 let mut ranges = Vec::new();
529
530 let search = AhoCorasickBuilder::new()
531 .auto_configure(&[&query])
532 .ascii_case_insensitive(!case_sensitive)
533 .build(&[&query]);
534 for (ix, mat) in search
535 .stream_find_iter(buffer.bytes_in_range(0..buffer.len()))
536 .enumerate()
537 {
538 if (ix + 1) % YIELD_INTERVAL == 0 {
539 yield_now().await;
540 }
541
542 let mat = mat.unwrap();
543
544 if whole_word {
545 let prev_kind = buffer.reversed_chars_at(mat.start()).next().map(char_kind);
546 let start_kind = char_kind(buffer.chars_at(mat.start()).next().unwrap());
547 let end_kind = char_kind(buffer.reversed_chars_at(mat.end()).next().unwrap());
548 let next_kind = buffer.chars_at(mat.end()).next().map(char_kind);
549 if Some(start_kind) == prev_kind || Some(end_kind) == next_kind {
550 continue;
551 }
552 }
553
554 ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
555 }
556
557 ranges
558}
559
560async fn regex_search(
561 buffer: MultiBufferSnapshot,
562 mut query: String,
563 case_sensitive: bool,
564 whole_word: bool,
565) -> Result<Vec<Range<Anchor>>> {
566 if whole_word {
567 let mut word_query = String::new();
568 word_query.push_str("\\b");
569 word_query.push_str(&query);
570 word_query.push_str("\\b");
571 query = word_query;
572 }
573
574 let mut ranges = Vec::new();
575
576 if query.contains("\n") || query.contains("\\n") {
577 let regex = RegexBuilder::new(&query)
578 .case_insensitive(!case_sensitive)
579 .multi_line(true)
580 .build()?;
581 for (ix, mat) in regex.find_iter(&buffer.text()).enumerate() {
582 if (ix + 1) % YIELD_INTERVAL == 0 {
583 yield_now().await;
584 }
585
586 ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
587 }
588 } else {
589 let regex = RegexBuilder::new(&query)
590 .case_insensitive(!case_sensitive)
591 .build()?;
592
593 let mut line = String::new();
594 let mut line_offset = 0;
595 for (chunk_ix, chunk) in buffer
596 .chunks(0..buffer.len(), false)
597 .map(|c| c.text)
598 .chain(["\n"])
599 .enumerate()
600 {
601 if (chunk_ix + 1) % YIELD_INTERVAL == 0 {
602 yield_now().await;
603 }
604
605 for (newline_ix, text) in chunk.split('\n').enumerate() {
606 if newline_ix > 0 {
607 for mat in regex.find_iter(&line) {
608 let start = line_offset + mat.start();
609 let end = line_offset + mat.end();
610 ranges.push(buffer.anchor_after(start)..buffer.anchor_before(end));
611 }
612
613 line_offset += line.len() + 1;
614 line.clear();
615 }
616 line.push_str(text);
617 }
618 }
619 }
620
621 Ok(ranges)
622}
623
624#[cfg(test)]
625mod tests {
626 use super::*;
627 use editor::{DisplayPoint, Editor, EditorSettings, MultiBuffer};
628 use gpui::{color::Color, TestAppContext};
629 use std::sync::Arc;
630 use unindent::Unindent as _;
631
632 #[gpui::test]
633 async fn test_find_simple(mut cx: TestAppContext) {
634 let fonts = cx.font_cache();
635 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
636 theme.find.match_background = Color::red();
637 let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
638
639 let buffer = cx.update(|cx| {
640 MultiBuffer::build_simple(
641 &r#"
642 A regular expression (shortened as regex or regexp;[1] also referred to as
643 rational expression[2][3]) is a sequence of characters that specifies a search
644 pattern in text. Usually such patterns are used by string-searching algorithms
645 for "find" or "find and replace" operations on strings, or for input validation.
646 "#
647 .unindent(),
648 cx,
649 )
650 });
651 let editor = cx.add_view(Default::default(), |cx| {
652 Editor::new(buffer.clone(), Arc::new(EditorSettings::test), None, cx)
653 });
654
655 let find_bar = cx.add_view(Default::default(), |cx| {
656 let mut find_bar = FindBar::new(watch::channel_with(settings).1, cx);
657 find_bar.active_item_changed(Some(Box::new(editor.clone())), cx);
658 find_bar
659 });
660
661 // Search for a string that appears with different casing.
662 // By default, search is case-insensitive.
663 find_bar.update(&mut cx, |find_bar, cx| {
664 find_bar.set_query("us", cx);
665 });
666 editor.next_notification(&cx).await;
667 editor.update(&mut cx, |editor, cx| {
668 assert_eq!(
669 editor.all_highlighted_ranges(cx),
670 &[
671 (
672 DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19),
673 Color::red(),
674 ),
675 (
676 DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
677 Color::red(),
678 ),
679 ]
680 );
681 });
682
683 // Switch to a case sensitive search.
684 find_bar.update(&mut cx, |find_bar, cx| {
685 find_bar.toggle_mode(&ToggleMode(SearchMode::CaseSensitive), cx);
686 });
687 editor.next_notification(&cx).await;
688 editor.update(&mut cx, |editor, cx| {
689 assert_eq!(
690 editor.all_highlighted_ranges(cx),
691 &[(
692 DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
693 Color::red(),
694 )]
695 );
696 });
697
698 // Search for a string that appears both as a whole word and
699 // within other words. By default, all results are found.
700 find_bar.update(&mut cx, |find_bar, cx| {
701 find_bar.set_query("or", cx);
702 });
703 editor.next_notification(&cx).await;
704 editor.update(&mut cx, |editor, cx| {
705 assert_eq!(
706 editor.all_highlighted_ranges(cx),
707 &[
708 (
709 DisplayPoint::new(0, 24)..DisplayPoint::new(0, 26),
710 Color::red(),
711 ),
712 (
713 DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
714 Color::red(),
715 ),
716 (
717 DisplayPoint::new(2, 71)..DisplayPoint::new(2, 73),
718 Color::red(),
719 ),
720 (
721 DisplayPoint::new(3, 1)..DisplayPoint::new(3, 3),
722 Color::red(),
723 ),
724 (
725 DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
726 Color::red(),
727 ),
728 (
729 DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
730 Color::red(),
731 ),
732 (
733 DisplayPoint::new(3, 60)..DisplayPoint::new(3, 62),
734 Color::red(),
735 ),
736 ]
737 );
738 });
739
740 // Switch to a whole word search.
741 find_bar.update(&mut cx, |find_bar, cx| {
742 find_bar.toggle_mode(&ToggleMode(SearchMode::WholeWord), cx);
743 });
744 editor.next_notification(&cx).await;
745 editor.update(&mut cx, |editor, cx| {
746 assert_eq!(
747 editor.all_highlighted_ranges(cx),
748 &[
749 (
750 DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
751 Color::red(),
752 ),
753 (
754 DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
755 Color::red(),
756 ),
757 (
758 DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
759 Color::red(),
760 ),
761 ]
762 );
763 });
764
765 editor.update(&mut cx, |editor, cx| {
766 editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
767 });
768 find_bar.update(&mut cx, |find_bar, cx| {
769 assert_eq!(find_bar.active_match_index, Some(0));
770 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
771 assert_eq!(
772 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
773 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
774 );
775 });
776 find_bar.read_with(&cx, |find_bar, _| {
777 assert_eq!(find_bar.active_match_index, Some(0));
778 });
779
780 find_bar.update(&mut cx, |find_bar, cx| {
781 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
782 assert_eq!(
783 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
784 [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
785 );
786 });
787 find_bar.read_with(&cx, |find_bar, _| {
788 assert_eq!(find_bar.active_match_index, Some(1));
789 });
790
791 find_bar.update(&mut cx, |find_bar, cx| {
792 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
793 assert_eq!(
794 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
795 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
796 );
797 });
798 find_bar.read_with(&cx, |find_bar, _| {
799 assert_eq!(find_bar.active_match_index, Some(2));
800 });
801
802 find_bar.update(&mut cx, |find_bar, cx| {
803 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
804 assert_eq!(
805 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
806 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
807 );
808 });
809 find_bar.read_with(&cx, |find_bar, _| {
810 assert_eq!(find_bar.active_match_index, Some(0));
811 });
812
813 find_bar.update(&mut cx, |find_bar, cx| {
814 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
815 assert_eq!(
816 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
817 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
818 );
819 });
820 find_bar.read_with(&cx, |find_bar, _| {
821 assert_eq!(find_bar.active_match_index, Some(2));
822 });
823
824 find_bar.update(&mut cx, |find_bar, cx| {
825 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
826 assert_eq!(
827 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
828 [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
829 );
830 });
831 find_bar.read_with(&cx, |find_bar, _| {
832 assert_eq!(find_bar.active_match_index, Some(1));
833 });
834
835 find_bar.update(&mut cx, |find_bar, cx| {
836 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
837 assert_eq!(
838 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
839 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
840 );
841 });
842 find_bar.read_with(&cx, |find_bar, _| {
843 assert_eq!(find_bar.active_match_index, Some(0));
844 });
845
846 // Park the cursor in between matches and ensure that going to the previous match selects
847 // the closest match to the left.
848 editor.update(&mut cx, |editor, cx| {
849 editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
850 });
851 find_bar.update(&mut cx, |find_bar, cx| {
852 assert_eq!(find_bar.active_match_index, Some(1));
853 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
854 assert_eq!(
855 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
856 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
857 );
858 });
859 find_bar.read_with(&cx, |find_bar, _| {
860 assert_eq!(find_bar.active_match_index, Some(0));
861 });
862
863 // Park the cursor in between matches and ensure that going to the next match selects the
864 // closest match to the right.
865 editor.update(&mut cx, |editor, cx| {
866 editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
867 });
868 find_bar.update(&mut cx, |find_bar, cx| {
869 assert_eq!(find_bar.active_match_index, Some(1));
870 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
871 assert_eq!(
872 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
873 [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
874 );
875 });
876 find_bar.read_with(&cx, |find_bar, _| {
877 assert_eq!(find_bar.active_match_index, Some(1));
878 });
879
880 // Park the cursor after the last match and ensure that going to the previous match selects
881 // the last match.
882 editor.update(&mut cx, |editor, cx| {
883 editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
884 });
885 find_bar.update(&mut cx, |find_bar, cx| {
886 assert_eq!(find_bar.active_match_index, Some(2));
887 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
888 assert_eq!(
889 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
890 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
891 );
892 });
893 find_bar.read_with(&cx, |find_bar, _| {
894 assert_eq!(find_bar.active_match_index, Some(2));
895 });
896
897 // Park the cursor after the last match and ensure that going to the next match selects the
898 // first match.
899 editor.update(&mut cx, |editor, cx| {
900 editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
901 });
902 find_bar.update(&mut cx, |find_bar, cx| {
903 assert_eq!(find_bar.active_match_index, Some(2));
904 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
905 assert_eq!(
906 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
907 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
908 );
909 });
910 find_bar.read_with(&cx, |find_bar, _| {
911 assert_eq!(find_bar.active_match_index, Some(0));
912 });
913
914 // Park the cursor before the first match and ensure that going to the previous match
915 // selects the last match.
916 editor.update(&mut cx, |editor, cx| {
917 editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
918 });
919 find_bar.update(&mut cx, |find_bar, cx| {
920 assert_eq!(find_bar.active_match_index, Some(0));
921 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
922 assert_eq!(
923 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
924 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
925 );
926 });
927 find_bar.read_with(&cx, |find_bar, _| {
928 assert_eq!(find_bar.active_match_index, Some(2));
929 });
930 }
931}