xbp 10.40.1

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
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
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//! Walk a codebase and extract TODO/FIXME-style markers.

use crate::commands::text_util::normalize_whitespace;
use crate::utils::find_xbp_config_upwards;
use crate::utils::xbp_ignore::{XbpIgnoreSet, DEFAULT_DISCOVERY_SKIP_DIRS};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::Path;
use walkdir::WalkDir;

const MAX_FILE_BYTES: u64 = 1_500_000;
/// Default surrounding window when no construct (function/table/…) is detected.
const CONTEXT_RADIUS: usize = 10;
/// Hard cap so huge functions do not blow issue bodies.
const MAX_CONTEXT_LINES: usize = 120;

// C-style / SQL / Lisp line comments (may trail code).
// `///` / `//!` doc comments are rejected after the match (regex crate has no lookaround).
// `\b` after the kind avoids matching section headers like `// TODOs (`xbp todos`)`.
static TODO_SLASH_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(?i)(?://|--|;)\s*(TODO|FIXME|XXX|HACK)\b(?:\s*\([^)]*\))?\s*:?\s*(.*)$")
        .expect("todo slash regex")
});

// Shell/Python `#` comments only at line start; excludes markdown `##` headers.
static TODO_HASH_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(?i)^\s*#\s*(TODO|FIXME|XXX|HACK)\b(?:\s*\([^)]*\))?\s*:?\s*(.*)$")
        .expect("todo hash regex")
});

static TODO_BLOCK_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"(?i)/\*\s*(TODO|FIXME|XXX|HACK)\b(?:\s*\([^)]*\))?\s*:?\s*(.*?)\*/")
        .expect("todo block regex")
});

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TodoHit {
    pub fingerprint: String,
    pub kind: String,
    pub path: String,
    pub line: usize,
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<String>,
}

pub fn scan_todos(root: &Path) -> Result<Vec<TodoHit>, String> {
    let ignore = build_ignore_set(root);
    let mut hits = Vec::new();

    for entry in WalkDir::new(root)
        .into_iter()
        .filter_entry(|e| {
            let name = e.file_name().to_string_lossy();
            if e.depth() > 0 && DEFAULT_DISCOVERY_SKIP_DIRS.iter().any(|d| *d == name) {
                return false;
            }
            if e.file_type().is_dir() {
                let rel = relative_path(root, e.path());
                if ignore.matches_dir(&rel) {
                    return false;
                }
            }
            true
        })
        .filter_map(|e| e.ok())
    {
        if !entry.file_type().is_file() {
            continue;
        }
        let path = entry.path();
        let rel = relative_path(root, path);
        if ignore.matches_file(&rel) {
            continue;
        }
        if should_skip_file(path) {
            continue;
        }
        let meta = match fs::metadata(path) {
            Ok(m) => m,
            Err(_) => continue,
        };
        if meta.len() > MAX_FILE_BYTES {
            continue;
        }
        let content = match fs::read_to_string(path) {
            Ok(c) => c,
            Err(_) => continue, // binary / non-utf8
        };
        hits.extend(extract_hits_from_content(&rel, &content));
    }

    hits.sort_by(|a, b| {
        a.path
            .cmp(&b.path)
            .then(a.line.cmp(&b.line))
            .then(a.kind.cmp(&b.kind))
    });
    // Dedupe identical fingerprints (keep first/earliest line).
    let mut seen = std::collections::HashSet::new();
    hits.retain(|h| seen.insert(h.fingerprint.clone()));
    Ok(hits)
}

pub fn extract_hits_from_content(rel_path: &str, content: &str) -> Vec<TodoHit> {
    let mut hits = Vec::new();
    let lines: Vec<&str> = content.lines().collect();
    for (idx, line) in lines.iter().enumerate() {
        let line_no = idx + 1;
        // Skip Rust/C++ doc-comment lines entirely (`///`, `//!`).
        let trimmed_line = line.trim_start();
        if trimmed_line.starts_with("///") || trimmed_line.starts_with("//!") {
            continue;
        }

        let caps = TODO_SLASH_RE
            .captures(line)
            .filter(|caps| !is_doc_comment_slash_match(line, caps))
            .filter(|caps| {
                !is_inside_double_quotes(line, caps.get(0).map(|m| m.start()).unwrap_or(0))
            })
            .or_else(|| {
                // Reject markdown headings like `## TODO`
                let trimmed = line.trim_start();
                if trimmed.starts_with("##") {
                    return None;
                }
                TODO_HASH_RE.captures(line)
            })
            .or_else(|| {
                TODO_BLOCK_RE.captures(line).filter(|caps| {
                    !is_inside_double_quotes(line, caps.get(0).map(|m| m.start()).unwrap_or(0))
                })
            });
        let Some(caps) = caps else {
            continue;
        };
        let kind = caps.get(1).map(|m| m.as_str()).unwrap_or("TODO");
        let text = caps.get(2).map(|m| m.as_str()).unwrap_or("").trim();
        if text.is_empty() && kind.eq_ignore_ascii_case("XXX") {
            continue;
        }
        let text = if text.is_empty() {
            format!("({kind} without text)")
        } else {
            text.to_string()
        };
        let context = rich_context(rel_path, &lines, idx);
        hits.push(TodoHit {
            fingerprint: fingerprint(rel_path, kind, &text),
            kind: kind.to_ascii_uppercase(),
            path: rel_path.replace('\\', "/"),
            line: line_no,
            text,
            context: Some(context),
        });
    }
    hits
}

pub fn fingerprint(path: &str, kind: &str, text: &str) -> String {
    let normalized_path = path.replace('\\', "/");
    let normalized_text = normalize_whitespace(text);
    let kind = kind.trim().to_ascii_uppercase();
    let mut hasher = Sha256::new();
    hasher.update(normalized_path.as_bytes());
    hasher.update([0]);
    hasher.update(kind.as_bytes());
    hasher.update([0]);
    hasher.update(normalized_text.as_bytes());
    format!("{:x}", hasher.finalize())
}

/// True when a `// TODO` match is actually inside `///` or `////` (doc comment).
fn is_doc_comment_slash_match(line: &str, caps: &regex::Captures<'_>) -> bool {
    let Some(whole) = caps.get(0) else {
        return false;
    };
    let start = whole.start();
    let bytes = line.as_bytes();
    // Match began at a `//` that is preceded by `/` → part of `///`…
    if start > 0 && bytes[start - 1] == b'/' {
        return true;
    }
    // Or `//` is immediately followed by another `/` before the marker (///TODO).
    if bytes.get(start + 2) == Some(&b'/') {
        return true;
    }
    false
}

/// Rough string-literal check so clap `about = "…// TODO…"` is not filed as a TODO.
fn is_inside_double_quotes(line: &str, index: usize) -> bool {
    let mut in_string = false;
    let mut escaped = false;
    for (i, ch) in line.char_indices() {
        if i >= index {
            break;
        }
        if escaped {
            escaped = false;
            continue;
        }
        if in_string && ch == '\\' {
            escaped = true;
            continue;
        }
        if ch == '"' {
            in_string = !in_string;
        }
    }
    in_string
}

fn context_snippet(lines: &[&str], idx: usize, radius: usize) -> String {
    let start = idx.saturating_sub(radius);
    let end = (idx + radius + 1).min(lines.len());
    format_context_slice(lines, start, end)
}

/// Prefer a full surrounding construct (function / table / rule) when the TODO
/// sits above or inside one; otherwise fall back to ±`CONTEXT_RADIUS` lines.
fn rich_context(rel_path: &str, lines: &[&str], idx: usize) -> String {
    let lang = ContextLang::from_path(rel_path);
    if let Some((start, end)) = expand_construct_range(lines, idx, lang) {
        return format_context_slice(lines, start, end);
    }
    context_snippet(lines, idx, CONTEXT_RADIUS)
}

fn format_context_slice(lines: &[&str], start: usize, end: usize) -> String {
    let start = start.min(lines.len());
    let mut end = end.min(lines.len()).max(start);
    // Truncate from the bottom if the construct is enormous.
    if end.saturating_sub(start) > MAX_CONTEXT_LINES {
        end = start + MAX_CONTEXT_LINES;
    }
    let last_line_no = end.max(1);
    let width = ((last_line_no as f64).log10().floor() as usize) + 1;
    lines[start..end]
        .iter()
        .enumerate()
        .map(|(offset, line)| {
            let line_no = start + offset + 1;
            format!("{line_no:>width$} | {line}")
        })
        .collect::<Vec<_>>()
        .join("\n")
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ContextLang {
    Rust,
    JsTs,
    Python,
    Sql,
    Css,
    Go,
    Generic,
}

impl ContextLang {
    fn from_path(path: &str) -> Self {
        let lower = path.replace('\\', "/").to_ascii_lowercase();
        let ext = lower.rsplit('.').next().unwrap_or("");
        match ext {
            "rs" => Self::Rust,
            "js" | "jsx" | "mjs" | "cjs" | "ts" | "tsx" | "mts" | "cts" => Self::JsTs,
            "py" | "pyi" => Self::Python,
            "sql" | "pgsql" | "psql" => Self::Sql,
            "css" | "scss" | "sass" | "less" => Self::Css,
            "go" => Self::Go,
            _ => Self::Generic,
        }
    }
}

/// Expand to the full construct the TODO is above or inside.
/// Returns 0-based [start, end) line indices.
fn expand_construct_range(
    lines: &[&str],
    hit_idx: usize,
    lang: ContextLang,
) -> Option<(usize, usize)> {
    // 1) Prefer a construct that *starts* shortly after the TODO (comment above fn/table).
    if let Some(decl) = find_following_declaration(lines, hit_idx, lang) {
        if let Some(end) = find_construct_end(lines, decl, lang) {
            let start = hit_idx; // include the TODO line itself
            if end > start {
                return Some((start, (end + 1).min(lines.len())));
            }
        }
    }

    // 2) Else, if the TODO is *inside* a construct, expand to that whole body.
    if let Some(decl) = find_enclosing_declaration(lines, hit_idx, lang) {
        if let Some(end) = find_construct_end(lines, decl, lang) {
            if end >= hit_idx {
                // Include a few leading comment/doc lines above the declaration.
                let start = expand_leading_comments(lines, decl);
                return Some((start, (end + 1).min(lines.len())));
            }
        }
    }

    None
}

fn find_following_declaration(lines: &[&str], hit_idx: usize, lang: ContextLang) -> Option<usize> {
    // Only treat as "above a function" when the next real code arrives soon.
    let search_limit = (hit_idx + 1 + 12).min(lines.len());
    let mut i = hit_idx + 1;
    while i < search_limit {
        let trimmed = lines[i].trim();
        if trimmed.is_empty()
            || is_comment_only_line(trimmed, lang)
            || is_attribute_or_decorator(trimmed, lang)
        {
            i += 1;
            continue;
        }
        if is_declaration_start(trimmed, lang) {
            return Some(i);
        }
        // Hit other real code → not sitting above a construct.
        return None;
    }
    None
}

fn is_attribute_or_decorator(trimmed: &str, lang: ContextLang) -> bool {
    match lang {
        ContextLang::Rust => trimmed.starts_with("#[") || trimmed.starts_with("#!["),
        ContextLang::Python | ContextLang::JsTs => trimmed.starts_with('@'),
        _ => false,
    }
}

fn find_enclosing_declaration(lines: &[&str], hit_idx: usize, lang: ContextLang) -> Option<usize> {
    // Walk upward for a declaration whose body still covers hit_idx.
    let mut i = hit_idx;
    let mut scanned = 0usize;
    while i > 0 && scanned < MAX_CONTEXT_LINES {
        i -= 1;
        scanned += 1;
        let trimmed = lines[i].trim();
        if !is_declaration_start(trimmed, lang) {
            continue;
        }
        if let Some(end) = find_construct_end(lines, i, lang) {
            if end >= hit_idx {
                return Some(i);
            }
        }
    }
    None
}

fn expand_leading_comments(lines: &[&str], decl_idx: usize) -> usize {
    let mut start = decl_idx;
    while start > 0 {
        let prev = lines[start - 1].trim();
        if prev.is_empty()
            || prev.starts_with("//")
            || prev.starts_with('#')
            || prev.starts_with("/*")
            || prev.starts_with('*')
            || prev.starts_with("///")
            || prev.starts_with("//!")
            || prev.starts_with("--")
        {
            start -= 1;
            // Stop after collecting a reasonable doc block.
            if decl_idx.saturating_sub(start) > 20 {
                break;
            }
            continue;
        }
        break;
    }
    start
}

fn is_comment_only_line(trimmed: &str, lang: ContextLang) -> bool {
    match lang {
        ContextLang::Python => trimmed.starts_with('#'),
        ContextLang::Sql => trimmed.starts_with("--") || trimmed.starts_with("/*"),
        ContextLang::Css => trimmed.starts_with("/*") || trimmed.starts_with("//"),
        _ => {
            trimmed.starts_with("//")
                || trimmed.starts_with("/*")
                || trimmed.starts_with('*')
                || trimmed.starts_with('#')
                || trimmed.starts_with("--")
        }
    }
}

fn is_declaration_start(trimmed: &str, lang: ContextLang) -> bool {
    if trimmed.is_empty() {
        return false;
    }
    match lang {
        ContextLang::Rust => is_rust_declaration(trimmed),
        ContextLang::JsTs => is_js_declaration(trimmed),
        ContextLang::Python => is_python_declaration(trimmed),
        ContextLang::Sql => is_sql_declaration(trimmed),
        ContextLang::Css => is_css_rule_start(trimmed),
        ContextLang::Go => is_go_declaration(trimmed),
        ContextLang::Generic => {
            is_rust_declaration(trimmed)
                || is_js_declaration(trimmed)
                || is_python_declaration(trimmed)
                || is_sql_declaration(trimmed)
                || is_go_declaration(trimmed)
        }
    }
}

fn is_rust_declaration(trimmed: &str) -> bool {
    // Strip common visibility / qualifiers prefixes loosely.
    let t = trimmed.trim_start_matches("pub(crate) ").trim_start();
    let t = t.trim_start_matches("pub ").trim_start();
    let t = t.trim_start_matches("async ").trim_start();
    let t = t.trim_start_matches("const ").trim_start();
    let t = t.trim_start_matches("unsafe ").trim_start();
    t.starts_with("fn ")
        || t.starts_with("fn(") // unlikely but
        || t.starts_with("struct ")
        || t.starts_with("enum ")
        || t.starts_with("impl ")
        || t.starts_with("trait ")
        || t.starts_with("mod ")
        || t.starts_with("type ")
}

fn is_js_declaration(trimmed: &str) -> bool {
    let t = strip_js_export_prefix(trimmed.trim_start());
    if t.starts_with("function ")
        || t.starts_with("function*")
        || t.starts_with("class ")
        || t.starts_with("interface ")
        || t.starts_with("type ")
        || t.starts_with("enum ")
        || t.starts_with("namespace ")
    {
        return true;
    }
    // const foo = (…) => / function / async
    if (t.starts_with("const ") || t.starts_with("let ") || t.starts_with("var "))
        && (t.contains("=>") || t.contains("function") || t.contains("= (") || t.contains("=("))
    {
        return true;
    }
    // method / constructor-ish: name(...) {  on its own line
    is_js_method_header(t)
}

fn strip_js_export_prefix(mut t: &str) -> &str {
    loop {
        let next = t
            .strip_prefix("export ")
            .or_else(|| t.strip_prefix("default "))
            .or_else(|| t.strip_prefix("async "))
            .or_else(|| t.strip_prefix("declare "))
            .or_else(|| t.strip_prefix("public "))
            .or_else(|| t.strip_prefix("private "))
            .or_else(|| t.strip_prefix("protected "))
            .or_else(|| t.strip_prefix("static "))
            .or_else(|| t.strip_prefix("readonly "));
        match next {
            Some(rest) => t = rest.trim_start(),
            None => break,
        }
    }
    t
}

fn is_js_method_header(t: &str) -> bool {
    // Rough: ident(args) {   or   ident(args)
    // Avoid if/for/while/switch.
    let t = t.trim();
    if t.starts_with("if ")
        || t.starts_with("if(")
        || t.starts_with("for ")
        || t.starts_with("for(")
        || t.starts_with("while ")
        || t.starts_with("while(")
        || t.starts_with("switch ")
        || t.starts_with("switch(")
        || t.starts_with("catch ")
        || t.starts_with("catch(")
    {
        return false;
    }
    let Some(open) = t.find('(') else {
        return false;
    };
    let name = t[..open].trim();
    if name.is_empty()
        || !name
            .chars()
            .next()
            .is_some_and(|c| c.is_ascii_alphabetic() || c == '_' || c == '$')
    {
        return false;
    }
    if name.contains('=') || name.contains(':') {
        return false;
    }
    // Prefer lines that open a body soon.
    t.contains('{') || t.ends_with(')') || t.ends_with('{')
}

fn is_python_declaration(trimmed: &str) -> bool {
    let t = trimmed.trim_start();
    t.starts_with("def ")
        || t.starts_with("async def ")
        || t.starts_with("class ")
        || t.starts_with("@") // decorator leading into def/class
}

fn is_sql_declaration(trimmed: &str) -> bool {
    let upper = trimmed.to_ascii_uppercase();
    upper.starts_with("CREATE TABLE")
        || upper.starts_with("CREATE OR REPLACE")
        || upper.starts_with("CREATE FUNCTION")
        || upper.starts_with("CREATE PROCEDURE")
        || upper.starts_with("CREATE VIEW")
        || upper.starts_with("CREATE INDEX")
        || upper.starts_with("CREATE UNIQUE INDEX")
        || upper.starts_with("CREATE TYPE")
        || upper.starts_with("ALTER TABLE")
        || upper.starts_with("CREATE SCHEMA")
}

fn is_css_rule_start(trimmed: &str) -> bool {
    if trimmed.starts_with('}') || trimmed.starts_with("//") || trimmed.starts_with("/*") {
        return false;
    }
    // @media / @keyframes / @supports
    if trimmed.starts_with('@') {
        return true;
    }
    if trimmed.contains('{') {
        return true;
    }
    // Multi-line selector list
    if trimmed.ends_with(',') && !trimmed.contains(':') {
        return true;
    }
    // Bare selector line (class/id/element) without a property semicolon.
    let looks_like_selector = trimmed.starts_with('.')
        || trimmed.starts_with('#')
        || trimmed.starts_with('[')
        || trimmed
            .chars()
            .next()
            .is_some_and(|c| c.is_ascii_alphabetic());
    looks_like_selector && !trimmed.contains(';')
}

fn is_go_declaration(trimmed: &str) -> bool {
    let t = trimmed.trim_start();
    t.starts_with("func ") || t.starts_with("type ") || t.starts_with("package ")
}

fn find_construct_end(lines: &[&str], decl_idx: usize, lang: ContextLang) -> Option<usize> {
    match lang {
        ContextLang::Python => find_python_block_end(lines, decl_idx),
        ContextLang::Sql => find_sql_statement_end(lines, decl_idx),
        ContextLang::Css => find_brace_block_end(lines, decl_idx),
        ContextLang::Rust | ContextLang::JsTs | ContextLang::Go | ContextLang::Generic => {
            // Prefer braces; if the declaration line has no `{` soon, scan a few lines.
            if let Some(end) = find_brace_block_end(lines, decl_idx) {
                return Some(end);
            }
            // Single-line / semicolon terminated (e.g. `type Foo = Bar;`)
            find_semicolon_end(lines, decl_idx)
        }
    }
}

fn find_brace_block_end(lines: &[&str], decl_idx: usize) -> Option<usize> {
    let mut depth = 0i32;
    let mut seen_open = false;
    let limit = (decl_idx + MAX_CONTEXT_LINES).min(lines.len());
    for i in decl_idx..limit {
        let line = lines[i];
        for ch in line.chars() {
            match ch {
                '{' => {
                    depth += 1;
                    seen_open = true;
                }
                '}' => {
                    depth -= 1;
                    if seen_open && depth == 0 {
                        return Some(i);
                    }
                }
                _ => {}
            }
        }
    }
    None
}

fn find_semicolon_end(lines: &[&str], decl_idx: usize) -> Option<usize> {
    let limit = (decl_idx + 30).min(lines.len());
    for i in decl_idx..limit {
        if lines[i].contains(';') {
            return Some(i);
        }
    }
    None
}

fn find_python_block_end(lines: &[&str], decl_idx: usize) -> Option<usize> {
    // Skip decorators to the actual def/class when starting on @.
    let mut start = decl_idx;
    while start < lines.len() {
        let t = lines[start].trim_start();
        if t.starts_with('@') {
            start += 1;
            continue;
        }
        break;
    }
    if start >= lines.len() {
        return None;
    }
    let header = lines[start];
    let base_indent = leading_indent(header);
    if !header.trim_end().ends_with(':')
        && !lines
            .get(start)
            .map(|l| l.contains("def ") || l.contains("class "))
            .unwrap_or(false)
    {
        // Still accept multi-line headers; fall through.
    }
    let limit = (start + MAX_CONTEXT_LINES).min(lines.len());
    let mut last = start;
    for i in (start + 1)..limit {
        let line = lines[i];
        if line.trim().is_empty() {
            last = i;
            continue;
        }
        let indent = leading_indent(line);
        if indent <= base_indent && !line.trim_start().starts_with('#') {
            // Dedented to same or outer level → block ended on previous non-empty.
            return Some(last);
        }
        last = i;
    }
    Some(last)
}

fn leading_indent(line: &str) -> usize {
    line.chars()
        .take_while(|c| *c == ' ' || *c == '\t')
        .map(|c| if c == '\t' { 4 } else { 1 })
        .sum()
}

fn find_sql_statement_end(lines: &[&str], decl_idx: usize) -> Option<usize> {
    let mut paren = 0i32;
    let limit = (decl_idx + MAX_CONTEXT_LINES).min(lines.len());
    for i in decl_idx..limit {
        for ch in lines[i].chars() {
            match ch {
                '(' => paren += 1,
                ')' => paren -= 1,
                ';' if paren <= 0 => return Some(i),
                _ => {}
            }
        }
    }
    // No semicolon — take until blank line after content or max window.
    let mut last = decl_idx;
    for i in decl_idx..limit {
        if lines[i].trim().is_empty() && i > decl_idx {
            return Some(last);
        }
        last = i;
    }
    Some(last)
}

fn relative_path(root: &Path, path: &Path) -> String {
    path.strip_prefix(root)
        .unwrap_or(path)
        .to_string_lossy()
        .replace('\\', "/")
}

fn should_skip_file(path: &Path) -> bool {
    let name = path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("")
        .to_ascii_lowercase();
    if name.ends_with(".lock")
        || name.ends_with(".min.js")
        || name.ends_with(".min.css")
        || name.ends_with(".map")
        || name.ends_with(".png")
        || name.ends_with(".jpg")
        || name.ends_with(".jpeg")
        || name.ends_with(".gif")
        || name.ends_with(".webp")
        || name.ends_with(".ico")
        || name.ends_with(".woff")
        || name.ends_with(".woff2")
        || name.ends_with(".ttf")
        || name.ends_with(".pdf")
        || name.ends_with(".zip")
        || name == "cargo.lock"
        || name == "pnpm-lock.yaml"
        || name == "package-lock.json"
        || name == "yarn.lock"
        || name == "todo-issues.json"
        || name == "ledger.json"
        || name == "path-index.json"
        || name == "effort.json"
        || name == "catalog.json"
        // Docs / trackers — not actionable code TODOs
        || name == "todo.md"
        || name == "readme.md"
        || name == "changelog.md"
        || name == "agents.md"
        || name.ends_with(".mdx")
    {
        return true;
    }
    // Skip generated MCP catalog path segments
    let rel = path
        .to_string_lossy()
        .replace('\\', "/")
        .to_ascii_lowercase();
    if rel.contains("/generated/catalog.json") || rel.contains("/target/") {
        return true;
    }
    false
}

fn build_ignore_set(root: &Path) -> XbpIgnoreSet {
    let mut set =
        XbpIgnoreSet::from_patterns(DEFAULT_DISCOVERY_SKIP_DIRS.iter().map(|d| format!("{d}/")));
    for candidate in [
        root.join(".xbpignore"),
        root.join(".xbp").join(".xbpignore"),
        root.join(".gitignore"),
    ] {
        if let Ok(content) = fs::read_to_string(candidate) {
            let other = XbpIgnoreSet::from_file_content(&content);
            set.merge(&other);
        }
    }
    if let Some(found) = find_xbp_config_upwards(root) {
        if let Ok(content) = fs::read_to_string(&found.config_path) {
            if let Ok(cfg) = serde_yaml::from_str::<serde_yaml::Value>(&content) {
                if let Some(paths) = cfg
                    .get("ignore_paths")
                    .or_else(|| cfg.get("ignored_paths"))
                    .and_then(|v| v.as_sequence())
                {
                    let extra: Vec<String> = paths
                        .iter()
                        .filter_map(|v| v.as_str().map(str::to_string))
                        .collect();
                    let other = XbpIgnoreSet::from_patterns(extra);
                    set.merge(&other);
                }
            }
        }
    }
    set
}

trait IgnoreMatch {
    fn matches_dir(&self, rel: &str) -> bool;
    fn matches_file(&self, rel: &str) -> bool;
}

impl IgnoreMatch for XbpIgnoreSet {
    fn matches_dir(&self, rel: &str) -> bool {
        self.is_ignored_relative(rel, true)
    }
    fn matches_file(&self, rel: &str) -> bool {
        self.is_ignored_relative(rel, false)
    }
}

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

    #[test]
    fn extracts_rust_and_python_todos() {
        // Build markers at runtime so this source file is not itself a scan hit.
        let content = [
            format!("// {}: wire secrets fallback", "TODO"),
            "fn main() {}".to_string(),
            format!("# {}: handle empty list", "FIXME"),
            format!("/* {}: temporary */", "HACK"),
        ]
        .join("\n");
        let hits = extract_hits_from_content("src/main.rs", &content);
        assert!(hits
            .iter()
            .any(|h| h.kind == "TODO" && h.text.contains("wire secrets")));
        assert!(hits.iter().any(|h| h.kind == "FIXME"));
        assert!(hits.iter().any(|h| h.kind == "HACK"));
    }

    #[test]
    fn fingerprint_stable_across_line_moves() {
        let a = fingerprint("src/a.rs", "TODO", "fix the thing");
        let b = fingerprint("src/a.rs", "TODO", "  fix the   thing ");
        assert_eq!(a, b);
        let c = fingerprint("src\\a.rs", "todo", "fix the thing");
        assert_eq!(a, c);
    }

    #[test]
    fn fingerprint_differs_for_different_text() {
        let a = fingerprint("src/a.rs", "TODO", "one");
        let b = fingerprint("src/a.rs", "TODO", "two");
        assert_ne!(a, b);
    }

    #[test]
    fn ignores_markdown_headers_and_string_literals() {
        let content = [
            "        body.push_str(\"## TODO section\");".to_string(),
            "## TODO should not match as a heading either".to_string(),
            format!("// {}: real one", "TODO"),
        ]
        .join("\n");
        let hits = extract_hits_from_content("src/x.rs", &content);
        assert_eq!(hits.len(), 1);
        assert!(hits[0].text.contains("real one"));
    }

    #[test]
    fn ignores_rust_doc_comments() {
        let content = "///     FIXME: 1\n//! TODO: module docs\n// TODO: real code todo\n";
        let hits = extract_hits_from_content("src/lib.rs", content);
        assert_eq!(hits.len(), 1);
        assert!(hits[0].text.contains("real code todo"));
    }

    #[test]
    fn ignores_todos_inside_string_literals() {
        // Build the false-positive line without embedding a live marker comment in this file.
        let about = format!(
            "            about = \"Scan {} markers and file issues\",",
            concat!("// ", "TODO")
        );
        let content = [
            about,
            format!("            // {}: actual work item", "TODO"),
        ]
        .join("\n");
        let hits = extract_hits_from_content("src/cli.rs", &content);
        assert_eq!(hits.len(), 1);
        assert!(hits[0].text.contains("actual work item"));
    }

    #[test]
    fn context_includes_ten_line_window_with_line_numbers() {
        let mut lines = Vec::new();
        for i in 1..=25 {
            lines.push(format!("line_{i}"));
        }
        // Put marker on line 12 (index 11).
        lines[11] = format!("// {}: mid", "TODO");
        let content = lines.join("\n");
        let hits = extract_hits_from_content("notes.txt", &content);
        assert_eq!(hits.len(), 1);
        let ctx = hits[0].context.as_deref().unwrap_or("");
        // ±10 around line 12 → lines 2..=22
        assert!(ctx.contains("2 | line_2"), "{ctx}");
        assert!(
            ctx.contains("12 | // TODO: mid") || ctx.contains("TODO: mid"),
            "{ctx}"
        );
        assert!(ctx.contains("22 | line_22"), "{ctx}");
        assert!(
            !ctx.contains("1 | line_1\n") || ctx.contains("1 |"),
            "{ctx}"
        );
        assert!(!ctx.contains("line_25"), "{ctx}");
    }

    #[test]
    fn context_captures_entire_rust_function_when_todo_is_above() {
        let content = [
            "use std::io;".to_string(),
            format!("// {}: handle empty path", "TODO"),
            "pub fn load_config(path: &str) -> Result<String, String> {".to_string(),
            "    if path.is_empty() {".to_string(),
            "        return Err(\"empty\".into());".to_string(),
            "    }".to_string(),
            "    std::fs::read_to_string(path).map_err(|e| e.to_string())".to_string(),
            "}".to_string(),
            "fn other() {}".to_string(),
        ]
        .join("\n");
        let hits = extract_hits_from_content("src/config.rs", &content);
        assert_eq!(hits.len(), 1);
        let ctx = hits[0].context.as_deref().unwrap_or("");
        assert!(ctx.contains("load_config"), "{ctx}");
        assert!(ctx.contains("read_to_string"), "{ctx}");
        assert!(ctx.contains("}"), "{ctx}");
        assert!(
            !ctx.contains("fn other"),
            "should stop at function end: {ctx}"
        );
    }

    #[test]
    fn context_captures_sql_create_table_when_fixme_is_above() {
        let content = [
            format!(
                "-- {}: existing drift is user_permission_scopes table, we migrate to grants",
                "FIXME"
            ),
            "CREATE TABLE IF NOT EXISTS athena.user_permission_scopes (".to_string(),
            "  id bigint NOT NULL,".to_string(),
            "  user_id uuid NOT NULL,".to_string(),
            "  scope text NOT NULL".to_string(),
            ");".to_string(),
            "CREATE TABLE other (id int);".to_string(),
        ]
        .join("\n");
        let hits = extract_hits_from_content("sql/001_scopes.sql", &content);
        assert_eq!(hits.len(), 1);
        let ctx = hits[0].context.as_deref().unwrap_or("");
        assert!(
            ctx.contains("CREATE TABLE IF NOT EXISTS athena.user_permission_scopes"),
            "{ctx}"
        );
        assert!(ctx.contains("scope text NOT NULL"), "{ctx}");
        assert!(
            !ctx.contains("CREATE TABLE other"),
            "should stop at first statement: {ctx}"
        );
    }

    #[test]
    fn context_captures_python_function_body() {
        let content = [
            format!("# {}: validate input", "TODO"),
            "def parse_row(raw: str) -> dict:".to_string(),
            "    if not raw:".to_string(),
            "        return {}".to_string(),
            "    return {\"v\": raw}".to_string(),
            "".to_string(),
            "def other():".to_string(),
            "    pass".to_string(),
        ]
        .join("\n");
        let hits = extract_hits_from_content("app/parse.py", &content);
        assert_eq!(hits.len(), 1);
        let ctx = hits[0].context.as_deref().unwrap_or("");
        assert!(ctx.contains("def parse_row"), "{ctx}");
        assert!(ctx.contains("return {\"v\": raw}"), "{ctx}");
        assert!(!ctx.contains("def other"), "{ctx}");
    }

    #[test]
    fn context_captures_ts_function_when_todo_inside() {
        let content = [
            "export function hydrate(userId: string) {".to_string(),
            "  const scope = { userId };".to_string(),
            format!("  // {}: cache miss path", "FIXME"),
            "  return fetchUser(scope);".to_string(),
            "}".to_string(),
            "export function other() { return 1; }".to_string(),
        ]
        .join("\n");
        let hits = extract_hits_from_content("src/user.ts", &content);
        assert_eq!(hits.len(), 1);
        let ctx = hits[0].context.as_deref().unwrap_or("");
        assert!(ctx.contains("export function hydrate"), "{ctx}");
        assert!(ctx.contains("fetchUser"), "{ctx}");
        assert!(!ctx.contains("function other"), "{ctx}");
    }
}