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_children(self.active_editor.as_ref().and_then(|editor| {
115 let (_, highlighted_ranges) =
116 editor.read(cx).highlighted_ranges_for_type::<Self>()?;
117 let match_ix = cmp::min(self.active_match_index? + 1, highlighted_ranges.len());
118 let message = if highlighted_ranges.is_empty() {
119 "No matches".to_string()
120 } else {
121 format!("{}/{}", match_ix, highlighted_ranges.len())
122 };
123 Some(
124 Label::new(message, theme.find.match_index.text.clone())
125 .contained()
126 .with_style(theme.find.match_index.container)
127 .aligned()
128 .boxed(),
129 )
130 }))
131 .with_child(
132 Flex::row()
133 .with_child(self.render_nav_button("<", Direction::Prev, cx))
134 .with_child(self.render_nav_button(">", Direction::Next, cx))
135 .aligned()
136 .boxed(),
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 let buffer = editor.buffer().read(cx).read(cx);
516 match ranges.binary_search_by(|probe| {
517 if probe.end.cmp(&position, &*buffer).unwrap().is_lt() {
518 Ordering::Less
519 } else if probe.start.cmp(&position, &*buffer).unwrap().is_gt() {
520 Ordering::Greater
521 } else {
522 Ordering::Equal
523 }
524 }) {
525 Ok(i) | Err(i) => Some(cmp::min(i, ranges.len().saturating_sub(1))),
526 }
527 }
528}
529
530const YIELD_INTERVAL: usize = 20000;
531
532async fn search(
533 buffer: MultiBufferSnapshot,
534 query: String,
535 case_sensitive: bool,
536 whole_word: bool,
537) -> Vec<Range<Anchor>> {
538 let mut ranges = Vec::new();
539
540 let search = AhoCorasickBuilder::new()
541 .auto_configure(&[&query])
542 .ascii_case_insensitive(!case_sensitive)
543 .build(&[&query]);
544 for (ix, mat) in search
545 .stream_find_iter(buffer.bytes_in_range(0..buffer.len()))
546 .enumerate()
547 {
548 if (ix + 1) % YIELD_INTERVAL == 0 {
549 yield_now().await;
550 }
551
552 let mat = mat.unwrap();
553
554 if whole_word {
555 let prev_kind = buffer.reversed_chars_at(mat.start()).next().map(char_kind);
556 let start_kind = char_kind(buffer.chars_at(mat.start()).next().unwrap());
557 let end_kind = char_kind(buffer.reversed_chars_at(mat.end()).next().unwrap());
558 let next_kind = buffer.chars_at(mat.end()).next().map(char_kind);
559 if Some(start_kind) == prev_kind || Some(end_kind) == next_kind {
560 continue;
561 }
562 }
563
564 ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
565 }
566
567 ranges
568}
569
570async fn regex_search(
571 buffer: MultiBufferSnapshot,
572 mut query: String,
573 case_sensitive: bool,
574 whole_word: bool,
575) -> Result<Vec<Range<Anchor>>> {
576 if whole_word {
577 let mut word_query = String::new();
578 word_query.push_str("\\b");
579 word_query.push_str(&query);
580 word_query.push_str("\\b");
581 query = word_query;
582 }
583
584 let mut ranges = Vec::new();
585
586 if query.contains("\n") || query.contains("\\n") {
587 let regex = RegexBuilder::new(&query)
588 .case_insensitive(!case_sensitive)
589 .multi_line(true)
590 .build()?;
591 for (ix, mat) in regex.find_iter(&buffer.text()).enumerate() {
592 if (ix + 1) % YIELD_INTERVAL == 0 {
593 yield_now().await;
594 }
595
596 ranges.push(buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end()));
597 }
598 } else {
599 let regex = RegexBuilder::new(&query)
600 .case_insensitive(!case_sensitive)
601 .build()?;
602
603 let mut line = String::new();
604 let mut line_offset = 0;
605 for (chunk_ix, chunk) in buffer
606 .chunks(0..buffer.len(), None)
607 .map(|c| c.text)
608 .chain(["\n"])
609 .enumerate()
610 {
611 if (chunk_ix + 1) % YIELD_INTERVAL == 0 {
612 yield_now().await;
613 }
614
615 for (newline_ix, text) in chunk.split('\n').enumerate() {
616 if newline_ix > 0 {
617 for mat in regex.find_iter(&line) {
618 let start = line_offset + mat.start();
619 let end = line_offset + mat.end();
620 ranges.push(buffer.anchor_after(start)..buffer.anchor_before(end));
621 }
622
623 line_offset += line.len() + 1;
624 line.clear();
625 }
626 line.push_str(text);
627 }
628 }
629 }
630
631 Ok(ranges)
632}
633
634#[cfg(test)]
635mod tests {
636 use super::*;
637 use editor::{DisplayPoint, Editor, EditorSettings, MultiBuffer};
638 use gpui::{color::Color, TestAppContext};
639 use std::sync::Arc;
640 use unindent::Unindent as _;
641
642 #[gpui::test]
643 async fn test_find_simple(mut cx: TestAppContext) {
644 let fonts = cx.font_cache();
645 let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default());
646 theme.find.match_background = Color::red();
647 let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
648
649 let buffer = cx.update(|cx| {
650 MultiBuffer::build_simple(
651 &r#"
652 A regular expression (shortened as regex or regexp;[1] also referred to as
653 rational expression[2][3]) is a sequence of characters that specifies a search
654 pattern in text. Usually such patterns are used by string-searching algorithms
655 for "find" or "find and replace" operations on strings, or for input validation.
656 "#
657 .unindent(),
658 cx,
659 )
660 });
661 let editor = cx.add_view(Default::default(), |cx| {
662 Editor::new(buffer.clone(), Arc::new(EditorSettings::test), cx)
663 });
664
665 let find_bar = cx.add_view(Default::default(), |cx| {
666 let mut find_bar = FindBar::new(watch::channel_with(settings).1, cx);
667 find_bar.active_item_changed(Some(Box::new(editor.clone())), cx);
668 find_bar
669 });
670
671 // Search for a string that appears with different casing.
672 // By default, search is case-insensitive.
673 find_bar.update(&mut cx, |find_bar, cx| {
674 find_bar.set_query("us", cx);
675 });
676 editor.next_notification(&cx).await;
677 editor.update(&mut cx, |editor, cx| {
678 assert_eq!(
679 editor.all_highlighted_ranges(cx),
680 &[
681 (
682 DisplayPoint::new(2, 17)..DisplayPoint::new(2, 19),
683 Color::red(),
684 ),
685 (
686 DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
687 Color::red(),
688 ),
689 ]
690 );
691 });
692
693 // Switch to a case sensitive search.
694 find_bar.update(&mut cx, |find_bar, cx| {
695 find_bar.toggle_mode(&ToggleMode(SearchMode::CaseSensitive), cx);
696 });
697 editor.next_notification(&cx).await;
698 editor.update(&mut cx, |editor, cx| {
699 assert_eq!(
700 editor.all_highlighted_ranges(cx),
701 &[(
702 DisplayPoint::new(2, 43)..DisplayPoint::new(2, 45),
703 Color::red(),
704 )]
705 );
706 });
707
708 // Search for a string that appears both as a whole word and
709 // within other words. By default, all results are found.
710 find_bar.update(&mut cx, |find_bar, cx| {
711 find_bar.set_query("or", cx);
712 });
713 editor.next_notification(&cx).await;
714 editor.update(&mut cx, |editor, cx| {
715 assert_eq!(
716 editor.all_highlighted_ranges(cx),
717 &[
718 (
719 DisplayPoint::new(0, 24)..DisplayPoint::new(0, 26),
720 Color::red(),
721 ),
722 (
723 DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
724 Color::red(),
725 ),
726 (
727 DisplayPoint::new(2, 71)..DisplayPoint::new(2, 73),
728 Color::red(),
729 ),
730 (
731 DisplayPoint::new(3, 1)..DisplayPoint::new(3, 3),
732 Color::red(),
733 ),
734 (
735 DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
736 Color::red(),
737 ),
738 (
739 DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
740 Color::red(),
741 ),
742 (
743 DisplayPoint::new(3, 60)..DisplayPoint::new(3, 62),
744 Color::red(),
745 ),
746 ]
747 );
748 });
749
750 // Switch to a whole word search.
751 find_bar.update(&mut cx, |find_bar, cx| {
752 find_bar.toggle_mode(&ToggleMode(SearchMode::WholeWord), cx);
753 });
754 editor.next_notification(&cx).await;
755 editor.update(&mut cx, |editor, cx| {
756 assert_eq!(
757 editor.all_highlighted_ranges(cx),
758 &[
759 (
760 DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43),
761 Color::red(),
762 ),
763 (
764 DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13),
765 Color::red(),
766 ),
767 (
768 DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58),
769 Color::red(),
770 ),
771 ]
772 );
773 });
774
775 editor.update(&mut cx, |editor, cx| {
776 editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
777 });
778 find_bar.update(&mut cx, |find_bar, cx| {
779 assert_eq!(find_bar.active_match_index, Some(0));
780 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
781 assert_eq!(
782 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
783 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
784 );
785 });
786 find_bar.read_with(&cx, |find_bar, _| {
787 assert_eq!(find_bar.active_match_index, Some(0));
788 });
789
790 find_bar.update(&mut cx, |find_bar, cx| {
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(3, 11)..DisplayPoint::new(3, 13)]
795 );
796 });
797 find_bar.read_with(&cx, |find_bar, _| {
798 assert_eq!(find_bar.active_match_index, Some(1));
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, 56)..DisplayPoint::new(3, 58)]
806 );
807 });
808 find_bar.read_with(&cx, |find_bar, _| {
809 assert_eq!(find_bar.active_match_index, Some(2));
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(0, 41)..DisplayPoint::new(0, 43)]
817 );
818 });
819 find_bar.read_with(&cx, |find_bar, _| {
820 assert_eq!(find_bar.active_match_index, Some(0));
821 });
822
823 find_bar.update(&mut cx, |find_bar, cx| {
824 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
825 assert_eq!(
826 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
827 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
828 );
829 });
830 find_bar.read_with(&cx, |find_bar, _| {
831 assert_eq!(find_bar.active_match_index, Some(2));
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, 11)..DisplayPoint::new(3, 13)]
839 );
840 });
841 find_bar.read_with(&cx, |find_bar, _| {
842 assert_eq!(find_bar.active_match_index, Some(1));
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(0, 41)..DisplayPoint::new(0, 43)]
850 );
851 });
852 find_bar.read_with(&cx, |find_bar, _| {
853 assert_eq!(find_bar.active_match_index, Some(0));
854 });
855
856 // Park the cursor in between matches and ensure that going to the previous match selects
857 // the closest match to the left.
858 editor.update(&mut cx, |editor, cx| {
859 editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
860 });
861 find_bar.update(&mut cx, |find_bar, cx| {
862 assert_eq!(find_bar.active_match_index, Some(1));
863 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
864 assert_eq!(
865 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
866 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
867 );
868 });
869 find_bar.read_with(&cx, |find_bar, _| {
870 assert_eq!(find_bar.active_match_index, Some(0));
871 });
872
873 // Park the cursor in between matches and ensure that going to the next match selects the
874 // closest match to the right.
875 editor.update(&mut cx, |editor, cx| {
876 editor.select_display_ranges(&[DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)], cx);
877 });
878 find_bar.update(&mut cx, |find_bar, cx| {
879 assert_eq!(find_bar.active_match_index, Some(1));
880 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
881 assert_eq!(
882 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
883 [DisplayPoint::new(3, 11)..DisplayPoint::new(3, 13)]
884 );
885 });
886 find_bar.read_with(&cx, |find_bar, _| {
887 assert_eq!(find_bar.active_match_index, Some(1));
888 });
889
890 // Park the cursor after the last match and ensure that going to the previous match selects
891 // the last match.
892 editor.update(&mut cx, |editor, cx| {
893 editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
894 });
895 find_bar.update(&mut cx, |find_bar, cx| {
896 assert_eq!(find_bar.active_match_index, Some(2));
897 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
898 assert_eq!(
899 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
900 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
901 );
902 });
903 find_bar.read_with(&cx, |find_bar, _| {
904 assert_eq!(find_bar.active_match_index, Some(2));
905 });
906
907 // Park the cursor after the last match and ensure that going to the next match selects the
908 // first match.
909 editor.update(&mut cx, |editor, cx| {
910 editor.select_display_ranges(&[DisplayPoint::new(3, 60)..DisplayPoint::new(3, 60)], cx);
911 });
912 find_bar.update(&mut cx, |find_bar, cx| {
913 assert_eq!(find_bar.active_match_index, Some(2));
914 find_bar.go_to_match(&GoToMatch(Direction::Next), cx);
915 assert_eq!(
916 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
917 [DisplayPoint::new(0, 41)..DisplayPoint::new(0, 43)]
918 );
919 });
920 find_bar.read_with(&cx, |find_bar, _| {
921 assert_eq!(find_bar.active_match_index, Some(0));
922 });
923
924 // Park the cursor before the first match and ensure that going to the previous match
925 // selects the last match.
926 editor.update(&mut cx, |editor, cx| {
927 editor.select_display_ranges(&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)], cx);
928 });
929 find_bar.update(&mut cx, |find_bar, cx| {
930 assert_eq!(find_bar.active_match_index, Some(0));
931 find_bar.go_to_match(&GoToMatch(Direction::Prev), cx);
932 assert_eq!(
933 editor.update(cx, |editor, cx| editor.selected_display_ranges(cx)),
934 [DisplayPoint::new(3, 56)..DisplayPoint::new(3, 58)]
935 );
936 });
937 find_bar.read_with(&cx, |find_bar, _| {
938 assert_eq!(find_bar.active_match_index, Some(2));
939 });
940 }
941}