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, _, _, _>((cx.view_id(), 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 MouseEventHandler::new::<Self, _, _, _>(
255 (cx.view_id(), 10 + direction as usize),
256 cx,
257 |state, _| {
258 let style = if state.hovered {
259 &theme.hovered_mode_button
260 } else {
261 &theme.mode_button
262 };
263 Label::new(icon.to_string(), style.text.clone())
264 .contained()
265 .with_style(style.container)
266 .boxed()
267 },
268 )
269 .on_click(move |cx| cx.dispatch_action(GoToMatch(direction)))
270 .with_cursor_style(CursorStyle::PointingHand)
271 .boxed()
272 }
273
274 fn deploy(workspace: &mut Workspace, Deploy(focus): &Deploy, cx: &mut ViewContext<Workspace>) {
275 let settings = workspace.settings();
276 workspace.active_pane().update(cx, |pane, cx| {
277 let findbar_was_visible = pane
278 .active_toolbar()
279 .map_or(false, |toolbar| toolbar.downcast::<Self>().is_some());
280
281 pane.show_toolbar(cx, |cx| FindBar::new(settings, cx));
282
283 if let Some(find_bar) = pane
284 .active_toolbar()
285 .and_then(|toolbar| toolbar.downcast::<Self>())
286 {
287 if !findbar_was_visible {
288 let editor = pane.active_item().unwrap().act_as::<Editor>(cx).unwrap();
289 let display_map = editor
290 .update(cx, |editor, cx| editor.snapshot(cx))
291 .display_snapshot;
292 let selection = editor
293 .read(cx)
294 .newest_selection::<usize>(&display_map.buffer_snapshot);
295
296 let mut text: String;
297 if selection.start == selection.end {
298 let point = selection.start.to_display_point(&display_map);
299 let range = editor::movement::surrounding_word(&display_map, point);
300 let range = range.start.to_offset(&display_map, Bias::Left)
301 ..range.end.to_offset(&display_map, Bias::Right);
302 text = display_map.buffer_snapshot.text_for_range(range).collect();
303 if text.trim().is_empty() {
304 text = String::new();
305 }
306 } else {
307 text = display_map
308 .buffer_snapshot
309 .text_for_range(selection.start..selection.end)
310 .collect();
311 }
312
313 if !text.is_empty() {
314 find_bar.update(cx, |find_bar, cx| find_bar.set_query(&text, cx));
315 }
316 }
317
318 if *focus {
319 if !findbar_was_visible {
320 let query_editor = find_bar.read(cx).query_editor.clone();
321 query_editor.update(cx, |query_editor, cx| {
322 query_editor.select_all(&editor::SelectAll, cx);
323 });
324 }
325 cx.focus(&find_bar);
326 }
327 }
328 });
329 }
330
331 fn dismiss(workspace: &mut Workspace, _: &Dismiss, cx: &mut ViewContext<Workspace>) {
332 workspace
333 .active_pane()
334 .update(cx, |pane, cx| pane.dismiss_toolbar(cx));
335 }
336
337 fn focus_editor(&mut self, _: &FocusEditor, cx: &mut ViewContext<Self>) {
338 if let Some(active_editor) = self.active_editor.as_ref() {
339 cx.focus(active_editor);
340 }
341 }
342
343 fn is_mode_enabled(&self, mode: SearchMode) -> bool {
344 match mode {
345 SearchMode::WholeWord => self.whole_word_mode,
346 SearchMode::CaseSensitive => self.case_sensitive_mode,
347 SearchMode::Regex => self.regex_mode,
348 }
349 }
350
351 fn toggle_mode(&mut self, ToggleMode(mode): &ToggleMode, cx: &mut ViewContext<Self>) {
352 let value = match mode {
353 SearchMode::WholeWord => &mut self.whole_word_mode,
354 SearchMode::CaseSensitive => &mut self.case_sensitive_mode,
355 SearchMode::Regex => &mut self.regex_mode,
356 };
357 *value = !*value;
358 self.update_matches(cx);
359 cx.notify();
360 }
361
362 fn go_to_match(&mut self, GoToMatch(direction): &GoToMatch, cx: &mut ViewContext<Self>) {
363 if let Some(mut index) = self.active_match_index {
364 if let Some(editor) = self.active_editor.as_ref() {
365 editor.update(cx, |editor, cx| {
366 let newest_selection = editor.newest_anchor_selection().cloned();
367 if let Some(((_, ranges), newest_selection)) = editor
368 .highlighted_ranges_for_type::<Self>()
369 .zip(newest_selection)
370 {
371 let position = newest_selection.head();
372 let buffer = editor.buffer().read(cx).read(cx);
373 if ranges[index].start.cmp(&position, &buffer).unwrap().is_gt() {
374 if *direction == Direction::Prev {
375 if index == 0 {
376 index = ranges.len() - 1;
377 } else {
378 index -= 1;
379 }
380 }
381 } else if ranges[index].end.cmp(&position, &buffer).unwrap().is_lt() {
382 if *direction == Direction::Next {
383 index = 0;
384 }
385 } else if *direction == Direction::Prev {
386 if index == 0 {
387 index = ranges.len() - 1;
388 } else {
389 index -= 1;
390 }
391 } else if *direction == Direction::Next {
392 if index == ranges.len() - 1 {
393 index = 0
394 } else {
395 index += 1;
396 }
397 }
398
399 let range_to_select = ranges[index].clone();
400 drop(buffer);
401 editor.select_ranges([range_to_select], Some(Autoscroll::Fit), cx);
402 }
403 });
404 }
405 }
406 }
407
408 fn go_to_match_on_pane(pane: &mut Pane, action: &GoToMatch, cx: &mut ViewContext<Pane>) {
409 if let Some(find_bar) = pane.toolbar::<FindBar>() {
410 find_bar.update(cx, |find_bar, cx| find_bar.go_to_match(action, cx));
411 }
412 }
413
414 fn on_query_editor_event(
415 &mut self,
416 _: ViewHandle<Editor>,
417 event: &editor::Event,
418 cx: &mut ViewContext<Self>,
419 ) {
420 match event {
421 editor::Event::Edited => {
422 self.query_contains_error = false;
423 self.clear_matches(cx);
424 self.update_matches(cx);
425 cx.notify();
426 }
427 _ => {}
428 }
429 }
430
431 fn on_active_editor_event(
432 &mut self,
433 _: ViewHandle<Editor>,
434 event: &editor::Event,
435 cx: &mut ViewContext<Self>,
436 ) {
437 match event {
438 editor::Event::Edited => self.update_matches(cx),
439 editor::Event::SelectionsChanged => self.update_match_index(cx),
440 _ => {}
441 }
442 }
443
444 fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
445 for editor in self.highlighted_editors.drain() {
446 if let Some(editor) = editor.upgrade(cx) {
447 if Some(&editor) != self.active_editor.as_ref() {
448 editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
449 }
450 }
451 }
452 }
453
454 fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
455 let query = self.query_editor.read(cx).text(cx);
456 self.pending_search.take();
457 if let Some(editor) = self.active_editor.as_ref() {
458 if query.is_empty() {
459 self.active_match_index.take();
460 editor.update(cx, |editor, cx| editor.clear_highlighted_ranges::<Self>(cx));
461 } else {
462 let buffer = editor.read(cx).buffer().read(cx).snapshot(cx);
463 let case_sensitive = self.case_sensitive_mode;
464 let whole_word = self.whole_word_mode;
465 let ranges = if self.regex_mode {
466 cx.background()
467 .spawn(regex_search(buffer, query, case_sensitive, whole_word))
468 } else {
469 cx.background().spawn(async move {
470 Ok(search(buffer, query, case_sensitive, whole_word).await)
471 })
472 };
473
474 let editor = editor.downgrade();
475 self.pending_search = Some(cx.spawn(|this, mut cx| async move {
476 match ranges.await {
477 Ok(ranges) => {
478 if let Some(editor) = cx.read(|cx| editor.upgrade(cx)) {
479 this.update(&mut cx, |this, cx| {
480 this.highlighted_editors.insert(editor.downgrade());
481 editor.update(cx, |editor, cx| {
482 let theme = &this.settings.borrow().theme.find;
483 editor.highlight_ranges::<Self>(
484 ranges,
485 theme.match_background,
486 cx,
487 )
488 });
489 this.update_match_index(cx);
490 });
491 }
492 }
493 Err(_) => {
494 this.update(&mut cx, |this, cx| {
495 this.query_contains_error = true;
496 cx.notify();
497 });
498 }
499 }
500 }));
501 }
502 }
503 }
504
505 fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
506 self.active_match_index = self.active_match_index(cx);
507 cx.notify();
508 }
509
510 fn active_match_index(&mut self, cx: &mut ViewContext<Self>) -> Option<usize> {
511 let editor = self.active_editor.as_ref()?;
512 let editor = editor.read(cx);
513 let position = editor.newest_anchor_selection()?.head();
514 let ranges = editor.highlighted_ranges_for_type::<Self>()?.1;
515 if ranges.is_empty() {
516 None
517 } else {
518 let buffer = editor.buffer().read(cx).read(cx);
519 match ranges.binary_search_by(|probe| {
520 if probe.end.cmp(&position, &*buffer).unwrap().is_lt() {
521 Ordering::Less
522 } else if probe.start.cmp(&position, &*buffer).unwrap().is_gt() {
523 Ordering::Greater
524 } else {
525 Ordering::Equal
526 }
527 }) {
528 Ok(i) | Err(i) => Some(cmp::min(i, ranges.len() - 1)),
529 }
530 }
531 }
532}
533
534const YIELD_INTERVAL: usize = 20000;
535
536async fn search(
537 buffer: MultiBufferSnapshot,
538 query: String,
539 case_sensitive: bool,
540 whole_word: bool,
541) -> Vec<Range<Anchor>> {
542 let mut ranges = Vec::new();
543
544 let search = AhoCorasickBuilder::new()
545 .auto_configure(&[&query])
546 .ascii_case_insensitive(!case_sensitive)
547 .build(&[&query]);
548 for (ix, mat) in search
549 .stream_find_iter(buffer.bytes_in_range(0..buffer.len()))
550 .enumerate()
551 {
552 if (ix + 1) % YIELD_INTERVAL == 0 {
553 yield_now().await;
554 }
555
556 let mat = mat.unwrap();
557
558 if whole_word {
559 let prev_kind = buffer.reversed_chars_at(mat.start()).next().map(char_kind);
560 let start_kind = char_kind(buffer.chars_at(mat.start()).next().unwrap());
561 let end_kind = char_kind(buffer.reversed_chars_at(mat.end()).next().unwrap());
562 let next_kind = buffer.chars_at(mat.end()).next().map(char_kind);
563 if Some(start_kind) == prev_kind || Some(end_kind) == next_kind {
564 continue;
565 }
566 }
567
568 ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
569 }
570
571 ranges
572}
573
574async fn regex_search(
575 buffer: MultiBufferSnapshot,
576 mut query: String,
577 case_sensitive: bool,
578 whole_word: bool,
579) -> Result<Vec<Range<Anchor>>> {
580 if whole_word {
581 let mut word_query = String::new();
582 word_query.push_str("\\b");
583 word_query.push_str(&query);
584 word_query.push_str("\\b");
585 query = word_query;
586 }
587
588 let mut ranges = Vec::new();
589
590 if query.contains("\n") || query.contains("\\n") {
591 let regex = RegexBuilder::new(&query)
592 .case_insensitive(!case_sensitive)
593 .multi_line(true)
594 .build()?;
595 for (ix, mat) in regex.find_iter(&buffer.text()).enumerate() {
596 if (ix + 1) % YIELD_INTERVAL == 0 {
597 yield_now().await;
598 }
599
600 ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
601 }
602 } else {
603 let regex = RegexBuilder::new(&query)
604 .case_insensitive(!case_sensitive)
605 .build()?;
606
607 let mut line = String::new();
608 let mut line_offset = 0;
609 for (chunk_ix, chunk) in buffer
610 .chunks(0..buffer.len())
611 .map(|c| c.text)
612 .chain(["\n"])
613 .enumerate()
614 {
615 if (chunk_ix + 1) % YIELD_INTERVAL == 0 {
616 yield_now().await;
617 }
618
619 for (newline_ix, text) in chunk.split('\n').enumerate() {
620 if newline_ix > 0 {
621 for mat in regex.find_iter(&line) {
622 let start = line_offset + mat.start();
623 let end = line_offset + mat.end();
624 ranges.push(buffer.anchor_after(start)..buffer.anchor_before(end));
625 }
626
627 line_offset += line.len() + 1;
628 line.clear();
629 }
630 line.push_str(text);
631 }
632 }
633 }
634
635 Ok(ranges)
636}
637
638#[cfg(test)]
639mod tests {
640 use super::*;
641 use editor::{DisplayPoint, Editor, EditorSettings, MultiBuffer};
642 use gpui::{color::Color, TestAppContext};
643 use std::sync::Arc;
644 use unindent::Unindent as _;
645
646 #[gpui::test]
647 async fn test_find_simple(mut cx: TestAppContext) {
648 let fonts = cx.font_cache();
649 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
650 theme.find.match_background = Color::red();
651 let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
652
653 let buffer = cx.update(|cx| {
654 MultiBuffer::build_simple(
655 &r#"
656 A regular expression (shortened as regex or regexp;[1] also referred to as
657 rational expression[2][3]) is a sequence of characters that specifies a search
658 pattern in text. Usually such patterns are used by string-searching algorithms
659 for "find" or "find and replace" operations on strings, or for input validation.
660 "#
661 .unindent(),
662 cx,
663 )
664 });
665 let editor = cx.add_view(Default::default(), |cx| {
666 Editor::new(buffer.clone(), Arc::new(EditorSettings::test), cx)
667 });
668
669 let find_bar = cx.add_view(Default::default(), |cx| {
670 let mut find_bar = FindBar::new(watch::channel_with(settings).1, cx);
671 find_bar.active_item_changed(Some(Box::new(editor.clone())), cx);
672 find_bar
673 });
674
675 // Search for a string that appears with different casing.
676 // By default, search is case-insensitive.
677 find_bar.update(&mut cx, |find_bar, cx| {
678 find_bar.set_query("us", cx);
679 });
680 editor.next_notification(&cx).await;
681 editor.update(&mut cx, |editor, cx| {
682 assert_eq!(
683 editor.all_highlighted_ranges(cx),
684 &[
685 (
686 DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19),
687 Color::red(),
688 ),
689 (
690 DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
691 Color::red(),
692 ),
693 ]
694 );
695 });
696
697 // Switch to a case sensitive search.
698 find_bar.update(&mut cx, |find_bar, cx| {
699 find_bar.toggle_mode(&ToggleMode(SearchMode::CaseSensitive), cx);
700 });
701 editor.next_notification(&cx).await;
702 editor.update(&mut cx, |editor, cx| {
703 assert_eq!(
704 editor.all_highlighted_ranges(cx),
705 &[(
706 DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
707 Color::red(),
708 )]
709 );
710 });
711
712 // Search for a string that appears both as a whole word and
713 // within other words. By default, all results are found.
714 find_bar.update(&mut cx, |find_bar, cx| {
715 find_bar.set_query("or", cx);
716 });
717 editor.next_notification(&cx).await;
718 editor.update(&mut cx, |editor, cx| {
719 assert_eq!(
720 editor.all_highlighted_ranges(cx),
721 &[
722 (
723 DisplayPoint::new(0, 24)..DisplayPoint::new(0, 26),
724 Color::red(),
725 ),
726 (
727 DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
728 Color::red(),
729 ),
730 (
731 DisplayPoint::new(2, 71)..DisplayPoint::new(2, 73),
732 Color::red(),
733 ),
734 (
735 DisplayPoint::new(3, 1)..DisplayPoint::new(3, 3),
736 Color::red(),
737 ),
738 (
739 DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
740 Color::red(),
741 ),
742 (
743 DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
744 Color::red(),
745 ),
746 (
747 DisplayPoint::new(3, 60)..DisplayPoint::new(3, 62),
748 Color::red(),
749 ),
750 ]
751 );
752 });
753
754 // Switch to a whole word search.
755 find_bar.update(&mut cx, |find_bar, cx| {
756 find_bar.toggle_mode(&ToggleMode(SearchMode::WholeWord), cx);
757 });
758 editor.next_notification(&cx).await;
759 editor.update(&mut cx, |editor, cx| {
760 assert_eq!(
761 editor.all_highlighted_ranges(cx),
762 &[
763 (
764 DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
765 Color::red(),
766 ),
767 (
768 DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
769 Color::red(),
770 ),
771 (
772 DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
773 Color::red(),
774 ),
775 ]
776 );
777 });
778
779 editor.update(&mut cx, |editor, cx| {
780 editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
781 });
782 find_bar.update(&mut cx, |find_bar, cx| {
783 assert_eq!(find_bar.active_match_index, Some(0));
784 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
785 assert_eq!(
786 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
787 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
788 );
789 });
790 find_bar.read_with(&cx, |find_bar, _| {
791 assert_eq!(find_bar.active_match_index, Some(0));
792 });
793
794 find_bar.update(&mut cx, |find_bar, cx| {
795 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
796 assert_eq!(
797 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
798 [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
799 );
800 });
801 find_bar.read_with(&cx, |find_bar, _| {
802 assert_eq!(find_bar.active_match_index, Some(1));
803 });
804
805 find_bar.update(&mut cx, |find_bar, cx| {
806 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
807 assert_eq!(
808 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
809 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
810 );
811 });
812 find_bar.read_with(&cx, |find_bar, _| {
813 assert_eq!(find_bar.active_match_index, Some(2));
814 });
815
816 find_bar.update(&mut cx, |find_bar, cx| {
817 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
818 assert_eq!(
819 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
820 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
821 );
822 });
823 find_bar.read_with(&cx, |find_bar, _| {
824 assert_eq!(find_bar.active_match_index, Some(0));
825 });
826
827 find_bar.update(&mut cx, |find_bar, cx| {
828 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
829 assert_eq!(
830 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
831 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
832 );
833 });
834 find_bar.read_with(&cx, |find_bar, _| {
835 assert_eq!(find_bar.active_match_index, Some(2));
836 });
837
838 find_bar.update(&mut cx, |find_bar, cx| {
839 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
840 assert_eq!(
841 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
842 [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
843 );
844 });
845 find_bar.read_with(&cx, |find_bar, _| {
846 assert_eq!(find_bar.active_match_index, Some(1));
847 });
848
849 find_bar.update(&mut cx, |find_bar, cx| {
850 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
851 assert_eq!(
852 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
853 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
854 );
855 });
856 find_bar.read_with(&cx, |find_bar, _| {
857 assert_eq!(find_bar.active_match_index, Some(0));
858 });
859
860 // Park the cursor in between matches and ensure that going to the previous match selects
861 // the closest match to the left.
862 editor.update(&mut cx, |editor, cx| {
863 editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
864 });
865 find_bar.update(&mut cx, |find_bar, cx| {
866 assert_eq!(find_bar.active_match_index, Some(1));
867 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
868 assert_eq!(
869 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
870 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
871 );
872 });
873 find_bar.read_with(&cx, |find_bar, _| {
874 assert_eq!(find_bar.active_match_index, Some(0));
875 });
876
877 // Park the cursor in between matches and ensure that going to the next match selects the
878 // closest match to the right.
879 editor.update(&mut cx, |editor, cx| {
880 editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
881 });
882 find_bar.update(&mut cx, |find_bar, cx| {
883 assert_eq!(find_bar.active_match_index, Some(1));
884 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
885 assert_eq!(
886 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
887 [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
888 );
889 });
890 find_bar.read_with(&cx, |find_bar, _| {
891 assert_eq!(find_bar.active_match_index, Some(1));
892 });
893
894 // Park the cursor after the last match and ensure that going to the previous match selects
895 // the last match.
896 editor.update(&mut cx, |editor, cx| {
897 editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
898 });
899 find_bar.update(&mut cx, |find_bar, cx| {
900 assert_eq!(find_bar.active_match_index, Some(2));
901 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
902 assert_eq!(
903 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
904 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
905 );
906 });
907 find_bar.read_with(&cx, |find_bar, _| {
908 assert_eq!(find_bar.active_match_index, Some(2));
909 });
910
911 // Park the cursor after the last match and ensure that going to the next match selects the
912 // first match.
913 editor.update(&mut cx, |editor, cx| {
914 editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
915 });
916 find_bar.update(&mut cx, |find_bar, cx| {
917 assert_eq!(find_bar.active_match_index, Some(2));
918 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
919 assert_eq!(
920 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
921 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
922 );
923 });
924 find_bar.read_with(&cx, |find_bar, _| {
925 assert_eq!(find_bar.active_match_index, Some(0));
926 });
927
928 // Park the cursor before the first match and ensure that going to the previous match
929 // selects the last match.
930 editor.update(&mut cx, |editor, cx| {
931 editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
932 });
933 find_bar.update(&mut cx, |find_bar, cx| {
934 assert_eq!(find_bar.active_match_index, Some(0));
935 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
936 assert_eq!(
937 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
938 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
939 );
940 });
941 find_bar.read_with(&cx, |find_bar, _| {
942 assert_eq!(find_bar.active_match_index, Some(2));
943 });
944 }
945}