batless 0.5.0

A non-blocking, LLM-friendly code viewer inspired by bat
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
//! Code summarization functionality for batless
//!
//! This module extracts important code structures and patterns from source files
//! to provide concise summaries of the code content.

use crate::summary_item::SummaryItem;
use crate::{summary::SummaryLevel, traits::SummaryExtraction};
use std::collections::HashSet;

/// Code summary extractor
pub struct SummaryExtractor;

impl SummaryExtractor {
    /// Extract a summary of important code structures from the given lines
    pub fn extract_summary(
        lines: &[String],
        language: Option<&str>,
        level: SummaryLevel,
    ) -> Vec<SummaryItem> {
        if !level.is_enabled() {
            return Vec::new();
        }

        let mut summary = Vec::new();
        let mut seen_patterns = HashSet::new();
        let max_items = match level {
            SummaryLevel::Minimal => 25,
            SummaryLevel::Standard => 50,
            SummaryLevel::Detailed => 100,
            SummaryLevel::None => 0,
        };

        for (idx, line) in lines.iter().enumerate() {
            let trimmed = line.trim();
            let line_number = idx + 1;

            // Skip empty lines but include important comments
            if trimmed.is_empty() {
                continue;
            }

            // Include important comments
            if Self::is_important_comment(trimmed) {
                summary.push(SummaryItem::new(line, line_number, None, "comment"));
                continue;
            }

            // Skip other comments
            if trimmed.starts_with("//") || trimmed.starts_with('#') {
                continue;
            }

            // Check if this line contains summary-worthy content
            if trimmed.starts_with("//") || trimmed.starts_with('#') {
                if matches!(level, SummaryLevel::Detailed) {
                    let pattern_key = Self::extract_pattern_key(trimmed);
                    if seen_patterns.insert(pattern_key) {
                        summary.push(SummaryItem::new(line, line_number, None, "comment"));
                    }
                }
                continue;
            }

            if Self::is_summary_worthy(trimmed, language, level) {
                // Avoid duplicate patterns in summary
                let pattern_key = Self::extract_pattern_key(trimmed);
                if !seen_patterns.contains(&pattern_key) {
                    let kind = Self::infer_kind(trimmed);
                    summary.push(SummaryItem::new(line, line_number, None, kind));
                    seen_patterns.insert(pattern_key);
                }
            }

            if max_items > 0 && summary.len() >= max_items {
                break;
            }
        }

        // Limit summary size to keep it concise
        if max_items > 0 && summary.len() > max_items {
            summary.truncate(max_items);
        }

        summary
    }

    /// Infer the kind of a code structure from the line text
    fn infer_kind(line: &str) -> &'static str {
        let t = line.trim();
        if t.starts_with("fn ")
            || t.starts_with("pub fn ")
            || t.starts_with("async fn ")
            || t.starts_with("def ")
            || t.starts_with("async def ")
            || t.starts_with("function ")
            || t.starts_with("async function ")
            || t.starts_with("func ")
        {
            "function"
        } else if t.starts_with("class ") || t.starts_with("pub class ") {
            "class"
        } else if t.starts_with("struct ") || t.starts_with("pub struct ") {
            "struct"
        } else if t.starts_with("enum ") || t.starts_with("pub enum ") {
            "enum"
        } else if t.starts_with("trait ")
            || t.starts_with("pub trait ")
            || t.starts_with("interface ")
            || t.starts_with("protocol ")
        {
            "trait"
        } else if t.starts_with("impl ") {
            "impl"
        } else if t.starts_with("import ")
            || t.starts_with("use ")
            || t.starts_with("from ")
            || t.starts_with("require ")
        {
            "import"
        } else if t.starts_with("//") || t.starts_with('#') || t.starts_with("/**") {
            "comment"
        } else {
            "other"
        }
    }

    /// Check if a line contains summary-worthy code structures
    fn is_summary_worthy(line: &str, language: Option<&str>, level: SummaryLevel) -> bool {
        let trimmed = line.trim();

        // Skip empty lines and basic comments
        if trimmed.is_empty() || trimmed.starts_with("//") || trimmed.starts_with('#') {
            return false;
        }

        let language_match = match language {
            Some("Python") => Self::is_python_summary_worthy(trimmed),
            Some("Rust") => Self::is_rust_summary_worthy(trimmed),
            Some("JavaScript" | "TypeScript") => Self::is_js_ts_summary_worthy(trimmed),
            Some("Java") => Self::is_java_summary_worthy(trimmed),
            Some("C" | "C++") => Self::is_c_cpp_summary_worthy(trimmed),
            Some("Go") => Self::is_go_summary_worthy(trimmed),
            Some("Ruby") => Self::is_ruby_summary_worthy(trimmed),
            Some("PHP") => Self::is_php_summary_worthy(trimmed),
            Some("Swift") => Self::is_swift_summary_worthy(trimmed),
            Some("Kotlin") => Self::is_kotlin_summary_worthy(trimmed),
            Some("Scala") => Self::is_scala_summary_worthy(trimmed),
            Some("Haskell") => Self::is_haskell_summary_worthy(trimmed),
            Some("Clojure") => Self::is_clojure_summary_worthy(trimmed),
            Some("Elixir") => Self::is_elixir_summary_worthy(trimmed),
            Some("Erlang") => Self::is_erlang_summary_worthy(trimmed),
            _ => Self::is_generic_summary_worthy(trimmed),
        };

        let core_match = Self::is_core_structure(trimmed);
        let detail_match = Self::is_detail_structure(trimmed);

        match level {
            SummaryLevel::Minimal => core_match,
            SummaryLevel::Standard => language_match || core_match,
            SummaryLevel::Detailed => language_match || core_match || detail_match,
            SummaryLevel::None => false,
        }
    }

    /// Python-specific summary patterns
    fn is_python_summary_worthy(line: &str) -> bool {
        line.starts_with("def ")
            || line.starts_with("class ")
            || line.starts_with("import ")
            || line.starts_with("from ")
            || line.starts_with('@')  // decorators
            || line.starts_with("async def ")
            || line.starts_with("if __name__ == ")
    }

    /// Rust-specific summary patterns
    fn is_rust_summary_worthy(line: &str) -> bool {
        // Function definitions
        ((line.starts_with("fn ") || line.starts_with("pub fn ") || line.starts_with("async fn "))
            && line.contains('{'))
        // Struct/enum/trait definitions
        || (line.starts_with("struct ") || line.starts_with("pub struct "))
        || (line.starts_with("enum ") || line.starts_with("pub enum "))
        || (line.starts_with("trait ") || line.starts_with("pub trait "))
        || line.starts_with("impl ")
        // Imports and constants
        || line.starts_with("use ")
        || (line.starts_with("const ") || line.starts_with("pub const "))
        || (line.starts_with("static ") || line.starts_with("pub static "))
        // Macros
        || line.starts_with("macro_rules!")
    }

    /// JavaScript/TypeScript-specific summary patterns
    fn is_js_ts_summary_worthy(line: &str) -> bool {
        line.starts_with("function ")
            || line.starts_with("class ")
            || line.starts_with("interface ")
            || line.starts_with("type ")
            || line.starts_with("export ")
            || line.starts_with("import ")
            || line.starts_with("async function ")
            || (line.starts_with("const ") && (line.contains("function") || line.contains("=>")))
            || (line.starts_with("let ") && (line.contains("function") || line.contains("=>")))
            || (line.starts_with("var ") && (line.contains("function") || line.contains("=>")))
    }

    /// Java-specific summary patterns
    fn is_java_summary_worthy(line: &str) -> bool {
        line.starts_with("public class ")
            || line.starts_with("private class ")
            || line.starts_with("protected class ")
            || line.starts_with("class ")
            || line.starts_with("interface ")
            || line.starts_with("enum ")
            || line.starts_with("public ")
            || line.starts_with("private ")
            || line.starts_with("protected ")
            || line.starts_with("import ")
            || line.starts_with("package ")
            || line.contains("void main(")
    }

    /// C/C++-specific summary patterns
    fn is_c_cpp_summary_worthy(line: &str) -> bool {
        line.starts_with("#include ")
            || line.starts_with("#define ")
            || line.starts_with("typedef ")
            || line.starts_with("struct ")
            || line.starts_with("class ")
            || line.starts_with("namespace ")
            || line.starts_with("template ")
            || (line.contains('(') && line.contains(')') && line.contains('{'))  // function definitions
            || line.starts_with("extern ")
            || line.starts_with("static ")
    }

    /// Go-specific summary patterns
    fn is_go_summary_worthy(line: &str) -> bool {
        line.starts_with("func ")
            || line.starts_with("type ")
            || line.starts_with("var ")
            || line.starts_with("const ")
            || line.starts_with("package ")
            || line.starts_with("import ")
            || line.starts_with("interface ")
            || line.starts_with("struct ")
    }

    /// Ruby-specific summary patterns
    fn is_ruby_summary_worthy(line: &str) -> bool {
        line.starts_with("def ")
            || line.starts_with("class ")
            || line.starts_with("module ")
            || line.starts_with("require ")
            || line.starts_with("include ")
            || line.starts_with("extend ")
            || line.starts_with("attr_")
    }

    /// PHP-specific summary patterns
    fn is_php_summary_worthy(line: &str) -> bool {
        line.starts_with("function ")
            || line.starts_with("class ")
            || line.starts_with("interface ")
            || line.starts_with("trait ")
            || line.starts_with("namespace ")
            || line.starts_with("use ")
            || line.starts_with("require ")
            || line.starts_with("include ")
            || line.starts_with("public function ")
            || line.starts_with("private function ")
            || line.starts_with("protected function ")
    }

    /// Swift-specific summary patterns
    fn is_swift_summary_worthy(line: &str) -> bool {
        line.starts_with("func ")
            || line.starts_with("class ")
            || line.starts_with("struct ")
            || line.starts_with("enum ")
            || line.starts_with("protocol ")
            || line.starts_with("extension ")
            || line.starts_with("import ")
            || line.starts_with("var ")
            || line.starts_with("let ")
            || line.starts_with("typealias ")
    }

    /// Kotlin-specific summary patterns
    fn is_kotlin_summary_worthy(line: &str) -> bool {
        line.starts_with("fun ")
            || line.starts_with("class ")
            || line.starts_with("interface ")
            || line.starts_with("object ")
            || line.starts_with("enum class ")
            || line.starts_with("data class ")
            || line.starts_with("sealed class ")
            || line.starts_with("import ")
            || line.starts_with("package ")
            || line.starts_with("val ")
            || line.starts_with("var ")
    }

    /// Scala-specific summary patterns
    fn is_scala_summary_worthy(line: &str) -> bool {
        line.starts_with("def ")
            || line.starts_with("class ")
            || line.starts_with("object ")
            || line.starts_with("trait ")
            || line.starts_with("case class ")
            || line.starts_with("sealed trait ")
            || line.starts_with("import ")
            || line.starts_with("package ")
            || line.starts_with("val ")
            || line.starts_with("var ")
    }

    /// Haskell-specific summary patterns
    fn is_haskell_summary_worthy(line: &str) -> bool {
        line.contains(" :: ")  // type signatures
            || line.starts_with("data ")
            || line.starts_with("type ")
            || line.starts_with("newtype ")
            || line.starts_with("class ")
            || line.starts_with("instance ")
            || line.starts_with("import ")
            || line.starts_with("module ")
    }

    /// Clojure-specific summary patterns
    fn is_clojure_summary_worthy(line: &str) -> bool {
        line.starts_with("(defn ")
            || line.starts_with("(defn- ")
            || line.starts_with("(defmacro ")
            || line.starts_with("(def ")
            || line.starts_with("(defprotocol ")
            || line.starts_with("(defrecord ")
            || line.starts_with("(deftype ")
            || line.starts_with("(ns ")
            || line.starts_with("(:require ")
            || line.starts_with("(:use ")
    }

    /// Elixir-specific summary patterns
    fn is_elixir_summary_worthy(line: &str) -> bool {
        line.starts_with("def ")
            || line.starts_with("defp ")
            || line.starts_with("defmodule ")
            || line.starts_with("defprotocol ")
            || line.starts_with("defimpl ")
            || line.starts_with("defstruct ")
            || line.starts_with("defmacro ")
            || line.starts_with("import ")
            || line.starts_with("alias ")
            || line.starts_with("use ")
    }

    /// Erlang-specific summary patterns
    fn is_erlang_summary_worthy(line: &str) -> bool {
        line.starts_with("-module(")
            || line.starts_with("-export(")
            || line.starts_with("-import(")
            || line.starts_with("-include(")
            || line.starts_with("-record(")
            || line.starts_with("-type(")
            || line.starts_with("-spec(")
            || (line.contains('(') && line.contains("->")) // function definitions
    }

    /// Generic patterns for unknown languages
    fn is_generic_summary_worthy(line: &str) -> bool {
        // Function-like patterns
        (line.starts_with("def ") && line.contains(':'))
            || (line.starts_with("class ") && line.contains(':'))
            || (line.starts_with("function ") && line.contains('{'))
            || ((line.starts_with("fn ") || line.starts_with("pub fn ")) && line.contains('{'))
            || (line.starts_with("struct ") || line.starts_with("pub struct "))
            || (line.starts_with("enum ") || line.starts_with("pub enum "))
            // Import-like patterns
            || line.starts_with("import ")
            || line.starts_with("use ")
            || line.starts_with("export ")
            || line.starts_with("module ")
            || line.starts_with("package ")
            || line.starts_with("namespace ")
            // Declaration patterns
            || line.starts_with("typedef ")
            || line.starts_with("interface ")
            || line.starts_with("protocol ")
            || line.starts_with("trait ")
    }

    /// Core structures used for minimal summaries
    fn is_core_structure(line: &str) -> bool {
        const CORE_PREFIXES: &[&str] = &[
            "fn ",
            "pub fn ",
            "async fn ",
            "def ",
            "async def ",
            "function ",
            "class ",
            "struct ",
            "enum ",
            "trait ",
            "impl ",
            "interface ",
            "module ",
            "package ",
            "namespace ",
            "import ",
            "export ",
            "use ",
            "type ",
        ];

        let lower = line.trim_start().to_lowercase();
        CORE_PREFIXES.iter().any(|prefix| lower.starts_with(prefix))
    }

    /// Additional structures surfaced for detailed summaries
    fn is_detail_structure(line: &str) -> bool {
        let trimmed = line.trim_start();
        let lower = trimmed.to_lowercase();
        lower.starts_with("let ")
            || lower.starts_with("mut ")
            || lower.starts_with("const ")
            || lower.starts_with("static ")
            || lower.starts_with("pub const ")
            || lower.starts_with("pub static ")
            || lower.starts_with("var ")
            || lower.starts_with("val ")
            || lower.starts_with("#[")
            || lower.starts_with('@')
            || lower.starts_with("///")
            || lower.starts_with("//!")
            || lower.starts_with("/**")
            || lower.contains("todo")
            || (lower.contains(" = ")
                && (lower.starts_with("let ")
                    || lower.starts_with("const ")
                    || lower.starts_with("var ")
                    || lower.starts_with("val ")))
    }

    /// Check if a comment is important enough to include in summary
    fn is_important_comment(line: &str) -> bool {
        let line_lower = line.to_lowercase();
        // Check for specific important markers at word boundaries
        line_lower.contains("todo:")
            || line_lower.contains("fixme:")
            || line_lower.contains("hack:")
            || line_lower.contains("note:")
            || line_lower.contains("warning:")
            || line_lower.contains("important:")
            || line_lower.starts_with("///")  // Rust doc comments
            || line_lower.starts_with("/**")  // Multi-line doc comments
            || line_lower.starts_with("#!")   // Shebang or module-level comments
            || line_lower.starts_with("##") // Important markdown headers
    }

    /// Extract a pattern key for deduplication
    fn extract_pattern_key(line: &str) -> String {
        let trimmed = line.trim();

        // Extract the essential pattern (first few words)
        let words: Vec<&str> = trimmed.split_whitespace().take(3).collect();
        words.join(" ")
    }

    /// Get summary statistics
    pub fn get_summary_stats(
        original_lines: &[String],
        summary_lines: &[SummaryItem],
    ) -> SummaryStats {
        SummaryStats {
            original_line_count: original_lines.len(),
            summary_line_count: summary_lines.len(),
            compression_ratio: if original_lines.is_empty() {
                0.0
            } else {
                summary_lines.len() as f64 / original_lines.len() as f64
            },
            reduction_percentage: if original_lines.is_empty() {
                0.0
            } else {
                (1.0 - (summary_lines.len() as f64 / original_lines.len() as f64)) * 100.0
            },
        }
    }
}

impl SummaryExtraction for SummaryExtractor {
    fn extract_summary(
        &self,
        lines: &[String],
        language: Option<&str>,
        level: SummaryLevel,
    ) -> Vec<SummaryItem> {
        Self::extract_summary(lines, language, level)
    }

    fn is_summary_worthy(&self, line: &str, language: Option<&str>, level: SummaryLevel) -> bool {
        Self::is_summary_worthy(line, language, level)
    }
}

/// Statistics about the summarization process
#[derive(Debug, Clone)]
pub struct SummaryStats {
    pub original_line_count: usize,
    pub summary_line_count: usize,
    pub compression_ratio: f64,
    pub reduction_percentage: f64,
}

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

    #[test]
    fn test_python_summary() {
        let lines = vec![
            "def main():".to_string(),
            "    print('hello')".to_string(),
            "class MyClass:".to_string(),
            "    pass".to_string(),
            "import os".to_string(),
        ];

        let summary =
            SummaryExtractor::extract_summary(&lines, Some("Python"), SummaryLevel::Standard);
        assert_eq!(summary.len(), 3); // def, class, import
        assert!(summary.iter().any(|s| s.line == "def main():"));
        assert!(summary.iter().any(|s| s.line == "class MyClass:"));
        assert!(summary.iter().any(|s| s.line == "import os"));
    }

    #[test]
    fn test_rust_summary() {
        let lines = vec![
            "fn main() {".to_string(),
            "    println!(\"Hello\");".to_string(),
            "}".to_string(),
            "struct Point {".to_string(),
            "    x: i32,".to_string(),
            "}".to_string(),
            "use std::collections::HashMap;".to_string(),
        ];

        let summary =
            SummaryExtractor::extract_summary(&lines, Some("Rust"), SummaryLevel::Standard);
        assert_eq!(summary.len(), 3); // fn, struct, use
        assert!(summary.iter().any(|s| s.line == "fn main() {"));
        assert!(summary.iter().any(|s| s.line == "struct Point {"));
        assert!(summary
            .iter()
            .any(|s| s.line == "use std::collections::HashMap;"));
    }

    #[test]
    fn test_javascript_summary() {
        let lines = vec![
            "function hello() {".to_string(),
            "    console.log('hello');".to_string(),
            "}".to_string(),
            "class MyClass {".to_string(),
            "    constructor() {}".to_string(),
            "}".to_string(),
            "export default MyClass;".to_string(),
        ];

        let summary =
            SummaryExtractor::extract_summary(&lines, Some("JavaScript"), SummaryLevel::Standard);
        assert_eq!(summary.len(), 3); // function, class, export
        assert!(summary.iter().any(|s| s.line == "function hello() {"));
        assert!(summary.iter().any(|s| s.line == "class MyClass {"));
        assert!(summary.iter().any(|s| s.line == "export default MyClass;"));
    }

    #[test]
    fn test_empty_input() {
        let lines = vec![];
        let summary =
            SummaryExtractor::extract_summary(&lines, Some("Python"), SummaryLevel::Standard);
        assert!(summary.is_empty());
    }

    #[test]
    fn test_comments_filtering() {
        let lines = vec![
            "// This is a comment".to_string(),
            "fn main() {".to_string(),
            "    // Another comment".to_string(),
            "    println!(\"Hello\");".to_string(),
            "}".to_string(),
            "/// This is an important doc comment".to_string(),
        ];

        let summary =
            SummaryExtractor::extract_summary(&lines, Some("Rust"), SummaryLevel::Standard);
        assert_eq!(summary.len(), 2); // fn and doc comment
        assert!(summary.iter().any(|s| s.line == "fn main() {"));
        assert!(summary
            .iter()
            .any(|s| s.line == "/// This is an important doc comment"));
    }

    #[test]
    fn test_deduplication() {
        let lines = vec![
            "fn test1() {".to_string(),
            "fn test2() {".to_string(),
            "fn test3() {".to_string(),
        ];

        let summary =
            SummaryExtractor::extract_summary(&lines, Some("Rust"), SummaryLevel::Standard);
        // Should include all since they have different names
        assert_eq!(summary.len(), 3);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn test_summary_stats() {
        use crate::summary_item::SummaryItem;
        let original = vec!["line1".to_string(); 100];
        let summary: Vec<SummaryItem> = (0..20)
            .map(|i| SummaryItem::new("line1", i + 1, None, "other"))
            .collect();

        let stats = SummaryExtractor::get_summary_stats(&original, &summary);
        assert_eq!(stats.original_line_count, 100);
        assert_eq!(stats.summary_line_count, 20);
        assert_eq!(stats.compression_ratio, 0.2);
        assert_eq!(stats.reduction_percentage, 80.0);
    }

    #[test]
    fn test_generic_language() {
        let lines = vec![
            "function test() {".to_string(),
            "    return true;".to_string(),
            "}".to_string(),
            "import something;".to_string(),
        ];

        let summary = SummaryExtractor::extract_summary(&lines, None, SummaryLevel::Standard);
        assert_eq!(summary.len(), 2); // function and import
        assert!(summary.iter().any(|s| s.line == "function test() {"));
        assert!(summary.iter().any(|s| s.line == "import something;"));
    }

    #[test]
    fn test_summary_levels_adjust_content() {
        let lines = vec![
            "fn main() {}".to_string(),
            "let config = load();".to_string(),
            "use crate::utils;".to_string(),
        ];

        let minimal =
            SummaryExtractor::extract_summary(&lines, Some("Rust"), SummaryLevel::Minimal);
        assert!(minimal.iter().any(|s| s.line == "fn main() {}"));
        assert!(
            !minimal.iter().any(|s| s.line.contains("let config")),
            "Minimal summaries should skip fine-grained assignments"
        );

        let detailed =
            SummaryExtractor::extract_summary(&lines, Some("Rust"), SummaryLevel::Detailed);
        assert!(detailed.iter().any(|s| s.line.contains("let config")));
        assert!(detailed
            .iter()
            .any(|s| s.line.contains("use crate::utils;")));
    }

    #[test]
    fn test_line_numbers_in_summary() {
        let lines = vec![
            "// comment".to_string(),
            "fn main() {".to_string(),
            "    println!(\"Hello\");".to_string(),
            "}".to_string(),
            "struct Foo {".to_string(),
        ];

        let summary =
            SummaryExtractor::extract_summary(&lines, Some("Rust"), SummaryLevel::Standard);
        let fn_item = summary.iter().find(|s| s.line == "fn main() {").unwrap();
        assert_eq!(fn_item.line_number, 2);
        let struct_item = summary.iter().find(|s| s.line == "struct Foo {").unwrap();
        assert_eq!(struct_item.line_number, 5);
    }
}