croft-software 0.2.82

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
//! SARIF regions (§3.30) mapped onto the text croft has in hand.
//!
//! A region is expressed in the producer's units: 1-based lines split by
//! `run.newlineSequences`, and columns counted per `run.columnKind`. croft's
//! editor wants 0-based lines and columns counted in `char`s, so every
//! conversion goes through [`TextRange`], whose columns are code points.

use super::model::{Region, Run};

/// §3.14.27. The spec gives no default, so a log that omits it is read as
/// UTF-16: that is what CodeQL, the .NET SDK and VS Code all assume.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnKind {
    Utf16CodeUnits,
    UnicodeCodePoints,
}

pub fn column_kind(run: &Run) -> ColumnKind {
    match run.column_kind.as_deref() {
        Some("unicodeCodePoints") => ColumnKind::UnicodeCodePoints,
        _ => ColumnKind::Utf16CodeUnits,
    }
}

/// §3.14.26: the newline sequences the producer counted lines by, tried
/// greedily in order. Absent means `["\r\n", "\n"]`.
pub fn newline_sequences(run: &Run) -> Vec<String> {
    match &run.newline_sequences {
        Some(seqs) if seqs.iter().any(|s| !s.is_empty()) => {
            seqs.iter().filter(|s| !s.is_empty()).cloned().collect()
        }
        _ => vec!["\r\n".into(), "\n".into()],
    }
}

/// A position in croft terms: 0-based line, 0-based column in `char`s.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Pos {
    pub line: usize,
    pub col: usize,
}

/// A half-open text range. `clamped` is set when the region pointed past the
/// end of the text and had to be pulled back, which usually means the file
/// changed since the log was written.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextRange {
    pub start: Pos,
    pub end: Pos,
    pub clamped: bool,
}

/// How the text was split into lines for a given set of newline sequences.
pub struct Lines<'a> {
    text: &'a str,
    /// `(start, end_excluding_newline, end_including_newline)` byte offsets.
    spans: Vec<(usize, usize, usize)>,
}

impl<'a> Lines<'a> {
    pub fn new(text: &'a str, newlines: &[String]) -> Lines<'a> {
        let mut spans = Vec::new();
        let mut start = 0;
        let mut i = 0;
        let bytes = text.as_bytes();
        while i < bytes.len() {
            match newlines.iter().find(|n| text[i..].starts_with(n.as_str())) {
                Some(n) => {
                    spans.push((start, i, i + n.len()));
                    i += n.len();
                    start = i;
                }
                None => i += text[i..].chars().next().map_or(1, char::len_utf8),
            }
        }
        if start < text.len() {
            spans.push((start, text.len(), text.len()));
        }
        Lines { text, spans }
    }

    pub fn count(&self) -> usize {
        self.spans.len()
    }

    /// Line `n` including its newline; the empty string past the end.
    fn full(&self, n: usize) -> &'a str {
        self.spans.get(n).map_or("", |&(s, _, e)| &self.text[s..e])
    }

    /// Byte offset of `pos` (column in chars) in the whole text.
    fn byte_of(&self, pos: Pos) -> usize {
        match self.spans.get(pos.line) {
            None => self.text.len(),
            Some(&(s, _, e)) => {
                let line = &self.text[s..e];
                s + line
                    .char_indices()
                    .nth(pos.col)
                    .map_or(line.len(), |(b, _)| b)
            }
        }
    }
}

/// Units one `char` occupies under `kind`.
fn width(c: char, kind: ColumnKind) -> usize {
    match kind {
        ColumnKind::Utf16CodeUnits => c.len_utf16(),
        ColumnKind::UnicodeCodePoints => 1,
    }
}

/// Convert a 0-based offset in `kind` units within `s` into a `char` count.
/// Returns `(chars, overflowed)`; an offset inside a surrogate pair rounds up.
fn units_to_chars(s: &str, units: usize, kind: ColumnKind) -> (usize, bool) {
    let mut seen = 0;
    for (n, c) in s.chars().enumerate() {
        if seen >= units {
            return (n, false);
        }
        seen += width(c, kind);
    }
    (s.chars().count(), seen < units)
}

/// A 1-based SARIF line/column (column in `kind` units) as a croft position.
/// `default_end` places a missing column one past the line's last character.
fn line_col(lines: &Lines, line: i64, col: Option<i64>, kind: ColumnKind) -> (Pos, bool) {
    let mut clamped = false;
    let mut l = usize::try_from(line.max(1) - 1).unwrap_or(0);
    let last = lines.count().saturating_sub(1);
    if lines.count() == 0 {
        return (Pos { line: 0, col: 0 }, line > 1);
    }
    if l > last {
        l = last;
        clamped = true;
    }
    let (s, e, _) = lines.spans[l];
    let body = &lines.text[s..e];
    let col = match col {
        None => body.chars().count(),
        Some(c) => {
            let (n, over) = units_to_chars(
                lines.full(l),
                usize::try_from(c.max(1) - 1).unwrap_or(0),
                kind,
            );
            let body_chars = body.chars().count();
            let full_chars = lines.full(l).chars().count();
            // A column may address the newline itself (to include it); one
            // past the whole line is the next line's column 1.
            if over || n > full_chars {
                clamped = true;
                body_chars
            } else if n == full_chars && full_chars > body_chars {
                return (
                    Pos {
                        line: l + 1,
                        col: 0,
                    },
                    clamped,
                );
            } else {
                n
            }
        }
    };
    (Pos { line: l, col }, clamped)
}

/// §3.30.2: resolve a text region. Line/column properties take precedence;
/// `charOffset`/`charLength` are used when `startLine` is absent. `None` for a
/// region with no text properties (binary-only).
pub fn text_range(region: &Region, lines: &Lines, kind: ColumnKind) -> Option<TextRange> {
    if let Some(start_line) = region.start_line.filter(|&l| l > 0) {
        let (start, c1) = line_col(lines, start_line, region.start_column.or(Some(1)), kind);
        let end_line = region.end_line.unwrap_or(start_line).max(start_line);
        let (end, c2) = line_col(lines, end_line, region.end_column, kind);
        let end = end.max(start);
        return Some(TextRange {
            start,
            end,
            clamped: c1 || c2,
        });
    }
    let offset = region.char_offset.filter(|&o| o >= 0)?;
    let length = region.char_length.unwrap_or(0).max(0);
    let (start, c1) = offset_pos(lines, offset as usize, kind);
    let (end, c2) = offset_pos(lines, offset.saturating_add(length) as usize, kind);
    Some(TextRange {
        start,
        end,
        clamped: c1 || c2,
    })
}

/// A 0-based character offset (in `kind` units) as a position.
fn offset_pos(lines: &Lines, units: usize, kind: ColumnKind) -> (Pos, bool) {
    let mut left = units;
    for (n, &(s, _, e)) in lines.spans.iter().enumerate() {
        let full = &lines.text[s..e];
        let w: usize = full.chars().map(|c| width(c, kind)).sum();
        if left < w {
            let (col, _) = units_to_chars(full, left, kind);
            return (Pos { line: n, col }, false);
        }
        left -= w;
    }
    // Exactly at the end is a valid insertion point; beyond it is clamped.
    let at_end = Pos {
        line: lines.count(),
        col: 0,
    };
    let at_end = match lines.spans.last() {
        Some(&(_, e, full_e)) if e == full_e => Pos {
            line: lines.count() - 1,
            col: lines.full(lines.count() - 1).chars().count(),
        },
        _ => at_end,
    };
    (at_end, left > 0)
}

/// A range as byte offsets into the text `lines` was built from.
pub fn byte_span(lines: &Lines, range: &TextRange) -> (usize, usize) {
    let a = lines.byte_of(range.start);
    (a, lines.byte_of(range.end).max(a))
}

/// The text a range covers, for comparing against `region.snippet`.
pub fn slice<'t>(lines: &Lines<'t>, range: &TextRange) -> &'t str {
    let a = lines.byte_of(range.start);
    let b = lines.byte_of(range.end).max(a);
    &lines.text[a..b]
}

/// Re-anchor a region whose file drifted since the log was written: if the
/// text at the stated range no longer equals `snippet.text`, find the
/// occurrence of the snippet nearest the stated start line. `None` when the
/// region has no snippet, already matches, or the snippet is gone.
pub fn reanchor(region: &Region, lines: &Lines, kind: ColumnKind) -> Option<TextRange> {
    let snippet = region.snippet.as_ref()?.text.as_deref()?;
    if snippet.is_empty() {
        return None;
    }
    let stated = text_range(region, lines, kind);
    if let Some(r) = stated
        && !r.clamped
        && slice(lines, &r) == snippet
    {
        return None;
    }
    let near = stated.map_or(0, |r| lines.byte_of(r.start));
    let best = lines
        .text
        .match_indices(snippet)
        .map(|(b, _)| b)
        .min_by_key(|&b| b.abs_diff(near))?;
    let start = pos_of_byte(lines, best);
    let end = pos_of_byte(lines, best + snippet.len());
    Some(TextRange {
        start,
        end,
        clamped: false,
    })
}

/// Where a result's region starts in `text`, the file as it is now, for
/// opening it (#577): the snippet's new place when the file drifted since
/// the log was written ([`reanchor`]), else the stated range, which also
/// covers a region given only by `charOffset`. The bool says the snippet
/// moved it. `None` for a region with no text position at all.
pub fn locate(
    region: &Region,
    context: Option<&Region>,
    text: &str,
    newlines: &[String],
    kind: ColumnKind,
) -> Option<(Pos, bool)> {
    let lines = Lines::new(text, newlines);
    if let Some(moved) = reanchor(region, &lines, kind) {
        return Some((moved.start, true));
    }
    // The region's own snippet is gone or absent, but the surrounding
    // `contextRegion` (§3.29.5) may have moved as a whole: find it, and put
    // the region at the same distance from its start as it was.
    if let (Some(ctx), Some(start_line)) = (context, region.start_line.filter(|&l| l > 0))
        && let Some(ctx_start) = ctx.start_line.filter(|&l| l > 0)
        && start_line >= ctx_start
        && let Some(moved) = reanchor(ctx, &lines, kind)
    {
        let line = moved.start.line + (start_line - ctx_start) as usize;
        if line < lines.count() {
            let col = region.start_column.unwrap_or(1).max(1) as usize - 1;
            let col = col.min(lines.full(line).chars().count());
            return Some((Pos { line, col }, true));
        }
    }
    text_range(region, &lines, kind).map(|r| (r.start, false))
}

fn pos_of_byte(lines: &Lines, byte: usize) -> Pos {
    for (n, &(s, _, e)) in lines.spans.iter().enumerate() {
        if byte < e || (byte == e && n + 1 == lines.count() && byte == lines.text.len()) {
            if byte >= e {
                return Pos {
                    line: n,
                    col: lines.text[s..e].chars().count(),
                };
            }
            return Pos {
                line: n,
                col: lines.text[s..byte].chars().count(),
            };
        }
    }
    Pos {
        line: lines.count(),
        col: 0,
    }
}

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

    /// The spec's own example text (§3.30.2 NOTE 1).
    const SPEC: &str = "abcd\r\nefg\r\nhijk\r\nlmn\r\n";

    fn crlf() -> Vec<String> {
        vec!["\r\n".into(), "\n".into()]
    }

    fn region(json: &str) -> Region {
        serde_json::from_str(json).unwrap()
    }

    fn run(json: &str) -> Run {
        serde_json::from_str(json).unwrap()
    }

    fn text_of(text: &str, json: &str) -> String {
        let lines = Lines::new(text, &crlf());
        let r = text_range(&region(json), &lines, ColumnKind::UnicodeCodePoints).unwrap();
        slice(&lines, &r).to_string()
    }

    #[test]
    fn locate_follows_offsets_and_moved_snippets() {
        let kind = ColumnKind::UnicodeCodePoints;
        // An offset-only region gets its line and column.
        let (pos, moved) =
            locate(&region(r#"{"charOffset":7}"#), None, SPEC, &crlf(), kind).unwrap();
        assert_eq!((pos, moved), (Pos { line: 1, col: 1 }, false));
        // Two lines were inserted above since the scan: the snippet is
        // followed to its new place.
        let now = "new\r\nnew\r\nabcd\r\nefg\r\nhijk\r\n";
        let r = region(r#"{"startLine":2,"startColumn":1,"endColumn":4,"snippet":{"text":"efg"}}"#);
        assert_eq!(
            locate(&r, None, now, &crlf(), kind),
            Some((Pos { line: 3, col: 0 }, true))
        );
        // Unmoved, it is the stated place.
        assert_eq!(
            locate(&r, None, SPEC, &crlf(), kind),
            Some((Pos { line: 1, col: 0 }, false))
        );
        // No text position at all.
        assert_eq!(
            locate(&region(r#"{"byteOffset":3}"#), None, SPEC, &crlf(), kind),
            None
        );
    }

    #[test]
    fn locate_follows_a_moved_context_region_when_the_snippet_is_gone() {
        let kind = ColumnKind::UnicodeCodePoints;
        // The flagged line itself was edited, but the function around it
        // moved down two lines intact.
        let now = "new\r\nnew\r\nfn f() {\r\n    call(y);\r\n}\r\n";
        let r = region(r#"{"startLine":2,"startColumn":5,"snippet":{"text":"call(x);"}}"#);
        let ctx = region(r#"{"startLine":1,"endLine":3,"snippet":{"text":"fn f() {"}}"#);
        assert_eq!(
            locate(&r, Some(&ctx), now, &crlf(), kind),
            Some((Pos { line: 3, col: 4 }, true))
        );
        // Without the context it falls back to the stated place.
        assert_eq!(
            locate(&r, None, now, &crlf(), kind),
            Some((Pos { line: 1, col: 4 }, false))
        );
    }

    // ── run-level settings ──────────────────────────────────────────────

    #[test]
    fn column_kind_defaults_to_utf16() {
        assert_eq!(column_kind(&run("{}")), ColumnKind::Utf16CodeUnits);
        assert_eq!(
            column_kind(&run(r#"{"columnKind":"unicodeCodePoints"}"#)),
            ColumnKind::UnicodeCodePoints
        );
        assert_eq!(
            column_kind(&run(r#"{"columnKind":"utf16CodeUnits"}"#)),
            ColumnKind::Utf16CodeUnits
        );
    }

    #[test]
    fn newline_sequences_default() {
        assert_eq!(newline_sequences(&run("{}")), crlf());
        assert_eq!(
            newline_sequences(&run(r#"{"newlineSequences":["\n","
"]}"#)),
            vec!["\n".to_string(), "\u{2028}".to_string()]
        );
    }

    #[test]
    fn lines_split_greedily() {
        assert_eq!(Lines::new(SPEC, &crlf()).count(), 4);
        // With "\r" listed before "\r\n", "\r\n" is two breaks.
        let odd = vec!["\r".to_string(), "\n".to_string()];
        assert_eq!(Lines::new("a\r\nb", &odd).count(), 3);
        // No trailing newline: the last line still counts.
        assert_eq!(Lines::new("a\nb", &crlf()).count(), 2);
    }

    // ── §3.30.2 examples ────────────────────────────────────────────────

    #[test]
    fn example_1_line_column() {
        assert_eq!(
            text_of(
                SPEC,
                r#"{"startLine":1,"startColumn":2,"endLine":1,"endColumn":4}"#
            ),
            "bc"
        );
    }

    #[test]
    fn example_1_char_offset() {
        assert_eq!(text_of(SPEC, r#"{"charOffset":1,"charLength":2}"#), "bc");
    }

    #[test]
    fn example_3_whole_line_without_newline() {
        assert_eq!(text_of(SPEC, r#"{"startLine":2}"#), "efg");
    }

    #[test]
    fn example_4_two_lines() {
        assert_eq!(
            text_of(SPEC, r#"{"startLine":2,"endLine":3}"#),
            "efg\r\nhijk"
        );
    }

    #[test]
    fn example_5_line_with_its_newline() {
        assert_eq!(
            text_of(SPEC, r#"{"startLine":2,"endLine":3,"endColumn":1}"#),
            "efg\r\n"
        );
    }

    #[test]
    fn example_6_insertion_point() {
        let lines = Lines::new(SPEC, &crlf());
        let r = text_range(
            &region(r#"{"startLine":1,"startColumn":2,"endColumn":2}"#),
            &lines,
            ColumnKind::UnicodeCodePoints,
        )
        .unwrap();
        assert_eq!(r.start, Pos { line: 0, col: 1 });
        assert_eq!(r.end, r.start);
        let r = text_range(
            &region(r#"{"charOffset":1}"#),
            &lines,
            ColumnKind::UnicodeCodePoints,
        )
        .unwrap();
        assert_eq!(
            (r.start, r.end),
            (Pos { line: 0, col: 1 }, Pos { line: 0, col: 1 })
        );
    }

    #[test]
    fn example_8_end_of_file() {
        let lines = Lines::new(SPEC, &crlf());
        let r = text_range(
            &region(r#"{"charOffset":22,"charLength":0}"#),
            &lines,
            ColumnKind::UnicodeCodePoints,
        )
        .unwrap();
        assert!(!r.clamped);
        assert_eq!(slice(&lines, &r), "");
        assert_eq!(r.start, Pos { line: 4, col: 0 });
    }

    #[test]
    fn line_properties_win_over_char_offset() {
        // §3.30.2: startLine > 0 makes it a line/column region.
        assert_eq!(
            text_of(SPEC, r#"{"startLine":3,"charOffset":0,"charLength":2}"#),
            "hijk"
        );
    }

    // ── column units ────────────────────────────────────────────────────

    #[test]
    fn utf16_columns_count_surrogate_pairs_twice() {
        // "😀" is one code point, two UTF-16 units.
        let text = "a😀bc\n";
        let lines = Lines::new(text, &crlf());
        let r = text_range(
            &region(r#"{"startLine":1,"startColumn":4,"endColumn":5}"#),
            &lines,
            ColumnKind::Utf16CodeUnits,
        )
        .unwrap();
        assert_eq!(slice(&lines, &r), "b");
        assert_eq!(r.start, Pos { line: 0, col: 2 });
    }

    #[test]
    fn code_point_columns_count_surrogate_pairs_once() {
        let text = "a😀bc\n";
        let lines = Lines::new(text, &crlf());
        let r = text_range(
            &region(r#"{"startLine":1,"startColumn":3,"endColumn":4}"#),
            &lines,
            ColumnKind::UnicodeCodePoints,
        )
        .unwrap();
        assert_eq!(slice(&lines, &r), "b");
    }

    #[test]
    fn utf16_char_offset() {
        let text = "😀x";
        let lines = Lines::new(text, &crlf());
        let r = text_range(
            &region(r#"{"charOffset":2,"charLength":1}"#),
            &lines,
            ColumnKind::Utf16CodeUnits,
        )
        .unwrap();
        assert_eq!(slice(&lines, &r), "x");
    }

    #[test]
    fn leading_whitespace_is_not_skipped() {
        // VS Code pushes startColumn past indentation; the spec does not.
        assert_eq!(
            text_of(
                "    let x;\n",
                r#"{"startLine":1,"startColumn":1,"endColumn":5}"#
            ),
            "    "
        );
    }

    // ── out of range ────────────────────────────────────────────────────

    #[test]
    fn line_past_end_is_clamped_and_flagged() {
        let lines = Lines::new("one\ntwo\n", &crlf());
        let r = text_range(
            &region(r#"{"startLine":9}"#),
            &lines,
            ColumnKind::UnicodeCodePoints,
        )
        .unwrap();
        assert!(r.clamped);
        assert!(r.start.line < 3);
    }

    #[test]
    fn column_past_end_of_line_is_clamped() {
        let lines = Lines::new("one\ntwo\n", &crlf());
        let r = text_range(
            &region(r#"{"startLine":1,"startColumn":2,"endColumn":40}"#),
            &lines,
            ColumnKind::UnicodeCodePoints,
        )
        .unwrap();
        assert!(r.clamped);
        assert_eq!(slice(&lines, &r), "ne");
    }

    #[test]
    fn binary_only_region_has_no_text_range() {
        let lines = Lines::new(SPEC, &crlf());
        assert!(
            text_range(
                &region(r#"{"byteOffset":4}"#),
                &lines,
                ColumnKind::Utf16CodeUnits
            )
            .is_none()
        );
    }

    // ── re-anchoring ────────────────────────────────────────────────────

    #[test]
    fn reanchor_follows_a_moved_snippet() {
        // The log was written when `bad()` was on line 2; two lines were
        // inserted above it since.
        let text = "fn a() {}\n// new\n// new\n    bad();\n";
        let lines = Lines::new(text, &crlf());
        let r =
            region(r#"{"startLine":2,"startColumn":5,"endColumn":11,"snippet":{"text":"bad();"}}"#);
        let moved = reanchor(&r, &lines, ColumnKind::UnicodeCodePoints).unwrap();
        assert_eq!(moved.start, Pos { line: 3, col: 4 });
        assert_eq!(slice(&lines, &moved), "bad();");
    }

    #[test]
    fn reanchor_prefers_the_nearest_occurrence() {
        let text = "x();\nfiller\nfiller\nfiller\nfiller\nfiller\nstated\nx();\n";
        let lines = Lines::new(text, &crlf());
        // Stated on line 7 ("stated"); the x() on line 8 is nearer than line 1.
        let r =
            region(r#"{"startLine":7,"startColumn":1,"endColumn":5,"snippet":{"text":"x();"}}"#);
        let moved = reanchor(&r, &lines, ColumnKind::UnicodeCodePoints).unwrap();
        assert_eq!(moved.start.line, 7);
    }

    #[test]
    fn reanchor_is_none_when_already_matching_or_gone() {
        let lines = Lines::new("ok();\n", &crlf());
        let here =
            region(r#"{"startLine":1,"startColumn":1,"endColumn":6,"snippet":{"text":"ok();"}}"#);
        assert!(reanchor(&here, &lines, ColumnKind::UnicodeCodePoints).is_none());
        let gone = region(r#"{"startLine":1,"snippet":{"text":"missing"}}"#);
        assert!(reanchor(&gone, &lines, ColumnKind::UnicodeCodePoints).is_none());
        let none = region(r#"{"startLine":1}"#);
        assert!(reanchor(&none, &lines, ColumnKind::UnicodeCodePoints).is_none());
    }

    #[test]
    fn reanchor_multiline_snippet() {
        let text = "a\nb\nfoo(\n  1)\n";
        let lines = Lines::new(text, &crlf());
        let r = region(r#"{"startLine":1,"endLine":2,"snippet":{"text":"foo(\n  1)"}}"#);
        let moved = reanchor(&r, &lines, ColumnKind::UnicodeCodePoints).unwrap();
        assert_eq!(moved.start, Pos { line: 2, col: 0 });
        assert_eq!(moved.end, Pos { line: 3, col: 4 });
    }
}