howmany 3.0.0

A blazingly fast, intelligent code analysis tool with parallel processing, caching, and beautiful visualizations
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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
pub mod comment_patterns;
pub mod scanner;

pub use comment_patterns::CommentPattern;
pub use scanner::LineTally;

use crate::core::stats::{AggregatedStats, StatsCalculator};
use crate::core::types::{CodeStats, FileStats};
use crate::utils::cache::{CacheKey, FileCache};
use crate::utils::errors::{HowManyError, Result};
use std::borrow::Cow;
use std::fs;
use std::io::BufReader;
use std::path::Path;

/// Read buffer size for line scanning.
///
/// Source files cluster well below this, so most files are read in one syscall.
const READ_BUFFER_BYTES: usize = 64 * 1024;

/// Lowercase `s`, borrowing when it is already lowercase.
fn lower(s: &str) -> Cow<'_, str> {
    if s.bytes().any(|b| b.is_ascii_uppercase()) {
        Cow::Owned(s.to_lowercase())
    } else {
        Cow::Borrowed(s)
    }
}

/// Lowercase `path`'s extension, borrowing when it is already lowercase.
fn lower_extension(path: &Path) -> Option<Cow<'_, str>> {
    path.extension()?.to_str().map(lower)
}

/// The comment-syntax key for `path`.
///
/// Its lowercase extension, or -- for a file that has none -- its lowercase
/// name without a leading dot. That is what lets `Dockerfile`, `Makefile` and
/// `.editorconfig` be classified with their real comment syntax instead of
/// being treated as an unknown format whose every line is code.
pub fn classify_key(path: &Path) -> Cow<'_, str> {
    lower_extension(path)
        .or_else(|| {
            let name = path.file_name()?.to_str()?;
            Some(lower(name.strip_prefix('.').unwrap_or(name)))
        })
        .unwrap_or(Cow::Borrowed(""))
}

/// The syntax key implied by a `#!` line.
///
/// An extension-less `bootstrap` or `configure` script is ordinary source, and
/// its shebang says which language. Without this its comments were counted as
/// code, because no extension meant no comment syntax.
pub fn shebang_key(head: &[u8]) -> Option<&'static str> {
    let rest = head.strip_prefix(b"#!")?;
    let line = &rest[..rest.iter().position(|b| *b == b'\n').unwrap_or(rest.len())];
    let text = std::str::from_utf8(line).ok()?;

    // `#!/usr/bin/env -S python3 -u` names the interpreter after the `env` and
    // any of its flags.
    let interpreter = text
        .split_whitespace()
        .find(|word| !word.starts_with('-') && !word.ends_with("env"))?
        .rsplit('/')
        .next()?;

    Some(
        match interpreter.trim_end_matches(|c: char| c.is_ascii_digit() || c == '.') {
            "sh" | "bash" | "dash" | "ksh" | "ash" => "sh",
            "zsh" => "zsh",
            "fish" => "fish",
            "python" | "python-" => "py",
            "node" | "deno" | "bun" => "js",
            "ruby" => "rb",
            "perl" => "pl",
            "php" => "php",
            "lua" => "lua",
            "Rscript" => "r",
            "pwsh" | "powershell" => "ps1",
            "tclsh" | "wish" => "sh",
            _ => return None,
        },
    )
}

/// Classify `reader` under the syntax registered for `key`.
///
/// When `key` is unknown the reader's buffered head is consulted for a shebang.
/// `fill_buf` peeks, so this costs no extra syscall and consumes nothing.
fn classify_with<R: std::io::BufRead>(reader: &mut R, key: &str) -> Result<scanner::LineTally> {
    let resolved = if comment_patterns::is_known(key) {
        Cow::Borrowed(key)
    } else {
        match shebang_key(reader.fill_buf()?) {
            Some(from_shebang) => Cow::Borrowed(from_shebang),
            None => Cow::Borrowed(key),
        }
    };

    Ok(if comment_patterns::is_prose_format(&resolved) {
        scanner::classify_markdown(reader)?
    } else {
        scanner::classify(reader, comment_patterns::lookup_or_empty(&resolved))?
    })
}

/// Classify `bytes` under the syntax registered for `key`.
fn classify_bytes_with(bytes: &[u8], key: &str) -> scanner::LineTally {
    let resolved = if comment_patterns::is_known(key) {
        Cow::Borrowed(key)
    } else {
        Cow::Borrowed(shebang_key(bytes).unwrap_or(key))
    };

    if comment_patterns::is_prose_format(&resolved) {
        scanner::classify_markdown_bytes(bytes)
    } else {
        scanner::classify_bytes(bytes, comment_patterns::lookup_or_empty(&resolved))
    }
}

/// Files up to this size are read whole into a reused buffer; larger ones are
/// streamed so that one enormous file cannot dictate a worker's memory use.
///
/// Source files are overwhelmingly smaller than this -- the threshold exists for
/// the vendored bundle and the checked-in dataset, not for code.
const WHOLE_FILE_LIMIT: u64 = 8 * 1024 * 1024;

thread_local! {
    /// Per-worker scratch space for whole-file reads.
    ///
    /// Reused across every file a thread handles, so counting a repository
    /// performs a handful of allocations rather than one per file. The previous
    /// `BufReader::with_capacity(64 KiB)` per file made the allocator the
    /// bottleneck: counting on all sixteen threads of a 12+4 core machine was
    /// slower than counting on four.
    static READ_SCRATCH: std::cell::RefCell<Vec<u8>> =
        std::cell::RefCell::new(Vec::with_capacity(READ_BUFFER_BYTES));
}

/// Read `file` of known `size` into `buf`, replacing its contents.
fn read_into(file: &mut fs::File, size: u64, buf: &mut Vec<u8>) -> Result<()> {
    use std::io::Read;
    buf.clear();
    buf.reserve(size as usize + 1);
    file.read_to_end(buf)?;
    Ok(())
}

/// The error a caller gets for handing a directory to a file counter.
fn is_a_directory(path: &Path) -> HowManyError {
    HowManyError::file_processing(format!("{} is a directory, not a file", path.display()))
}

/// Counts lines of code, comments, documentation and blanks.
///
/// Construction is free -- all comment tables are process-wide statics -- so one
/// counter per worker thread costs nothing.
pub struct CodeCounter {
    stats_calculator: StatsCalculator,
}

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

impl CodeCounter {
    pub fn new() -> Self {
        Self {
            stats_calculator: StatsCalculator::new(),
        }
    }

    /// Comment syntax registered for `extension`, if any.
    pub fn comment_pattern(&self, extension: &str) -> Option<CommentPattern> {
        comment_patterns::lookup(extension)
    }

    /// True when `extension` has registered comment syntax.
    pub fn supports_extension(&self, extension: &str) -> bool {
        comment_patterns::is_known(extension)
    }

    /// Count one file.
    ///
    /// A single `open` supplies both the contents and the size; the previous
    /// implementation opened the file and then issued a second `stat` for its
    /// length, doubling the syscalls per file.
    pub fn count_file(&self, path: &Path) -> Result<FileStats> {
        // Unix opens a directory happily and fails only on the first read;
        // Windows refuses the open outright with a permission error. Checking
        // on the way out of a failed open costs nothing on the happy path and
        // makes the error identical on every platform.
        let file = fs::File::open(path).map_err(|err| {
            if path.is_dir() {
                is_a_directory(path)
            } else {
                err.into()
            }
        })?;
        let metadata = file.metadata()?;

        if metadata.is_dir() {
            return Err(is_a_directory(path));
        }

        let file_size = metadata.len();
        let mut reader = BufReader::with_capacity(READ_BUFFER_BYTES, file);
        let tally = classify_with(&mut reader, &classify_key(path))?;

        Ok(tally.into_file_stats(file_size))
    }

    /// Count a file whose size is already known, skipping the metadata call.
    ///
    /// Traversal already paid for the size; reusing it removes a `stat` per
    /// file from the hot path.
    pub fn count_file_with_size(&self, path: &Path, file_size: u64) -> Result<FileStats> {
        let mut file = fs::File::open(path)?;
        let key = classify_key(path);

        if file_size > WHOLE_FILE_LIMIT {
            let mut reader = BufReader::with_capacity(READ_BUFFER_BYTES, file);
            let tally = classify_with(&mut reader, &key)?;
            return Ok(tally.into_file_stats(file_size));
        }

        READ_SCRATCH.with(|scratch| {
            let mut buf = scratch.borrow_mut();
            read_into(&mut file, file_size, &mut buf)?;
            Ok(classify_bytes_with(&buf, &key).into_file_stats(file_size))
        })
    }

    /// Classify an in-memory buffer as if it had extension `extension`.
    ///
    /// Exposed so that classification can be exercised without touching disk.
    pub fn count_bytes(&self, contents: &[u8], extension: &str) -> Result<FileStats> {
        let mut reader = contents;
        let tally = classify_with(&mut reader, &lower(extension))?;
        Ok(tally.into_file_stats(contents.len() as u64))
    }

    /// Comprehensive statistics for a single file.
    pub fn calculate_file_stats(&self, path: &Path) -> Result<AggregatedStats> {
        let file_stats = self.count_file(path)?;
        let path_str = path.to_string_lossy().to_string();

        let start_time = std::time::Instant::now();
        let mut aggregated_stats = self
            .stats_calculator
            .calculate_file_stats(&file_stats, &path_str)?;
        crate::core::stats::aggregation::StatsAggregator::update_timing(
            &mut aggregated_stats,
            start_time,
        );

        Ok(aggregated_stats)
    }

    /// Comprehensive statistics for a project.
    pub fn calculate_project_stats(
        &self,
        code_stats: &CodeStats,
        individual_files: &[(String, FileStats)],
    ) -> Result<AggregatedStats> {
        let start_time = std::time::Instant::now();
        let mut aggregated_stats = self
            .stats_calculator
            .calculate_project_stats(code_stats, individual_files)?;
        crate::core::stats::aggregation::StatsAggregator::update_timing(
            &mut aggregated_stats,
            start_time,
        );

        Ok(aggregated_stats)
    }

    pub fn stats_calculator(&self) -> &StatsCalculator {
        &self.stats_calculator
    }

    /// Sum per-file statistics into project totals, keyed by extension.
    pub fn aggregate_stats(&self, file_stats: Vec<(String, FileStats)>) -> CodeStats {
        aggregate(file_stats)
    }
}

/// Sum per-file statistics into project totals, keyed by extension.
pub fn aggregate<I>(file_stats: I) -> CodeStats
where
    I: IntoIterator<Item = (String, FileStats)>,
{
    let mut totals = CodeStats::default();

    for (extension, stats) in file_stats {
        totals.total_files += 1;
        totals.total_lines += stats.total_lines;
        totals.total_code_lines += stats.code_lines;
        totals.total_comment_lines += stats.comment_lines;
        totals.total_blank_lines += stats.blank_lines;
        totals.total_size += stats.file_size;
        totals.total_doc_lines += stats.doc_lines;

        let entry = totals
            .stats_by_extension
            .entry(extension)
            .or_insert_with(|| (0, FileStats::default()));

        entry.0 += 1;
        entry.1.total_lines += stats.total_lines;
        entry.1.code_lines += stats.code_lines;
        entry.1.comment_lines += stats.comment_lines;
        entry.1.blank_lines += stats.blank_lines;
        entry.1.file_size += stats.file_size;
        entry.1.doc_lines += stats.doc_lines;
    }

    totals
}

/// A [`CodeCounter`] that reuses results for files that have not changed.
pub struct CachedCodeCounter {
    counter: CodeCounter,
    cache: FileCache,
    cache_hits: usize,
    cache_misses: usize,
}

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

impl CachedCodeCounter {
    /// A counter whose cache lives only as long as it does.
    pub fn new() -> Self {
        Self::with_cache(FileCache::new())
    }

    /// A counter backed by the on-disk cache for `root`.
    ///
    /// The cache is scoped to the project so that its load and save cost is
    /// proportional to that project rather than to every directory the machine
    /// has ever analyzed.
    pub fn for_root(root: &Path) -> Self {
        Self::with_cache(FileCache::scoped(root))
    }

    /// Build a counter over an explicit cache, bypassing the on-disk one.
    pub fn with_cache(cache: FileCache) -> Self {
        Self {
            counter: CodeCounter::new(),
            cache,
            cache_hits: 0,
            cache_misses: 0,
        }
    }

    pub fn count_file(&mut self, path: &Path) -> Result<FileStats> {
        let key = CacheKey::for_path(path);

        if let Some(cached) = key.as_ref().and_then(|k| self.cache.get_with_key(path, k)) {
            self.cache_hits += 1;
            return Ok(cached.clone());
        }

        self.cache_misses += 1;
        let file_stats = match &key {
            Some(k) => self.counter.count_file_with_size(path, k.size)?,
            None => self.counter.count_file(path)?,
        };

        if let Some(k) = key {
            self.cache
                .insert_with_key(path.to_path_buf(), file_stats.clone(), k);
        }

        Ok(file_stats)
    }

    pub fn save_cache(&self) -> Result<()> {
        self.cache.save()
    }

    pub fn cleanup_cache(&mut self) {
        self.cache.cleanup_missing_files();
    }

    pub fn cache_size(&self) -> usize {
        self.cache.size()
    }

    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }

    pub fn cache_stats(&self) -> (usize, usize) {
        (self.cache_hits, self.cache_misses)
    }

    pub fn cache_hit_rate(&self) -> f64 {
        let total = self.cache_hits + self.cache_misses;
        if total > 0 {
            self.cache_hits as f64 / total as f64
        } else {
            0.0
        }
    }

    pub fn aggregate_stats(&self, file_stats: Vec<(String, FileStats)>) -> CodeStats {
        self.counter.aggregate_stats(file_stats)
    }

    pub fn calculate_file_stats(&self, path: &Path) -> Result<AggregatedStats> {
        self.counter.calculate_file_stats(path)
    }

    pub fn calculate_project_stats(
        &self,
        code_stats: &CodeStats,
        individual_files: &[(String, FileStats)],
    ) -> Result<AggregatedStats> {
        self.counter
            .calculate_project_stats(code_stats, individual_files)
    }

    pub fn stats_calculator(&self) -> &StatsCalculator {
        self.counter.stats_calculator()
    }

    /// Hand back the cache so it can be persisted or merged elsewhere.
    pub fn into_cache(self) -> FileCache {
        self.cache
    }
}

/// Extension key used for per-extension aggregation.
pub fn extension_key(path: &Path) -> String {
    path.extension()
        .and_then(|ext| ext.to_str())
        .unwrap_or("no_ext")
        .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::test_utils::TestProject;
    use std::collections::BTreeMap;

    #[test]
    fn test_rust_file_counting() {
        let project = TestProject::new("test_rust").unwrap();
        let file_path = project.create_rust_file("test.rs", 2, 3).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.total_lines > 0);
        assert!(stats.code_lines > 0);
        assert!(stats.comment_lines > 0);
        assert!(stats.doc_lines > 0);
        assert!(stats.blank_lines > 0);
    }

    #[test]
    fn test_python_file_counting() {
        let project = TestProject::new("test_python").unwrap();
        let file_path = project.create_python_file("test.py", 2).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.total_lines > 0);
        assert!(stats.code_lines > 0);
        assert!(stats.comment_lines > 0);
        assert!(stats.doc_lines > 0); // Python docstrings
    }

    #[test]
    fn test_javascript_file_counting() {
        let project = TestProject::new("test_javascript").unwrap();
        let file_path = project.create_javascript_file("test.js", 2).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.total_lines > 0);
        assert!(stats.code_lines > 0);
        assert!(stats.comment_lines > 0);
        assert!(stats.doc_lines > 0); // JSDoc comments
    }

    #[test]
    fn test_markdown_file_counting() {
        let project = TestProject::new("test_markdown").unwrap();
        let content = r#"# Title

This is documentation content.

```rust
fn main() {
    println!("Hello, world!");
}
```

More documentation.

<!-- HTML comment -->
"#;
        let file_path = project.create_file("test.md", content).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.total_lines > 0);
        assert!(stats.code_lines > 0); // Code blocks
        assert!(stats.comment_lines > 0); // HTML comments
        assert!(stats.doc_lines > 0); // Markdown content
    }

    #[test]
    fn test_empty_file() {
        let project = TestProject::new("test_empty").unwrap();
        let file_path = project.create_file("empty.rs", "").unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert_eq!(stats.total_lines, 0);
        assert_eq!(stats.code_lines, 0);
        assert_eq!(stats.comment_lines, 0);
        assert_eq!(stats.doc_lines, 0);
        assert_eq!(stats.blank_lines, 0);
        assert_eq!(stats.file_size, 0);
    }

    #[test]
    fn test_only_blank_lines() {
        let project = TestProject::new("test_blank").unwrap();
        let file_path = project.create_file("blank.rs", "\n\n\n\n").unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert_eq!(stats.total_lines, 4);
        assert_eq!(stats.code_lines, 0);
        assert_eq!(stats.comment_lines, 0);
        assert_eq!(stats.doc_lines, 0);
        assert_eq!(stats.blank_lines, 4);
    }

    #[test]
    fn test_aggregation() {
        let counter = CodeCounter::new();

        let file_stats = vec![
            (
                "rs".to_string(),
                FileStats {
                    total_lines: 100,
                    code_lines: 70,
                    comment_lines: 20,
                    blank_lines: 10,
                    file_size: 1000,
                    doc_lines: 15,
                },
            ),
            (
                "rs".to_string(),
                FileStats {
                    total_lines: 50,
                    code_lines: 35,
                    comment_lines: 10,
                    blank_lines: 5,
                    file_size: 500,
                    doc_lines: 8,
                },
            ),
            (
                "py".to_string(),
                FileStats {
                    total_lines: 80,
                    code_lines: 60,
                    comment_lines: 15,
                    blank_lines: 5,
                    file_size: 800,
                    doc_lines: 12,
                },
            ),
        ];

        let aggregated = counter.aggregate_stats(file_stats);

        assert_eq!(aggregated.total_files, 3);
        assert_eq!(aggregated.total_lines, 230);
        assert_eq!(aggregated.total_code_lines, 165);
        assert_eq!(aggregated.total_comment_lines, 45);
        assert_eq!(aggregated.total_blank_lines, 20);
        assert_eq!(aggregated.total_size, 2300);
        assert_eq!(aggregated.total_doc_lines, 35);

        assert_eq!(aggregated.stats_by_extension.len(), 2);

        let rust_stats = &aggregated.stats_by_extension["rs"];
        assert_eq!(rust_stats.0, 2);
        assert_eq!(rust_stats.1.total_lines, 150);

        let python_stats = &aggregated.stats_by_extension["py"];
        assert_eq!(python_stats.0, 1);
        assert_eq!(python_stats.1.total_lines, 80);
    }

    /// Aggregation must be a pure sum: whatever order files arrive in, the
    /// totals are identical. Parallel counting relies on this.
    #[test]
    fn aggregation_is_order_independent() {
        let counter = CodeCounter::new();
        let mut files: Vec<(String, FileStats)> = (0..50)
            .map(|i| {
                (
                    if i % 3 == 0 { "rs" } else { "py" }.to_string(),
                    FileStats {
                        total_lines: i + 1,
                        code_lines: i,
                        comment_lines: 1,
                        blank_lines: 0,
                        file_size: (i as u64) * 10,
                        doc_lines: 0,
                    },
                )
            })
            .collect();

        let forward = counter.aggregate_stats(files.clone());
        files.reverse();
        let reversed = counter.aggregate_stats(files);

        assert_eq!(forward, reversed);
    }

    #[test]
    fn test_comment_patterns() {
        let counter = CodeCounter::new();

        let rust_pattern = counter.comment_pattern("rs").unwrap();
        assert!(rust_pattern.single_line.contains(&"//"));
        assert!(rust_pattern.doc_patterns.contains(&"///"));
        assert!(rust_pattern.doc_patterns.contains(&"//!"));

        let python_pattern = counter.comment_pattern("py").unwrap();
        assert!(python_pattern.single_line.contains(&"#"));
        assert!(python_pattern.doc_patterns.contains(&"\"\"\""));

        let js_pattern = counter.comment_pattern("js").unwrap();
        assert!(js_pattern.single_line.contains(&"//"));
        assert!(js_pattern.doc_patterns.contains(&"/**"));
    }

    #[test]
    fn test_new_language_patterns() {
        let counter = CodeCounter::new();

        let ps_pattern = counter.comment_pattern("ps1").unwrap();
        assert!(ps_pattern.single_line.contains(&"#"));
        assert!(ps_pattern.multi_line_start.contains(&"<#"));

        let elm_pattern = counter.comment_pattern("elm").unwrap();
        assert!(elm_pattern.single_line.contains(&"--"));
        assert!(elm_pattern.multi_line_start.contains(&"{-"));
        assert!(elm_pattern.doc_patterns.contains(&"{-|"));

        let julia_pattern = counter.comment_pattern("jl").unwrap();
        assert!(julia_pattern.single_line.contains(&"#"));
        assert!(julia_pattern.multi_line_start.contains(&"#="));

        let sql_pattern = counter.comment_pattern("sql").unwrap();
        assert!(sql_pattern.single_line.contains(&"--"));
        assert!(sql_pattern.multi_line_start.contains(&"/*"));

        let elixir_pattern = counter.comment_pattern("ex").unwrap();
        assert!(elixir_pattern.single_line.contains(&"#"));
        assert!(elixir_pattern.doc_patterns.contains(&"@doc"));

        assert!(counter
            .comment_pattern("yaml")
            .unwrap()
            .single_line
            .contains(&"#"));

        let zig_pattern = counter.comment_pattern("zig").unwrap();
        assert!(zig_pattern.single_line.contains(&"//"));
        assert!(zig_pattern.doc_patterns.contains(&"///"));

        let clj_pattern = counter.comment_pattern("clj").unwrap();
        assert!(clj_pattern.single_line.contains(&";"));
        assert!(clj_pattern.doc_patterns.contains(&";;"));

        let fs_pattern = counter.comment_pattern("fs").unwrap();
        assert!(fs_pattern.single_line.contains(&"//"));
        assert!(fs_pattern.multi_line_start.contains(&"(*"));
        assert!(fs_pattern.doc_patterns.contains(&"///"));

        let dart_pattern = counter.comment_pattern("dart").unwrap();
        assert!(dart_pattern.single_line.contains(&"//"));
        assert!(dart_pattern.doc_patterns.contains(&"///"));

        let matlab_pattern = counter.comment_pattern("m").unwrap();
        assert!(matlab_pattern.single_line.contains(&"%"));
        assert!(matlab_pattern.doc_patterns.contains(&"%%"));

        let r_pattern = counter.comment_pattern("r").unwrap();
        assert!(r_pattern.single_line.contains(&"#"));
        assert!(r_pattern.doc_patterns.contains(&"#'"));
    }

    #[test]
    fn test_mixed_comment_types() {
        let project = TestProject::new("test_mixed").unwrap();
        let content = r#"
// Single line comment
/* Multi-line comment
   continues here */
/// Documentation comment
fn main() {
    // Another comment
    println!("Hello, world!");
    /* Inline comment */ let x = 5;
}
"#;
        let file_path = project.create_file("test.rs", content).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.comment_lines >= 4);
        assert!(stats.doc_lines >= 1);
        assert!(stats.code_lines >= 3);
    }

    #[test]
    fn test_multiline_strings_vs_comments() {
        let project = TestProject::new("test_multiline").unwrap();
        let content = r#"
def test_function():
    """This is a docstring
    that spans multiple lines
    and should be counted as doc"""
    # This is a comment
    code = '''This is a string
    not a comment'''
    return code
"#;
        let file_path = project.create_file("test.py", content).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.doc_lines >= 3);
        assert!(stats.comment_lines >= 1);
        assert!(stats.code_lines >= 3);
    }

    #[test]
    fn test_file_extension_detection() {
        let project = TestProject::new("test_extensions").unwrap();

        let files = vec![
            ("test.rs", "fn main() {}", "rs"),
            ("test.py", "def main():", "py"),
            ("test.js", "function main() {}", "js"),
            ("test.ts", "function main(): void {}", "ts"),
            ("test.java", "public class Test {}", "java"),
            ("test.cpp", "int main() {}", "cpp"),
            ("test.c", "int main() {}", "c"),
            ("test.go", "func main() {}", "go"),
            ("test.rb", "def main", "rb"),
            ("test.php", "<?php function main() {}", "php"),
            ("test.cs", "public class Test {}", "cs"),
            ("test.swift", "func main() {}", "swift"),
            ("test.kt", "fun main() {}", "kt"),
            ("test.scala", "object Main {}", "scala"),
            ("test.md", "# Header", "md"),
            ("test.html", "<html></html>", "html"),
            ("test.css", "body { color: red; }", "css"),
            ("test.json", "{\"key\": \"value\"}", "json"),
            ("test.xml", "<root></root>", "xml"),
            ("test.yaml", "key: value", "yaml"),
            ("test.yml", "key: value", "yml"),
            ("test.toml", "key = \"value\"", "toml"),
        ];

        let counter = CodeCounter::new();

        for (filename, content, expected_ext) in files {
            let file_path = project.create_file(filename, content).unwrap();
            let stats = counter.count_file(&file_path).unwrap();

            assert!(
                stats.total_lines > 0,
                "File {} should have content",
                filename
            );
            assert!(
                counter.supports_extension(expected_ext),
                "missing comment patterns for {expected_ext}"
            );
        }
    }

    /// Extensions differing only in case must classify identically.
    #[test]
    fn extension_matching_is_case_insensitive() {
        let project = TestProject::new("case").unwrap();
        let counter = CodeCounter::new();
        let content = "// comment\nfn main() {}\n";

        let lower = counter
            .count_file(&project.create_file("a.rs", content).unwrap())
            .unwrap();
        let upper = counter
            .count_file(&project.create_file("b.RS", content).unwrap())
            .unwrap();

        assert_eq!(lower, upper, "uppercase extension classified differently");
    }

    #[test]
    fn test_binary_file_handling() {
        let project = TestProject::new("test_binary").unwrap();

        let binary_content = vec![0u8, 1, 2, 3, 255, 254, 253];
        let file_path = project.root.join("binary.bin");
        std::fs::write(&file_path, binary_content).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        // No newline, so the whole buffer is one line.
        assert_eq!(stats.total_lines, 1);
        assert!(stats.is_consistent());
    }

    /// A file that is not valid UTF-8 must be counted, not dropped. Previously
    /// the read failed and the file disappeared from the totals entirely.
    #[test]
    fn non_utf8_files_are_counted() {
        let project = TestProject::new("non_utf8").unwrap();
        let file_path = project.root.join("latin1.rs");
        std::fs::write(&file_path, b"// caf\xe9\nfn main() {}\n").unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert_eq!(stats.total_lines, 2);
        assert_eq!(stats.comment_lines, 1);
        assert_eq!(stats.code_lines, 1);
    }

    #[test]
    fn test_very_long_lines() {
        let project = TestProject::new("test_long_lines").unwrap();

        let long_line = "// ".to_string() + &"x".repeat(10000);
        let content = format!("{}\nfn main() {{}}\n{}", long_line, long_line);
        let file_path = project.create_file("long.rs", &content).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert_eq!(stats.total_lines, 3);
        assert_eq!(stats.comment_lines, 2);
        assert_eq!(stats.code_lines, 1);
    }

    #[test]
    fn test_nested_comments() {
        let project = TestProject::new("test_nested").unwrap();
        let content = r#"
/* Outer comment
   /* Nested comment */
   Still outer comment */
fn main() {
    // Regular comment
}
"#;
        let file_path = project.create_file("nested.rs", content).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.comment_lines >= 3);
        assert!(stats.code_lines >= 2);
    }

    #[test]
    fn test_comment_patterns_comprehensive() {
        let counter = CodeCounter::new();

        let expected_languages = vec![
            "rs", "py", "js", "ts", "jsx", "tsx", "java", "c", "cpp", "cc", "cxx", "h", "hpp",
            "cs", "go", "rb", "php", "swift", "kt", "scala", "html", "css", "scss", "sass", "md",
            "yaml", "yml", "json", "toml", "xml", "sh", "bash", "zsh", "fish", "ps1", "elm", "jl",
            "sql", "ex", "exs", "zig", "clj", "cljs", "fs", "fsx", "fsi",
        ];

        for lang in expected_languages {
            let pattern = counter
                .comment_pattern(lang)
                .unwrap_or_else(|| panic!("Missing comment patterns for language: {lang}"));

            if !pattern.single_line.is_empty() {
                assert!(
                    !pattern.single_line[0].is_empty(),
                    "Empty single-line comment pattern for {lang}"
                );
            }
        }
    }

    #[test]
    fn test_code_vs_comment_detection() {
        let project = TestProject::new("test_detection").unwrap();
        let content = r#"
fn main() {
    let url = "https://example.com"; // Not a comment marker in string
    let comment = "// This is not a comment";
    // This IS a comment
    println!("/* Not a comment */");
    /* This IS a comment */
    let regex = r"//.*"; // Regex pattern, not comment
}
"#;
        let file_path = project.create_file("tricky.rs", content).unwrap();

        let counter = CodeCounter::new();
        let stats = counter.count_file(&file_path).unwrap();

        assert!(stats.comment_lines >= 2);
        assert!(stats.code_lines >= 4);
    }

    #[test]
    fn test_calculate_file_stats_comprehensive() {
        let project = TestProject::new("test_comprehensive").unwrap();
        let file_path = project
            .create_rust_file("comprehensive.rs", 20, 10)
            .unwrap();

        let counter = CodeCounter::new();
        let aggregated_stats = counter.calculate_file_stats(&file_path).unwrap();

        assert!(aggregated_stats.basic.total_lines > 0);
        assert_eq!(
            aggregated_stats.basic.code_lines
                + aggregated_stats.basic.comment_lines
                + aggregated_stats.basic.doc_lines
                + aggregated_stats.basic.blank_lines,
            aggregated_stats.basic.total_lines,
            "line categories must partition total_lines"
        );
        assert!(aggregated_stats.ratios.code_ratio > 0.0);
        assert!(aggregated_stats.ratios.code_ratio <= 1.0);

        assert!(!aggregated_stats.metadata.version.is_empty());
        assert!(!aggregated_stats.metadata.timestamp.is_empty());
        assert_eq!(aggregated_stats.metadata.file_count_analyzed, 1);
    }

    #[test]
    fn test_calculate_project_stats_comprehensive() {
        let project = TestProject::new("test_project_stats").unwrap();

        project.create_rust_file("main.rs", 15, 8).unwrap();
        project.create_rust_file("lib.rs", 25, 12).unwrap();
        project.create_python_file("script.py", 20).unwrap();

        let counter = CodeCounter::new();

        let mut stats_by_extension = BTreeMap::new();
        stats_by_extension.insert(
            "rs".to_string(),
            (
                2,
                FileStats {
                    total_lines: 100,
                    code_lines: 70,
                    comment_lines: 20,
                    doc_lines: 5,
                    blank_lines: 10,
                    file_size: 2000,
                },
            ),
        );
        stats_by_extension.insert(
            "py".to_string(),
            (
                1,
                FileStats {
                    total_lines: 50,
                    code_lines: 35,
                    comment_lines: 10,
                    doc_lines: 2,
                    blank_lines: 5,
                    file_size: 1000,
                },
            ),
        );

        let code_stats = CodeStats {
            total_files: 3,
            total_lines: 150,
            total_code_lines: 105,
            total_comment_lines: 30,
            total_doc_lines: 7,
            total_blank_lines: 15,
            total_size: 3000,
            stats_by_extension,
        };

        let individual_files = vec![
            (
                project.root.join("main.rs").to_string_lossy().to_string(),
                FileStats {
                    total_lines: 50,
                    code_lines: 35,
                    comment_lines: 10,
                    doc_lines: 2,
                    blank_lines: 5,
                    file_size: 1000,
                },
            ),
            (
                project.root.join("lib.rs").to_string_lossy().to_string(),
                FileStats {
                    total_lines: 50,
                    code_lines: 35,
                    comment_lines: 10,
                    doc_lines: 3,
                    blank_lines: 5,
                    file_size: 1000,
                },
            ),
            (
                project.root.join("script.py").to_string_lossy().to_string(),
                FileStats {
                    total_lines: 50,
                    code_lines: 35,
                    comment_lines: 10,
                    doc_lines: 2,
                    blank_lines: 5,
                    file_size: 1000,
                },
            ),
        ];

        let aggregated_stats = counter
            .calculate_project_stats(&code_stats, &individual_files)
            .unwrap();

        assert_eq!(aggregated_stats.basic.total_files, 3);
        assert_eq!(aggregated_stats.basic.total_lines, 150);
        assert_eq!(aggregated_stats.basic.code_lines, 105);
        assert!(aggregated_stats.ratios.code_ratio > 0.0);

        assert_eq!(aggregated_stats.metadata.file_count_analyzed, 3);
        assert!(aggregated_stats.metadata.languages_detected.len() >= 2);
    }

    #[test]
    fn test_aggregate_stats_functionality() {
        let counter = CodeCounter::new();

        let file_stats = vec![
            (
                "rs".to_string(),
                FileStats {
                    total_lines: 100,
                    code_lines: 70,
                    comment_lines: 20,
                    doc_lines: 5,
                    blank_lines: 10,
                    file_size: 2000,
                },
            ),
            (
                "rs".to_string(),
                FileStats {
                    total_lines: 50,
                    code_lines: 35,
                    comment_lines: 10,
                    doc_lines: 2,
                    blank_lines: 5,
                    file_size: 1000,
                },
            ),
            (
                "py".to_string(),
                FileStats {
                    total_lines: 80,
                    code_lines: 60,
                    comment_lines: 15,
                    doc_lines: 3,
                    blank_lines: 5,
                    file_size: 1500,
                },
            ),
        ];

        let aggregated = counter.aggregate_stats(file_stats);

        assert_eq!(aggregated.total_files, 3);
        assert_eq!(aggregated.total_lines, 230);
        assert_eq!(aggregated.total_code_lines, 165);
        assert_eq!(aggregated.total_comment_lines, 45);
        assert_eq!(aggregated.total_doc_lines, 10);
        assert_eq!(aggregated.total_blank_lines, 20);
        assert_eq!(aggregated.total_size, 4500);

        assert_eq!(aggregated.stats_by_extension.len(), 2);

        let rust_stats = &aggregated.stats_by_extension["rs"];
        assert_eq!(rust_stats.0, 2);
        assert_eq!(rust_stats.1.total_lines, 150);
        assert_eq!(rust_stats.1.code_lines, 105);

        let python_stats = &aggregated.stats_by_extension["py"];
        assert_eq!(python_stats.0, 1);
        assert_eq!(python_stats.1.total_lines, 80);
        assert_eq!(python_stats.1.code_lines, 60);
    }

    #[test]
    fn test_stats_calculator_access() {
        let counter = CodeCounter::new();
        let stats_calc = counter.stats_calculator();
        let stats_calc2 = counter.stats_calculator();
        assert!(std::ptr::eq(stats_calc, stats_calc2));
    }

    #[test]
    fn test_error_handling() {
        let counter = CodeCounter::new();

        let non_existent = std::path::Path::new("/non/existent/file.rs");
        assert!(counter.count_file(non_existent).is_err());

        let temp_dir = tempfile::tempdir().unwrap();
        let err = counter.count_file(temp_dir.path()).unwrap_err();
        assert!(
            err.to_string().contains("directory"),
            "directory error should say so, got: {err}"
        );
    }

    #[test]
    fn test_performance_with_large_file() {
        let project = TestProject::new("test_performance").unwrap();

        let mut large_content = String::new();
        for i in 0..1000 {
            large_content.push_str(&format!("// Comment line {}\n", i));
            large_content.push_str(&format!("fn function_{}() {{\n", i));
            large_content.push_str("    println!(\"Hello\");\n");
            large_content.push_str("}\n\n");
        }

        let file_path = project.create_file("large.rs", &large_content).unwrap();

        let counter = CodeCounter::new();
        let start = std::time::Instant::now();
        let stats = counter.count_file(&file_path).unwrap();
        let duration = start.elapsed();

        assert!(duration.as_secs() < 1);
        assert_eq!(stats.comment_lines, 1000);
        assert!(stats.code_lines >= 2000);
        assert!(stats.total_lines >= 4000);
    }

    /// Counting with a known size must agree with counting that discovers it.
    #[test]
    fn count_with_known_size_matches_full_count() {
        let project = TestProject::new("known_size").unwrap();
        let path = project.create_rust_file("a.rs", 5, 3).unwrap();
        let size = std::fs::metadata(&path).unwrap().len();

        let counter = CodeCounter::new();
        assert_eq!(
            counter.count_file(&path).unwrap(),
            counter.count_file_with_size(&path, size).unwrap()
        );
    }

    /// In-memory classification must agree with reading the same bytes off disk.
    #[test]
    fn count_bytes_matches_count_file() {
        let project = TestProject::new("bytes").unwrap();
        let counter = CodeCounter::new();

        for (name, ext, content) in [
            ("a.rs", "rs", "// c\nfn main() {}\n\n"),
            ("b.py", "py", "# c\ndef f():\n    pass\n"),
            ("c.md", "md", "# T\n\ntext\n"),
        ] {
            let path = project.create_file(name, content).unwrap();
            assert_eq!(
                counter.count_file(&path).unwrap(),
                counter.count_bytes(content.as_bytes(), ext).unwrap(),
                "{name} differed between disk and memory"
            );
        }
    }

    /// The cache must never change the answer, only the cost of getting it.
    #[test]
    fn cached_counting_matches_uncached() {
        let project = TestProject::new("cache_equiv").unwrap();
        let paths: Vec<_> = (0..12)
            .map(|i| {
                project
                    .create_rust_file(&format!("f{i}.rs"), i % 5 + 1, i % 3)
                    .unwrap()
            })
            .collect();

        let plain = CodeCounter::new();
        let mut cached = CachedCodeCounter::with_cache(FileCache::new());

        for path in &paths {
            let expected = plain.count_file(path).unwrap();
            assert_eq!(cached.count_file(path).unwrap(), expected, "first pass");
            assert_eq!(cached.count_file(path).unwrap(), expected, "cached pass");
        }

        let (hits, misses) = cached.cache_stats();
        assert_eq!(misses, paths.len(), "every file should miss exactly once");
        assert_eq!(hits, paths.len(), "every file should hit exactly once");
    }

    /// Editing a file must invalidate its cache entry.
    #[test]
    fn cache_invalidates_when_content_changes() {
        let project = TestProject::new("cache_invalidate").unwrap();
        let path = project.create_file("a.rs", "fn a() {}\n").unwrap();

        let mut cached = CachedCodeCounter::with_cache(FileCache::new());
        let before = cached.count_file(&path).unwrap();
        assert_eq!(before.total_lines, 1);

        project
            .create_file("a.rs", "fn a() {}\nfn b() {}\nfn c() {}\n")
            .unwrap();

        let after = cached.count_file(&path).unwrap();
        assert_eq!(after.total_lines, 3, "cache returned stale statistics");
    }
}