croft-software 0.2.232

VSCode-style TUI written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! Find and Replace inside one buffer's lines, what the editor's Find bar and
//! the log view search with: each line's matches, the count, the next and
//! previous match from a position, Replace All, and a replacement's case and
//! escapes, each optionally limited to a range (Find in Selection). Nothing
//! here draws; the Find bar is `widgets/editor_find.rs`.

use crate::search::{Pattern, SearchOpts, split_for_highlight};

/// A buffer range as `(start, end)` `(row, char column)` positions.
pub type FindRange = ((usize, usize), (usize, usize));

/// `range` held inside `lines`: rows past the end come back to the last
/// line, columns past a line's end to that end, and the ends in order.
pub fn clamp_range(lines: &[String], range: FindRange) -> FindRange {
    let last = lines.len().saturating_sub(1);
    let fit = |(r, c): (usize, usize)| {
        let r = r.min(last);
        let len = lines.get(r).map_or(0, |l| l.chars().count());
        (r, c.min(len))
    };
    let (a, b) = (fit(range.0), fit(range.1));
    if a <= b { (a, b) } else { (b, a) }
}

/// The char columns of row `row` a find limited to `range` covers
/// (#1577): `None` with no range (the whole line), `Some((0, 0))` for a row
/// outside it.
pub fn find_window(range: Option<FindRange>, row: usize) -> Option<(usize, usize)> {
    let ((r0, c0), (r1, c1)) = range?;
    if row < r0 || row > r1 {
        return Some((0, 0));
    }
    Some((
        if row == r0 { c0 } else { 0 },
        if row == r1 { c1 } else { usize::MAX },
    ))
}

/// Whether the match at `(col, len)` lies wholly inside `window`.
fn in_window(window: Option<(usize, usize)>, col: usize, len: usize) -> bool {
    window.is_none_or(|(lo, hi)| col >= lo && col + len <= hi)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MatchPos {
    pub row: usize,
    pub col_chars: usize,
    pub len_chars: usize,
}

pub fn line_matches(line: &str, opts: SearchOpts, needle: &str) -> Vec<(usize, usize)> {
    let mut out: Vec<(usize, usize)> = Vec::new();
    let mut col_chars = 0usize;
    for (chunk, is_match) in split_for_highlight(line, needle, opts) {
        let chunk_chars = chunk.chars().count();
        // Zero-width matches are invisible: nothing to paint, count, or
        // step to — the find bar's layer skips them even though the split
        // now reports them (the workspace replace preview needs them).
        if is_match && chunk_chars > 0 {
            out.push((col_chars, chunk_chars));
        }
        col_chars = col_chars.saturating_add(chunk_chars);
    }
    out
}

/// [`line_matches`] kept to those wholly inside `window` (see
/// [`find_window`]). The line is matched whole, so a word or line boundary
/// is judged on the real text, never on one the window cut (#1577).
pub fn line_matches_in(
    line: &str,
    opts: SearchOpts,
    needle: &str,
    window: Option<(usize, usize)>,
) -> Vec<(usize, usize)> {
    let mut out = line_matches(line, opts, needle);
    out.retain(|&(col, len)| in_window(window, col, len));
    out
}

pub fn count_matches(lines: &[String], needle: &str, opts: SearchOpts) -> usize {
    count_matches_in(lines, needle, opts, None)
}

/// [`count_matches`] limited to `range` of `lines` (Find in Selection).
pub fn count_matches_in(
    lines: &[String],
    needle: &str,
    opts: SearchOpts,
    range: Option<FindRange>,
) -> usize {
    if needle.is_empty() {
        return 0;
    }
    let mut total = 0usize;
    for (r, line) in lines.iter().enumerate() {
        total =
            total.saturating_add(line_matches_in(line, opts, needle, find_window(range, r)).len());
    }
    total
}

pub fn find_next_match(
    lines: &[String],
    needle: &str,
    opts: SearchOpts,
    from_row: usize,
    from_col_chars: usize,
    skip_current: bool,
) -> Option<MatchPos> {
    find_next_match_in(
        lines,
        needle,
        opts,
        None,
        from_row,
        from_col_chars,
        skip_current,
    )
}

/// [`find_next_match`] over the matches inside `range` of `lines` only.
pub fn find_next_match_in(
    lines: &[String],
    needle: &str,
    opts: SearchOpts,
    range: Option<FindRange>,
    from_row: usize,
    from_col_chars: usize,
    skip_current: bool,
) -> Option<MatchPos> {
    if needle.is_empty() || lines.is_empty() {
        return None;
    }
    let n = lines.len();
    for delta in 0..=n {
        let row = (from_row + delta) % n;
        let matches = line_matches_in(&lines[row], opts, needle, find_window(range, row));
        for (col, len) in matches {
            if delta == 0 && row == from_row {
                let cmp = if skip_current {
                    col <= from_col_chars
                } else {
                    col < from_col_chars
                };
                if cmp {
                    continue;
                }
            }
            return Some(MatchPos {
                row,
                col_chars: col,
                len_chars: len,
            });
        }
        if delta == n {
            break;
        }
    }
    None
}

pub fn find_prev_match(
    lines: &[String],
    needle: &str,
    opts: SearchOpts,
    from_row: usize,
    from_col_chars: usize,
    skip_current: bool,
) -> Option<MatchPos> {
    find_prev_match_in(
        lines,
        needle,
        opts,
        None,
        from_row,
        from_col_chars,
        skip_current,
    )
}

/// [`find_prev_match`] over the matches inside `range` of `lines` only.
pub fn find_prev_match_in(
    lines: &[String],
    needle: &str,
    opts: SearchOpts,
    range: Option<FindRange>,
    from_row: usize,
    from_col_chars: usize,
    skip_current: bool,
) -> Option<MatchPos> {
    if needle.is_empty() || lines.is_empty() {
        return None;
    }
    let n = lines.len();
    for delta in 0..=n {
        let row = (from_row + n - (delta % n)) % n;
        let matches = line_matches_in(&lines[row], opts, needle, find_window(range, row));
        let mut best: Option<(usize, usize)> = None;
        for (col, len) in matches {
            if delta == 0 && row == from_row {
                let cmp = if skip_current {
                    col >= from_col_chars
                } else {
                    col > from_col_chars
                };
                if cmp {
                    continue;
                }
            }
            best = Some((col, len));
        }
        if let Some((col, len)) = best {
            return Some(MatchPos {
                row,
                col_chars: col,
                len_chars: len,
            });
        }
        if delta == n {
            break;
        }
    }
    None
}

/// Compile the find query + `SearchOpts` into the pattern
/// `split_for_highlight` matches with (no trimming — a query with leading /
/// trailing spaces matches literally, like the highlighter), look-around and
/// backreferences included (#1552), so replace touches precisely what the
/// find bar shows.
fn build_find_regex(needle: &str, opts: SearchOpts) -> Option<Pattern> {
    if needle.is_empty() {
        return None;
    }
    let body = if opts.use_regex {
        needle.to_string()
    } else {
        regex::escape(needle)
    };
    Pattern::for_find(&body, opts)
}

/// Expand what the match at `(col_chars, len_chars)` in `line` should be
/// replaced with. Literal mode returns `replacement` verbatim; regex mode
/// expands `$1` / `${name}` capture references against that exact match,
/// like VS Code. Returns `None` when no match starts at `col_chars` with
/// that length (a stale position), so callers never splice blindly.
pub fn expand_replacement(
    line: &str,
    col_chars: usize,
    len_chars: usize,
    needle: &str,
    replacement: &str,
    opts: SearchOpts,
) -> Option<String> {
    // Literal mode asks the find bar's own matcher, which is what put the
    // match on screen. Its whole-word test accepts `$x` in `a $x b`, where
    // the regex's `\b` (between a space and `$`) never matches, so Replace
    // on `$var` or `@user` jumped without ever replacing.
    if !opts.use_regex {
        return line_matches(line, opts, needle)
            .contains(&(col_chars, len_chars))
            .then(|| shaped(line, col_chars, len_chars, replacement.to_string(), opts));
    }
    let re = build_find_regex(needle, opts)?;
    for (start, end) in re.spans(line) {
        if line[..start].chars().count() != col_chars {
            continue;
        }
        if line[start..end].chars().count() != len_chars {
            return None;
        }
        return re
            .expand_at(line, start, end, &unescape_escapes(replacement))
            .map(|dst| shaped(line, col_chars, len_chars, dst, opts));
    }
    None
}

/// `replacement` in the case of the `len_chars` characters at `col_chars`
/// when Preserve Case is on (#1612), as typed otherwise.
fn shaped(
    line: &str,
    col_chars: usize,
    len_chars: usize,
    replacement: String,
    opts: SearchOpts,
) -> String {
    if !opts.preserve_case {
        return replacement;
    }
    let matched: String = line.chars().skip(col_chars).take(len_chars).collect();
    crate::search::preserve_case(&matched, &replacement)
}

/// Translate VS Code's regex-replacement escapes (`\n`, `\t`, `\r`, `\\`)
/// into their characters, unknown escapes passing through; the `$`
/// references are read against the pattern when `Pattern::expand_at`
/// expands them. Literal (non-regex) replacements are never unescaped.
fn unescape_escapes(replacement: &str) -> String {
    let mut out = String::with_capacity(replacement.len());
    let mut it = replacement.chars();
    while let Some(c) = it.next() {
        if c != '\\' {
            out.push(c);
            continue;
        }
        match it.next() {
            Some('n') => out.push('\n'),
            Some('t') => out.push('\t'),
            Some('r') => out.push('\r'),
            Some('\\') => out.push('\\'),
            Some(other) => {
                out.push('\\');
                out.push(other);
            }
            None => out.push('\\'),
        }
    }
    out
}

/// `replacement` shaped after `matched` under Preserve Case (#1612).
fn case_of(matched: &str, replacement: &str, opts: SearchOpts) -> String {
    if opts.preserve_case {
        crate::search::preserve_case(matched, replacement)
    } else {
        replacement.to_string()
    }
}

/// Replace every match of `needle` across `lines` — exactly the matches
/// the find bar counts, paints, and navigates: all of them enumerate via
/// [`split_for_highlight`], so Replace All can never rewrite text the bar
/// refused to show (zero-width regex matches, case foldings the literal
/// scanner cannot map back into the line). Regex replacements expand `$n`
/// capture references and VS Code's `\n`/`\t` escapes; a replacement
/// newline splits the line. Returns `None` when the needle is empty or
/// the pattern can't compile, so callers leave the buffer untouched.
#[cfg(test)]
pub fn replace_all_in_lines(
    lines: &[String],
    needle: &str,
    replacement: &str,
    opts: SearchOpts,
) -> Option<(Vec<String>, usize)> {
    replace_all_in_lines_in(lines, needle, replacement, opts, None)
}

/// [`replace_all_in_lines`] touching only the matches wholly inside `range`
/// of `lines`, found on the whole lines (#1577).
pub fn replace_all_in_lines_in(
    lines: &[String],
    needle: &str,
    replacement: &str,
    opts: SearchOpts,
    range: Option<FindRange>,
) -> Option<(Vec<String>, usize)> {
    if needle.is_empty() {
        return None;
    }
    let re = if opts.use_regex {
        Some(build_find_regex(needle, opts)?)
    } else {
        None
    };
    let unescaped;
    let replacement = if opts.use_regex {
        unescaped = unescape_escapes(replacement);
        unescaped.as_str()
    } else {
        replacement
    };
    let mut total = 0usize;
    let mut out: Vec<String> = Vec::with_capacity(lines.len());
    for (r, line) in lines.iter().enumerate() {
        let window = find_window(range, r);
        let segs = split_for_highlight(line, needle, opts);
        if !segs.iter().any(|(_, is_match)| *is_match) {
            out.push(line.clone());
            continue;
        }
        let mut new = String::new();
        // Byte offset of the current segment in `line`, for captures_at.
        let mut pos = 0usize;
        // Char column of the current segment, for the window test.
        let mut col = 0usize;
        for (chunk, is_match) in &segs {
            let chunk_chars = chunk.chars().count();
            let inside = in_window(window, col, chunk_chars);
            col += chunk_chars;
            if !is_match || chunk.is_empty() || !inside {
                // Zero-width matches stay unreplaced in the find bar (the
                // highlighter shows nothing there); the empty chunk still
                // contributes no text.
                new.push_str(chunk);
            } else {
                total += 1;
                match &re {
                    // The segment came from this same pattern (the two
                    // builders emit identical regexes), so re-running it at
                    // the segment's offset recovers the capture groups. The
                    // guard falls back to the original text rather than
                    // ever splicing a mismatched span.
                    Some(re) => match re.expand_at(line, pos, pos + chunk.len(), replacement) {
                        Some(expanded) => new.push_str(&case_of(chunk, &expanded, opts)),
                        None => new.push_str(chunk),
                    },
                    // Literal replacement goes in verbatim: no capture
                    // references, no escapes.
                    None => new.push_str(&case_of(chunk, replacement, opts)),
                }
            }
            pos += chunk.len();
        }
        out.extend(
            crate::text_edit::normalize_newlines(&new)
                .split('\n')
                .map(str::to_string),
        );
    }
    Some((out, total))
}

/// 1-based "N of M" for the current cursor + first match at-or-after.
pub fn match_index_at(
    lines: &[String],
    needle: &str,
    opts: SearchOpts,
    target_row: usize,
    target_col_chars: usize,
) -> Option<usize> {
    match_index_at_in(lines, needle, opts, None, target_row, target_col_chars)
}

/// [`match_index_at`] numbering only the matches inside `range` of `lines`.
pub fn match_index_at_in(
    lines: &[String],
    needle: &str,
    opts: SearchOpts,
    range: Option<FindRange>,
    target_row: usize,
    target_col_chars: usize,
) -> Option<usize> {
    if needle.is_empty() {
        return None;
    }
    let mut idx = 0usize;
    for (r, line) in lines.iter().enumerate() {
        for (col, _len) in line_matches_in(line, opts, needle, find_window(range, r)) {
            idx += 1;
            if r == target_row && col == target_col_chars {
                return Some(idx);
            }
            if r > target_row {
                return None;
            }
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    fn lines(strs: &[&str]) -> Vec<String> {
        strs.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn non_ascii_case_folds_neither_panic_nor_shift_the_replacement() {
        let opts = SearchOpts::default();
        let lines = vec![String::from("ẞ foo İ"), String::from("\u{212A} foo İİ")];
        assert_eq!(count_matches(&lines, "foo", opts), 2);
        let (out, n) = replace_all_in_lines(&lines, "foo", "X", opts).unwrap();
        assert_eq!(n, 2);
        assert_eq!(out, vec!["ẞ X İ".to_string(), "\u{212A} X İİ".to_string()]);
    }

    #[test]
    fn a_numbered_group_followed_by_a_word_character_still_expands() {
        let opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let (out, _) =
            replace_all_in_lines(&[String::from("foo bar")], "(foo)", "$1_old", opts).unwrap();
        assert_eq!(out, vec!["foo_old bar".to_string()]);
        let (out, _) = replace_all_in_lines(&[String::from("foo")], "foo", "$$1", opts).unwrap();
        assert_eq!(out, vec!["$1".to_string()]);
    }

    /// #1577: a range limits the count to the matches inside it.
    #[test]
    fn a_range_limits_the_count_to_its_matches() {
        let src = lines(&[
            "def a(x):",
            "    return x + 1",
            "def b(x):",
            "    return x * 2",
        ]);
        let x = SearchOpts::default();
        assert_eq!(count_matches_in(&src, "x", x, Some(((2, 0), (3, 16)))), 2);
        assert_eq!(count_matches_in(&src, "x", x, Some(((1, 11), (1, 12)))), 1);
        assert_eq!(count_matches_in(&src, "x", x, Some(((0, 6), (1, 4)))), 1);
        assert_eq!(count_matches_in(&src, "x", x, None), 4);
    }

    /// #1577: matches are found on the whole lines, so the range's edges are
    /// no word or line boundary: selecting `foo` out of `foobar` finds no
    /// whole word `foo` and no `^foo$`, as the highlighter shows none.
    #[test]
    fn a_range_edge_is_not_a_word_or_line_boundary() {
        let src = lines(&["foobar", "foo"]);
        let range = Some(((0, 0), (0, 3)));
        let word = SearchOpts {
            whole_word: true,
            ..SearchOpts::default()
        };
        assert_eq!(count_matches_in(&src, "foo", word, range), 0);
        let re = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        assert_eq!(count_matches_in(&src, "^foo$", re, range), 0);
        assert_eq!(
            replace_all_in_lines_in(&src, "foo", "X", word, range).map(|(_, n)| n),
            Some(0)
        );
        assert_eq!(
            find_next_match_in(&src, "foo", word, range, 0, 0, false),
            None
        );
        // The same text selected whole is a word and a line.
        assert_eq!(
            count_matches_in(&src, "foo", word, Some(((1, 0), (1, 3)))),
            1
        );
    }

    /// #1577: Replace All in a range touches the matches inside it only and
    /// keeps the text either side as it was.
    #[test]
    fn a_range_replace_keeps_the_text_around_it() {
        let src = lines(&["x = x", "y = x + x", "z = x"]);
        let (out, n) = replace_all_in_lines_in(
            &src,
            "x",
            "long",
            SearchOpts::default(),
            Some(((0, 4), (1, 5))),
        )
        .unwrap();
        assert_eq!(n, 2);
        assert_eq!(out, lines(&["x = long", "y = long + x", "z = x"]));
    }

    /// Negative (#1577): a range past the buffer is held inside it and an
    /// inverted one is put in order, rather than panicking.
    #[test]
    fn a_stale_range_is_clamped_not_trusted() {
        let src = lines(&["ab", "cd"]);
        assert_eq!(clamp_range(&src, ((9, 9), (0, 1))), ((0, 1), (1, 2)));
    }

    #[test]
    fn replace_all_touches_exactly_what_the_highlighter_shows() {
        // The find bar's count, paint, and navigation all enumerate via
        // split_for_highlight; Replace All must not rewrite text they
        // never showed. Zero-width regex matches are skipped by the
        // highlighter, and used to be rewritten anyway.
        let re_opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let (out, n) = replace_all_in_lines(&lines(&["banana"]), "a*", "-", re_opts).unwrap();
        assert_eq!(
            (out, n),
            (lines(&["b-n-n-"]), 3),
            "zero-width matches are not replaced"
        );
        let (out, n) = replace_all_in_lines(&lines(&["banana"]), "^", "// ", re_opts).unwrap();
        assert_eq!(
            (out, n),
            (lines(&["banana"]), 0),
            "an all-zero-width pattern replaces nothing"
        );
        let lit = SearchOpts::default();
        // U+212A KELVIN SIGN: lowercasing changes its byte length, so the
        // line is matched on its own bytes by case folding; the highlighter
        // shows the match and Replace All rewrites exactly that.
        let kelvin = "300\u{212A}";
        let segs = split_for_highlight(kelvin, "k", lit);
        assert!(segs.iter().any(|(t, m)| *m && t == "\u{212A}"), "{segs:?}");
        let (out, n) = replace_all_in_lines(&lines(&[kelvin]), "k", "x", lit).unwrap();
        assert_eq!((out, n), (lines(&["300x"]), 1));
    }

    #[test]
    fn regex_replacements_interpret_backslash_escapes() {
        // VS Code's regex replace turns \n and \t into real characters; a
        // replacement newline splits the line. Literal mode stays verbatim.
        let re_opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let (out, n) = replace_all_in_lines(&lines(&["abc"]), "b", r"x\ny", re_opts).unwrap();
        assert_eq!((out, n), (lines(&["ax", "yc"]), 1));
        let (out, _) = replace_all_in_lines(&lines(&["abc"]), "b", r"x\ty", re_opts).unwrap();
        assert_eq!(out, lines(&["ax\tyc"]));
        let (out, _) = replace_all_in_lines(&lines(&["abc"]), "b", r"x\\ny", re_opts).unwrap();
        assert_eq!(
            out,
            lines(&[r"ax\nyc"]),
            "an escaped backslash stays literal"
        );
        let (out, _) =
            replace_all_in_lines(&lines(&["abc"]), "b", r"x\ny", SearchOpts::default()).unwrap();
        assert_eq!(
            out,
            lines(&[r"ax\nyc"]),
            "literal mode inserts the text verbatim"
        );
    }

    #[test]
    fn carriage_returns_in_a_replacement_become_real_line_breaks() {
        // The `\r` escape (and a pasted CRLF) must not leave a bare CR
        // inside a buffer line: a CRLF file would then save `\r\r\n`, and
        // every consumer counting lines would disagree with the text.
        let re_opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let (out, n) = replace_all_in_lines(&lines(&["abc"]), "b", r"x\r\ny", re_opts).unwrap();
        assert_eq!(
            (out, n),
            (lines(&["ax", "yc"]), 1),
            "CRLF splits once, no stray CR"
        );
        let (out, _) = replace_all_in_lines(&lines(&["abc"]), "b", r"x\ry", re_opts).unwrap();
        assert_eq!(out, lines(&["ax", "yc"]), "a lone CR is a line break too");
        for line in replace_all_in_lines(&lines(&["abc"]), "b", r"x\r\ny", re_opts)
            .unwrap()
            .0
        {
            assert!(
                !line.contains('\r'),
                "no CR may survive in a line: {line:?}"
            );
        }
    }

    #[test]
    fn count_matches_returns_zero_for_empty_needle() {
        let buf = lines(&["alpha", "beta"]);
        assert_eq!(count_matches(&buf, "", SearchOpts::default()), 0);
    }

    #[test]
    fn count_matches_counts_every_occurrence_across_lines() {
        let buf = lines(&["alpha alpha", "beta", "alpha gamma"]);
        assert_eq!(count_matches(&buf, "alpha", SearchOpts::default()), 3);
    }

    #[test]
    fn find_next_match_returns_first_match_after_cursor_on_same_line() {
        let buf = lines(&["the quick brown fox"]);
        let m = find_next_match(&buf, "o", SearchOpts::default(), 0, 0, false).unwrap();
        assert_eq!(m.row, 0);
        assert_eq!(m.col_chars, 12);
    }

    #[test]
    fn find_next_match_wraps_around_to_top_when_cursor_is_past_the_last_match() {
        let buf = lines(&["alpha", "beta", "alpha"]);
        let m = find_next_match(&buf, "alpha", SearchOpts::default(), 2, 5, true).unwrap();
        assert_eq!(m.row, 0);
        assert_eq!(m.col_chars, 0);
    }

    #[test]
    fn find_prev_match_returns_closest_match_before_cursor_not_the_leftmost() {
        let buf = lines(&["alpha beta alpha"]);
        let m = find_prev_match(&buf, "alpha", SearchOpts::default(), 0, 12, true).unwrap();
        assert_eq!(m.row, 0);
        assert_eq!(
            m.col_chars, 11,
            "Shift+Enter must walk the user back match-by-match — the nearest match before the cursor (col 11) wins over the leftmost one"
        );
    }

    #[test]
    fn find_prev_match_wraps_backwards_when_cursor_is_before_every_match() {
        let buf = lines(&["alpha", "beta", "alpha"]);
        let m = find_prev_match(&buf, "alpha", SearchOpts::default(), 0, 0, true).unwrap();
        assert_eq!(m.row, 2);
        assert_eq!(m.col_chars, 0);
    }

    #[test]
    fn find_returns_none_when_needle_is_absent() {
        let buf = lines(&["alpha", "beta"]);
        assert!(find_next_match(&buf, "zzz", SearchOpts::default(), 0, 0, true).is_none());
        assert!(find_prev_match(&buf, "zzz", SearchOpts::default(), 0, 0, true).is_none());
    }

    #[test]
    fn expand_replacement_literal_returns_replacement_verbatim() {
        let got = expand_replacement(
            "let alpha = 1;",
            4,
            5,
            "alpha",
            "beta",
            SearchOpts::default(),
        );
        assert_eq!(got.as_deref(), Some("beta"));
    }

    #[test]
    fn expand_replacement_literal_keeps_dollar_verbatim() {
        // A `$` in a literal replacement is inserted as-is, never parsed as
        // a capture reference.
        let got = expand_replacement("price", 0, 5, "price", "$total", SearchOpts::default());
        assert_eq!(got.as_deref(), Some("$total"));
    }

    #[test]
    fn expand_replacement_regex_expands_capture_groups() {
        let opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let got = expand_replacement("foo(1, 2)", 0, 9, r"foo\((\d), (\d)\)", "foo($2, $1)", opts);
        assert_eq!(got.as_deref(), Some("foo(2, 1)"));
    }

    /// #1552: a backreference the find bar now shows is replaced too, by
    /// Replace and Replace All, `$1` and escapes included.
    #[test]
    fn replace_runs_a_backreference_the_find_bar_shows() {
        let opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let got = expand_replacement("saw the the cat", 4, 7, r"(\w+) \1", "[$1]", opts);
        assert_eq!(got.as_deref(), Some("[the]"));
        let buf = lines(&["saw the the cat", "ok ok", "one"]);
        let (new_lines, n) = replace_all_in_lines(&buf, r"(\w+) \1", r"$1\t", opts).unwrap();
        assert_eq!(n, 2);
        assert_eq!(new_lines, lines(&["saw the\t cat", "ok\t", "one"]));
    }

    #[test]
    fn expand_replacement_returns_none_for_a_stale_position() {
        // No match starts at col 1 — a stale MatchPos must never splice.
        let got = expand_replacement("alpha", 1, 5, "alpha", "beta", SearchOpts::default());
        assert_eq!(got, None);
    }

    #[test]
    fn expand_replacement_takes_a_whole_word_match_the_find_bar_shows() {
        let opts = SearchOpts {
            whole_word: true,
            ..SearchOpts::default()
        };
        assert_eq!(line_matches("a $x b", opts, "$x"), vec![(2, 2)]);
        let got = expand_replacement("a $x b", 2, 2, "$x", "$y", opts);
        assert_eq!(got.as_deref(), Some("$y"));
    }

    #[test]
    fn expand_replacement_matches_case_insensitively_by_default() {
        let got = expand_replacement("Alpha beta", 0, 5, "alpha", "gamma", SearchOpts::default());
        assert_eq!(got.as_deref(), Some("gamma"));
    }

    #[test]
    fn replace_all_in_lines_replaces_every_match_and_counts() {
        let buf = lines(&["alpha alpha", "beta", "alpha"]);
        let (new_lines, n) =
            replace_all_in_lines(&buf, "alpha", "x", SearchOpts::default()).unwrap();
        assert_eq!(n, 3);
        assert_eq!(new_lines, lines(&["x x", "beta", "x"]));
    }

    /// #1612: the editor's Replace / Replace All keep each match's case
    /// under Preserve Case, literal and regex alike.
    #[test]
    fn find_bar_replace_preserves_each_match_s_case() {
        let opts = SearchOpts {
            preserve_case: true,
            ..SearchOpts::default()
        };
        let buf = lines(&["UserService USER_TABLE get_user"]);
        let (new_lines, n) = replace_all_in_lines(&buf, "user", "account", opts).unwrap();
        assert_eq!(n, 3);
        assert_eq!(
            new_lines,
            lines(&["AccountService ACCOUNT_TABLE get_account"])
        );
        let got = expand_replacement("x USER_TABLE", 2, 4, "user", "account", opts);
        assert_eq!(got.as_deref(), Some("ACCOUNT"));
        let re = SearchOpts {
            use_regex: true,
            ..opts
        };
        let (new_lines, _) =
            replace_all_in_lines(&lines(&["User_1"]), r"user_(\d)", "account_$1", re).unwrap();
        assert_eq!(new_lines, lines(&["Account_1"]));
        let got = expand_replacement("User_1", 0, 6, r"user_(\d)", "account_$1", re);
        assert_eq!(got.as_deref(), Some("Account_1"));
    }

    /// #1612 negative: Preserve Case off, and a stale position, behave as
    /// before.
    #[test]
    fn find_bar_replace_without_preserve_case_is_as_typed() {
        let buf = lines(&["UserService"]);
        let (new_lines, _) =
            replace_all_in_lines(&buf, "user", "account", SearchOpts::default()).unwrap();
        assert_eq!(new_lines, lines(&["accountService"]));
        let opts = SearchOpts {
            preserve_case: true,
            ..SearchOpts::default()
        };
        assert_eq!(
            expand_replacement("UserService", 3, 4, "user", "x", opts),
            None
        );
    }

    #[test]
    fn replace_all_in_lines_honours_whole_word() {
        let buf = lines(&["cat catalog cat"]);
        let opts = SearchOpts {
            whole_word: true,
            ..SearchOpts::default()
        };
        let (new_lines, n) = replace_all_in_lines(&buf, "cat", "dog", opts).unwrap();
        assert_eq!(n, 2, "'catalog' must survive a whole-word replace");
        assert_eq!(new_lines, lines(&["dog catalog dog"]));
    }

    #[test]
    fn replace_all_in_lines_regex_expands_captures_per_match() {
        let buf = lines(&["a=1 b=2"]);
        let opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let (new_lines, n) = replace_all_in_lines(&buf, r"(\w)=(\d)", "$2:$1", opts).unwrap();
        assert_eq!(n, 2);
        assert_eq!(new_lines, lines(&["1:a 2:b"]));
    }

    /// #1399: the editor's find bar keeps a `$NAME` that is no group as
    /// text, in Replace and Replace All, while `$1` still expands.
    #[test]
    fn a_dollar_name_that_is_no_group_stays_literal_in_the_editor() {
        let opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let got = expand_replacement("cp /opt/app", 3, 8, r"/opt/(\S+)", "$PREFIX/$1", opts);
        assert_eq!(got.as_deref(), Some("$PREFIX/app"));
        let buf = lines(&["cp /opt/app", "cp /opt/app.conf"]);
        let (new_lines, n) =
            replace_all_in_lines(&buf, r"/opt/(\S+)", "${PREFIX}/$1", opts).unwrap();
        assert_eq!(n, 2);
        assert_eq!(
            new_lines,
            lines(&["cp ${PREFIX}/app", "cp ${PREFIX}/app.conf"])
        );
    }

    /// #1636: the editor's find bar keeps a numbered reference with no
    /// group as text, in Replace and Replace All.
    #[test]
    fn a_numbered_reference_with_no_group_stays_literal_in_the_editor() {
        let opts = SearchOpts {
            use_regex: true,
            ..SearchOpts::default()
        };
        let got = expand_replacement("apple", 0, 5, "(apple)", "v$10 costs $5", opts);
        assert_eq!(got.as_deref(), Some("vapple0 costs $5"));
        let buf = lines(&["apple", "apple pie"]);
        let (new_lines, n) = replace_all_in_lines(&buf, "(apple)", "$1 costs $5", opts).unwrap();
        assert_eq!(n, 2);
        assert_eq!(new_lines, lines(&["apple costs $5", "apple costs $5 pie"]));
    }

    #[test]
    fn replace_all_in_lines_returns_none_for_empty_needle() {
        let buf = lines(&["alpha"]);
        assert!(replace_all_in_lines(&buf, "", "x", SearchOpts::default()).is_none());
    }

    #[test]
    fn case_sensitive_opt_distinguishes_cases() {
        let buf = lines(&["Alpha alpha"]);
        let opts = SearchOpts {
            case_sensitive: true,
            ..SearchOpts::default()
        };
        let m = find_next_match(&buf, "alpha", opts, 0, 0, false).unwrap();
        assert_eq!(
            m.col_chars, 6,
            "case-sensitive 'alpha' must skip the leading 'Alpha'"
        );
    }

    /// #1577: the columns of each row a find in a range highlights.
    #[test]
    fn find_window_covers_the_range_rows_only() {
        let range = Some(((2, 4), (4, 3)));
        assert_eq!(find_window(None, 9), None, "no range: the whole line");
        assert_eq!(find_window(range, 1), Some((0, 0)), "above");
        assert_eq!(find_window(range, 2), Some((4, usize::MAX)));
        assert_eq!(find_window(range, 3), Some((0, usize::MAX)));
        assert_eq!(find_window(range, 4), Some((0, 3)));
        assert_eq!(find_window(range, 5), Some((0, 0)), "below");
    }
}