hjkl-bonsai 0.5.2

Tree-sitter grammar registry + highlighter for the hjkl editor stack
Documentation
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
//! `CommentMarkerPass` — TODO/FIXME/NOTE/WARN comment-marker overlay.
//!
//! After `Highlighter::highlight_range` produces a `Vec<HighlightSpan>`,
//! call `CommentMarkerPass::apply` to append extra spans for marker words
//! (TODO, FIXME, FIX, NOTE, INFO, WARN) found inside comment spans. The
//! added spans carry capture names like `"comment.marker.todo"` and
//! `"comment.marker.tail.todo"` which `DotFallbackTheme` maps to coloured
//! badges.
//!
//! # Inheritance
//!
//! When two single-line comments are *consecutive* (only whitespace /
//! nothing between them), the second comment inherits the active marker
//! colour from the first. This mirrors sqeel's marker-overlay behaviour.
//! Set `with_inheritance(false)` to disable.
//!
//! # Seed scan
//!
//! When the highlight range starts mid-buffer the pass walks up to 500
//! lines backward (string-scan fallback) to seed the inherited colour so
//! the first visible comment already has the right tint.

use std::ops::Range;

use crate::highlighter::HighlightSpan;

// ---------------------------------------------------------------------------
// Public API types
// ---------------------------------------------------------------------------

/// A marker word and the capture names it emits.
#[derive(Clone, Debug)]
pub struct MarkerWord {
    /// The keyword to look for (ASCII, uppercase).
    pub word: &'static str,
    /// Capture name for the label (the word + surrounding badge).
    pub label_capture: &'static str,
    /// Capture name for the tail / continuation text.
    pub tail_capture: &'static str,
}

/// Default marker set.
pub fn default_markers() -> &'static [MarkerWord] {
    &[
        MarkerWord {
            word: "TODO",
            label_capture: "comment.marker.todo",
            tail_capture: "comment.marker.tail.todo",
        },
        MarkerWord {
            word: "FIXME",
            label_capture: "comment.marker.fixme",
            tail_capture: "comment.marker.tail.fixme",
        },
        MarkerWord {
            word: "FIX",
            label_capture: "comment.marker.fixme",
            tail_capture: "comment.marker.tail.fixme",
        },
        MarkerWord {
            word: "NOTE",
            label_capture: "comment.marker.note",
            tail_capture: "comment.marker.tail.note",
        },
        MarkerWord {
            word: "INFO",
            label_capture: "comment.marker.note",
            tail_capture: "comment.marker.tail.note",
        },
        MarkerWord {
            word: "WARN",
            label_capture: "comment.marker.warn",
            tail_capture: "comment.marker.tail.warn",
        },
    ]
}

/// Comment-marker overlay pass.
///
/// Call [`apply`](CommentMarkerPass::apply) after
/// `Highlighter::highlight_range` to splice marker + tail spans into the
/// flat span list. The caller then passes the augmented list to the theme
/// resolver / `build_by_row` as usual.
#[derive(Clone, Debug)]
pub struct CommentMarkerPass {
    markers: Vec<MarkerWord>,
    /// When `true` (default), consecutive single-line comments inherit the
    /// active marker capture from the previous comment line.
    inheritance: bool,
}

impl CommentMarkerPass {
    /// Create with the default marker set and inheritance enabled.
    pub fn new() -> Self {
        Self {
            markers: default_markers().to_vec(),
            inheritance: true,
        }
    }

    /// Replace the marker set.
    pub fn with_markers(mut self, markers: Vec<MarkerWord>) -> Self {
        self.markers = markers;
        self
    }

    /// Enable or disable cross-line inheritance.
    pub fn with_inheritance(mut self, on: bool) -> Self {
        self.inheritance = on;
        self
    }

    /// Append marker / tail spans onto `spans` in place.
    ///
    /// `bytes` is the full document so the pass can do a backward seed scan.
    /// `spans` must already contain comment spans from
    /// `Highlighter::highlight_range`; this pass identifies them by capture
    /// (`"comment"` or any capture starting with `"comment."`).
    pub fn apply(&self, spans: &mut Vec<HighlightSpan>, bytes: &[u8]) {
        // Collect comment spans sorted by start byte.
        let mut comments: Vec<Range<usize>> = spans
            .iter()
            .filter(|s| s.capture() == "comment" || s.capture().starts_with("comment."))
            .map(|s| s.byte_range.clone())
            .collect();
        comments.sort_by_key(|r| r.start);
        comments.dedup_by(|b, a| {
            // Merge overlapping / adjacent comment spans (block comments
            // can produce multiple spans for the same range).
            if b.start < a.end {
                a.end = a.end.max(b.end);
                true
            } else {
                false
            }
        });

        if comments.is_empty() {
            return;
        }

        // Seed the inherited capture by scanning backward from the first
        // comment span.
        let first_start = comments[0].start;
        let mut active: Option<&MarkerWord> = if self.inheritance {
            self.seed_active(bytes, first_start)
        } else {
            None
        };

        let mut extra: Vec<HighlightSpan> = Vec::new();

        let mut prev_end: Option<usize> = None;

        for comment_range in &comments {
            // Check whether this comment is consecutive with the previous one
            // (only whitespace between the two, on adjacent/same lines).
            let consecutive = if let Some(pe) = prev_end {
                self.inheritance && is_consecutive(bytes, pe, comment_range.start)
            } else {
                false
            };

            if !consecutive {
                // Gap — reset inherited colour.
                active = None;
            }

            // Compute body range (skip delimiter).
            let body_start = delimiter_skip(bytes, comment_range.start);
            let body_end = comment_range.end;

            if body_start >= body_end {
                prev_end = Some(comment_range.end);
                continue;
            }

            // Scan for markers in the body.
            let found = scan_markers(bytes, body_start, body_end, &self.markers);

            if found.is_empty() {
                // No marker on this comment — inherit active colour for
                // the whole body.
                if let Some(mw) = active {
                    extra.push(HighlightSpan {
                        byte_range: body_start..body_end,
                        capture: mw.tail_capture.to_string(),
                    });
                }
                prev_end = Some(comment_range.end);
                continue;
            }

            // Emit label + tail spans for each found marker.
            let mut cursor = body_start;
            for m in &found {
                // Tail from cursor to just before this marker's label start.
                let label_start = m.word_start.saturating_sub(1).max(body_start);
                if let Some(mw) = active
                    && cursor < label_start
                {
                    extra.push(HighlightSpan {
                        byte_range: cursor..label_start,
                        capture: mw.tail_capture.to_string(),
                    });
                }
                // Label span: char before marker through end of word.
                extra.push(HighlightSpan {
                    byte_range: label_start..m.word_end,
                    capture: m.marker.label_capture.to_string(),
                });
                // Trail char after the word (e.g. ':').
                let trail_end = if m.word_end < body_end {
                    m.word_end + 1
                } else {
                    m.word_end
                };
                if trail_end > m.word_end {
                    extra.push(HighlightSpan {
                        byte_range: m.word_end..trail_end,
                        capture: m.marker.label_capture.to_string(),
                    });
                }
                cursor = trail_end;
                active = Some(m.marker);
            }
            // Tail after the last marker.
            if let Some(mw) = active
                && cursor < body_end
            {
                extra.push(HighlightSpan {
                    byte_range: cursor..body_end,
                    capture: mw.tail_capture.to_string(),
                });
            }

            prev_end = Some(comment_range.end);
        }

        spans.extend(extra);
    }

    /// Scan backward from `first_comment_start` (up to 500 lines) using a
    /// string-scan fallback to seed the inherited colour.
    fn seed_active<'m>(
        &'m self,
        bytes: &[u8],
        first_comment_start: usize,
    ) -> Option<&'m MarkerWord> {
        const CAP: usize = 500;
        if first_comment_start == 0 {
            return None;
        }
        // Collect the starts of up to CAP lines before first_comment_start.
        let prefix = &bytes[..first_comment_start];
        let newline_positions: Vec<usize> = prefix
            .iter()
            .enumerate()
            .filter(|&(_, b)| *b == b'\n')
            .map(|(i, _)| i)
            .collect();
        let start_nl = newline_positions.len().saturating_sub(CAP);

        let line_starts: Vec<usize> = {
            let mut v = vec![if start_nl == 0 {
                0usize
            } else {
                newline_positions[start_nl - 1] + 1
            }];
            for &nl in &newline_positions[start_nl..] {
                v.push(nl + 1);
            }
            v
        };

        let mut active: Option<&'m MarkerWord> = None;

        for &ls in &line_starts {
            let le = bytes[ls..]
                .iter()
                .position(|&b| b == b'\n')
                .map(|p| ls + p)
                .unwrap_or(first_comment_start);
            let le = le.min(first_comment_start);
            let line_bytes = &bytes[ls..le];
            // String-scan fallback: look for comment delimiters.
            if let Some(del_off) = find_comment_delimiter(line_bytes) {
                let body_start = ls + del_off + 2;
                let body_end = le;
                if body_start < body_end {
                    let found = scan_markers(bytes, body_start, body_end, &self.markers);
                    if let Some(last) = found.last() {
                        active = Some(last.marker);
                    }
                    // else: inherit active unchanged.
                }
            } else {
                // Non-comment line resets.
                active = None;
            }
        }
        active
    }
}

impl Default for CommentMarkerPass {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// A found marker in a comment body.
struct FoundMarker<'m> {
    word_start: usize, // byte offset in `bytes`
    word_end: usize,
    marker: &'m MarkerWord,
}

/// Scan `bytes[body_start..body_end]` for word-boundary occurrences of each
/// marker word. Returns results sorted by `word_start`.
fn scan_markers<'m>(
    bytes: &[u8],
    body_start: usize,
    body_end: usize,
    markers: &'m [MarkerWord],
) -> Vec<FoundMarker<'m>> {
    let end = body_end.min(bytes.len());
    if body_start >= end {
        return Vec::new();
    }
    let body = &bytes[body_start..end];
    let mut out: Vec<FoundMarker<'m>> = Vec::new();

    for mw in markers {
        let wbytes = mw.word.as_bytes();
        let mut i = 0usize;
        while i + wbytes.len() <= body.len() {
            if &body[i..i + wbytes.len()] == wbytes {
                let left_ok = i == 0 || !body[i - 1].is_ascii_alphanumeric();
                let right_ok = body
                    .get(i + wbytes.len())
                    .map(|b| !b.is_ascii_alphanumeric())
                    .unwrap_or(true);
                if left_ok && right_ok {
                    out.push(FoundMarker {
                        word_start: body_start + i,
                        word_end: body_start + i + wbytes.len(),
                        marker: mw,
                    });
                    i += wbytes.len();
                    continue;
                }
            }
            i += 1;
        }
    }
    out.sort_by_key(|m| m.word_start);
    out
}

/// Skip over a known comment delimiter at `pos` in `bytes`.
/// Recognises `--`, `//`, `/*`, `#` (1 byte). Returns the byte offset of the
/// first body character.
fn delimiter_skip(bytes: &[u8], pos: usize) -> usize {
    if pos + 1 < bytes.len() {
        let (a, b) = (bytes[pos], bytes[pos + 1]);
        if (a == b'-' && b == b'-') || (a == b'/' && b == b'/') || (a == b'/' && b == b'*') {
            return pos + 2;
        }
    }
    if pos < bytes.len() && bytes[pos] == b'#' {
        return pos + 1;
    }
    pos
}

/// Returns the offset of the start of a comment delimiter within `line_bytes`
/// (relative to `line_bytes[0]`), or `None` if no delimiter is found.
/// Used by the seed fallback scanner.
fn find_comment_delimiter(line_bytes: &[u8]) -> Option<usize> {
    // Look for `--`, `//`, `/*`, `#`.
    let mut i = 0usize;
    while i + 1 < line_bytes.len() {
        let (a, b) = (line_bytes[i], line_bytes[i + 1]);
        if (a == b'-' && b == b'-') || (a == b'/' && b == b'/') || (a == b'/' && b == b'*') {
            return Some(i);
        }
        i += 1;
    }
    // Single-char `#`.
    if line_bytes.contains(&b'#') {
        return line_bytes.iter().position(|&b| b == b'#');
    }
    None
}

/// Return `true` when the two comment spans are on directly adjacent lines:
/// the gap contains only space / tab / `\r` and exactly one `\n`. A blank
/// line (two or more newlines) breaks the run, so an unrelated comment that
/// follows a blank line does not inherit the previous comment's marker color.
fn is_consecutive(bytes: &[u8], prev_end: usize, next_start: usize) -> bool {
    if prev_end > next_start || next_start > bytes.len() {
        return false;
    }
    let gap = &bytes[prev_end..next_start];
    let mut newlines = 0usize;
    for &b in gap {
        match b {
            b'\n' => {
                newlines += 1;
                if newlines > 1 {
                    return false;
                }
            }
            b' ' | b'\t' | b'\r' => {}
            _ => return false,
        }
    }
    newlines == 1
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Highlighter;
    use crate::runtime::{Grammar, GrammarLoader, LangSpec, ManifestMeta, QuerySource};
    use std::sync::{Arc, OnceLock};

    /// Tree-sitter-rust pinned rev for tests; matches what `bonsai.toml` ships.
    const RUST_GIT: &str = "https://github.com/tree-sitter/tree-sitter-rust";
    const RUST_REV: &str = "e86119bdb4968b9799f6a014ca2401c178d54b5f";

    /// Shared rust [`Grammar`], compiled once across all comment-marker tests
    /// in a single process. Persistent under \$XDG_CACHE_HOME so subsequent
    /// `cargo test --ignored` runs are warm.
    fn rust_grammar() -> Arc<Grammar> {
        static G: OnceLock<Arc<Grammar>> = OnceLock::new();
        G.get_or_init(|| {
            let meta = ManifestMeta {
                helix_repo: "https://github.com/helix-editor/helix".into(),
                helix_rev: "87d5c05c4432a079d3b7aaa10cda1cfe1803c18c".into(),
                nvim_treesitter_repo: "https://github.com/nvim-treesitter/nvim-treesitter".into(),
                nvim_treesitter_rev: "cf12346a3414fa1b06af75c79faebe7f76df080a".into(),
            };
            let loader = GrammarLoader::user_default(&meta).expect("XDG paths");
            let spec = LangSpec {
                git_url: RUST_GIT.into(),
                git_rev: RUST_REV.into(),
                subpath: None,
                extensions: vec!["rs".into()],
                c_files: vec!["src/parser.c".into(), "src/scanner.c".into()],
                query_source: QuerySource::Helix,
                query_subdir: None,
                source: None,
            };
            Arc::new(Grammar::load("rust", &spec, &loader, &meta).expect("rust grammar"))
        })
        .clone()
    }

    fn rust_comment_spans(src: &[u8]) -> Vec<HighlightSpan> {
        let mut h = Highlighter::new(rust_grammar()).unwrap();
        h.parse_initial(src);
        h.highlight_range(src, 0..src.len())
    }

    // Helper: apply pass, return added spans (captures starting with
    // "comment.marker").
    fn marker_spans(src: &[u8]) -> Vec<HighlightSpan> {
        let mut spans = rust_comment_spans(src);
        let pass = CommentMarkerPass::new();
        pass.apply(&mut spans, src);
        spans
            .into_iter()
            .filter(|s| s.capture().starts_with("comment.marker"))
            .collect()
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn single_line_todo_emits_label_and_tail() {
        // "// TODO: refactor" — expect a label span (comment.marker.todo)
        // and a tail span (comment.marker.tail.todo).
        let src = b"// TODO: refactor";
        let ms = marker_spans(src);
        assert!(
            ms.iter().any(|s| s.capture() == "comment.marker.todo"),
            "expected label span; got {ms:#?}"
        );
        assert!(
            ms.iter().any(|s| s.capture() == "comment.marker.tail.todo"),
            "expected tail span; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn multi_line_block_todo_spans_full_body() {
        // /* TODO: long\nexplanation */ — body crosses newline; both label and
        // tail should be present.
        let src = b"/* TODO: long\nexplanation */";
        let ms = marker_spans(src);
        assert!(
            ms.iter().any(|s| s.capture() == "comment.marker.todo"),
            "expected label; got {ms:#?}"
        );
        assert!(
            ms.iter().any(|s| s.capture() == "comment.marker.tail.todo"),
            "expected tail; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn consecutive_single_line_inheritance() {
        // "// TODO foo\n// continuation" — second comment should carry
        // comment.marker.tail.todo from the first.
        let src = b"// TODO foo\n// continuation";
        let ms = marker_spans(src);
        // Second line starts at byte 12; find a tail span there.
        let has_inherited_tail = ms
            .iter()
            .any(|s| s.capture() == "comment.marker.tail.todo" && s.byte_range.start >= 12);
        assert!(
            has_inherited_tail,
            "expected inherited tail on second line; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn inheritance_breaks_on_blank_line() {
        // "// TODO foo\n\n// unrelated" — blank line between comments must
        // reset the active marker color; second comment gets no tail.
        let src = b"// TODO foo\n\n// unrelated";
        let ms = marker_spans(src);
        // Second comment starts at byte 13. Anything at or past 13 is inherited
        // tail leaking across the blank line.
        let leaked = ms
            .iter()
            .any(|s| s.capture() == "comment.marker.tail.todo" && s.byte_range.start >= 13);
        assert!(!leaked, "blank line should break inheritance; got {ms:#?}");
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn inheritance_breaks_on_non_comment_line() {
        // "// TODO\n  let x = 1;\n// next" — third comment has no inherited colour.
        let src = b"// TODO\n  let x = 1;\n// next";
        let ms = marker_spans(src);
        // Comment starting at byte 21 (after "let" line) should have no tail.
        let last_comment_byte = src.iter().rposition(|&b| b == b'/').unwrap_or(0) - 1;
        let inherited = ms.iter().any(|s| {
            s.capture() == "comment.marker.tail.todo" && s.byte_range.start > last_comment_byte
        });
        assert!(
            !inherited,
            "expected no inherited tail on '// next'; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn inheritance_off_does_not_carry() {
        let src = b"// TODO foo\n// continuation";
        let mut spans = rust_comment_spans(src);
        let pass = CommentMarkerPass::new().with_inheritance(false);
        pass.apply(&mut spans, src);
        let ms: Vec<_> = spans
            .into_iter()
            .filter(|s| s.capture().starts_with("comment.marker"))
            .collect();
        // The second comment line should have no marker spans at all.
        let has_second_line_marker = ms.iter().any(|s| s.byte_range.start >= 12);
        assert!(
            !has_second_line_marker,
            "expected no spans on second line with inheritance off; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn marker_word_boundary_no_match() {
        // "TODOlist" and "XTODO" must not trigger.
        let src = b"// TODOlist\n// XTODO";
        let ms = marker_spans(src);
        assert!(
            ms.is_empty(),
            "expected no marker spans for non-boundary words; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn multiple_markers_one_comment() {
        // "// TODO foo FIXME bar" — two label spans, different captures.
        let src = b"// TODO foo FIXME bar";
        let ms = marker_spans(src);
        let has_todo = ms.iter().any(|s| s.capture() == "comment.marker.todo");
        let has_fixme = ms.iter().any(|s| s.capture() == "comment.marker.fixme");
        assert!(has_todo, "expected todo label; got {ms:#?}");
        assert!(has_fixme, "expected fixme label; got {ms:#?}");
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn fixme_marker_emits_correct_capture() {
        let src = b"// FIXME: broken";
        let ms = marker_spans(src);
        assert!(
            ms.iter().any(|s| s.capture() == "comment.marker.fixme"),
            "expected fixme label; got {ms:#?}"
        );
        assert!(
            ms.iter()
                .any(|s| s.capture() == "comment.marker.tail.fixme"),
            "expected fixme tail; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn fix_marker_uses_fixme_capture() {
        let src = b"// FIX: broken";
        let ms = marker_spans(src);
        assert!(
            ms.iter().any(|s| s.capture() == "comment.marker.fixme"),
            "FIX should map to comment.marker.fixme; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn note_and_info_use_note_capture() {
        for word in [b"NOTE" as &[u8], b"INFO"] {
            let src = [b"// ".as_ref(), word, b": context"].concat();
            let ms = marker_spans(&src);
            assert!(
                ms.iter().any(|s| s.capture() == "comment.marker.note"),
                "{} should map to comment.marker.note; got {ms:#?}",
                std::str::from_utf8(word).unwrap()
            );
        }
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn warn_marker_emits_correct_capture() {
        let src = b"// WARN: danger";
        let ms = marker_spans(src);
        assert!(
            ms.iter().any(|s| s.capture() == "comment.marker.warn"),
            "expected warn label; got {ms:#?}"
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn apply_is_idempotent_on_no_comments() {
        // No comment in the source — pass should be a no-op.
        let src = b"fn main() {}";
        let mut spans = rust_comment_spans(src);
        let before = spans.len();
        let pass = CommentMarkerPass::new();
        pass.apply(&mut spans, src);
        let after = spans.len();
        assert_eq!(before, after, "no-comment source should not grow spans");
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn default_pass_is_same_as_new() {
        let a = CommentMarkerPass::new();
        let b = CommentMarkerPass::default();
        assert_eq!(a.inheritance, b.inheritance);
        assert_eq!(a.markers.len(), b.markers.len());
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn scan_markers_word_boundary_left() {
        // "XTODO" — left boundary fails.
        let bytes = b"// XTODO";
        let markers = default_markers();
        let found = scan_markers(bytes, 3, bytes.len(), markers);
        assert!(
            found.is_empty(),
            "XTODO should not match; got {found:?}",
            found = found.iter().map(|m| m.marker.word).collect::<Vec<_>>()
        );
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn scan_markers_word_boundary_right() {
        // "TODOlist" — right boundary fails.
        let bytes = b"// TODOlist";
        let markers = default_markers();
        let found = scan_markers(bytes, 3, bytes.len(), markers);
        assert!(found.is_empty(), "TODOlist should not match");
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn is_consecutive_whitespace_only() {
        let bytes = b"// a\n// b";
        // prev_end=4 (after first comment), next_start=5 (start of second).
        assert!(is_consecutive(bytes, 4, 5));
    }

    #[test]
    #[ignore = "network + compiler: clones tree-sitter-rust then builds it"]
    fn is_consecutive_non_whitespace_between() {
        let bytes = b"// a\nlet x=1;\n// b";
        assert!(!is_consecutive(bytes, 4, 14));
    }
}