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