1pub(crate) mod dialog;
9mod update;
10
11use crate::buffer::TextBuffer;
12use crate::canvas_editor::{CodeEditor, Message};
13use iced::widget::Id;
14use std::borrow::Cow;
15use std::thread;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct SearchMatch {
22 pub line: usize,
24 pub col: usize,
26}
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum SearchFocusedField {
31 Search,
33 Replace,
35}
36
37#[derive(Debug, Clone)]
41pub struct SearchState {
42 pub query: String,
44 pub replace_with: String,
46 pub case_sensitive: bool,
48 pub is_open: bool,
50 pub is_replace_mode: bool,
52 pub matches: Vec<SearchMatch>,
54 pub current_match_index: Option<usize>,
56 pub search_input_id: Id,
58 pub replace_input_id: Id,
60 pub focused_field: SearchFocusedField,
62 buffer_line_count: usize,
64}
65
66impl Default for SearchState {
67 fn default() -> Self {
68 Self {
69 query: String::new(),
70 replace_with: String::new(),
71 case_sensitive: false,
72 is_open: false,
73 is_replace_mode: false,
74 matches: Vec::new(),
75 current_match_index: None,
76 search_input_id: Id::unique(),
77 replace_input_id: Id::unique(),
78 focused_field: SearchFocusedField::Search,
79 buffer_line_count: 0,
80 }
81 }
82}
83
84impl SearchState {
85 #[must_use]
87 pub fn new() -> Self {
88 Self::default()
89 }
90
91 pub fn open_search(&mut self) {
93 self.is_open = true;
94 self.is_replace_mode = false;
95 self.focused_field = SearchFocusedField::Search;
96 }
97
98 pub fn open_replace(&mut self) {
100 self.is_open = true;
101 self.is_replace_mode = true;
102 self.focused_field = SearchFocusedField::Search;
103 }
104
105 pub fn close(&mut self) {
107 self.is_open = false;
108 }
109
110 pub fn focus_next_field(&mut self) {
112 if self.is_replace_mode {
113 self.focused_field = match self.focused_field {
114 SearchFocusedField::Search => SearchFocusedField::Replace,
115 SearchFocusedField::Replace => SearchFocusedField::Search,
116 };
117 }
118 }
120
121 pub fn focus_previous_field(&mut self) {
123 if self.is_replace_mode {
124 self.focused_field = match self.focused_field {
125 SearchFocusedField::Search => SearchFocusedField::Replace,
126 SearchFocusedField::Replace => SearchFocusedField::Search,
127 };
128 }
129 }
131
132 pub fn set_query(&mut self, query: String, buffer: &TextBuffer) {
134 self.query = query;
135 self.update_matches(buffer);
136 }
137
138 pub fn set_replace_with(&mut self, replace_with: String) {
140 self.replace_with = replace_with;
141 }
142
143 pub fn toggle_case_sensitive(&mut self, buffer: &TextBuffer) {
145 self.case_sensitive = !self.case_sensitive;
146 self.update_matches(buffer);
147 }
148
149 pub fn update_matches(&mut self, buffer: &TextBuffer) {
151 self.matches = find_matches(
152 buffer,
153 &self.query,
154 self.case_sensitive,
155 Some(MAX_MATCHES),
156 );
157 self.buffer_line_count = buffer.line_count();
158
159 if self.matches.is_empty() {
161 self.current_match_index = None;
162 } else if self.current_match_index.is_none() {
163 self.current_match_index = Some(0);
164 } else if let Some(idx) = self.current_match_index {
165 if idx >= self.matches.len() {
167 self.current_match_index =
168 Some(self.matches.len().saturating_sub(1));
169 }
170 }
171 }
172
173 pub(crate) fn update_matches_after_edit(
179 &mut self,
180 buffer: &TextBuffer,
181 start_line: usize,
182 old_end_exclusive: usize,
183 ) {
184 if self.query.is_empty() || self.buffer_line_count == 0 {
185 self.update_matches(buffer);
186 return;
187 }
188
189 let old_line_count = self.buffer_line_count;
190 let new_line_count = buffer.line_count();
191 let start_line = start_line.min(old_line_count).min(new_line_count);
192 let old_end_exclusive =
193 old_end_exclusive.min(old_line_count).max(start_line);
194 let new_end_exclusive = if new_line_count >= old_line_count {
195 old_end_exclusive
196 .saturating_add(new_line_count - old_line_count)
197 .min(new_line_count)
198 } else {
199 old_end_exclusive
200 .saturating_sub(old_line_count - new_line_count)
201 .max(start_line)
202 .min(new_line_count)
203 };
204
205 let replace_start =
206 self.matches.partition_point(|item| item.line < start_line);
207 let replace_end =
208 self.matches.partition_point(|item| item.line < old_end_exclusive);
209 let replacement = find_matches_in_range(
210 buffer,
211 &self.query,
212 self.case_sensitive,
213 start_line,
214 new_end_exclusive,
215 Some(MAX_MATCHES),
216 );
217 let replacement_len = replacement.len();
218
219 self.matches.splice(replace_start..replace_end, replacement);
220 let shifted_suffix_start = replace_start + replacement_len;
221 for item in &mut self.matches[shifted_suffix_start..] {
222 item.line = if new_line_count >= old_line_count {
223 item.line.saturating_add(new_line_count - old_line_count)
224 } else {
225 item.line.saturating_sub(old_line_count - new_line_count)
226 };
227 }
228 self.matches.truncate(MAX_MATCHES);
229 self.buffer_line_count = new_line_count;
230
231 if self.matches.is_empty() {
236 self.current_match_index = None;
237 } else if let Some(idx) = self.current_match_index
238 && idx >= self.matches.len()
239 {
240 self.current_match_index = Some(self.matches.len() - 1);
241 }
242 }
243
244 pub fn next_match(&mut self) {
246 if self.matches.is_empty() {
247 return;
248 }
249
250 self.current_match_index = Some(match self.current_match_index {
251 Some(idx) => {
252 if idx + 1 >= self.matches.len() {
253 0 } else {
255 idx + 1
256 }
257 }
258 None => 0,
259 });
260 }
261
262 pub fn previous_match(&mut self) {
264 if self.matches.is_empty() {
265 return;
266 }
267
268 self.current_match_index = Some(match self.current_match_index {
269 Some(idx) => {
270 if idx == 0 {
271 self.matches.len() - 1 } else {
273 idx - 1
274 }
275 }
276 None => self.matches.len() - 1,
277 });
278 }
279
280 #[must_use]
282 pub fn current_match(&self) -> Option<SearchMatch> {
283 self.current_match_index.and_then(|idx| self.matches.get(idx).copied())
284 }
285
286 #[must_use]
288 pub fn match_count(&self) -> usize {
289 self.matches.len()
290 }
291
292 pub fn select_match_at_cursor(
303 &mut self,
304 cursor: (usize, usize),
305 selection: Option<((usize, usize), (usize, usize))>,
306 ) -> bool {
307 if self.matches.is_empty() || self.query.is_empty() {
308 return false;
309 }
310
311 let query_len = self.query.chars().count();
312 let exact_selection_index = selection.and_then(|(start, end)| {
313 if start.0 != end.0 {
314 return None;
315 }
316
317 self.matches_on_line(start.0).find(|&index| {
318 let match_item = self.matches[index];
319 match_item.col == start.1
320 && match_item.col.saturating_add(query_len) == end.1
321 })
322 });
323 let cursor_index = exact_selection_index.or_else(|| {
324 let line_matches = self.matches_on_line(cursor.0);
325 line_matches
326 .clone()
327 .find(|&index| {
328 let match_item = self.matches[index];
329 (match_item.col..=match_item.col.saturating_add(query_len))
330 .contains(&cursor.1)
331 })
332 .or_else(|| {
333 line_matches.min_by_key(|&index| {
334 let match_item = self.matches[index];
335 let match_end =
336 match_item.col.saturating_add(query_len);
337 if cursor.1 < match_item.col {
338 match_item.col - cursor.1
339 } else {
340 cursor.1.saturating_sub(match_end)
341 }
342 })
343 })
344 });
345
346 let Some(index) = cursor_index else {
347 return false;
348 };
349 if self.current_match_index == Some(index) {
350 return false;
351 }
352
353 self.current_match_index = Some(index);
354 true
355 }
356
357 fn matches_on_line(&self, line: usize) -> std::ops::Range<usize> {
359 let start = self.matches.partition_point(|item| item.line < line);
360 let end = self.matches.partition_point(|item| item.line <= line);
361 start..end
362 }
363
364 pub fn select_match_near_cursor(&mut self, cursor: (usize, usize)) {
369 if self.matches.is_empty() {
370 self.current_match_index = None;
371 return;
372 }
373
374 let (cursor_line, cursor_col) = cursor;
375 let insertion = self.matches.partition_point(|item| {
376 (item.line, item.col) < (cursor_line, cursor_col)
377 });
378 let mut left = insertion.checked_sub(1);
379 let mut right = (insertion < self.matches.len()).then_some(insertion);
380 let mut closest_index = insertion.min(self.matches.len() - 1);
381 let mut closest_distance = usize::MAX;
382
383 while left.is_some() || right.is_some() {
384 let left_line_distance = left.map_or(usize::MAX, |index| {
385 self.matches[index]
386 .line
387 .abs_diff(cursor_line)
388 .saturating_mul(1000)
389 });
390 let right_line_distance = right.map_or(usize::MAX, |index| {
391 self.matches[index]
392 .line
393 .abs_diff(cursor_line)
394 .saturating_mul(1000)
395 });
396
397 if left_line_distance.min(right_line_distance) > closest_distance {
398 break;
399 }
400
401 let index = match (left, right) {
402 (Some(index), Some(_))
403 if left_line_distance <= right_line_distance =>
404 {
405 left = index.checked_sub(1);
406 index
407 }
408 (_, Some(index)) => {
409 right =
410 (index + 1 < self.matches.len()).then_some(index + 1);
411 index
412 }
413 (Some(index), None) => {
414 left = index.checked_sub(1);
415 index
416 }
417 (None, None) => break,
418 };
419 let item = self.matches[index];
420 let distance = item
421 .line
422 .abs_diff(cursor_line)
423 .saturating_mul(1000)
424 .saturating_add(item.col.abs_diff(cursor_col));
425 if distance < closest_distance {
426 closest_distance = distance;
427 closest_index = index;
428 if distance == 0 {
429 break;
430 }
431 }
432 }
433
434 self.current_match_index = Some(closest_index);
435 }
436}
437
438#[must_use]
451pub fn find_matches(
452 buffer: &TextBuffer,
453 query: &str,
454 case_sensitive: bool,
455 limit: Option<usize>,
456) -> Vec<SearchMatch> {
457 if query.is_empty() {
458 return Vec::new();
459 }
460
461 let line_count = buffer.line_count();
462
463 if line_count > PARALLEL_SEARCH_THRESHOLD {
466 let num_threads =
467 std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
468
469 if num_threads > 1 {
470 let chunk_size = line_count.div_ceil(num_threads);
471
472 return thread::scope(|s| {
473 let mut handles = Vec::with_capacity(num_threads);
474
475 for i in 0..num_threads {
476 let start = i * chunk_size;
477 let end = (start + chunk_size).min(line_count);
478
479 if start >= end {
480 break;
481 }
482
483 handles.push(s.spawn(move || {
484 find_matches_in_range(
485 buffer,
486 query,
487 case_sensitive,
488 start,
489 end,
490 limit,
491 )
492 }));
493 }
494
495 let mut matches = Vec::new();
496 for handle in handles {
497 if let Ok(mut chunk_matches) = handle.join() {
498 matches.append(&mut chunk_matches);
499 if let Some(l) = limit
500 && matches.len() >= l
501 {
502 matches.truncate(l);
503 break;
504 }
505 }
506 }
507 matches
508 });
509 }
510 }
511
512 find_matches_in_range(buffer, query, case_sensitive, 0, line_count, limit)
513}
514
515const PARALLEL_SEARCH_THRESHOLD: usize = 1000;
517
518pub const MAX_MATCHES: usize = 10_000;
520
521pub fn get_visible_match_range(
526 matches: &[SearchMatch],
527 min_logical_line: usize,
528 max_logical_line: usize,
529) -> std::ops::Range<usize> {
530 if matches.is_empty() {
531 return 0..0;
532 }
533
534 let start_idx = matches.partition_point(|m| m.line < min_logical_line);
536
537 let mut end_idx = start_idx;
540 for match_item in matches.iter().skip(start_idx) {
541 if match_item.line > max_logical_line {
542 break;
543 }
544 end_idx += 1;
545 }
546
547 start_idx..end_idx
548}
549
550fn find_matches_in_range(
551 buffer: &TextBuffer,
552 query: &str,
553 case_sensitive: bool,
554 start_line: usize,
555 end_line: usize,
556 limit: Option<usize>,
557) -> Vec<SearchMatch> {
558 let mut matches = Vec::new();
559 let search_query = if case_sensitive {
560 Cow::Borrowed(query)
561 } else {
562 Cow::Owned(query.to_lowercase())
563 };
564
565 for line_idx in start_line..end_line {
566 if let Some(l) = limit
568 && matches.len() >= l
569 {
570 break;
571 }
572
573 let line = buffer.line(line_idx);
574
575 if line.len() < query.len() {
577 continue;
578 }
579
580 if case_sensitive {
581 find_matches_case_sensitive(
582 line,
583 search_query.as_ref(),
584 line_idx,
585 &mut matches,
586 );
587 } else {
588 find_matches_case_insensitive(
589 line,
590 search_query.as_ref(),
591 line_idx,
592 &mut matches,
593 );
594 }
595 }
596
597 matches
598}
599
600fn find_matches_case_sensitive(
606 line: &str,
607 search_query: &str,
608 line_idx: usize,
609 matches: &mut Vec<SearchMatch>,
610) {
611 let mut start_pos = 0;
612 while let Some(relative_pos) = line[start_pos..].find(search_query) {
613 let absolute_pos = start_pos + relative_pos;
614 let col = line[..absolute_pos].chars().count();
615 matches.push(SearchMatch { line: line_idx, col });
616 start_pos = absolute_pos + search_query.len();
617 }
618}
619
620fn find_matches_case_insensitive(
632 line: &str,
633 search_query: &str,
634 line_idx: usize,
635 matches: &mut Vec<SearchMatch>,
636) {
637 if line.is_ascii() {
641 let search_line = line.to_ascii_lowercase();
642 let mut start_pos = 0;
643 while let Some(relative_pos) =
644 search_line[start_pos..].find(search_query)
645 {
646 let absolute_pos = start_pos + relative_pos;
647 matches.push(SearchMatch { line: line_idx, col: absolute_pos });
648 start_pos = absolute_pos + search_query.len();
649 }
650 return;
651 }
652
653 let mut search_line = String::with_capacity(line.len());
654 let mut boundaries: Vec<(usize, usize)> =
655 Vec::with_capacity(line.len() + 1);
656 for (char_idx, ch) in line.chars().enumerate() {
657 boundaries.push((search_line.len(), char_idx));
658 for lower_ch in ch.to_lowercase() {
659 search_line.push(lower_ch);
660 }
661 }
662 boundaries.push((search_line.len(), line.chars().count()));
663
664 let mut start_pos = 0;
665 while let Some(relative_pos) = search_line[start_pos..].find(search_query) {
666 let absolute_pos = start_pos + relative_pos;
667 let col = match boundaries
668 .binary_search_by_key(&absolute_pos, |&(byte, _)| byte)
669 {
670 Ok(idx) => boundaries[idx].1,
671 Err(idx) => boundaries[idx.saturating_sub(1)].1,
672 };
673 matches.push(SearchMatch { line: line_idx, col });
674 start_pos = absolute_pos + search_query.len();
675 }
676}
677
678impl CodeEditor {
679 pub(crate) fn refresh_search_matches_if_needed(&mut self) {
685 if self.search_matches_visible() && !self.search_state.query.is_empty()
686 {
687 let start_line = self.pre_edit_line.saturating_sub(1);
688 let old_end_exclusive = self.pre_edit_last_line.saturating_add(2);
689 self.search_state.update_matches_after_edit(
690 &self.buffer,
691 start_line,
692 old_end_exclusive,
693 );
694
695 self.search_state
697 .select_match_near_cursor(self.cursors.primary_position());
698 }
699 }
700
701 pub(crate) fn search_matches_visible(&self) -> bool {
702 self.search_state.is_open
703 || (self.vim_enabled && self.vim_state.last_search().is_some())
704 }
705
706 #[must_use]
730 pub fn is_search_open(&self) -> bool {
731 self.search_state.is_open
732 }
733
734 pub fn open_search_dialog(&mut self) -> iced::Task<Message> {
756 self.update(&Message::OpenSearch)
757 }
758
759 pub fn open_search_replace_dialog(&mut self) -> iced::Task<Message> {
781 self.update(&Message::OpenSearchReplace)
782 }
783
784 pub fn close_search_dialog(&mut self) -> iced::Task<Message> {
803 self.update(&Message::CloseSearch)
804 }
805}
806
807#[cfg(test)]
808mod tests {
809 use super::*;
810
811 #[test]
812 fn test_find_matches_case_sensitive() {
813 let buffer = TextBuffer::new("Hello World\nhello world");
814 let matches = find_matches(&buffer, "hello", true, None);
815
816 assert_eq!(matches.len(), 1);
817 assert_eq!(matches[0].line, 1);
818 assert_eq!(matches[0].col, 0);
819 }
820
821 #[test]
822 fn test_find_matches_case_insensitive() {
823 let buffer = TextBuffer::new("Hello World\nhello world");
824 let matches = find_matches(&buffer, "hello", false, None);
825
826 assert_eq!(matches.len(), 2);
827 assert_eq!(matches[0].line, 0);
828 assert_eq!(matches[0].col, 0);
829 assert_eq!(matches[1].line, 1);
830 assert_eq!(matches[1].col, 0);
831 }
832
833 #[test]
834 fn test_find_matches_case_insensitive_unicode_length_changing() {
835 let buffer = TextBuffer::new("İstanbul");
842 let matches = find_matches(&buffer, "tanbul", false, None);
843
844 assert_eq!(matches.len(), 1);
845 assert_eq!(matches[0].line, 0);
846 assert_eq!(matches[0].col, 2);
847 }
848
849 #[test]
850 fn test_find_matches_case_insensitive_unicode_match_starts_after_expansion()
851 {
852 let buffer = TextBuffer::new("İSTANBUL");
856 let matches = find_matches(&buffer, "istanbul", false, None);
857
858 assert!(matches.is_empty());
862 }
863
864 #[test]
865 fn test_find_matches_case_insensitive_unicode_byte_shrinking() {
866 let buffer = TextBuffer::new("StraẞE");
870 let matches = find_matches(&buffer, "e", false, None);
871
872 assert_eq!(matches.len(), 1);
873 assert_eq!(matches[0].col, 5);
874 }
875
876 #[test]
877 fn test_find_matches_multiple_occurrences() {
878 let buffer = TextBuffer::new("foo bar foo baz foo");
879 let matches = find_matches(&buffer, "foo", false, None);
880
881 assert_eq!(matches.len(), 3);
882 assert_eq!(matches[0].col, 0);
883 assert_eq!(matches[1].col, 8);
884 assert_eq!(matches[2].col, 16);
885 }
886
887 #[test]
888 fn test_find_matches_multiline() {
889 let buffer = TextBuffer::new("line1\nfoo\nline3\nfoo");
890 let matches = find_matches(&buffer, "foo", false, None);
891
892 assert_eq!(matches.len(), 2);
893 assert_eq!(matches[0].line, 1);
894 assert_eq!(matches[1].line, 3);
895 }
896
897 #[test]
898 fn test_find_matches_empty_query() {
899 let buffer = TextBuffer::new("Hello World");
900 let matches = find_matches(&buffer, "", false, None);
901
902 assert_eq!(matches.len(), 0);
903 }
904
905 #[test]
906 fn test_find_matches_no_results() {
907 let buffer = TextBuffer::new("Hello World");
908 let matches = find_matches(&buffer, "xyz", false, None);
909
910 assert_eq!(matches.len(), 0);
911 }
912
913 #[test]
914 fn test_search_state_navigation() {
915 let buffer = TextBuffer::new("foo bar foo baz foo");
916 let mut state = SearchState::new();
917 state.set_query("foo".to_string(), &buffer);
918
919 assert_eq!(state.match_count(), 3);
920 assert_eq!(state.current_match_index, Some(0));
921
922 state.next_match();
923 assert_eq!(state.current_match_index, Some(1));
924
925 state.next_match();
926 assert_eq!(state.current_match_index, Some(2));
927
928 state.next_match();
930 assert_eq!(state.current_match_index, Some(0));
931
932 state.previous_match();
934 assert_eq!(state.current_match_index, Some(2));
935 }
936
937 #[test]
938 fn test_select_match_near_cursor_uses_sorted_candidates() {
939 let buffer = TextBuffer::new("foo\nnone\nfoo bar foo\nnone\nfoo");
940 let mut state = SearchState::new();
941 state.set_query("foo".to_string(), &buffer);
942
943 state.select_match_near_cursor((2, 7));
944 assert_eq!(state.current_match_index, Some(2));
945 assert_eq!(
946 state.current_match(),
947 Some(SearchMatch { line: 2, col: 8 })
948 );
949
950 state.select_match_near_cursor((4, 0));
951 assert_eq!(
952 state.current_match(),
953 Some(SearchMatch { line: 4, col: 0 })
954 );
955 }
956
957 #[test]
958 fn test_search_state_toggle_case() {
959 let buffer = TextBuffer::new("Hello hello");
960 let mut state = SearchState::new();
961 state.set_query("hello".to_string(), &buffer);
962
963 assert_eq!(state.match_count(), 2);
964
965 state.toggle_case_sensitive(&buffer);
966 assert_eq!(state.match_count(), 1);
967
968 state.toggle_case_sensitive(&buffer);
969 assert_eq!(state.match_count(), 2);
970 }
971
972 #[test]
973 fn test_incremental_match_update_replaces_only_affected_lines() {
974 let mut buffer = TextBuffer::new("foo\nfoo\nfoo");
975 let mut state = SearchState::new();
976 state.set_query("foo".to_string(), &buffer);
977
978 buffer.insert_char(1, 1, 'x');
979 state.update_matches_after_edit(&buffer, 1, 2);
980
981 assert_eq!(
982 state.matches,
983 vec![
984 SearchMatch { line: 0, col: 0 },
985 SearchMatch { line: 2, col: 0 },
986 ]
987 );
988 }
989
990 #[test]
991 fn test_incremental_match_update_shifts_suffix_after_newline() {
992 let mut buffer = TextBuffer::new("foo\nbar\nfoo");
993 let mut state = SearchState::new();
994 state.set_query("foo".to_string(), &buffer);
995
996 buffer.insert_newline(0, 0);
997 state.update_matches_after_edit(&buffer, 0, 2);
998
999 assert_eq!(
1000 state.matches,
1001 vec![
1002 SearchMatch { line: 1, col: 0 },
1003 SearchMatch { line: 3, col: 0 },
1004 ]
1005 );
1006 }
1007
1008 #[test]
1009 fn test_incremental_match_update_clamps_index_when_list_shrinks() {
1010 let mut buffer = TextBuffer::new("foo\nfoo\nfoo");
1015 let mut state = SearchState::new();
1016 state.set_query("foo".to_string(), &buffer);
1017 assert_eq!(state.matches.len(), 3);
1018 state.current_match_index = Some(2);
1019
1020 buffer.replace_range(2, 0, 3, "bar");
1022 state.update_matches_after_edit(&buffer, 2, 3);
1023
1024 assert_eq!(state.matches.len(), 2);
1025 assert_eq!(state.current_match_index, Some(1));
1026 assert_eq!(
1027 state.current_match(),
1028 Some(SearchMatch { line: 1, col: 0 })
1029 );
1030 }
1031
1032 #[test]
1033 fn test_find_matches_large_buffer_parallel() {
1034 let mut content = String::new();
1036 let num_lines = PARALLEL_SEARCH_THRESHOLD * 2;
1037 for i in 0..num_lines {
1038 content.push_str(&format!("line {} foo\n", i));
1039 }
1040 let buffer = TextBuffer::new(&content);
1041
1042 let matches = find_matches(&buffer, "foo", false, None);
1043
1044 assert_eq!(matches.len(), num_lines);
1045 assert_eq!(matches[0].line, 0);
1046 assert_eq!(matches[num_lines - 1].line, num_lines - 1);
1047
1048 for (i, m) in matches.iter().enumerate() {
1050 assert_eq!(m.line, i);
1051 }
1052 }
1053
1054 #[test]
1055 fn test_find_matches_limit() {
1056 let mut content = String::new();
1059 for _ in 0..11_000 {
1060 content.push_str("foo\n");
1061 }
1062 let buffer = TextBuffer::new(&content);
1063
1064 let matches = find_matches(&buffer, "foo", false, Some(MAX_MATCHES));
1065
1066 assert_eq!(matches.len(), MAX_MATCHES);
1068 }
1069
1070 #[test]
1071 fn test_get_visible_match_range() {
1072 let matches = vec![
1073 SearchMatch { line: 1, col: 0 },
1074 SearchMatch { line: 2, col: 0 },
1075 SearchMatch { line: 5, col: 0 },
1076 SearchMatch { line: 5, col: 5 },
1077 SearchMatch { line: 10, col: 0 },
1078 ];
1079
1080 assert_eq!(get_visible_match_range(&matches, 0, 15), 0..5);
1082
1083 assert_eq!(get_visible_match_range(&matches, 0, 0), 0..0);
1085
1086 assert_eq!(get_visible_match_range(&matches, 11, 20), 5..5);
1088
1089 assert_eq!(get_visible_match_range(&matches, 3, 4), 2..2);
1091
1092 assert_eq!(get_visible_match_range(&matches, 2, 10), 1..5);
1094
1095 assert_eq!(get_visible_match_range(&matches, 0, 4), 0..2);
1097
1098 assert_eq!(get_visible_match_range(&matches, 2, 5), 1..4);
1100
1101 assert_eq!(get_visible_match_range(&matches, 5, 5), 2..4);
1103 }
1104
1105 #[test]
1106 fn test_get_visible_match_range_empty() {
1107 let matches = vec![];
1108 assert_eq!(get_visible_match_range(&matches, 0, 100), 0..0);
1109 }
1110
1111 #[test]
1112 fn test_is_search_open_tracks_both_ways_of_opening_the_dialog() {
1113 let mut editor = CodeEditor::new("fn main() {}", "rs");
1114 assert!(!editor.is_search_open());
1115
1116 let _task = editor.open_search_dialog();
1117 assert!(editor.is_search_open());
1118
1119 let _task = editor.close_search_dialog();
1120 assert!(!editor.is_search_open());
1121
1122 let _task = editor.open_search_replace_dialog();
1125 assert!(editor.is_search_open());
1126 }
1127}