panoptico 1.1.1

AI-powered code review CLI for Pull Requests using Claude
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
// Author: Julian Bolivar
// Version: 1.1.0
// Date: 2026-02-18

//! Lexical-aware source code scanner for brace counting.
//!
//! The [`CodeScanner`] is a single-pass state machine that tracks lexical
//! context (code, string, comment, raw string, template literal) and counts
//! braces only in code context. This avoids false brace matches inside
//! strings, comments, and character literals.
//!
//! # Examples
//!
//! ```
//! use panoptico::languages::scanner::{CodeScanner, LexicalRules};
//!
//! let mut scanner = CodeScanner::new(LexicalRules::rust());
//! let results = scanner.scan_all("fn main() {\n    let s = \"{\";\n}\n");
//! assert_eq!(results[0].brace_delta, 1);  // fn main() {
//! assert_eq!(results[1].brace_delta, 0);  // brace in string — not counted
//! assert_eq!(results[2].brace_delta, -1); // closing }
//! ```

/// Lexical context states for the code scanner.
#[derive(Debug, Clone, PartialEq, Eq)]
enum LexicalState {
    /// Normal code — braces are structural.
    Code,
    /// Inside a line comment (until end of line).
    LineComment,
    /// Inside a block comment (`/* ... */`).
    /// `depth` tracks nesting (Rust supports nested block comments).
    BlockComment { depth: u32 },
    /// Inside a string literal (regular or byte string).
    /// `quote` is the delimiter (`"` or `'` or `` ` ``).
    /// `escapable` is `false` for raw strings.
    StringLiteral { quote: char, escapable: bool },
    /// Inside a Rust raw string: `r###"..."###`.
    /// `hashes` is the number of `#` delimiters.
    RawString { hashes: u32 },
    /// Inside a Python triple-quoted string (`"""` or `'''`).
    TripleQuote { quote: char },
    /// Inside a character literal (`'...'` in C/Rust/Java).
    CharLiteral,
    /// Inside a JS/TS template literal (`` `...` ``).
    TemplateLiteral,
}

/// Raw string syntax variants across languages.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RawStringSyntax {
    /// No raw string support.
    None,
    /// Rust-style: `r#"..."#` with variable hash count.
    RustHashes,
    /// Python-style: `"""..."""` or `'''...'''`.
    TripleQuote,
    /// Go-style: backtick raw strings (`` `...` ``).
    GoBacktick,
}

/// Per-language lexical rules that configure the scanner.
///
/// Each brace-family language provides a `LexicalRules` instance
/// that tells the scanner which syntactic constructs exist.
#[derive(Debug, Clone)]
pub struct LexicalRules {
    /// Line comment start token (e.g., `"//"`). Empty string = none.
    pub line_comment: &'static str,
    /// Whether `/* */` block comments exist.
    pub block_comments: bool,
    /// Whether block comments can nest (`/* /* */ */` — Rust only).
    pub nested_comments: bool,
    /// Whether the language has character literals with `'` (C, Rust, Java).
    /// `false` for Python (single-quote strings) and JS (single-quote strings).
    pub char_literals: bool,
    /// Raw string syntax variant.
    pub raw_strings: RawStringSyntax,
    /// Whether backtick template literals exist (JS/TS only).
    pub template_literals: bool,
}

impl LexicalRules {
    /// Rust: `//` line comments, `/* */` nested block comments,
    /// `'` char literals, `r#"..."#` raw strings.
    pub fn rust() -> Self {
        Self {
            line_comment: "//",
            block_comments: true,
            nested_comments: true,
            char_literals: true,
            raw_strings: RawStringSyntax::RustHashes,
            template_literals: false,
        }
    }

    /// C: `//` line comments, `/* */` non-nested block comments,
    /// `'` char literals.
    pub fn c() -> Self {
        Self {
            line_comment: "//",
            block_comments: true,
            nested_comments: false,
            char_literals: true,
            raw_strings: RawStringSyntax::None,
            template_literals: false,
        }
    }

    /// C++: Same as C. (C++11 raw strings `R"(...)"` are rare
    /// enough to ignore in heuristic parsing.)
    pub fn cpp() -> Self {
        Self::c()
    }

    /// Go: `//` + `/* */`, char literals, backtick raw strings.
    pub fn go() -> Self {
        Self {
            line_comment: "//",
            block_comments: true,
            nested_comments: false,
            char_literals: true,
            raw_strings: RawStringSyntax::GoBacktick,
            template_literals: false,
        }
    }

    /// Java: `//` + `/* */`, char literals.
    pub fn java() -> Self {
        Self {
            line_comment: "//",
            block_comments: true,
            nested_comments: false,
            char_literals: true,
            raw_strings: RawStringSyntax::None,
            template_literals: false,
        }
    }

    /// JavaScript: `//` + `/* */`, backtick template literals.
    /// Single-quote `'` is a string delimiter (NOT char literal).
    pub fn javascript() -> Self {
        Self {
            line_comment: "//",
            block_comments: true,
            nested_comments: false,
            char_literals: false,
            raw_strings: RawStringSyntax::None,
            template_literals: true,
        }
    }

    /// TypeScript: same lexical rules as JavaScript.
    pub fn typescript() -> Self {
        Self::javascript()
    }

    /// Python: `#` line comments, no block comments, no char literals,
    /// triple-quote raw strings.
    pub fn python() -> Self {
        Self {
            line_comment: "#",
            block_comments: false,
            nested_comments: false,
            char_literals: false,
            raw_strings: RawStringSyntax::TripleQuote,
            template_literals: false,
        }
    }
}

/// Result of scanning a single source line.
#[derive(Debug, Clone)]
pub struct ScannedLine {
    /// 1-indexed line number.
    pub line_number: u32,
    /// Net brace depth change on this line (code context only).
    /// Positive = net opening braces, negative = net closing braces.
    pub brace_delta: i32,
    /// Cumulative brace depth AFTER this line (saturates at 0).
    pub depth_after: u32,
    /// `true` if the entire line is inside a comment or string
    /// (no code tokens on this line). Used to skip keyword detection.
    pub is_non_code: bool,
}

/// Context-aware source code scanner.
///
/// Tracks lexical context (strings, comments, raw strings) across
/// lines to provide accurate brace depth counting. The scanner is
/// stateful: call [`scan_line`](Self::scan_line) in order, or use
/// [`scan_all`](Self::scan_all) for the entire file.
///
/// # Examples
///
/// ```
/// use panoptico::languages::scanner::{CodeScanner, LexicalRules};
///
/// let mut scanner = CodeScanner::new(LexicalRules::rust());
/// let results = scanner.scan_all("fn main() {\n    let s = \"{\";\n}\n");
/// assert_eq!(results[0].brace_delta, 1);  // fn main() {
/// assert_eq!(results[1].brace_delta, 0);  // brace in string — not counted
/// assert_eq!(results[2].brace_delta, -1); // closing }
/// ```
pub struct CodeScanner {
    rules: LexicalRules,
    state: LexicalState,
    depth: u32,
}

impl CodeScanner {
    /// Create a new scanner with language-specific lexical rules.
    pub fn new(rules: LexicalRules) -> Self {
        Self {
            rules,
            state: LexicalState::Code,
            depth: 0,
        }
    }

    /// Scan a single line, updating internal state.
    ///
    /// Must be called in order (line 1, line 2, ...) because the
    /// scanner carries context across lines (e.g., multi-line strings,
    /// block comments).
    pub fn scan_line(&mut self, line_number: u32, line: &str) -> ScannedLine {
        let chars: Vec<char> = line.chars().collect();
        let len = chars.len();
        let mut i = 0;
        let mut delta: i32 = 0;
        let was_code_at_start = self.state == LexicalState::Code;
        let mut entered_code = false;

        while i < len {
            match &self.state {
                LexicalState::Code => {
                    entered_code = true;

                    // Check for line comment
                    if !self.rules.line_comment.is_empty() {
                        let lc = self.rules.line_comment;
                        if lc == "//" && i + 1 < len && chars[i] == '/' && chars[i + 1] == '/' {
                            self.state = LexicalState::LineComment;
                            break; // rest of line is comment
                        }
                        if lc == "#" && chars[i] == '#' {
                            self.state = LexicalState::LineComment;
                            break;
                        }
                    }

                    // Check for block comment
                    if self.rules.block_comments
                        && i + 1 < len
                        && chars[i] == '/'
                        && chars[i + 1] == '*'
                    {
                        self.state = LexicalState::BlockComment { depth: 1 };
                        i += 2;
                        continue;
                    }

                    // Check for Rust raw string: r#"..."# or br#"..."#
                    if self.rules.raw_strings == RawStringSyntax::RustHashes {
                        if let Some(hashes) = self.try_rust_raw_string(&chars, i) {
                            self.state = LexicalState::RawString { hashes };
                            // Skip past r[b]###"
                            let mut skip = if chars[i] == 'b' { 2 } else { 1 }; // b?r
                            skip += hashes as usize; // #'s
                            skip += 1; // opening "
                            i += skip;
                            continue;
                        }
                    }

                    // Check for triple-quote strings (Python)
                    if self.rules.raw_strings == RawStringSyntax::TripleQuote
                        && i + 2 < len
                        && (chars[i] == '"' || chars[i] == '\'')
                        && chars[i + 1] == chars[i]
                        && chars[i + 2] == chars[i]
                    {
                        let q = chars[i];
                        self.state = LexicalState::TripleQuote { quote: q };
                        i += 3;
                        continue;
                    }

                    // Check for Go backtick raw string
                    if self.rules.raw_strings == RawStringSyntax::GoBacktick && chars[i] == '`' {
                        self.state = LexicalState::StringLiteral {
                            quote: '`',
                            escapable: false,
                        };
                        i += 1;
                        continue;
                    }

                    // Check for template literal (JS/TS)
                    if self.rules.template_literals && chars[i] == '`' {
                        self.state = LexicalState::TemplateLiteral;
                        i += 1;
                        continue;
                    }

                    // Check for character literal (Rust/C/Java)
                    if self.rules.char_literals && chars[i] == '\'' {
                        if self.is_char_literal(&chars, i) {
                            self.state = LexicalState::CharLiteral;
                            i += 1; // skip opening quote, content handled in CharLiteral state
                            continue;
                        }
                        // Not a char literal (e.g., Rust lifetime) — stay in Code
                        i += 1;
                        continue;
                    }

                    // Check for string literal with "
                    if chars[i] == '"' {
                        self.state = LexicalState::StringLiteral {
                            quote: '"',
                            escapable: true,
                        };
                        i += 1;
                        continue;
                    }

                    // Check for single-quote string (non-char-literal languages like JS/Python)
                    if !self.rules.char_literals && chars[i] == '\'' {
                        self.state = LexicalState::StringLiteral {
                            quote: '\'',
                            escapable: true,
                        };
                        i += 1;
                        continue;
                    }

                    // Check for byte string: b"..."
                    if chars[i] == 'b' && i + 1 < len && chars[i + 1] == '"' {
                        self.state = LexicalState::StringLiteral {
                            quote: '"',
                            escapable: true,
                        };
                        i += 2;
                        continue;
                    }

                    // Count braces
                    if chars[i] == '{' {
                        delta += 1;
                        self.depth = self.depth.saturating_add(1);
                    } else if chars[i] == '}' {
                        delta -= 1;
                        self.depth = self.depth.saturating_sub(1);
                    }

                    i += 1;
                }

                LexicalState::LineComment => {
                    // Consume rest of line (handled by break above)
                    break;
                }

                LexicalState::BlockComment { depth: d } => {
                    let d = *d;
                    if i + 1 < len && chars[i] == '*' && chars[i + 1] == '/' {
                        if d <= 1 {
                            self.state = LexicalState::Code;
                        } else {
                            self.state = LexicalState::BlockComment { depth: d - 1 };
                        }
                        i += 2;
                        continue;
                    }
                    if self.rules.nested_comments
                        && i + 1 < len
                        && chars[i] == '/'
                        && chars[i + 1] == '*'
                    {
                        self.state = LexicalState::BlockComment { depth: d + 1 };
                        i += 2;
                        continue;
                    }
                    i += 1;
                }

                LexicalState::StringLiteral { quote, escapable } => {
                    let q = *quote;
                    let esc = *escapable;
                    if esc && chars[i] == '\\' {
                        i += 2; // skip escaped char
                        continue;
                    }
                    if chars[i] == q {
                        self.state = LexicalState::Code;
                        i += 1;
                        continue;
                    }
                    i += 1;
                }

                LexicalState::RawString { hashes } => {
                    let h = *hashes;
                    // Look for closing " followed by h #'s
                    if chars[i] == '"' {
                        let mut count = 0u32;
                        let mut j = i + 1;
                        while j < len && chars[j] == '#' && count < h {
                            count += 1;
                            j += 1;
                        }
                        if count == h {
                            self.state = LexicalState::Code;
                            i = j;
                            continue;
                        }
                    }
                    i += 1;
                }

                LexicalState::TripleQuote { quote } => {
                    let q = *quote;
                    if i + 2 < len && chars[i] == q && chars[i + 1] == q && chars[i + 2] == q {
                        self.state = LexicalState::Code;
                        i += 3;
                        continue;
                    }
                    i += 1;
                }

                LexicalState::CharLiteral => {
                    if chars[i] == '\\' {
                        i += 2; // skip escaped char
                        continue;
                    }
                    if chars[i] == '\'' {
                        self.state = LexicalState::Code;
                        i += 1;
                        continue;
                    }
                    i += 1;
                }

                LexicalState::TemplateLiteral => {
                    if chars[i] == '\\' {
                        i += 2;
                        continue;
                    }
                    if chars[i] == '`' {
                        self.state = LexicalState::Code;
                        i += 1;
                        continue;
                    }
                    i += 1;
                }
            }
        }

        // Reset line comment at end of line
        if self.state == LexicalState::LineComment {
            self.state = LexicalState::Code;
        }

        let is_non_code = !was_code_at_start && !entered_code;

        ScannedLine {
            line_number,
            brace_delta: delta,
            depth_after: self.depth,
            is_non_code,
        }
    }

    /// Scan all lines of a source file.
    ///
    /// Convenience method that splits on `\n` and calls
    /// [`scan_line`](Self::scan_line) for each line.
    pub fn scan_all(&mut self, content: &str) -> Vec<ScannedLine> {
        content
            .lines()
            .enumerate()
            .map(|(i, line)| self.scan_line(u32::try_from(i + 1).expect("line within u32"), line))
            .collect()
    }

    /// Current cumulative brace depth.
    pub fn current_depth(&self) -> u32 {
        self.depth
    }

    /// Check if `chars[i]` starts a Rust raw string (`r"`, `r#"`, `br"`, `br#"`).
    /// Returns the hash count if it does.
    fn try_rust_raw_string(&self, chars: &[char], i: usize) -> Option<u32> {
        let len = chars.len();
        let mut pos = i;

        // Optional 'b' prefix
        if pos < len && chars[pos] == 'b' {
            pos += 1;
        }

        // Must have 'r'
        if pos >= len || chars[pos] != 'r' {
            return None;
        }
        pos += 1;

        // Count '#' characters
        let mut hashes = 0u32;
        while pos < len && chars[pos] == '#' {
            hashes += 1;
            pos += 1;
        }

        // Must end with '"'
        if pos < len && chars[pos] == '"' {
            // Disambiguation: r"..." is a raw string (0 hashes).
            // But we need at least 'r' followed by '"' or '#'s then '"'.
            Some(hashes)
        } else {
            None
        }
    }

    /// Heuristic to distinguish character literal `'x'` from Rust lifetime `'a`.
    ///
    /// Returns `true` if this looks like a char literal, `false` for lifetime.
    fn is_char_literal(&self, chars: &[char], i: usize) -> bool {
        let len = chars.len();
        // Need at least 3 chars: 'x' (quote, char, quote)
        if i + 2 >= len {
            return false;
        }

        let next = chars[i + 1];

        // Escaped char: '\n', '\\'
        if next == '\\' && i + 3 < len && chars[i + 3] == '\'' {
            return true;
        }
        // Simple char: 'a'
        if chars[i + 2] == '\'' {
            return true;
        }

        // Otherwise it's a lifetime or label
        false
    }
}

/// Find the closing brace for a construct that opens at `open_idx`.
///
/// Uses pre-scanned brace depths instead of naive counting.
/// The closing brace is the first line after `open_idx` where
/// `depth_after` returns to the level it was at before the
/// opening brace.
///
/// # Arguments
///
/// * `scanned` - Pre-scanned line results from [`CodeScanner::scan_all`].
/// * `open_idx` - Index into `scanned` of the line containing the opening brace.
///
/// # Returns
///
/// Index into `scanned` of the line containing the matching closing brace.
/// If no closing brace is found, returns the last line index (graceful degradation).
pub fn find_closing_brace(scanned: &[ScannedLine], open_idx: usize) -> usize {
    if open_idx >= scanned.len() {
        return scanned.len().saturating_sub(1);
    }

    let target_depth = scanned[open_idx].depth_after.saturating_sub(1);
    for (i, line) in scanned.iter().enumerate().skip(open_idx + 1) {
        if line.depth_after == target_depth && line.brace_delta < 0 {
            return i;
        }
    }
    // Unclosed brace — return last line (graceful degradation)
    scanned.len().saturating_sub(1)
}

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

    // --- Test 1: scanner_counts_braces_in_code ---
    #[test]
    fn scanner_counts_braces_in_code() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("fn foo() {\n    bar();\n}");
        assert_eq!(results[0].brace_delta, 1, "Opening brace on line 1");
        assert_eq!(results[1].brace_delta, 0, "No braces on line 2");
        assert_eq!(results[2].brace_delta, -1, "Closing brace on line 3");
        assert_eq!(results[0].depth_after, 1);
        assert_eq!(results[2].depth_after, 0);
    }

    // --- Test 2: scanner_ignores_braces_in_string_literal ---
    #[test]
    fn scanner_ignores_braces_in_string_literal() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("let s = \"{\";");
        assert_eq!(
            results[0].brace_delta, 0,
            "Brace inside string should not be counted"
        );
    }

    // --- Test 3: scanner_ignores_braces_in_line_comment ---
    #[test]
    fn scanner_ignores_braces_in_line_comment() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("// { not a brace");
        assert_eq!(
            results[0].brace_delta, 0,
            "Brace in line comment should not be counted"
        );
    }

    // --- Test 4: scanner_ignores_braces_in_block_comment ---
    #[test]
    fn scanner_ignores_braces_in_block_comment() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("/* { } */");
        assert_eq!(
            results[0].brace_delta, 0,
            "Braces in block comment should not be counted"
        );
    }

    // --- Test 5: scanner_ignores_braces_in_char_literal ---
    #[test]
    fn scanner_ignores_braces_in_char_literal() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("let c = '{';");
        assert_eq!(
            results[0].brace_delta, 0,
            "Brace in char literal should not be counted"
        );
    }

    // --- Test 6: scanner_handles_rust_raw_string ---
    #[test]
    fn scanner_handles_rust_raw_string() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("let s = r#\"{ }\"#;");
        assert_eq!(
            results[0].brace_delta, 0,
            "Braces in raw string should not be counted"
        );
    }

    // --- Test 7: scanner_handles_nested_block_comments_rust ---
    #[test]
    fn scanner_handles_nested_block_comments_rust() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("/* outer /* inner { } */ still comment { } */\nlet x = 1;");
        assert_eq!(
            results[0].brace_delta, 0,
            "Braces in nested block comment should not be counted"
        );
        assert!(
            results[0].is_non_code || results[0].brace_delta == 0,
            "Line should be non-code or have 0 delta"
        );
        assert!(!results[1].is_non_code, "Line after comment is code");
    }

    // --- Test 8: scanner_non_nested_block_comments_c ---
    #[test]
    fn scanner_non_nested_block_comments_c() {
        let mut scanner = CodeScanner::new(LexicalRules::c());
        // In C, block comments don't nest: /* start /* not nested */ ends here
        // After first */, we're back in code
        let results = scanner.scan_all("/* start /* not nested */ {");
        assert_eq!(
            results[0].brace_delta, 1,
            "After non-nested block comment ends, brace in code should be counted"
        );
    }

    // --- Test 9: scanner_handles_escape_in_string ---
    #[test]
    fn scanner_handles_escape_in_string() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("let s = \"escaped \\\" still string {\";");
        assert_eq!(
            results[0].brace_delta, 0,
            "Brace after escaped quote in string should not be counted"
        );
    }

    // --- Test 10: scanner_handles_multiline_string ---
    #[test]
    fn scanner_handles_multiline_string() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        // Three lines: line 1 opens a string, line 2 is entirely inside,
        // line 3 closes the string. The scanner doesn't enforce language
        // rules about line breaks in strings — it just tracks state.
        let results = scanner.scan_all("let s = \"hello\nworld { }\n\";");
        assert_eq!(results.len(), 3);
        assert_eq!(
            results[1].brace_delta, 0,
            "Braces on second line of multi-line string should not be counted"
        );
        assert!(
            results[1].is_non_code,
            "Second line should be marked as non-code (inside string)"
        );
    }

    // --- Test 11: scanner_handles_js_template_literal ---
    #[test]
    fn scanner_handles_js_template_literal() {
        let mut scanner = CodeScanner::new(LexicalRules::javascript());
        let results = scanner.scan_all("let s = `template { literal`;");
        assert_eq!(
            results[0].brace_delta, 0,
            "Brace in template literal should not be counted"
        );
    }

    // --- Test 12: scanner_handles_go_backtick_raw_string ---
    #[test]
    fn scanner_handles_go_backtick_raw_string() {
        let mut scanner = CodeScanner::new(LexicalRules::go());
        let results = scanner.scan_all("var s = `raw { string`");
        assert_eq!(
            results[0].brace_delta, 0,
            "Brace in Go backtick raw string should not be counted"
        );
    }

    // --- Test 13: scanner_lifetime_not_char_literal ---
    #[test]
    fn scanner_lifetime_not_char_literal() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("fn foo<'a>(x: &'a str) {");
        assert_eq!(
            results[0].brace_delta, 1,
            "Opening brace should be counted; 'a is a lifetime, not char literal"
        );
    }

    // --- Test 14: scanner_mixed_code_and_string_on_one_line ---
    #[test]
    fn scanner_mixed_code_and_string_on_one_line() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("let x = \"{\"; let y = 1;");
        assert!(
            !results[0].is_non_code,
            "Line with code and string should not be marked as non-code"
        );
        assert_eq!(
            results[0].brace_delta, 0,
            "Brace in string should not be counted"
        );
    }

    // --- Test 15: scanner_depth_tracks_across_lines ---
    #[test]
    fn scanner_depth_tracks_across_lines() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let code = "fn foo() {\n    if true {\n        bar();\n    }\n}";
        let results = scanner.scan_all(code);
        assert_eq!(results[0].depth_after, 1, "After fn foo() {{");
        assert_eq!(results[1].depth_after, 2, "After if true {{");
        assert_eq!(results[2].depth_after, 2, "bar() stays at depth 2");
        assert_eq!(results[3].depth_after, 1, "After inner }}");
        assert_eq!(results[4].depth_after, 0, "After outer }}");
    }

    // --- Test 16: scanner_depth_saturates_at_zero ---
    #[test]
    fn scanner_depth_saturates_at_zero() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let results = scanner.scan_all("}\n}");
        assert_eq!(
            results[0].depth_after, 0,
            "Extra closing brace should saturate at 0"
        );
        assert_eq!(
            results[1].depth_after, 0,
            "Second extra closing brace should still be 0"
        );
    }

    // --- Test 17: find_closing_brace_uses_scanned_depth ---
    #[test]
    fn find_closing_brace_uses_scanned_depth() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let code = "fn foo() {\n    if true {\n        bar();\n    }\n}";
        let scanned = scanner.scan_all(code);
        // find_closing_brace for line 0 (fn foo() {) should find line 4 (outer })
        let close = find_closing_brace(&scanned, 0);
        assert_eq!(close, 4, "Closing brace for fn foo() should be at line 5");
        // find_closing_brace for line 1 (if true {) should find line 3 (inner })
        let close = find_closing_brace(&scanned, 1);
        assert_eq!(close, 3, "Closing brace for if true should be at line 4");
    }

    // --- Test 18: find_closing_brace_skips_braces_in_strings ---
    #[test]
    fn find_closing_brace_skips_braces_in_strings() {
        let mut scanner = CodeScanner::new(LexicalRules::rust());
        let code = "fn foo() {\n    let s = \"}\";\n}";
        let scanned = scanner.scan_all(code);
        let close = find_closing_brace(&scanned, 0);
        assert_eq!(
            close, 2,
            "Should skip brace in string and find actual closing brace"
        );
    }
}