nusy-codegraph 0.15.2

Code-as-graph: Arrow-native code object representation with tree-sitter parsing
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
//! tree-sitter Rust parser — extract CodeNodes from Rust source files.
//!
//! Parses Rust source into a tree of CodeNodes representing files, functions,
//! structs, enums, traits, impl blocks, methods, macros, use declarations,
//! const/static items, type aliases, and modules. Builds containment hierarchy
//! via parent_id and extracts import information from use declarations.

use crate::parser::{ImportInfo, ParseError, ParseResult, sha256_hex};
use crate::schema::{CodeNode, CodeNodeKind};
use std::path::Path;

/// Parse a Rust source file into CodeNodes.
///
/// Returns nodes for the file and all top-level and nested items:
/// functions, structs, enums, traits, impl blocks (with methods),
/// macros, use declarations, const/static items, type aliases, and modules.
pub fn parse_rust_file(path: &Path, source: &str) -> Result<ParseResult, ParseError> {
    let mut parser = tree_sitter::Parser::new();
    let language = tree_sitter_rust::LANGUAGE;
    parser.set_language(&language.into())?;

    let tree = parser
        .parse(source, None)
        .ok_or_else(|| ParseError::ParseFailed {
            path: path.display().to_string(),
        })?;

    let path_str = path.display().to_string();
    let file_id = format!("file:{path_str}");

    // File node
    let file_node = CodeNode {
        id: file_id.clone(),
        kind: CodeNodeKind::File,
        parent_id: None,
        name: path
            .file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_default(),
        signature: None,
        docstring: None,
        body_hash: Some(sha256_hex(source.as_bytes())),
        body: None, // File-level body omitted (too large)
        loc: Some(source.lines().count() as i32),
        file_path: Some(path_str.clone()),
        ..Default::default()
    };

    let mut nodes = vec![file_node];
    let mut imports = Vec::new();

    let root = tree.root_node();
    let src = source.as_bytes();

    // Walk top-level children
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        extract_item(&child, src, &path_str, &file_id, &mut nodes, &mut imports);
    }

    Ok(ParseResult { nodes, imports })
}

// ---- Extraction dispatcher --------------------------------------------------

fn extract_item(
    node: &tree_sitter::Node,
    src: &[u8],
    path: &str,
    parent_id: &str,
    nodes: &mut Vec<CodeNode>,
    imports: &mut Vec<ImportInfo>,
) {
    match node.kind() {
        "function_item" => {
            let is_test = has_test_attribute(node, src);
            let extracted = extract_function(node, src, path, parent_id, is_test);
            nodes.push(extracted);
        }
        "struct_item" => {
            nodes.push(extract_struct(node, src, path, parent_id));
        }
        "enum_item" => {
            nodes.push(extract_enum(node, src, path, parent_id));
        }
        "impl_item" => {
            let impl_nodes = extract_impl(node, src, path, parent_id);
            nodes.extend(impl_nodes);
        }
        "trait_item" => {
            let trait_nodes = extract_trait(node, src, path, parent_id);
            nodes.extend(trait_nodes);
        }
        "mod_item" => {
            let mod_nodes = extract_mod(node, src, path, parent_id, imports);
            nodes.extend(mod_nodes);
        }
        "macro_definition" => {
            nodes.push(extract_macro(node, src, path, parent_id));
        }
        "use_declaration" => {
            let (use_node, import) = extract_use(node, src, path, parent_id);
            nodes.push(use_node);
            if let Some(imp) = import {
                imports.push(imp);
            }
        }
        "const_item" => {
            nodes.push(extract_const(node, src, path, parent_id));
        }
        "static_item" => {
            nodes.push(extract_static(node, src, path, parent_id));
        }
        "type_item" => {
            nodes.push(extract_type_alias(node, src, path, parent_id));
        }
        // Handle attributed items (e.g., #[test] fn ...)
        "attribute_item" => {
            // Attributes are handled by peeking at siblings in the parent context;
            // nothing to extract standalone.
        }
        _ => {}
    }
}

// ---- Individual extractors --------------------------------------------------

fn extract_function(
    node: &tree_sitter::Node,
    src: &[u8],
    path: &str,
    parent_id: &str,
    is_test: bool,
) -> CodeNode {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let kind = if is_test {
        CodeNodeKind::RustTest
    } else {
        CodeNodeKind::RustFn
    };

    let id = if is_test {
        format!("rust_test:{path}::{name}")
    } else {
        format!("rust_fn:{path}::{name}")
    };

    let signature = extract_rust_fn_signature(node, src);
    let body = node_text(node, src);
    let body_hash = sha256_hex(body.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;

    CodeNode {
        id,
        kind,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(signature),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    }
}

fn extract_struct(node: &tree_sitter::Node, src: &[u8], path: &str, parent_id: &str) -> CodeNode {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_struct:{path}::{name}");
    let body = node_text(node, src);
    let body_hash = sha256_hex(body.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;

    // Signature: first line up to opening brace or semicolon
    let signature = extract_first_line_signature(&body);

    CodeNode {
        id,
        kind: CodeNodeKind::RustStruct,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(signature),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    }
}

fn extract_enum(node: &tree_sitter::Node, src: &[u8], path: &str, parent_id: &str) -> CodeNode {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_enum:{path}::{name}");
    let body = node_text(node, src);
    let body_hash = sha256_hex(body.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;
    let signature = extract_first_line_signature(&body);

    CodeNode {
        id,
        kind: CodeNodeKind::RustEnum,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(signature),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    }
}

fn extract_impl(
    node: &tree_sitter::Node,
    src: &[u8],
    path: &str,
    parent_id: &str,
) -> Vec<CodeNode> {
    let body_text = node_text(node, src);

    // Build impl name: "impl Foo" or "impl Trait for Foo"
    let impl_name = extract_impl_name(node, src);
    let id = format!("rust_impl:{path}::{impl_name}");

    let body_hash = sha256_hex(body_text.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;
    let signature = extract_first_line_signature(&body_text);

    let impl_node = CodeNode {
        id: id.clone(),
        kind: CodeNodeKind::RustImpl,
        parent_id: Some(parent_id.to_string()),
        name: impl_name,
        signature: Some(signature),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body_text),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    };

    let mut nodes = vec![impl_node];

    // Extract methods from the impl body
    if let Some(body) = node.child_by_field_name("body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            if child.kind() == "function_item" {
                let is_test = has_test_attribute(&child, src);
                let method_name = node_child_text(&child, "name", src).unwrap_or_default();
                let kind = if is_test {
                    CodeNodeKind::RustTest
                } else {
                    CodeNodeKind::RustMethod
                };
                let method_id = if is_test {
                    format!("rust_test:{path}::{method_name}")
                } else {
                    format!("rust_method:{path}::{method_name}")
                };

                let method_sig = extract_rust_fn_signature(&child, src);
                let method_body = node_text(&child, src);
                let method_hash = sha256_hex(method_body.as_bytes());
                let method_loc = (child.end_position().row - child.start_position().row + 1) as i32;

                nodes.push(CodeNode {
                    id: method_id,
                    kind,
                    parent_id: Some(id.clone()),
                    name: method_name,
                    signature: Some(method_sig),
                    docstring: extract_rust_doc_comment(&child, src),
                    body_hash: Some(method_hash),
                    body: Some(method_body),
                    loc: Some(method_loc),
                    start_line: Some(child.start_position().row as u32 + 1),
                    end_line: Some(child.end_position().row as u32 + 1),
                    start_col: Some(child.start_position().column as u32),
                    end_col: Some(child.end_position().column as u32),
                    file_path: Some(path.to_string()),
                    byte_offset: Some(child.start_byte() as u64),
                    ..Default::default()
                });
            }
        }
    }

    nodes
}

fn extract_trait(
    node: &tree_sitter::Node,
    src: &[u8],
    path: &str,
    parent_id: &str,
) -> Vec<CodeNode> {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_trait:{path}::{name}");
    let body_text = node_text(node, src);
    let body_hash = sha256_hex(body_text.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;
    let signature = extract_first_line_signature(&body_text);

    let trait_node = CodeNode {
        id: id.clone(),
        kind: CodeNodeKind::RustTrait,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(signature),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body_text),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    };

    let mut nodes = vec![trait_node];

    // Extract methods from the trait body
    if let Some(body) = node.child_by_field_name("body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            if child.kind() == "function_item" || child.kind() == "function_signature_item" {
                let method_name = node_child_text(&child, "name", src).unwrap_or_default();
                let method_id = format!("rust_method:{path}::{method_name}");
                let method_sig = extract_rust_fn_signature(&child, src);
                let method_body = node_text(&child, src);
                let method_hash = sha256_hex(method_body.as_bytes());
                let method_loc = (child.end_position().row - child.start_position().row + 1) as i32;

                nodes.push(CodeNode {
                    id: method_id,
                    kind: CodeNodeKind::RustMethod,
                    parent_id: Some(id.clone()),
                    name: method_name,
                    signature: Some(method_sig),
                    docstring: extract_rust_doc_comment(&child, src),
                    body_hash: Some(method_hash),
                    body: Some(method_body),
                    loc: Some(method_loc),
                    start_line: Some(child.start_position().row as u32 + 1),
                    end_line: Some(child.end_position().row as u32 + 1),
                    start_col: Some(child.start_position().column as u32),
                    end_col: Some(child.end_position().column as u32),
                    file_path: Some(path.to_string()),
                    byte_offset: Some(child.start_byte() as u64),
                    ..Default::default()
                });
            }
        }
    }

    nodes
}

fn extract_mod(
    node: &tree_sitter::Node,
    src: &[u8],
    path: &str,
    parent_id: &str,
    imports: &mut Vec<ImportInfo>,
) -> Vec<CodeNode> {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_mod:{path}::{name}");
    let body_text = node_text(node, src);
    let body_hash = sha256_hex(body_text.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;

    let mod_node = CodeNode {
        id: id.clone(),
        kind: CodeNodeKind::RustMod,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(extract_first_line_signature(&body_text)),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body_text),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    };

    let mut nodes = vec![mod_node];

    // If the mod has a body (inline module), extract its children
    if let Some(body) = node.child_by_field_name("body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            extract_item(&child, src, path, &id, &mut nodes, imports);
        }
    }

    nodes
}

fn extract_macro(node: &tree_sitter::Node, src: &[u8], path: &str, parent_id: &str) -> CodeNode {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_macro:{path}::{name}");
    let body = node_text(node, src);
    let body_hash = sha256_hex(body.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;

    CodeNode {
        id,
        kind: CodeNodeKind::RustMacro,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(format!(
            "macro_rules! {}",
            node_child_text(node, "name", src).unwrap_or_default()
        )),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    }
}

fn extract_use(
    node: &tree_sitter::Node,
    src: &[u8],
    path: &str,
    parent_id: &str,
) -> (CodeNode, Option<ImportInfo>) {
    let full_text = node_text(node, src);
    let use_path = full_text
        .trim()
        .strip_prefix("use ")
        .unwrap_or(&full_text)
        .trim_end_matches(';')
        .trim()
        .to_string();

    // Parse the use path for ImportInfo
    let (module, names) = parse_use_path(&use_path);

    let id = format!("rust_use:{path}::{use_path}");
    let body_hash = sha256_hex(full_text.as_bytes());

    let code_node = CodeNode {
        id,
        kind: CodeNodeKind::RustUse,
        parent_id: Some(parent_id.to_string()),
        name: use_path.clone(),
        signature: Some(full_text.trim().to_string()),
        docstring: None,
        body_hash: Some(body_hash),
        body: Some(full_text),
        loc: Some(1),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    };

    let import = ImportInfo {
        file_node_id: format!("file:{path}"),
        module,
        names,
        is_relative: false,
    };

    (code_node, Some(import))
}

fn extract_const(node: &tree_sitter::Node, src: &[u8], path: &str, parent_id: &str) -> CodeNode {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_const:{path}::{name}");
    let body = node_text(node, src);
    let body_hash = sha256_hex(body.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;

    CodeNode {
        id,
        kind: CodeNodeKind::RustConst,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(extract_first_line_signature(&body)),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    }
}

fn extract_static(node: &tree_sitter::Node, src: &[u8], path: &str, parent_id: &str) -> CodeNode {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_static:{path}::{name}");
    let body = node_text(node, src);
    let body_hash = sha256_hex(body.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;

    CodeNode {
        id,
        kind: CodeNodeKind::RustStatic,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(extract_first_line_signature(&body)),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    }
}

fn extract_type_alias(
    node: &tree_sitter::Node,
    src: &[u8],
    path: &str,
    parent_id: &str,
) -> CodeNode {
    let name = node_child_text(node, "name", src).unwrap_or_default();
    let id = format!("rust_type_alias:{path}::{name}");
    let body = node_text(node, src);
    let body_hash = sha256_hex(body.as_bytes());
    let loc = (node.end_position().row - node.start_position().row + 1) as i32;

    CodeNode {
        id,
        kind: CodeNodeKind::RustTypeAlias,
        parent_id: Some(parent_id.to_string()),
        name,
        signature: Some(body.trim().to_string()),
        docstring: extract_rust_doc_comment(node, src),
        body_hash: Some(body_hash),
        body: Some(body),
        loc: Some(loc),
        start_line: Some(node.start_position().row as u32 + 1),
        end_line: Some(node.end_position().row as u32 + 1),
        start_col: Some(node.start_position().column as u32),
        end_col: Some(node.end_position().column as u32),
        file_path: Some(path.to_string()),
        byte_offset: Some(node.start_byte() as u64),
        ..Default::default()
    }
}

// ---- AST utility helpers ----------------------------------------------------

fn node_text(node: &tree_sitter::Node, src: &[u8]) -> String {
    node.utf8_text(src).unwrap_or("").to_string()
}

fn node_child_text(node: &tree_sitter::Node, field: &str, src: &[u8]) -> Option<String> {
    node.child_by_field_name(field).map(|n| node_text(&n, src))
}

/// Extract function signature: `fn name(params) -> ReturnType`
fn extract_rust_fn_signature(node: &tree_sitter::Node, src: &[u8]) -> String {
    let full_text = node_text(node, src);
    // Signature is everything up to the opening brace (or semicolon for trait methods)
    if let Some(brace_pos) = full_text.find('{') {
        full_text[..brace_pos].trim().to_string()
    } else if let Some(semi_pos) = full_text.find(';') {
        full_text[..semi_pos].trim().to_string()
    } else {
        full_text.lines().next().unwrap_or("").trim().to_string()
    }
}

/// Extract the first line of a body as a signature (for structs, enums, etc.)
fn extract_first_line_signature(body: &str) -> String {
    let first_line = body.lines().next().unwrap_or("").trim();
    // Strip trailing brace if present
    let sig = first_line.trim_end_matches('{').trim();
    sig.to_string()
}

/// Extract impl name: "Foo" for `impl Foo { ... }` or "Trait for Foo" for `impl Trait for Foo { ... }`
fn extract_impl_name(node: &tree_sitter::Node, src: &[u8]) -> String {
    let full_text = node_text(node, src);
    // Extract text between "impl" and "{"
    let after_impl = full_text
        .trim()
        .strip_prefix("impl")
        .unwrap_or(&full_text)
        .trim();

    if let Some(brace_pos) = after_impl.find('{') {
        after_impl[..brace_pos].trim().to_string()
    } else {
        // External impl (no body)
        after_impl.trim().to_string()
    }
}

/// Check if a function has a `#[test]` attribute by looking at preceding siblings.
fn has_test_attribute(node: &tree_sitter::Node, src: &[u8]) -> bool {
    // Check preceding siblings for attribute_item containing "test"
    let mut sibling = node.prev_sibling();
    while let Some(sib) = sibling {
        if sib.kind() == "attribute_item" {
            let text = node_text(&sib, src);
            if text.contains("#[test]") || text.contains("#[tokio::test]") {
                return true;
            }
        } else if sib.kind() != "line_comment" && sib.kind() != "block_comment" {
            // Stop at non-attribute, non-comment nodes
            break;
        }
        sibling = sib.prev_sibling();
    }
    false
}

/// Extract Rust doc comments (/// or //!) preceding a node.
fn extract_rust_doc_comment(node: &tree_sitter::Node, src: &[u8]) -> Option<String> {
    let mut doc_lines = Vec::new();
    let mut sibling = node.prev_sibling();

    while let Some(sib) = sibling {
        if sib.kind() == "line_comment" {
            let text = node_text(&sib, src);
            if let Some(doc) = text.strip_prefix("///") {
                doc_lines.push(doc.trim_start_matches(' ').to_string());
            } else if let Some(doc) = text.strip_prefix("//!") {
                doc_lines.push(doc.trim_start_matches(' ').to_string());
            } else {
                break;
            }
        } else if sib.kind() == "attribute_item" {
            // Skip attributes (they can appear between doc comments and the item)
            sibling = sib.prev_sibling();
            continue;
        } else {
            break;
        }
        sibling = sib.prev_sibling();
    }

    if doc_lines.is_empty() {
        return None;
    }

    doc_lines.reverse();
    Some(doc_lines.join("\n").trim().to_string())
}

/// Parse a Rust use path into module and imported names.
///
/// Examples:
/// - `std::collections::HashMap` -> ("std::collections", ["HashMap"])
/// - `std::collections::{HashMap, BTreeMap}` -> ("std::collections", ["HashMap", "BTreeMap"])
/// - `crate::parser::ParseResult` -> ("crate::parser", ["ParseResult"])
/// - `std::io` -> ("std", ["io"])
fn parse_use_path(path: &str) -> (String, Vec<String>) {
    let path = path.trim();

    // Handle glob imports: `use std::io::*;`
    if path.ends_with("::*") {
        let module = path.strip_suffix("::*").unwrap_or(path).to_string();
        return (module, vec!["*".to_string()]);
    }

    // Handle group imports: `use std::collections::{HashMap, BTreeMap};`
    if let Some(brace_start) = path.find("::{") {
        let module = path[..brace_start].to_string();
        let group = &path[brace_start + 3..];
        let group = group.trim_end_matches('}');
        let names: Vec<String> = group
            .split(',')
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();
        return (module, names);
    }

    // Simple path: `use std::collections::HashMap;`
    if let Some(last_sep) = path.rfind("::") {
        let module = path[..last_sep].to_string();
        let name = path[last_sep + 2..].to_string();
        (module, vec![name])
    } else {
        // Single segment: `use std;`
        (String::new(), vec![path.to_string()])
    }
}

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

    #[test]
    fn test_parse_simple_function() {
        let source = r#"fn foo() -> i32 {
    42
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        // Should have: file + function
        assert!(
            result.nodes.len() >= 2,
            "Expected >= 2 nodes, got {}",
            result.nodes.len()
        );

        let func = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustFn && n.name == "foo");
        assert!(func.is_some(), "should have RustFn node for foo");
        let func = func.unwrap();
        assert!(func.signature.as_ref().unwrap().contains("fn foo() -> i32"));
        assert!(func.body.as_ref().unwrap().contains("42"));
        assert!(func.body_hash.is_some());
    }

    #[test]
    fn test_parse_struct_with_fields() {
        let source = r#"struct Foo {
    x: i32,
    y: String,
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let st = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustStruct && n.name == "Foo");
        assert!(st.is_some(), "should have RustStruct node for Foo");
        let st = st.unwrap();
        assert!(st.signature.as_ref().unwrap().contains("struct Foo"));
    }

    #[test]
    fn test_parse_enum() {
        let source = r#"enum Color {
    Red,
    Blue,
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let en = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustEnum && n.name == "Color");
        assert!(en.is_some(), "should have RustEnum node for Color");
    }

    #[test]
    fn test_parse_impl_block_with_methods() {
        let source = r#"struct Foo;

impl Foo {
    fn bar(&self) -> i32 {
        42
    }

    fn baz(&mut self, x: i32) {
        self.x = x;
    }
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        // Check impl node
        let imp = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustImpl);
        assert!(imp.is_some(), "should have RustImpl node");
        let imp = imp.unwrap();
        assert!(imp.name.contains("Foo"), "impl name should contain Foo");

        // Check methods
        let methods: Vec<_> = result
            .nodes
            .iter()
            .filter(|n| n.kind == CodeNodeKind::RustMethod)
            .collect();
        assert_eq!(methods.len(), 2, "should have 2 methods");

        let bar = methods.iter().find(|n| n.name == "bar");
        assert!(bar.is_some(), "should have method bar");
        let bar = bar.unwrap();
        assert_eq!(
            bar.parent_id.as_deref(),
            Some(imp.id.as_str()),
            "method parent should be impl"
        );
        assert!(
            bar.signature
                .as_ref()
                .unwrap()
                .contains("fn bar(&self) -> i32")
        );
    }

    #[test]
    fn test_parse_trait() {
        let source = r#"trait MyTrait {
    fn required(&self) -> i32;

    fn provided(&self) -> bool {
        true
    }
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let tr = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustTrait && n.name == "MyTrait");
        assert!(tr.is_some(), "should have RustTrait node for MyTrait");

        // Check trait methods
        let methods: Vec<_> = result
            .nodes
            .iter()
            .filter(|n| n.kind == CodeNodeKind::RustMethod)
            .collect();
        assert!(methods.len() >= 1, "should have at least 1 method in trait");
    }

    #[test]
    fn test_parse_use_declaration() {
        let source = "use std::collections::HashMap;\n";
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let use_node = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustUse);
        assert!(use_node.is_some(), "should have RustUse node");

        assert!(!result.imports.is_empty(), "should have imports");
        let imp = &result.imports[0];
        assert_eq!(imp.module, "std::collections");
        assert_eq!(imp.names, vec!["HashMap"]);
    }

    #[test]
    fn test_parse_test_function() {
        let source = r#"#[test]
fn test_foo() {
    assert_eq!(1 + 1, 2);
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let test_fn = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustTest && n.name == "test_foo");
        assert!(test_fn.is_some(), "should have RustTest node for test_foo");
    }

    #[test]
    fn test_parse_macro() {
        let source = r#"macro_rules! my_macro {
    ($x:expr) => {
        $x + 1
    };
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let mac = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustMacro && n.name == "my_macro");
        assert!(mac.is_some(), "should have RustMacro node for my_macro");
    }

    #[test]
    fn test_parse_const_and_static() {
        let source = r#"const X: i32 = 5;
static Y: i32 = 10;"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let const_node = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustConst && n.name == "X");
        assert!(const_node.is_some(), "should have RustConst node for X");

        let static_node = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustStatic && n.name == "Y");
        assert!(static_node.is_some(), "should have RustStatic node for Y");
    }

    #[test]
    fn test_position_metadata_populated() {
        let source = r#"fn hello() -> &'static str {
    "world"
}"#;
        let path = PathBuf::from("src/main.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let func = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustFn && n.name == "hello")
            .expect("should have function hello");

        assert!(func.start_line.is_some(), "start_line should be set");
        assert!(func.end_line.is_some(), "end_line should be set");
        assert!(func.start_col.is_some(), "start_col should be set");
        assert!(func.end_col.is_some(), "end_col should be set");
        assert_eq!(func.file_path.as_deref(), Some("src/main.rs"));
        assert!(func.byte_offset.is_some(), "byte_offset should be set");

        // start_line should be 1 (1-indexed)
        assert_eq!(func.start_line, Some(1));
        assert!(func.end_line.unwrap() >= 1);
    }

    #[test]
    fn test_containment_hierarchy() {
        let source = r#"mod inner {
    struct Data {
        value: i32,
    }

    impl Data {
        fn get_value(&self) -> i32 {
            self.value
        }
    }
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let file_id = "file:src/lib.rs";

        // Module's parent is file
        let mod_node = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustMod && n.name == "inner")
            .expect("should have mod inner");
        assert_eq!(mod_node.parent_id.as_deref(), Some(file_id));

        // Struct's parent is module
        let struct_node = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustStruct && n.name == "Data")
            .expect("should have struct Data");
        assert_eq!(struct_node.parent_id.as_deref(), Some(mod_node.id.as_str()));

        // Impl's parent is module
        let impl_node = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustImpl)
            .expect("should have impl Data");
        assert_eq!(impl_node.parent_id.as_deref(), Some(mod_node.id.as_str()));

        // Method's parent is impl
        let method = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustMethod && n.name == "get_value")
            .expect("should have method get_value");
        assert_eq!(method.parent_id.as_deref(), Some(impl_node.id.as_str()));
    }

    #[test]
    fn test_parse_use_group() {
        let source = "use std::collections::{HashMap, BTreeMap};\n";
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        assert!(!result.imports.is_empty(), "should have imports");
        let imp = &result.imports[0];
        assert_eq!(imp.module, "std::collections");
        assert!(imp.names.contains(&"HashMap".to_string()));
        assert!(imp.names.contains(&"BTreeMap".to_string()));
    }

    #[test]
    fn test_parse_type_alias() {
        let source = "type Result<T> = std::result::Result<T, MyError>;\n";
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let ta = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustTypeAlias && n.name == "Result");
        assert!(ta.is_some(), "should have RustTypeAlias node for Result");
    }

    #[test]
    fn test_parse_impl_test_method() {
        let source = r#"struct Foo;

impl Foo {
    fn normal(&self) {}
}

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

    #[test]
    fn test_foo() {
        assert!(true);
    }
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let test_fn = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustTest && n.name == "test_foo");
        assert!(test_fn.is_some(), "should have RustTest for test_foo");
    }

    #[test]
    fn test_parse_doc_comments() {
        let source = r#"/// This is a documented function.
/// It does important things.
fn documented() -> bool {
    true
}"#;
        let path = PathBuf::from("src/lib.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed");

        let func = result
            .nodes
            .iter()
            .find(|n| n.kind == CodeNodeKind::RustFn && n.name == "documented")
            .expect("should have documented function");

        assert!(func.docstring.is_some(), "should have docstring");
        let doc = func.docstring.as_ref().unwrap();
        assert!(
            doc.contains("documented function"),
            "docstring should contain 'documented function', got: {doc}"
        );
    }

    #[test]
    fn test_parse_empty_file() {
        let source = "";
        let path = PathBuf::from("src/empty.rs");
        let result = parse_rust_file(&path, source).expect("parse should succeed on empty file");

        // Should have at least the file node
        assert_eq!(
            result.nodes.len(),
            1,
            "empty file should have just the file node"
        );
        assert_eq!(result.nodes[0].kind, CodeNodeKind::File);
    }

    #[test]
    fn test_parse_malformed_rust() {
        let source = "fn { broken syntax ;;;";
        let path = PathBuf::from("src/broken.rs");
        // tree-sitter is lenient — should not panic
        let result = parse_rust_file(&path, source);
        assert!(result.is_ok(), "malformed Rust should not panic");
    }

    #[test]
    fn test_use_path_parsing() {
        let (module, names) = parse_use_path("std::collections::HashMap");
        assert_eq!(module, "std::collections");
        assert_eq!(names, vec!["HashMap"]);

        let (module, names) = parse_use_path("std::collections::{HashMap, BTreeMap}");
        assert_eq!(module, "std::collections");
        assert_eq!(names, vec!["HashMap", "BTreeMap"]);

        let (module, names) = parse_use_path("std::io::*");
        assert_eq!(module, "std::io");
        assert_eq!(names, vec!["*"]);

        let (module, names) = parse_use_path("crate::parser::ParseResult");
        assert_eq!(module, "crate::parser");
        assert_eq!(names, vec!["ParseResult"]);
    }
}