loregrep 0.6.0

Repository indexing library for AI coding assistants. Tree-sitter parsing, fast in-memory indexing, and tool APIs for LLM integration.
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
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
use crate::analyzers::LanguageAnalyzer;
use crate::types::{
    AnalysisError, ExportStatement, FileAnalysis, FunctionCall, FunctionSignature, ImportStatement,
    Parameter, PartialAnalysis, Result, StructField, StructSignature, TreeNode, TypeKind,
};
use async_trait::async_trait;
use blake3;
use regex::Regex;
use std::time::Instant;
use streaming_iterator::StreamingIterator;
use tree_sitter::{Language, Node, Parser, Query, QueryCursor, Tree};

#[derive(Clone)]
pub struct PythonAnalyzer {
    language: Language,
}

impl PythonAnalyzer {
    pub fn new() -> Result<Self> {
        Ok(Self {
            language: tree_sitter_python::LANGUAGE.into(),
        })
    }

    /// Parse parameter node into structured parameter
    fn parse_parameter(&self, param_node: &Node, source: &str) -> Result<Parameter> {
        let query_str = r#"
            (parameter
              name: (identifier) @param_name
              type: (_)? @param_type
              default_value: (_)? @default_value
            )
            (typed_parameter
              name: (identifier) @param_name
              type: (_) @param_type
            )
            (default_parameter
              name: (identifier) @param_name
              default_value: (_) @default_value
            )
        "#;

        let query =
            Query::new(&self.language, query_str).map_err(|e| AnalysisError::QueryError {
                message: format!("{:?}", e),
            })?;

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(&query, *param_node, source.as_bytes());

        let mut param_name = String::new();
        let mut param_type = String::new();
        let mut default_value = String::new();

        while let Some(query_match) = matches.next() {
            for capture in query_match.captures {
                let capture_name = query.capture_names()[capture.index as usize];
                let text = self.safe_utf8_text(&capture.node, source);

                match capture_name {
                    "param_name" => param_name = text.to_string(),
                    "param_type" => param_type = text.to_string(),
                    "default_value" => default_value = text.to_string(),
                    _ => {}
                }
            }
        }

        // Fallback: try to get text directly if query didn't work
        if param_name.is_empty() {
            let node_text = self.safe_utf8_text(param_node, source);
            // Handle different Python parameter patterns
            if node_text.contains(':') {
                // Type annotated parameter: name: type = default
                let parts: Vec<&str> = node_text.split(':').collect();
                if let Some(name_part) = parts.first() {
                    param_name = name_part.trim().to_string();
                }
                if parts.len() > 1 {
                    let type_and_default = parts[1];
                    if let Some(equals_pos) = type_and_default.find('=') {
                        param_type = type_and_default[..equals_pos].trim().to_string();
                        default_value = type_and_default[equals_pos + 1..].trim().to_string();
                    } else {
                        param_type = type_and_default.trim().to_string();
                    }
                }
            } else if node_text.contains('=') {
                // Default parameter: name = default
                let parts: Vec<&str> = node_text.split('=').collect();
                if let Some(name_part) = parts.first() {
                    param_name = name_part.trim().to_string();
                }
                if parts.len() > 1 {
                    default_value = parts[1].trim().to_string();
                }
            } else {
                // Simple parameter: name
                param_name = node_text.trim().to_string();
            }
        }

        let mut param = Parameter::new(param_name, param_type);
        if !default_value.is_empty() {
            param = param.with_default(default_value);
        }

        Ok(param)
    }

    /// Extract decorators from a function
    fn extract_decorators(&self, function_node: &Node, source: &str) -> Vec<String> {
        let mut decorators = Vec::new();

        // Look for decorated_definition parent
        if let Some(parent) = function_node.parent() {
            if parent.kind() == "decorated_definition" {
                let mut child_cursor = parent.walk();
                if child_cursor.goto_first_child() {
                    loop {
                        let child = child_cursor.node();
                        if child.kind() == "decorator" {
                            let decorator_text = self.safe_utf8_text(&child, source);
                            if !decorator_text.is_empty() {
                                decorators.push(decorator_text.to_string());
                            }
                        }
                        if !child_cursor.goto_next_sibling() {
                            break;
                        }
                    }
                }
            }
        }

        decorators
    }

    /// Determine if a function is a method and what type
    fn analyze_method_type(
        &self,
        function_node: &Node,
        function_sig: &FunctionSignature,
        source: &str,
    ) -> (bool, bool, bool) {
        let mut is_method = false;
        let mut is_static = false;
        let mut is_class_method = false;

        // Check if function is inside a class
        let mut current = function_node.parent();
        while let Some(node) = current {
            if node.kind() == "class_definition" {
                is_method = true;
                break;
            }
            current = node.parent();
        }

        if is_method {
            // Check for @staticmethod or @classmethod decorators
            let decorators = self.extract_decorators(function_node, source);
            for decorator in &decorators {
                if decorator.contains("staticmethod") {
                    is_static = true;
                    break;
                } else if decorator.contains("classmethod") {
                    is_class_method = true;
                    break;
                }
            }

            // If not static and first parameter is 'self', it's an instance method
            if !is_static && !is_class_method {
                if let Some(first_param) = function_sig.parameters.first() {
                    if first_param.name == "self" {
                        // Instance method - not static
                    } else if first_param.name == "cls" {
                        is_class_method = true;
                    }
                }
            }
        }

        (is_method, is_static, is_class_method)
    }

    /// Find the name of the nearest enclosing `class_definition`, if any.
    /// Walks the ancestor chain from a `function_definition` node and reads the
    /// `name` field (an `identifier`) of the first `class_definition` found.
    /// Returns `None` for module-level (free) functions.
    fn enclosing_class_name(&self, function_node: &Node, source: &str) -> Option<String> {
        let mut current = function_node.parent();
        while let Some(node) = current {
            if node.kind() == "class_definition" {
                if let Some(name_node) = node.child_by_field_name("name") {
                    let name = self.safe_utf8_text(&name_node, source);
                    if !name.is_empty() {
                        return Some(name);
                    }
                }
                return None;
            }
            current = node.parent();
        }
        None
    }

    /// Calculate content hash for caching
    fn calculate_content_hash(&self, content: &str) -> String {
        blake3::hash(content.as_bytes()).to_hex().to_string()
    }

    /// Safely extract UTF-8 text from a tree-sitter node
    fn safe_utf8_text(&self, node: &Node, source: &str) -> String {
        let source_bytes = source.as_bytes();
        let start_byte = node.start_byte();
        let end_byte = node.end_byte();

        // Bounds check to prevent panic
        if start_byte >= source_bytes.len()
            || end_byte > source_bytes.len()
            || start_byte > end_byte
        {
            return String::new();
        }

        // Additional safety: try the tree-sitter method first, with fallback
        match std::panic::catch_unwind(|| node.utf8_text(source_bytes)) {
            Ok(Ok(text)) => text.to_string(),
            Ok(Err(_)) | Err(_) => {
                // Fallback: manual byte slice extraction
                match std::str::from_utf8(&source_bytes[start_byte..end_byte]) {
                    Ok(text) => text.to_string(),
                    Err(_) => String::new(),
                }
            }
        }
    }
}

#[async_trait]
impl LanguageAnalyzer for PythonAnalyzer {
    fn language(&self) -> &'static str {
        "python"
    }

    fn file_extensions(&self) -> &[&'static str] {
        &["py", "pyx", "pyi"]
    }

    fn supports_async(&self) -> bool {
        true
    }

    async fn analyze_file(&self, content: &str, file_path: &str) -> Result<FileAnalysis> {
        let start_time = Instant::now();
        let mut tree_node = TreeNode::new(file_path.to_string(), "python".to_string());

        // Calculate content hash
        tree_node.content_hash = self.calculate_content_hash(content);
        tree_node.last_modified = std::time::SystemTime::now();

        // Early validation - check for empty or invalid content
        if content.trim().is_empty() {
            tree_node.add_error("File is empty".to_string());
            let duration = start_time.elapsed().as_millis() as u64;
            return Ok(FileAnalysis::new(tree_node, duration));
        }

        // Parse with tree-sitter with comprehensive error handling
        let tree_result = std::panic::catch_unwind(|| {
            let mut parser = Parser::new();
            match parser.set_language(&self.language) {
                Ok(_) => parser.parse(content, None),
                Err(_) => None,
            }
        });

        let tree = match tree_result {
            Ok(Some(tree)) => tree,
            Ok(None) => {
                // Failed to parse - use fallback
                tree_node.add_error("Tree-sitter parsing failed, using fallback".to_string());
                let fallback_analysis = self.extract_with_fallback(content, file_path);
                let mut fallback_tree = TreeNode::new(file_path.to_string(), "python".to_string());
                fallback_tree.functions = fallback_analysis.functions;
                fallback_tree.structs = fallback_analysis.structs;
                fallback_tree.imports = fallback_analysis.imports;
                fallback_tree.exports = fallback_analysis.exports;
                fallback_tree.parse_errors = fallback_analysis.errors;
                fallback_tree.content_hash = self.calculate_content_hash(content);
                fallback_tree.last_modified = std::time::SystemTime::now();
                return Ok(FileAnalysis::new(
                    fallback_tree,
                    start_time.elapsed().as_millis() as u64,
                ));
            }
            Err(_) => {
                // Panic occurred during parsing - use fallback
                tree_node.add_error("Tree-sitter parsing panicked, using fallback".to_string());
                let fallback_analysis = self.extract_with_fallback(content, file_path);
                let mut fallback_tree = TreeNode::new(file_path.to_string(), "python".to_string());
                fallback_tree.functions = fallback_analysis.functions;
                fallback_tree.structs = fallback_analysis.structs;
                fallback_tree.imports = fallback_analysis.imports;
                fallback_tree.exports = fallback_analysis.exports;
                fallback_tree.parse_errors = fallback_analysis.errors;
                fallback_tree.content_hash = self.calculate_content_hash(content);
                fallback_tree.last_modified = std::time::SystemTime::now();
                return Ok(FileAnalysis::new(
                    fallback_tree,
                    start_time.elapsed().as_millis() as u64,
                ));
            }
        };

        // Extract all components with panic protection
        match std::panic::catch_unwind(|| self.extract_functions(&tree, content, file_path)) {
            Ok(Ok(functions)) => tree_node.functions = functions,
            Ok(Err(e)) => tree_node.add_error(format!("Function extraction failed: {}", e)),
            Err(_) => tree_node.add_error("Function extraction panicked".to_string()),
        }

        match std::panic::catch_unwind(|| self.extract_structs(&tree, content, file_path)) {
            Ok(Ok(structs)) => tree_node.structs = structs,
            Ok(Err(e)) => tree_node.add_error(format!("Class extraction failed: {}", e)),
            Err(_) => tree_node.add_error("Class extraction panicked".to_string()),
        }

        match std::panic::catch_unwind(|| self.extract_imports(&tree, content, file_path)) {
            Ok(Ok(imports)) => tree_node.imports = imports,
            Ok(Err(e)) => tree_node.add_error(format!("Import extraction failed: {}", e)),
            Err(_) => tree_node.add_error("Import extraction panicked".to_string()),
        }

        match std::panic::catch_unwind(|| self.extract_exports(&tree, content, file_path)) {
            Ok(Ok(exports)) => tree_node.exports = exports,
            Ok(Err(e)) => tree_node.add_error(format!("Export extraction failed: {}", e)),
            Err(_) => tree_node.add_error("Export extraction panicked".to_string()),
        }

        match std::panic::catch_unwind(|| self.extract_function_calls(&tree, content, file_path)) {
            Ok(Ok(function_calls)) => tree_node.function_calls = function_calls,
            Ok(Err(e)) => tree_node.add_error(format!("Function call extraction failed: {}", e)),
            Err(_) => tree_node.add_error("Function call extraction panicked".to_string()),
        }

        let duration = start_time.elapsed().as_millis() as u64;
        Ok(FileAnalysis::new(tree_node, duration))
    }

    fn extract_functions(
        &self,
        tree: &Tree,
        source: &str,
        file_path: &str,
    ) -> Result<Vec<FunctionSignature>> {
        // Query for function definitions - simplified based on actual tree structure
        let query_str = r#"
            (function_definition
              name: (identifier) @name
              parameters: (parameters) @params
              return_type: (_)? @return_type
            ) @function
        "#;

        let query =
            Query::new(&self.language, query_str).map_err(|e| AnalysisError::QueryError {
                message: format!("{:?}", e),
            })?;

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());

        let mut functions = Vec::new();

        while let Some(query_match) = matches.next() {
            let mut function_sig = FunctionSignature::new(String::new(), file_path.to_string());
            let mut function_node: Option<Node> = None;
            let mut is_async = false;

            for capture in query_match.captures {
                let capture_name = query.capture_names()[capture.index as usize];
                let text = self.safe_utf8_text(&capture.node, source);

                match capture_name {
                    "name" => function_sig.name = text.to_string(),
                    "function" => {
                        function_node = Some(capture.node);
                        let start_point = capture.node.start_position();
                        let end_point = capture.node.end_position();
                        function_sig.start_line = start_point.row as u32 + 1;
                        function_sig.end_line = end_point.row as u32 + 1;

                        // Check if this is an async function by looking at the function text
                        let function_text = self.safe_utf8_text(&capture.node, source);
                        is_async = function_text.starts_with("async ");
                    }
                    "params" => {
                        // Parse parameters
                        let mut param_cursor = capture.node.walk();
                        if param_cursor.goto_first_child() {
                            loop {
                                let param_child = param_cursor.node();
                                if param_child.kind() == "identifier"
                                    || param_child.kind() == "typed_parameter"
                                    || param_child.kind() == "default_parameter"
                                    || param_child.kind() == "list_splat_pattern"
                                    || param_child.kind() == "dictionary_splat_pattern"
                                {
                                    match self.parse_parameter(&param_child, source) {
                                        Ok(param) => function_sig.parameters.push(param),
                                        Err(_) => {
                                            // Fallback: just get the text and parse it manually
                                            let param_text =
                                                self.safe_utf8_text(&param_child, source);
                                            if !param_text.is_empty() && param_text != "," {
                                                // Parse Python parameter format: name: type = default
                                                if param_text.contains(':') {
                                                    let parts: Vec<&str> =
                                                        param_text.split(':').collect();
                                                    if let Some(name_part) = parts.first() {
                                                        let param_name =
                                                            name_part.trim().to_string();
                                                        let param_type = if parts.len() > 1 {
                                                            let type_and_default = parts[1];
                                                            if let Some(equals_pos) =
                                                                type_and_default.find('=')
                                                            {
                                                                type_and_default[..equals_pos]
                                                                    .trim()
                                                                    .to_string()
                                                            } else {
                                                                type_and_default.trim().to_string()
                                                            }
                                                        } else {
                                                            String::new()
                                                        };
                                                        let param =
                                                            Parameter::new(param_name, param_type);
                                                        function_sig.parameters.push(param);
                                                    }
                                                } else {
                                                    // Simple parameter name
                                                    let param = Parameter::new(
                                                        param_text.to_string(),
                                                        String::new(),
                                                    );
                                                    function_sig.parameters.push(param);
                                                }
                                            }
                                        }
                                    }
                                }
                                if !param_cursor.goto_next_sibling() {
                                    break;
                                }
                            }
                        }
                    }
                    "return_type" => {
                        function_sig.return_type = Some(text.to_string());
                    }
                    _ => {}
                }
            }

            // Set async flag
            function_sig.is_async = is_async;

            // Analyze method type and visibility
            if let Some(node) = function_node {
                let (is_method, is_static_method, is_class_method) =
                    self.analyze_method_type(&node, &function_sig, source);

                if is_method {
                    function_sig.is_static = is_static_method;
                    // In Python, methods are "public" unless they start with underscore
                    function_sig.is_public = !function_sig.name.starts_with('_');
                    // Record the enclosing class as the method's owner.
                    function_sig.owner = self.enclosing_class_name(&node, source);
                } else {
                    // Module-level function
                    function_sig.is_public = !function_sig.name.starts_with('_');
                }

                // Note: Python doesn't have const or extern functions like Rust
                function_sig.is_const = false;
                function_sig.is_extern = false;
            }

            if !function_sig.name.is_empty() {
                functions.push(function_sig);
            }
        }

        Ok(functions)
    }

    fn extract_structs(
        &self,
        tree: &Tree,
        source: &str,
        file_path: &str,
    ) -> Result<Vec<StructSignature>> {
        // In Python, we extract class definitions instead of structs
        let query_str = r#"
            (class_definition
              name: (identifier) @name
              superclasses: (argument_list)? @inheritance
              body: (block) @body
            ) @class
        "#;

        let query =
            Query::new(&self.language, query_str).map_err(|e| AnalysisError::QueryError {
                message: format!("{:?}", e),
            })?;

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());

        let mut classes = Vec::new();

        while let Some(query_match) = matches.next() {
            let mut class_sig = StructSignature::new(String::new(), file_path.to_string());
            // Every Python `class_definition` is a class.
            class_sig.kind = TypeKind::Class;

            for capture in query_match.captures {
                let capture_name = query.capture_names()[capture.index as usize];
                let text = self.safe_utf8_text(&capture.node, source);

                match capture_name {
                    "name" => class_sig.name = text.to_string(),
                    "inheritance" => {
                        // The `superclasses` field is an `argument_list` of the base
                        // classes, e.g. `(Base)` or `(A, B)`. Collect each base as a
                        // supertype. Named children skip the parentheses and commas;
                        // keyword arguments like `metaclass=...` are ignored.
                        let mut child_cursor = capture.node.walk();
                        for child in capture.node.named_children(&mut child_cursor) {
                            if child.kind() == "keyword_argument" {
                                continue;
                            }
                            let base = self.safe_utf8_text(&child, source);
                            if !base.is_empty() {
                                class_sig.supertypes.push(base);
                            }
                        }
                    }
                    "body" => {
                        // Extract class attributes/methods as "fields"
                        let mut child_cursor = capture.node.walk();
                        if child_cursor.goto_first_child() {
                            loop {
                                let child = child_cursor.node();
                                // Look for assignment statements (class attributes)
                                if child.kind() == "expression_statement" {
                                    let child_text = self.safe_utf8_text(&child, source);
                                    if !child_text.is_empty() {
                                        if child_text.contains('=')
                                            && !child_text.trim().starts_with("def ")
                                        {
                                            // This looks like a class attribute
                                            let parts: Vec<&str> = child_text.split('=').collect();
                                            if let Some(attr_name) = parts.first() {
                                                let attr_name = attr_name.trim().to_string();
                                                let is_public = !attr_name.starts_with('_');
                                                let field =
                                                    StructField::new(attr_name, "Any".to_string())
                                                        .with_visibility(is_public);
                                                class_sig.fields.push(field);
                                            }
                                        }
                                    }
                                }
                                if !child_cursor.goto_next_sibling() {
                                    break;
                                }
                            }
                        }
                    }
                    "class" => {
                        let start_point = capture.node.start_position();
                        let end_point = capture.node.end_position();
                        class_sig.start_line = start_point.row as u32 + 1;
                        class_sig.end_line = end_point.row as u32 + 1;
                    }
                    _ => {}
                }
            }

            // Python classes are "public" unless they start with underscore
            class_sig.is_public = !class_sig.name.starts_with('_');

            if !class_sig.name.is_empty() {
                classes.push(class_sig);
            }
        }

        Ok(classes)
    }

    fn extract_imports(
        &self,
        tree: &Tree,
        source: &str,
        file_path: &str,
    ) -> Result<Vec<ImportStatement>> {
        let query_str = r#"
            (import_statement
              name: (dotted_name) @import_path
            ) @import
            (import_from_statement
              module_name: (dotted_name)? @module
              name: (_) @import_items
            ) @from_import
            (future_import_statement
              name: (_) @future_import
            ) @future
        "#;

        let query =
            Query::new(&self.language, query_str).map_err(|e| AnalysisError::QueryError {
                message: format!("{:?}", e),
            })?;

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());

        let mut imports = Vec::new();

        while let Some(query_match) = matches.next() {
            let mut import_stmt = ImportStatement::new(String::new(), file_path.to_string());

            for capture in query_match.captures {
                let capture_name = query.capture_names()[capture.index as usize];
                let text = self.safe_utf8_text(&capture.node, source);

                match capture_name {
                    "import_path" => {
                        import_stmt.module_path = text.to_string();
                        // Python imports are external unless they start with . (relative)
                        import_stmt.is_external = !text.starts_with('.');
                    }
                    "module" => {
                        import_stmt.module_path = text.to_string();
                        import_stmt.is_external = !text.starts_with('.');
                    }
                    "import_items" => {
                        // Check for wildcard imports
                        import_stmt.is_glob = text.contains('*');
                        // For from imports, combine module and items
                        if !import_stmt.module_path.is_empty() {
                            import_stmt.module_path =
                                format!("{}.{}", import_stmt.module_path, text);
                        } else {
                            import_stmt.module_path = text.to_string();
                        }
                    }
                    "future_import" => {
                        import_stmt.module_path = format!("__future__.{}", text);
                        import_stmt.is_external = true;
                    }
                    "import" | "from_import" | "future" => {
                        let start_point = capture.node.start_position();
                        import_stmt.line_number = start_point.row as u32 + 1;
                    }
                    _ => {}
                }
            }

            if !import_stmt.module_path.is_empty() {
                imports.push(import_stmt);
            }
        }

        Ok(imports)
    }

    fn extract_exports(
        &self,
        tree: &Tree,
        source: &str,
        file_path: &str,
    ) -> Result<Vec<ExportStatement>> {
        // Python doesn't have explicit exports like JavaScript/TypeScript
        // We consider public (non-underscore prefixed) module-level items as exports
        let query_str = r#"
            (function_definition name: (identifier) @func_name) @func
            (class_definition name: (identifier) @class_name) @class
            (assignment 
              left: (identifier) @var_name
            ) @variable
        "#;

        let query =
            Query::new(&self.language, query_str).map_err(|e| AnalysisError::QueryError {
                message: format!("{:?}", e),
            })?;

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());

        let mut exports = Vec::new();

        while let Some(query_match) = matches.next() {
            let mut export_stmt = ExportStatement::new(String::new(), file_path.to_string());

            for capture in query_match.captures {
                let capture_name = query.capture_names()[capture.index as usize];
                let text = self.safe_utf8_text(&capture.node, source);

                match capture_name {
                    "func_name" | "class_name" | "var_name" => {
                        // Only consider public items (not starting with _) as exports
                        if !text.starts_with('_') {
                            export_stmt.exported_item = text.to_string();
                            export_stmt.is_public = true;
                        }
                    }
                    "func" | "class" | "variable" => {
                        let start_point = capture.node.start_position();
                        export_stmt.line_number = start_point.row as u32 + 1;
                    }
                    _ => {}
                }
            }

            if !export_stmt.exported_item.is_empty() && export_stmt.is_public {
                exports.push(export_stmt);
            }
        }

        Ok(exports)
    }

    fn extract_function_calls(
        &self,
        tree: &Tree,
        source: &str,
        file_path: &str,
    ) -> Result<Vec<FunctionCall>> {
        let query_str = r#"
            (call
              function: (identifier) @function_name
            ) @call
            (call
              function: (attribute
                object: (_) @receiver
                attribute: (identifier) @method_name
              )
            ) @method_call
        "#;

        let query =
            Query::new(&self.language, query_str).map_err(|e| AnalysisError::QueryError {
                message: format!("{:?}", e),
            })?;

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());

        let mut function_calls = Vec::new();

        while let Some(query_match) = matches.next() {
            let mut function_call = FunctionCall::new(String::new(), file_path.to_string(), 0);

            for capture in query_match.captures {
                let capture_name = query.capture_names()[capture.index as usize];
                let text = self.safe_utf8_text(&capture.node, source);

                match capture_name {
                    "function_name" => {
                        function_call.function_name = text.to_string();
                    }
                    "method_name" => {
                        function_call.function_name = text.to_string();
                    }
                    "receiver" => {
                        function_call = function_call.with_method_call(text.to_string());
                    }
                    "call" | "method_call" => {
                        let start_point = capture.node.start_position();
                        function_call.line_number = start_point.row as u32 + 1;
                        function_call.column = start_point.column as u32;
                    }
                    _ => {}
                }
            }

            if !function_call.function_name.is_empty() {
                function_calls.push(function_call);
            }
        }

        Ok(function_calls)
    }

    fn extract_with_fallback(&self, content: &str, file_path: &str) -> PartialAnalysis {
        let mut analysis =
            PartialAnalysis::new(file_path.to_string(), "python".to_string()).with_fallback();

        // Simple regex-based fallback parsing for Python

        // Try to extract function definitions with regex
        if let Ok(fn_regex) = Regex::new(r"(?m)^(\s*)(async\s+)?def\s+(\w+)\s*\(") {
            for caps in fn_regex.captures_iter(content) {
                if let Some(name_match) = caps.get(3) {
                    let mut func = FunctionSignature::new(
                        name_match.as_str().to_string(),
                        file_path.to_string(),
                    );
                    func.is_async = caps.get(2).is_some();
                    func.is_public = !name_match.as_str().starts_with('_');
                    analysis.functions.push(func);
                }
            }
        } else {
            analysis.add_error("Failed to create function regex".to_string());
        }

        // Try to extract class definitions with regex
        if let Ok(class_regex) = Regex::new(r"(?m)^(\s*)class\s+(\w+)(\s*\([^)]*\))?:") {
            for caps in class_regex.captures_iter(content) {
                if let Some(name_match) = caps.get(2) {
                    let mut class_sig = StructSignature::new(
                        name_match.as_str().to_string(),
                        file_path.to_string(),
                    );
                    class_sig.is_public = !name_match.as_str().starts_with('_');
                    analysis.structs.push(class_sig);
                }
            }
        } else {
            analysis.add_error("Failed to create class regex".to_string());
        }

        // Try to extract import statements
        if let Ok(import_regex) = Regex::new(r"(?m)^(from\s+[\w.]+\s+)?import\s+([\w.*,\s]+)") {
            for caps in import_regex.captures_iter(content) {
                if let Some(import_match) = caps.get(0) {
                    let import_stmt = ImportStatement::new(
                        import_match.as_str().to_string(),
                        file_path.to_string(),
                    );
                    analysis.imports.push(import_stmt);
                }
            }
        } else {
            analysis.add_error("Failed to create import regex".to_string());
        }

        analysis
    }
}

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

    #[tokio::test]
    async fn test_python_analyzer_basic() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        assert_eq!(analyzer.language(), "python");
        assert_eq!(analyzer.file_extensions(), &["py", "pyx", "pyi"]);
        assert!(analyzer.supports_async());
    }

    #[tokio::test]
    async fn test_debug_tree_sitter() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
def hello():
    print("Hello")
        "#;

        // Test direct tree-sitter parsing
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&analyzer.language).unwrap();
        let tree = parser.parse(code, None).unwrap();

        println!("Root node kind: {}", tree.root_node().kind());
        println!("Root node S-expression: {}", tree.root_node().to_sexp());

        // Test complex query like in extract_functions
        let complex_query = r#"
            (function_definition
              name: (identifier) @name
              parameters: (parameters) @params
            ) @function
        "#;
        println!("Testing complex query:");
        if let Ok(query) = tree_sitter::Query::new(&analyzer.language, complex_query) {
            let mut cursor = tree_sitter::QueryCursor::new();
            let matches = cursor.matches(&query, tree.root_node(), code.as_bytes());

            println!("Number of matches: {}", matches.count());

            // Re-run to actually process them
            let mut cursor = tree_sitter::QueryCursor::new();
            let mut matches = cursor.matches(&query, tree.root_node(), code.as_bytes());

            while let Some(query_match) = matches.next() {
                for capture in query_match.captures {
                    let capture_name = query.capture_names()[capture.index as usize];
                    let text = analyzer.safe_utf8_text(&capture.node, code);
                    println!("Complex Capture: {} = {}", capture_name, text);
                }
            }
        } else {
            println!("Complex query failed to parse");
        }

        // Try simpler query
        let simple_query = r#"(function_definition name: (identifier) @name) @function"#;
        println!("Testing simple query:");
        if let Ok(query) = tree_sitter::Query::new(&analyzer.language, simple_query) {
            let mut cursor = tree_sitter::QueryCursor::new();
            let mut matches = cursor.matches(&query, tree.root_node(), code.as_bytes());

            while let Some(query_match) = matches.next() {
                for capture in query_match.captures {
                    let capture_name = query.capture_names()[capture.index as usize];
                    let text = analyzer.safe_utf8_text(&capture.node, code);
                    println!("Simple Capture: {} = {}", capture_name, text);
                }
            }
        } else {
            println!("Simple query failed to parse");
        }
    }

    #[tokio::test]
    async fn test_extract_simple_function() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
def hello_world():
    print("Hello, world!")
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let functions = &analysis.tree_node.functions;

        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "hello_world");
        assert!(functions[0].is_public); // Doesn't start with _
        assert!(!functions[0].is_async);
        assert!(!functions[0].is_static);
    }

    #[tokio::test]
    async fn test_extract_async_function() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
async def fetch_data(url: str) -> str:
    """Fetch data from URL"""
    return "data"
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let functions = &analysis.tree_node.functions;

        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "fetch_data");
        assert!(functions[0].is_public);
        assert!(functions[0].is_async);
        assert_eq!(functions[0].return_type, Some("str".to_string()));
        assert_eq!(functions[0].parameters.len(), 1);
        assert_eq!(functions[0].parameters[0].name, "url");
        assert_eq!(functions[0].parameters[0].param_type, "str");
    }

    #[tokio::test]
    async fn test_extract_private_function() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
def _private_function():
    pass

def __dunder_method__(self):
    pass
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let functions = &analysis.tree_node.functions;

        assert_eq!(functions.len(), 2);

        let private_fn = functions
            .iter()
            .find(|f| f.name == "_private_function")
            .unwrap();
        assert!(!private_fn.is_public);

        let dunder_fn = functions
            .iter()
            .find(|f| f.name == "__dunder_method__")
            .unwrap();
        assert!(!dunder_fn.is_public);
    }

    #[tokio::test]
    async fn test_extract_class() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
class User:
    def __init__(self, name: str):
        self.name = name
        self.email = ""
    
    def get_name(self) -> str:
        return self.name
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let classes = &analysis.tree_node.structs;
        let functions = &analysis.tree_node.functions;

        // Should find the class
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].name, "User");
        assert!(classes[0].is_public);

        // Should find the methods
        assert_eq!(functions.len(), 2);
        let init_method = functions.iter().find(|f| f.name == "__init__").unwrap();
        assert!(!init_method.is_public); // Dunder methods are not public

        let get_name_method = functions.iter().find(|f| f.name == "get_name").unwrap();
        assert!(get_name_method.is_public);
    }

    #[tokio::test]
    async fn test_staticmethod_detected() {
        // Regression: decorators were previously read from an empty source, so
        // `@staticmethod` was never seen and a static method with a non-self
        // first parameter was misclassified. With the real source passed
        // through, `make` must be reported as static.
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
class Factory:
    @staticmethod
    def make(x):
        return x

    @classmethod
    def build(cls, y):
        return y

    def instance(self):
        return self
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let functions = &analysis.tree_node.functions;

        let make = functions.iter().find(|f| f.name == "make").unwrap();
        assert!(make.is_static, "@staticmethod should be reported as static");

        let instance = functions.iter().find(|f| f.name == "instance").unwrap();
        assert!(!instance.is_static, "instance method should not be static");
    }

    #[tokio::test]
    async fn test_extract_imports() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
import os
import sys
from typing import List, Dict
from .relative_module import something
from ..parent_module import other
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let imports = &analysis.tree_node.imports;

        assert!(imports.len() >= 3);

        // Check for standard library imports
        let os_import = imports.iter().find(|i| i.module_path.contains("os"));
        assert!(os_import.is_some());

        // Check for relative imports
        let relative_import = imports
            .iter()
            .find(|i| i.module_path.contains("relative_module"));
        if let Some(rel_import) = relative_import {
            assert!(!rel_import.is_external); // Relative imports are not external
        }
    }

    #[tokio::test]
    async fn test_extract_function_calls() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
def main():
    print("Hello")
    obj = SomeClass()
    result = obj.method_call()
    standalone_function()
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let function_calls = &analysis.tree_node.function_calls;

        assert!(!function_calls.is_empty());

        // Check for method calls
        let method_calls: Vec<&FunctionCall> = function_calls
            .iter()
            .filter(|fc| fc.is_method_call)
            .collect();

        // Should find at least one method call
        assert!(!method_calls.is_empty());
    }

    #[tokio::test]
    async fn test_function_with_parameters() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
def complex_function(a: int, b: str = "default", *args, **kwargs) -> bool:
    return True
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let functions = &analysis.tree_node.functions;

        assert_eq!(functions.len(), 1);
        let func = &functions[0];
        assert_eq!(func.name, "complex_function");
        assert_eq!(func.return_type, Some("bool".to_string()));

        // Should capture multiple parameters
        assert!(!func.parameters.is_empty());
    }

    #[tokio::test]
    async fn test_fallback_parsing() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        // Intentionally malformed Python code to trigger fallback
        let malformed_code = r#"
def valid_function():
    # This should be parsed normally
    pass

class InvalidSyntax
    # Missing colon - this might cause parsing issues
    pass

async def another_function(param: str) -> str:
    return param
        "#;

        let fallback_analysis = analyzer.extract_with_fallback(malformed_code, "test.py");

        assert!(fallback_analysis.fallback_used);
        assert!(!fallback_analysis.functions.is_empty());

        // Should find at least the valid functions via regex
        let function_names: Vec<&String> = fallback_analysis
            .functions
            .iter()
            .map(|f| &f.name)
            .collect();

        assert!(function_names.contains(&&"valid_function".to_string()));
        assert!(function_names.contains(&&"another_function".to_string()));
    }

    #[test]
    fn test_content_hash() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let content1 = "def test(): pass";
        let content2 = "def test(): pass";
        let content3 = "def different(): pass";

        let hash1 = analyzer.calculate_content_hash(content1);
        let hash2 = analyzer.calculate_content_hash(content2);
        let hash3 = analyzer.calculate_content_hash(content3);

        assert_eq!(hash1, hash2);
        assert_ne!(hash1, hash3);
        assert!(!hash1.is_empty());
    }

    #[tokio::test]
    async fn test_comprehensive_python_analysis() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let comprehensive_code = r#"
import asyncio
from typing import List, Optional

class DataProcessor:
    def __init__(self, name: str):
        self.name = name
        self._private_attr = None
    
    async def process_data(self, data: List[str]) -> Optional[str]:
        """Process a list of data strings."""
        result = await self._internal_process(data)
        return result
    
    def _internal_process(self, data: List[str]) -> str:
        return " ".join(data)

async def main():
    processor = DataProcessor("test")
    result = await processor.process_data(["hello", "world"])
    print(result)

if __name__ == "__main__":
    asyncio.run(main())
        "#;

        let analysis = analyzer
            .analyze_file(comprehensive_code, "processor.py")
            .await
            .expect("Analysis failed");
        let tree_node = &analysis.tree_node;

        // Verify all components were extracted
        assert!(!tree_node.functions.is_empty());
        assert!(!tree_node.structs.is_empty());
        assert!(!tree_node.imports.is_empty());

        // Verify timing information
        assert!(analysis.analysis_duration_ms > 0);

        assert!(analysis.success);

        // Verify content hash was calculated
        assert!(!tree_node.content_hash.is_empty());

        // Check specific extractions
        let processor_class = tree_node
            .structs
            .iter()
            .find(|s| s.name == "DataProcessor")
            .unwrap();
        assert!(processor_class.is_public);

        let process_data_method = tree_node
            .functions
            .iter()
            .find(|f| f.name == "process_data")
            .unwrap();
        assert!(process_data_method.is_async);
        assert!(process_data_method.is_public);

        let main_function = tree_node
            .functions
            .iter()
            .find(|f| f.name == "main")
            .unwrap();
        assert!(main_function.is_async);
        assert!(main_function.is_public);

        let internal_method = tree_node
            .functions
            .iter()
            .find(|f| f.name == "_internal_process")
            .unwrap();
        assert!(!internal_method.is_public); // Private method
    }

    // P1-3: Python class owner + superclass supertypes + kind=Class

    #[tokio::test]
    async fn test_method_owner_is_enclosing_class() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
class Foo(Base):
    def m(self):
        pass
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let functions = &analysis.tree_node.functions;

        let m = functions.iter().find(|f| f.name == "m").unwrap();
        assert_eq!(m.owner, Some("Foo".to_string()));
    }

    #[tokio::test]
    async fn test_class_kind_and_single_supertype() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
class Foo(Base):
    pass
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let classes = &analysis.tree_node.structs;

        let foo = classes.iter().find(|c| c.name == "Foo").unwrap();
        assert_eq!(foo.kind, TypeKind::Class);
        assert_eq!(foo.supertypes, vec!["Base".to_string()]);
    }

    #[tokio::test]
    async fn test_multiple_supertypes() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
class Multi(A, B):
    pass
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let classes = &analysis.tree_node.structs;

        let multi = classes.iter().find(|c| c.name == "Multi").unwrap();
        assert_eq!(multi.kind, TypeKind::Class);
        assert_eq!(multi.supertypes, vec!["A".to_string(), "B".to_string()]);
    }

    #[tokio::test]
    async fn test_module_level_function_has_no_owner() {
        let analyzer = PythonAnalyzer::new().expect("Failed to create PythonAnalyzer");

        let code = r#"
def top():
    pass
        "#;

        let analysis = analyzer
            .analyze_file(code, "test.py")
            .await
            .expect("Analysis failed");
        let functions = &analysis.tree_node.functions;

        let top = functions.iter().find(|f| f.name == "top").unwrap();
        assert_eq!(top.owner, None);
    }
}