sqry-lang-ruby 9.0.17

Ruby language plugin for sqry
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
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
use std::{
    collections::{HashMap, HashSet},
    path::Path,
};

use sqry_core::graph::unified::build::helper::CalleeKindHint;
use sqry_core::graph::unified::edge::FfiConvention;
use sqry_core::graph::unified::edge::kind::TypeOfContext;
use sqry_core::graph::unified::{GraphBuildHelper, StagingGraph};
use sqry_core::graph::{GraphBuilder, GraphBuilderError, GraphResult, Language, Position, Span};
use tree_sitter::{Node, Point, Tree};

use super::type_extractor::{canonical_type_string, extract_type_names};
use super::yard_parser::{extract_yard_comment, parse_yard_tags};

const DEFAULT_SCOPE_DEPTH: usize = 4;

/// File-level module name for exports.
/// In Ruby, public classes, modules, and methods are exported.
const FILE_MODULE_NAME: &str = "<file_module>";

type CallEdgeData = (String, String, usize, Span, bool);

/// Graph builder for Ruby source files.
///
/// This implementation follows the unified `ASTGraph` pattern used by other
/// language plugins. It builds method contexts first, then performs a second
/// traversal to emit call edges, FFI hooks, and call-site metadata.
#[derive(Debug, Clone, Copy)]
pub struct RubyGraphBuilder {
    max_scope_depth: usize,
}

impl Default for RubyGraphBuilder {
    fn default() -> Self {
        Self {
            max_scope_depth: DEFAULT_SCOPE_DEPTH,
        }
    }
}

impl RubyGraphBuilder {
    /// Create a builder with custom scope depth.
    #[must_use]
    pub fn new(max_scope_depth: usize) -> Self {
        Self { max_scope_depth }
    }
}

impl GraphBuilder for RubyGraphBuilder {
    fn build_graph(
        &self,
        tree: &Tree,
        content: &[u8],
        file: &Path,
        staging: &mut StagingGraph,
    ) -> GraphResult<()> {
        // Create helper for staging graph population
        let mut helper = GraphBuildHelper::new(staging, file, Language::Ruby);

        // Build AST graph for call context tracking
        let ast_graph = ASTGraph::from_tree(tree, content, self.max_scope_depth).map_err(|e| {
            GraphBuilderError::ParseError {
                span: Span::default(),
                reason: e,
            }
        })?;

        // Walk tree to find methods, classes, modules, calls, imports, and FFI
        walk_tree_for_graph(
            tree.root_node(),
            content,
            &ast_graph,
            &mut helper,
            &ast_graph.ffi_enabled_scopes,
        )?;

        apply_controller_dsl_hooks(&ast_graph, &mut helper);

        // Phase: Process YARD annotations for TypeOf and Reference edges
        process_yard_annotations(tree.root_node(), content, &mut helper)?;

        Ok(())
    }

    fn language(&self) -> Language {
        Language::Ruby
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Visibility {
    Public,
    Protected,
    Private,
}

impl Visibility {
    #[allow(dead_code)] // Reserved for visibility filtering in graph queries
    fn as_str(self) -> &'static str {
        match self {
            Visibility::Public => "public",
            Visibility::Protected => "protected",
            Visibility::Private => "private",
        }
    }

    fn from_keyword(keyword: &str) -> Option<Self> {
        match keyword {
            "public" => Some(Visibility::Public),
            "protected" => Some(Visibility::Protected),
            "private" => Some(Visibility::Private),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
enum RubyContextKind {
    Method,
    SingletonMethod,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ControllerDslKind {
    Before,
    After,
    Around,
}

#[allow(dead_code)] // Scaffolding for Rails controller DSL analysis
#[derive(Debug, Clone)]
struct ControllerDslHook {
    container: String,
    kind: ControllerDslKind,
    callbacks: Vec<String>,
    only: Option<Vec<String>>,   // action filters
    except: Option<Vec<String>>, // action filters
}

#[derive(Debug, Clone)]
struct RubyContext {
    qualified_name: String,
    container: Option<String>,
    kind: RubyContextKind,
    visibility: Visibility,
    start_position: Point,
    end_position: Point,
}

impl RubyContext {
    #[allow(dead_code)] // Reserved for future filtering logic
    fn is_method(&self) -> bool {
        matches!(
            self.kind,
            RubyContextKind::Method | RubyContextKind::SingletonMethod
        )
    }

    fn is_singleton(&self) -> bool {
        matches!(self.kind, RubyContextKind::SingletonMethod)
    }

    fn qualified_name(&self) -> &str {
        &self.qualified_name
    }

    fn container(&self) -> Option<&str> {
        self.container.as_deref()
    }

    fn visibility(&self) -> Visibility {
        self.visibility
    }
}

struct ASTGraph {
    contexts: Vec<RubyContext>,
    node_to_context: HashMap<usize, usize>,
    /// Scopes (namespaces) that have `extend FFI::Library` - used for FFI edge emission
    ffi_enabled_scopes: HashSet<Vec<String>>,
    #[allow(dead_code)] // Reserved for Rails controller DSL analysis
    controller_dsl_hooks: Vec<ControllerDslHook>,
}

impl ASTGraph {
    fn from_tree(tree: &Tree, content: &[u8], max_depth: usize) -> Result<Self, String> {
        let mut builder = ContextBuilder::new(content, max_depth)?;
        builder.walk(tree.root_node())?;
        Ok(Self {
            contexts: builder.contexts,
            node_to_context: builder.node_to_context,
            ffi_enabled_scopes: builder.ffi_enabled_scopes,
            controller_dsl_hooks: builder.controller_dsl_hooks,
        })
    }

    #[allow(dead_code)] // Reserved for future context queries
    fn contexts(&self) -> &[RubyContext] {
        &self.contexts
    }

    fn context_for_node(&self, node: &Node<'_>) -> Option<&RubyContext> {
        self.node_to_context
            .get(&node.id())
            .and_then(|idx| self.contexts.get(*idx))
    }
}

/// Walk the tree and populate the staging graph.
fn walk_tree_for_graph(
    node: Node,
    content: &[u8],
    ast_graph: &ASTGraph,
    helper: &mut sqry_core::graph::unified::GraphBuildHelper,
    ffi_enabled_scopes: &HashSet<Vec<String>>,
) -> GraphResult<()> {
    // Track current namespace for FFI scope detection
    let mut current_namespace: Vec<String> = Vec::new();

    walk_tree_for_graph_impl(
        node,
        content,
        ast_graph,
        helper,
        ffi_enabled_scopes,
        &mut current_namespace,
    )
}

fn apply_controller_dsl_hooks(ast_graph: &ASTGraph, helper: &mut GraphBuildHelper) {
    if ast_graph.controller_dsl_hooks.is_empty() {
        return;
    }

    let mut actions_by_container: HashMap<String, Vec<String>> = HashMap::new();
    for context in &ast_graph.contexts {
        if !matches!(context.kind, RubyContextKind::Method) {
            continue;
        }
        let Some(container) = context.container() else {
            continue;
        };
        let Some(action_name) = context.qualified_name.rsplit('#').next() else {
            continue;
        };
        actions_by_container
            .entry(container.to_string())
            .or_default()
            .push(action_name.to_string());
    }

    let mut emitted: HashSet<(String, String)> = HashSet::new();
    for hook in &ast_graph.controller_dsl_hooks {
        let Some(actions) = actions_by_container.get(&hook.container) else {
            continue;
        };

        for action in actions {
            let included = if let Some(only) = &hook.only {
                only.iter().any(|name| name == action)
            } else if let Some(except) = &hook.except {
                !except.iter().any(|name| name == action)
            } else {
                true
            };

            if !included {
                continue;
            }

            for callback in &hook.callbacks {
                if callback.trim().is_empty() {
                    continue;
                }

                let action_qname = format!("{}#{}", hook.container, action);
                let callback_qname = format!("{}#{}", hook.container, callback);
                if !emitted.insert((action_qname.clone(), callback_qname.clone())) {
                    continue;
                }

                let action_id = helper.ensure_method(&action_qname, None, false, false);
                let callback_id = helper.ensure_method(&callback_qname, None, false, false);
                helper.add_call_edge_full_with_span(action_id, callback_id, 255, false, vec![]);
            }
        }
    }
}

/// Internal implementation that tracks namespace context.
#[allow(
    clippy::too_many_lines,
    reason = "Ruby graph extraction handles DSLs and FFI patterns in one traversal."
)]
fn walk_tree_for_graph_impl(
    node: Node,
    content: &[u8],
    ast_graph: &ASTGraph,
    helper: &mut sqry_core::graph::unified::GraphBuildHelper,
    ffi_enabled_scopes: &HashSet<Vec<String>>,
    current_namespace: &mut Vec<String>,
) -> GraphResult<()> {
    match node.kind() {
        "class" => {
            // Extract class name
            if let Some(name_node) = node.child_by_field_name("name")
                && let Ok(class_name) = name_node.utf8_text(content)
            {
                let span = span_from_points(node.start_position(), node.end_position());
                let qualified_name = class_name.to_string();
                let class_id = helper.add_class(&qualified_name, Some(span));

                // Export all classes from the file module
                // In Ruby, all classes are public by default and accessible from outside
                let module_id = helper.add_module(FILE_MODULE_NAME, None);
                helper.add_export_edge(module_id, class_id);

                // Check for superclass (class Foo < Bar)
                if let Some(superclass_node) = node.child_by_field_name("superclass")
                    && let Ok(superclass_name) = superclass_node.utf8_text(content)
                {
                    let superclass_name = superclass_name.trim();
                    if !superclass_name.is_empty() {
                        // Create node for the parent class and add Inherits edge
                        let parent_id = helper.add_class(superclass_name, None);
                        helper.add_inherits_edge(class_id, parent_id);
                    }
                }

                // Push class name to namespace for FFI scope tracking
                current_namespace.push(class_name.trim().to_string());

                // Recurse into children with updated namespace
                let mut cursor = node.walk();
                for child in node.children(&mut cursor) {
                    walk_tree_for_graph_impl(
                        child,
                        content,
                        ast_graph,
                        helper,
                        ffi_enabled_scopes,
                        current_namespace,
                    )?;
                }

                current_namespace.pop();
                return Ok(());
            }
        }
        "module" => {
            // Extract module name
            if let Some(name_node) = node.child_by_field_name("name")
                && let Ok(module_name) = name_node.utf8_text(content)
            {
                let span = span_from_points(node.start_position(), node.end_position());
                let qualified_name = module_name.to_string();
                let mod_id = helper.add_module(&qualified_name, Some(span));

                // Export all modules from the file module
                // In Ruby, all modules are public by default and accessible from outside
                let file_module_id = helper.add_module(FILE_MODULE_NAME, None);
                helper.add_export_edge(file_module_id, mod_id);

                // Push module name to namespace for FFI scope tracking
                current_namespace.push(module_name.trim().to_string());

                // Recurse into children with updated namespace
                let mut cursor = node.walk();
                for child in node.children(&mut cursor) {
                    walk_tree_for_graph_impl(
                        child,
                        content,
                        ast_graph,
                        helper,
                        ffi_enabled_scopes,
                        current_namespace,
                    )?;
                }

                current_namespace.pop();
                return Ok(());
            }
        }
        "method" | "singleton_method" => {
            // Extract method context from AST graph
            if let Some(context) = ast_graph.context_for_node(&node) {
                let span = span_from_points(context.start_position, context.end_position);

                // Detect async patterns in Ruby (Fiber, Thread, async gem patterns)
                let is_async = detect_async_method(node, content);

                // Extract parameter signature
                let params = node
                    .child_by_field_name("parameters")
                    .and_then(|params_node| extract_method_parameters(params_node, content));

                // Extract return type from type annotations
                let return_type = extract_return_type(node, content);

                // Build complete signature: "params -> return_type" or just "params" or just "-> return_type"
                let signature = match (params.as_ref(), return_type.as_ref()) {
                    (Some(p), Some(r)) => Some(format!("{p} -> {r}")),
                    (Some(p), None) => Some(p.clone()),
                    (None, Some(r)) => Some(format!("-> {r}")),
                    (None, None) => None,
                };

                // Get visibility from context
                let visibility = context.visibility().as_str();

                // Add method node with signature metadata
                let method_id = helper.add_method_with_signature(
                    context.qualified_name(),
                    Some(span),
                    is_async,
                    context.is_singleton(),
                    Some(visibility),
                    signature.as_deref(),
                );

                // Export public methods from file module
                // Private/protected methods should NOT be exported
                if context.visibility() == Visibility::Public {
                    let module_id = helper.add_module(FILE_MODULE_NAME, None);
                    helper.add_export_edge(module_id, method_id);
                }
            }
        }
        "assignment" => {
            // Handle constant assignments (CONSTANT = value)
            if let Some(left_node) = node.child_by_field_name("left")
                && left_node.kind() == "constant"
                && let Ok(const_name) = left_node.utf8_text(content)
            {
                // Create qualified name with namespace
                let qualified_name = if current_namespace.is_empty() {
                    const_name.to_string()
                } else {
                    format!("{}::{}", current_namespace.join("::"), const_name)
                };

                let span = span_from_points(node.start_position(), node.end_position());
                let const_id = helper.add_constant(&qualified_name, Some(span));

                // Export public constants from file module
                let module_id = helper.add_module(FILE_MODULE_NAME, None);
                helper.add_export_edge(module_id, const_id);
            }
        }
        "call" | "command" | "command_call" | "identifier" | "super" => {
            // Check for include/extend statements (mixin pattern)
            if is_include_or_extend_statement(node, content) {
                handle_include_extend(node, content, helper, current_namespace);
            }
            // Ruby allows bare identifier statements like `validate` which can either be a local
            // variable reference or an implicit receiver method call. We only attempt to treat
            // identifiers as calls when they appear in statement position.
            else if node.kind() == "identifier" && !is_statement_identifier_call_candidate(node) {
                // Not a standalone statement; avoid misclassifying identifiers inside expressions.
            } else if is_require_statement(node, content) {
                // Build import edge
                if let Some((from_qname, to_qname)) =
                    build_import_for_staging(node, content, helper.file_path())
                {
                    // Ensure both module nodes exist
                    let from_id = helper.add_import(&from_qname, None);
                    let to_id = helper.add_import(
                        &to_qname,
                        Some(span_from_points(node.start_position(), node.end_position())),
                    );

                    // Add import edge
                    helper.add_import_edge(from_id, to_id);
                }
            } else if is_ffi_attach_function(node, content, ffi_enabled_scopes, current_namespace) {
                // FFI attach_function call - create FfiCall edge
                build_ffi_edge_for_attach_function(node, content, helper, current_namespace);
            } else {
                // Build call edge
                if let Ok(Some((source_qname, target_qname, argument_count, span, is_singleton))) =
                    build_call_for_staging(ast_graph, node, content)
                {
                    // Ensure both nodes exist
                    let source_id = helper.ensure_method(&source_qname, None, false, is_singleton);
                    let target_id =
                        helper.ensure_callee(&target_qname, span, CalleeKindHint::Function);

                    // Add call edge
                    let argument_count = u8::try_from(argument_count).unwrap_or(u8::MAX);
                    helper.add_call_edge_full_with_span(
                        source_id,
                        target_id,
                        argument_count,
                        false,
                        vec![span],
                    );
                }
            }
        }
        _ => {}
    }

    // Recurse into children
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_tree_for_graph_impl(
            child,
            content,
            ast_graph,
            helper,
            ffi_enabled_scopes,
            current_namespace,
        )?;
    }

    Ok(())
}

/// Check if a call is an FFI `attach_function` within an FFI-enabled scope.
///
/// Ruby FFI pattern:
/// ```ruby
/// module MyLib
///   extend FFI::Library
///   ffi_lib 'c'
///   attach_function :puts, [:string], :int
/// end
/// ```
fn is_ffi_attach_function(
    node: Node,
    content: &[u8],
    ffi_enabled_scopes: &HashSet<Vec<String>>,
    current_namespace: &[String],
) -> bool {
    // Extract method name
    let method_name = match node.kind() {
        "command" => node
            .child_by_field_name("name")
            .and_then(|n| n.utf8_text(content).ok()),
        "call" | "command_call" => node
            .child_by_field_name("method")
            .and_then(|n| n.utf8_text(content).ok()),
        _ => None,
    };

    let Some(method_name) = method_name else {
        return false;
    };
    let method_name = method_name.trim();
    if !matches!(
        method_name,
        "attach_function" | "attach_variable" | "ffi_lib" | "callback"
    ) {
        return false;
    }

    let receiver = match node.kind() {
        "call" | "command_call" | "method_call" => node
            .child_by_field_name("receiver")
            .and_then(|n| n.utf8_text(content).ok()),
        _ => None,
    };
    if let Some(receiver) = receiver {
        let trimmed = receiver.trim();
        if trimmed == "FFI" || trimmed.contains("FFI::Library") || trimmed.starts_with("FFI::") {
            return true;
        }
    }

    ffi_enabled_scopes.contains(current_namespace)
}

/// Build an `FfiCall` edge for an FFI `attach_function` call.
///
/// Extracts the Ruby method name and native function name from:
/// `attach_function :ruby_name, :native_name, [:args], :return_type`
/// or
/// `attach_function :name, [:args], :return_type` (same name for both)
fn build_ffi_edge_for_attach_function(
    node: Node,
    content: &[u8],
    helper: &mut sqry_core::graph::unified::GraphBuildHelper,
    current_namespace: &[String],
) {
    // Extract the function name from the first symbol argument
    let arguments = node.child_by_field_name("arguments");

    // For command nodes, arguments might be inline children
    let func_name = if let Some(args) = arguments {
        extract_first_symbol_from_arguments(args, content)
    } else {
        // Try to find symbol children directly (for command nodes)
        let mut cursor = node.walk();
        let mut found_name = false;
        let mut result = None;
        for child in node.children(&mut cursor) {
            if !child.is_named() {
                continue;
            }
            // Skip the method name itself
            if !found_name {
                found_name = true;
                continue;
            }
            // First symbol after method name is the function name
            if matches!(child.kind(), "symbol" | "simple_symbol")
                && let Ok(text) = child.utf8_text(content)
            {
                result = Some(text.trim().trim_start_matches(':').to_string());
                break;
            }
        }
        result
    };

    let Some(func_name) = func_name else {
        return;
    };

    // Build qualified caller name (the module containing the FFI binding)
    let caller_name = if current_namespace.is_empty() {
        "<module>".to_string()
    } else {
        current_namespace.join("::")
    };

    // Create caller node (the FFI module)
    let caller_id = helper.add_module(&caller_name, None);

    // Create FFI function node (the native function being bound)
    let ffi_func_name = format!("ffi::{func_name}");
    let span = span_from_points(node.start_position(), node.end_position());
    let ffi_func_id = helper.add_function(&ffi_func_name, Some(span), false, false);

    // Add FfiCall edge with C convention (Ruby FFI uses C ABI)
    helper.add_ffi_edge(caller_id, ffi_func_id, FfiConvention::C);
}

/// Extract the first symbol from arguments (for `attach_function`).
fn extract_first_symbol_from_arguments(arguments: Node, content: &[u8]) -> Option<String> {
    let mut cursor = arguments.walk();
    for child in arguments.children(&mut cursor) {
        if matches!(child.kind(), "symbol" | "simple_symbol")
            && let Ok(text) = child.utf8_text(content)
        {
            return Some(text.trim().trim_start_matches(':').to_string());
        }
        // Handle bare_symbol (just the identifier after :)
        if child.kind() == "bare_symbol"
            && let Ok(text) = child.utf8_text(content)
        {
            return Some(text.trim().to_string());
        }
    }
    None
}

/// Build call edge information for the staging graph.
fn build_call_for_staging(
    ast_graph: &ASTGraph,
    call_node: Node<'_>,
    content: &[u8],
) -> GraphResult<Option<CallEdgeData>> {
    let Some(call_context) = ast_graph.context_for_node(&call_node) else {
        return Ok(None);
    };

    let Some(method_call) = extract_method_call(call_node, content)? else {
        return Ok(None);
    };

    if is_visibility_command(&method_call) {
        return Ok(None);
    }

    let source_qualified = call_context.qualified_name().to_string();
    let target_name = resolve_callee(&method_call, call_context);

    if target_name.is_empty() {
        return Ok(None);
    }

    let span = span_from_node(call_node);
    let argument_count = count_arguments(method_call.arguments, content);
    let is_singleton = call_context.is_singleton();

    Ok(Some((
        source_qualified,
        target_name,
        argument_count,
        span,
        is_singleton,
    )))
}

/// Build import edge information for the staging graph.
fn build_import_for_staging(
    require_node: Node<'_>,
    content: &[u8],
    file_path: &str,
) -> Option<(String, String)> {
    // Extract the method name (require or require_relative)
    let method_name = match require_node.kind() {
        "command" => require_node
            .child_by_field_name("name")
            .and_then(|n| n.utf8_text(content).ok())
            .map(|s| s.trim().to_string()),
        "call" | "method_call" => require_node
            .child_by_field_name("method")
            .and_then(|n| n.utf8_text(content).ok())
            .map(|s| s.trim().to_string()),
        _ => None,
    };

    let method_name = method_name?;

    // Only handle require and require_relative
    if !matches!(method_name.as_str(), "require" | "require_relative") {
        return None;
    }

    // Extract the module name from arguments
    let arguments = require_node.child_by_field_name("arguments");
    let module_name = if let Some(args) = arguments {
        extract_require_module_name(args, content)
    } else {
        // For command nodes, the first child after the method name is the argument
        let mut cursor = require_node.walk();
        let mut found_name = false;
        let mut result = None;
        for child in require_node.children(&mut cursor) {
            if !child.is_named() {
                continue;
            }
            if !found_name {
                found_name = true;
                continue;
            }
            // This is the argument (string node)
            result = extract_string_content(child, content);
            break;
        }
        result
    };

    let module_name = module_name?;

    if module_name.is_empty() {
        return None;
    }

    // Resolve the import path to a canonical module identifier
    let is_relative = method_name == "require_relative";
    let resolved_path = resolve_ruby_require(&module_name, is_relative, file_path);

    // Return from/to qualified names
    Some(("<module>".to_string(), resolved_path))
}

fn is_statement_identifier_call_candidate(node: Node<'_>) -> bool {
    node.kind() == "identifier"
        && node
            .parent()
            .is_some_and(|p| matches!(p.kind(), "body_statement" | "program"))
}

/// Detect async patterns in Ruby methods (best-effort detection).
///
/// Ruby doesn't have native async/await, but uses patterns like:
/// - `Fiber` class for coroutines
/// - `Thread` class for threading
/// - `async` gem patterns (`Async do ... end`)
/// - `concurrent-ruby` patterns
///
/// This is a heuristic check looking for async-related keywords in method body.
fn detect_async_method(method_node: Node<'_>, content: &[u8]) -> bool {
    // Get method body
    let body_node = method_node.child_by_field_name("body");
    if body_node.is_none() {
        return false;
    }
    let body_node = body_node.unwrap();

    // Convert body to text and look for async patterns
    if let Ok(body_text) = body_node.utf8_text(content) {
        let body_lower = body_text.to_lowercase();

        // Check for common async patterns
        if body_lower.contains("fiber.")
            || body_lower.contains("fiber.new")
            || body_lower.contains("fiber.yield")
            || body_lower.contains("fiber.resume")
            || body_lower.contains("thread.new")
            || body_lower.contains("thread.start")
            || body_lower.contains("async do")
            || body_lower.contains("async {")
            || body_lower.contains("async.reactor")
            || body_lower.contains("concurrent::")
        {
            return true;
        }
    }

    false
}

/// Check if a Ruby call node is an `include` or `extend` statement (mixin pattern).
fn is_include_or_extend_statement(node: Node<'_>, content: &[u8]) -> bool {
    let method_name = match node.kind() {
        "command" => node
            .child_by_field_name("name")
            .and_then(|n| n.utf8_text(content).ok()),
        "call" | "method_call" => node
            .child_by_field_name("method")
            .and_then(|n| n.utf8_text(content).ok()),
        _ => None,
    };

    method_name.is_some_and(|name| matches!(name.trim(), "include" | "extend"))
}

/// Handle `include` or `extend` statements to create Implements edges.
///
/// Ruby mixins work as follows:
/// - `include ModuleName`: Instance methods from module become instance methods
/// - `extend ModuleName`: Instance methods from module become class methods
///
/// Both are represented as Implements edges from the class to the module.
fn handle_include_extend(
    node: Node<'_>,
    content: &[u8],
    helper: &mut sqry_core::graph::unified::GraphBuildHelper,
    current_namespace: &[String],
) {
    // Extract the module name from arguments
    let module_name = if let Some(args) = node.child_by_field_name("arguments") {
        extract_first_constant_from_arguments(args, content)
    } else if node.kind() == "command" {
        // For command nodes, the first named child after the method name is the module
        let mut cursor = node.walk();
        let mut found_method = false;
        let mut result = None;
        for child in node.children(&mut cursor) {
            if !child.is_named() {
                continue;
            }
            // Skip the method name itself
            if !found_method {
                found_method = true;
                continue;
            }
            // First constant after method name is the module name
            if child.kind() == "constant"
                && let Ok(text) = child.utf8_text(content)
            {
                result = Some(text.trim().to_string());
                break;
            }
        }
        result
    } else {
        None
    };

    let Some(module_name) = module_name else {
        return;
    };

    // Build qualified class name (the class doing the include/extend)
    let class_name = if current_namespace.is_empty() {
        return; // Can't include/extend outside a class
    } else {
        current_namespace.join("::")
    };

    // Create nodes
    let class_id = helper.add_class(&class_name, None);
    let module_id = helper.add_module(&module_name, None);

    // Add Implements edge (class implements module)
    helper.add_implements_edge(class_id, module_id);
}

/// Extract the first constant from an argument list.
fn extract_first_constant_from_arguments(args_node: Node<'_>, content: &[u8]) -> Option<String> {
    let mut cursor = args_node.walk();
    for child in args_node.children(&mut cursor) {
        if !child.is_named() {
            continue;
        }
        // Look for constant nodes
        if child.kind() == "constant"
            && let Ok(text) = child.utf8_text(content)
        {
            return Some(text.trim().to_string());
        }
    }
    None
}

/// Check if a node is a `require/require_relative` statement.
fn is_require_statement(node: Node<'_>, content: &[u8]) -> bool {
    let method_name = match node.kind() {
        "command" => node
            .child_by_field_name("name")
            .and_then(|n| n.utf8_text(content).ok()),
        "call" | "method_call" => node
            .child_by_field_name("method")
            .and_then(|n| n.utf8_text(content).ok()),
        _ => None,
    };

    method_name.is_some_and(|name| matches!(name.trim(), "require" | "require_relative"))
}

struct ContextBuilder<'a> {
    contexts: Vec<RubyContext>,
    node_to_context: HashMap<usize, usize>,
    namespace: Vec<String>,
    visibility_stack: Vec<Visibility>,
    ffi_enabled_scopes: HashSet<Vec<String>>,
    controller_dsl_hooks: Vec<ControllerDslHook>,
    max_depth: usize,
    content: &'a [u8],
    guard: sqry_core::query::security::RecursionGuard,
}

impl<'a> ContextBuilder<'a> {
    fn new(content: &'a [u8], max_depth: usize) -> Result<Self, String> {
        let recursion_limits = sqry_core::config::RecursionLimits::load_or_default()
            .map_err(|e| format!("Failed to load recursion limits: {e}"))?;
        let file_ops_depth = recursion_limits
            .effective_file_ops_depth()
            .map_err(|e| format!("Invalid file_ops_depth configuration: {e}"))?;
        let guard = sqry_core::query::security::RecursionGuard::new(file_ops_depth)
            .map_err(|e| format!("Failed to create recursion guard: {e}"))?;

        Ok(Self {
            contexts: Vec::new(),
            node_to_context: HashMap::new(),
            namespace: Vec::new(),
            visibility_stack: vec![Visibility::Public],
            ffi_enabled_scopes: HashSet::new(),
            controller_dsl_hooks: Vec::new(),
            max_depth,
            content,
            guard,
        })
    }

    /// # Errors
    ///
    /// Returns error if recursion depth exceeds the guard's limit.
    fn walk(&mut self, node: Node<'a>) -> Result<(), String> {
        self.guard
            .enter()
            .map_err(|e| format!("Recursion limit exceeded: {e}"))?;

        match node.kind() {
            "class" => self.visit_class(node)?,
            "module" => self.visit_module(node)?,
            "singleton_class" => self.visit_singleton_class(node)?,
            "method" => self.visit_method(node)?,
            "singleton_method" => self.visit_singleton_method(node)?,
            "command" | "command_call" | "call" => {
                self.detect_ffi_extend(node)?;
                self.detect_controller_dsl(node)?;
                self.adjust_visibility(node)?;
                self.walk_children(node)?;
            }
            "identifier" => {
                // Bare identifiers like `private`, `protected`, `public` at statement level
                // can adjust visibility scope
                self.adjust_visibility_from_identifier(node)?;
                self.walk_children(node)?;
            }
            _ => self.walk_children(node)?,
        }

        self.guard.exit();
        Ok(())
    }

    fn visit_class(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node
            .child_by_field_name("name")
            .ok_or_else(|| "class node missing name".to_string())?;
        let class_name = self.node_text(name_node)?;

        if self.namespace.len() > self.max_depth {
            return Ok(());
        }

        self.namespace.push(class_name);
        self.visibility_stack.push(Visibility::Public);

        self.walk_children(node)?;

        self.visibility_stack.pop();
        self.namespace.pop();
        Ok(())
    }

    fn visit_module(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node
            .child_by_field_name("name")
            .ok_or_else(|| "module node missing name".to_string())?;
        let module_name = self.node_text(name_node)?;

        if self.namespace.len() > self.max_depth {
            return Ok(());
        }

        self.namespace.push(module_name);
        self.visibility_stack.push(Visibility::Public);

        self.walk_children(node)?;

        self.visibility_stack.pop();
        self.namespace.pop();
        Ok(())
    }

    fn visit_method(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node
            .child_by_field_name("name")
            .ok_or_else(|| "method node missing name".to_string())?;
        let method_name = self.node_text(name_node)?;

        let (qualified_name, container) =
            method_qualified_name(&self.namespace, &method_name, false);

        let visibility = inline_visibility_for_method(node, self.content)
            .unwrap_or_else(|| *self.visibility_stack.last().unwrap_or(&Visibility::Public));

        let context = RubyContext {
            qualified_name,
            container,
            kind: RubyContextKind::Method,
            visibility,
            start_position: node.start_position(),
            end_position: node.end_position(),
        };

        let idx = self.contexts.len();
        self.contexts.push(context);
        associate_descendants(node, idx, &mut self.node_to_context);

        self.walk_children(node)?;
        Ok(())
    }

    fn visit_singleton_class(&mut self, node: Node<'a>) -> Result<(), String> {
        // Extract the object: class << self, class << MyClass, etc.
        let value_node = node
            .child_by_field_name("value")
            .ok_or_else(|| "singleton_class missing value".to_string())?;
        let object_text = self.node_text(value_node)?;

        // Determine the scope name for methods in this singleton class
        let scope_name = if object_text == "self" {
            // class << self inside Foo → methods are Foo.method
            if let Some(current_class) = self.namespace.last() {
                format!("<<{current_class}>>")
            } else {
                "<<main>>".to_string()
            }
        } else {
            // class << SomeClass → methods are SomeClass.method
            format!("<<{object_text}>>")
        };

        if self.namespace.len() > self.max_depth {
            return Ok(());
        }

        // Push the singleton scope
        self.namespace.push(scope_name);
        self.visibility_stack.push(Visibility::Public);

        // Walk children, converting methods to singleton methods
        self.visit_singleton_class_body(node)?;

        // Pop the singleton scope
        self.visibility_stack.pop();
        self.namespace.pop();
        Ok(())
    }

    fn visit_singleton_class_body(&mut self, node: Node<'a>) -> Result<(), String> {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if !child.is_named() {
                continue;
            }

            // Methods inside singleton_class are automatically singleton methods
            if child.kind() == "method" {
                self.visit_method_as_singleton(child)?;
            } else {
                self.walk(child)?;
            }
        }
        Ok(())
    }

    fn visit_method_as_singleton(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node
            .child_by_field_name("name")
            .ok_or_else(|| "method node missing name".to_string())?;
        let method_name = self.node_text(name_node)?;

        // Build as singleton method - strip the <<...>> wrapper from namespace
        let actual_namespace: Vec<String> = self
            .namespace
            .iter()
            .map(|s| {
                if s.starts_with("<<") && s.ends_with(">>") {
                    // Extract the class name from <<ClassName>>
                    s[2..s.len() - 2].to_string()
                } else {
                    s.clone()
                }
            })
            .collect();

        let (qualified_name, container) =
            method_qualified_name(&actual_namespace, &method_name, true);

        let visibility = inline_visibility_for_method(node, self.content)
            .unwrap_or_else(|| *self.visibility_stack.last().unwrap_or(&Visibility::Public));

        let context = RubyContext {
            qualified_name,
            container,
            kind: RubyContextKind::SingletonMethod,
            visibility,
            start_position: node.start_position(),
            end_position: node.end_position(),
        };

        let idx = self.contexts.len();
        self.contexts.push(context);
        associate_descendants(node, idx, &mut self.node_to_context);

        self.walk_children(node)?;
        Ok(())
    }

    fn visit_singleton_method(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node
            .child_by_field_name("name")
            .ok_or_else(|| "singleton_method missing name".to_string())?;
        let method_name = self.node_text(name_node)?;

        let object_node = node
            .child_by_field_name("object")
            .ok_or_else(|| "singleton_method missing object".to_string())?;
        let object_text = self.node_text(object_node)?;

        let (qualified_name, container) =
            singleton_qualified_name(&self.namespace, object_text.trim(), &method_name);

        let visibility = inline_visibility_for_method(node, self.content)
            .unwrap_or_else(|| *self.visibility_stack.last().unwrap_or(&Visibility::Public));

        let context = RubyContext {
            qualified_name,
            container,
            kind: RubyContextKind::SingletonMethod,
            visibility,
            start_position: node.start_position(),
            end_position: node.end_position(),
        };

        let idx = self.contexts.len();
        self.contexts.push(context);
        associate_descendants(node, idx, &mut self.node_to_context);

        self.walk_children(node)?;
        Ok(())
    }

    fn detect_ffi_extend(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node.child_by_field_name("name");
        let Some(name_node) = name_node else {
            return Ok(());
        };

        let keyword = self.node_text(name_node)?;
        if keyword.trim() != "extend" {
            return Ok(());
        }

        let arg_text = if let Some(arguments) = node.child_by_field_name("arguments") {
            node_text_raw(arguments, self.content).unwrap_or_default()
        } else {
            let mut cursor = node.walk();
            let mut found_name = false;
            let mut result = String::new();
            for child in node.children(&mut cursor) {
                if !child.is_named() {
                    continue;
                }
                if !found_name {
                    found_name = true;
                    continue;
                }
                if let Some(text) = node_text_raw(child, self.content) {
                    result = text;
                    break;
                }
            }
            result
        };

        if arg_text.contains("FFI::Library") {
            // Mark current scope as FFI-enabled
            self.ffi_enabled_scopes.insert(self.namespace.clone());
        }

        Ok(())
    }

    fn detect_controller_dsl(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node
            .child_by_field_name("name")
            .or_else(|| node.child_by_field_name("method"));
        let Some(name_node) = name_node else {
            return Ok(());
        };
        let dsl = self.node_text(name_node)?;

        let kind = match dsl.as_str() {
            "before_action" => Some(ControllerDslKind::Before),
            "after_action" => Some(ControllerDslKind::After),
            "around_action" => Some(ControllerDslKind::Around),
            _ => None,
        };
        let Some(kind) = kind else {
            return Ok(());
        };

        if self.namespace.is_empty() {
            return Ok(());
        }
        let container = self.namespace.join("::");

        let mut callbacks: Vec<String> = Vec::new();
        let mut only: Option<Vec<String>> = None;
        let mut except: Option<Vec<String>> = None;

        if let Some(arguments) = node.child_by_field_name("arguments") {
            let mut cursor = arguments.walk();
            for child in arguments.children(&mut cursor) {
                if !child.is_named() {
                    continue;
                }
                let kind = child.kind();
                match kind {
                    "symbol" | "simple_symbol" | "array" if callbacks.is_empty() => {
                        let mut v = extract_symbols_from_node(child, self.content);
                        callbacks.append(&mut v);
                    }
                    "pair" => {
                        // Handle direct pair node (only: [...] or except: [...])
                        let key = child.child_by_field_name("key");
                        let val = child.child_by_field_name("value");
                        if key.is_none() || val.is_none() {
                            continue;
                        }
                        let key_text = self.node_text(key.unwrap()).unwrap_or_default();
                        let symbols = extract_symbols_from_node(val.unwrap(), self.content);
                        if key_text.contains("only") && !symbols.is_empty() {
                            only = Some(symbols);
                        } else if key_text.contains("except") && !symbols.is_empty() {
                            except = Some(symbols);
                        }
                    }
                    "hash" => {
                        // Parse pairs like only: [:new, :create]
                        let mut hcur = child.walk();
                        for pair in child.children(&mut hcur) {
                            if !pair.is_named() {
                                continue;
                            }
                            if pair.kind() != "pair" {
                                continue;
                            }
                            let key = pair.child_by_field_name("key");
                            let val = pair.child_by_field_name("value");
                            if key.is_none() || val.is_none() {
                                continue;
                            }
                            let key_text = self.node_text(key.unwrap()).unwrap_or_default();
                            let symbols = extract_symbols_from_node(val.unwrap(), self.content);
                            if key_text.contains("only") && !symbols.is_empty() {
                                only = Some(symbols);
                            } else if key_text.contains("except") && !symbols.is_empty() {
                                except = Some(symbols);
                            }
                        }
                    }
                    _ => {}
                }
            }
        } else {
            // Fallback: parse from raw node text
            if let Some(raw) = node_text_raw(node, self.content) {
                let (cbs, o, e) = parse_controller_dsl_args(&raw);
                callbacks = cbs;
                only = o;
                except = e;
            }
        }

        if callbacks.is_empty() {
            return Ok(());
        }

        self.controller_dsl_hooks.push(ControllerDslHook {
            container,
            kind,
            callbacks,
            only,
            except,
        });
        Ok(())
    }

    fn adjust_visibility(&mut self, node: Node<'a>) -> Result<(), String> {
        let name_node = node.child_by_field_name("name");
        let Some(name_node) = name_node else {
            return Ok(());
        };

        let keyword = self.node_text(name_node)?;
        let Some(new_visibility) = Visibility::from_keyword(keyword.trim()) else {
            return Ok(());
        };

        // Only adjust default visibility when command has no arguments.
        if !has_call_arguments(node)
            && let Some(last) = self.visibility_stack.last_mut()
        {
            *last = new_visibility;
        }
        Ok(())
    }

    /// Handle bare identifiers that can be visibility keywords (private, protected, public)
    fn adjust_visibility_from_identifier(&mut self, node: Node<'a>) -> Result<(), String> {
        let keyword = self.node_text(node)?;
        let Some(new_visibility) = Visibility::from_keyword(keyword.trim()) else {
            return Ok(());
        };

        // Bare identifier as statement adjusts visibility for following methods
        if let Some(last) = self.visibility_stack.last_mut() {
            *last = new_visibility;
        }

        Ok(())
    }

    fn walk_children(&mut self, node: Node<'a>) -> Result<(), String> {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.is_named() {
                self.walk(child)?;
            }
        }
        Ok(())
    }

    fn node_text(&self, node: Node<'a>) -> Result<String, String> {
        node.utf8_text(self.content)
            .map(|s| s.trim().to_string())
            .map_err(|err| err.to_string())
    }
}

#[derive(Clone)]
struct MethodCall<'a> {
    name: String,
    receiver: Option<String>,
    arguments: Option<Node<'a>>,
    node: Node<'a>,
}

fn extract_method_call<'a>(node: Node<'a>, content: &[u8]) -> GraphResult<Option<MethodCall<'a>>> {
    let method_name = match node.kind() {
        "call" | "command_call" | "method_call" => {
            let method_node = node
                .child_by_field_name("method")
                .ok_or_else(|| builder_parse_error(node, "call node missing method name"))?;
            node_text(method_node, content)?
        }
        "command" => {
            let name_node = node
                .child_by_field_name("name")
                .ok_or_else(|| builder_parse_error(node, "command node missing name"))?;
            node_text(name_node, content)?
        }
        "super" => "super".to_string(),
        "identifier" => {
            if !should_treat_identifier_as_call(node) {
                return Ok(None);
            }
            node_text(node, content)?
        }
        _ => return Ok(None),
    };

    let receiver = match node.kind() {
        "call" | "command_call" | "method_call" => node
            .child_by_field_name("receiver")
            .and_then(|r| node_text(r, content).ok()),
        _ => None,
    };

    let arguments = node.child_by_field_name("arguments");

    Ok(Some(MethodCall {
        name: method_name,
        receiver,
        arguments,
        node,
    }))
}

fn should_treat_identifier_as_call(node: Node<'_>) -> bool {
    if let Some(parent) = node.parent() {
        let kind = parent.kind();
        if matches!(
            kind,
            "call"
                | "command"
                | "command_call"
                | "method_call"
                | "method"
                | "singleton_method"
                | "alias"
                | "symbol"
        ) {
            return false;
        }

        if kind.contains("assignment")
            || matches!(
                kind,
                "parameters"
                    | "method_parameters"
                    | "block_parameters"
                    | "lambda_parameters"
                    | "constant_path"
                    | "module"
                    | "class"
                    | "hash"
                    | "pair"
                    | "array"
                    | "argument_list"
            )
        {
            return false;
        }
    }

    true
}

/// Resolves the fully-qualified name of a method call's target (callee).
///
/// Handles different receiver patterns:
/// - `self.method` → qualified with current container
/// - `Constant.method` → qualified with constant name
/// - Bare `method` → qualified based on context (instance vs singleton)
///
/// # Arguments
/// * `method_call` - The extracted method call information
/// * `context` - The containing Ruby context (method, class, etc.)
///
/// # Returns
/// Fully-qualified callee name, or bare method name if no context available
fn resolve_callee(method_call: &MethodCall<'_>, context: &RubyContext) -> String {
    let name = method_call.name.trim();
    if name.is_empty() {
        return String::new();
    }

    // Special handling for inheritance super calls
    if name == "super" {
        // Use current method's qualified name as the target of the super call
        // This acts as a placeholder that downstream processors can resolve
        // to the actual parent implementation when available.
        return format!("super::{}", context.qualified_name());
    }

    if let Some(receiver) = method_call.receiver.as_deref() {
        let receiver = receiver.trim();
        if receiver == "self" {
            if let Some(container) = context.container() {
                return format!("{container}.{name}");
            }
            return format!("self.{name}");
        }

        if receiver.contains("::") || receiver.starts_with("::") || is_constant(receiver) {
            let cleaned = receiver.trim_start_matches("::");
            // Handle Class.new.method pattern → Class#method (instance method)
            if let Some(class_name) = cleaned.strip_suffix(".new") {
                return format!("{class_name}#{name}");
            }
            return format!("{cleaned}.{name}");
        }

        // Instance variable or expression receiver - fall back to method name.
        return name.to_string();
    }

    if context.is_singleton() {
        if let Some(container) = context.container() {
            return format!("{container}.{name}");
        }
        return name.to_string();
    }

    if let Some(container) = context.container() {
        return format!("{container}#{name}");
    }

    name.to_string()
}

/// Counts the number of actual arguments in a method call.
///
/// Filters out delimiters (parentheses, commas) and empty nodes to count
/// only meaningful arguments.
///
/// # Arguments
/// * `arguments` - Optional `argument_list` AST node
/// * `content` - Source file bytes for text extraction
///
/// # Returns
/// Number of non-empty, non-delimiter arguments
fn count_arguments(arguments: Option<Node<'_>>, content: &[u8]) -> usize {
    let Some(arguments) = arguments else {
        return 0;
    };

    let mut count = 0;
    let mut cursor = arguments.walk();
    for child in arguments.children(&mut cursor) {
        if child.is_named()
            && !is_literal_delimiter(child.kind())
            && node_text(child, content)
                .map(|s| !s.trim().is_empty())
                .unwrap_or(false)
        {
            count += 1;
        }
    }
    count
}

/// Associates all descendant AST nodes with a context index.
///
/// Performs a depth-first traversal to map every child node ID to the
/// parent context (method, class, etc.). This enables fast context lookup
/// during call edge extraction.
///
/// # Arguments
/// * `node` - Root AST node to traverse
/// * `idx` - Context index to associate with all descendants
/// * `map` - Mutable `node_id` → `context_index` map
fn associate_descendants(node: Node<'_>, idx: usize, map: &mut HashMap<usize, usize>) {
    let mut stack = vec![node];
    while let Some(current) = stack.pop() {
        map.insert(current.id(), idx);
        let mut cursor = current.walk();
        for child in current.children(&mut cursor) {
            stack.push(child);
        }
    }
}

/// Builds a fully-qualified name for an instance or singleton method.
///
/// Ruby naming conventions:
/// - Instance methods: `Class#method`
/// - Singleton methods: `Class.method`
///
/// # Arguments
/// * `namespace` - Stack of containing modules/classes (e.g., `["Module", "Class"]`)
/// * `method_name` - Base method name
/// * `singleton` - Whether this is a singleton (class) method
///
/// # Returns
/// Tuple of (`qualified_name`, `optional_container`)
fn method_qualified_name(
    namespace: &[String],
    method_name: &str,
    singleton: bool,
) -> (String, Option<String>) {
    if namespace.is_empty() {
        return (method_name.to_string(), None);
    }

    let container = namespace.join("::");
    let qualified = if singleton {
        format!("{container}.{method_name}")
    } else {
        format!("{container}#{method_name}")
    };
    (qualified, Some(container))
}

/// Builds a qualified name for a singleton method definition.
///
/// Handles both `def self.method` and `def SomeClass.method` patterns.
/// Resolves `self` relative to the current namespace.
///
/// # Arguments
/// * `current_namespace` - Current nesting context (modules/classes)
/// * `object_text` - The receiver text ("self" or a constant path)
/// * `method_name` - Base method name
///
/// # Returns
/// Tuple of (`qualified_name`, `optional_container`)
fn singleton_qualified_name(
    current_namespace: &[String],
    object_text: &str,
    method_name: &str,
) -> (String, Option<String>) {
    if object_text == "self" {
        if current_namespace.is_empty() {
            (method_name.to_string(), None)
        } else {
            let container = current_namespace.join("::");
            (format!("{container}.{method_name}"), Some(container))
        }
    } else {
        let parts = split_constant_path(object_text);
        if parts.is_empty() {
            (method_name.to_string(), None)
        } else {
            let container = parts.join("::");
            (format!("{container}.{method_name}"), Some(container))
        }
    }
}

/// Splits a Ruby constant path into individual segments.
///
/// Handles leading `::` for absolute paths and filters empty segments.
///
/// # Examples
/// - `"::Module::Class"` → `["Module", "Class"]`
/// - `"Foo::Bar"` → `["Foo", "Bar"]`
///
/// # Arguments
/// * `path` - Constant path string (e.g., "`Module::Class`")
///
/// # Returns
/// Vector of non-empty path segments
fn split_constant_path(path: &str) -> Vec<String> {
    path.trim()
        .trim_start_matches("::")
        .split("::")
        .filter_map(|seg| {
            let trimmed = seg.trim();
            if trimmed.is_empty() {
                None
            } else {
                Some(trimmed.to_string())
            }
        })
        .collect()
}

/// Checks if a string represents a Ruby constant (starts with uppercase).
///
/// Ruby constants must begin with an uppercase ASCII letter.
///
/// # Arguments
/// * `text` - String to test
///
/// # Returns
/// true if text starts with uppercase letter, false otherwise
fn is_constant(text: &str) -> bool {
    text.chars().next().is_some_and(|c| c.is_ascii_uppercase())
}

/// Detects visibility modifier commands without arguments.
///
/// Bare `private`, `public`, or `protected` calls (without method names)
/// are visibility scope changes, not method calls to track as edges.
///
/// # Arguments
/// * `method_call` - Extracted method call
///
/// # Returns
/// true if this is a visibility scope change command
fn is_visibility_command(method_call: &MethodCall<'_>) -> bool {
    matches!(
        method_call.name.as_str(),
        "public" | "private" | "protected"
    ) && method_call.receiver.is_none()
        && !has_call_arguments(method_call.node)
}

/// Checks if a call/command node has any arguments.
///
/// Used to distinguish `private` (scope change) from `private :method_name` (method-level).
///
/// # Arguments
/// * `node` - AST node to check
///
/// # Returns
/// true if node has named argument children
fn has_call_arguments(node: Node<'_>) -> bool {
    if let Some(arguments) = node.child_by_field_name("arguments") {
        let mut cursor = arguments.walk();
        for child in arguments.children(&mut cursor) {
            if child.is_named() {
                return true;
            }
        }
    }
    false
}

fn inline_visibility_for_method(node: Node<'_>, content: &[u8]) -> Option<Visibility> {
    let parent = node.parent()?;
    let visibility_node = match parent.kind() {
        "call" | "command" | "command_call" => parent,
        "argument_list" => parent.parent()?,
        _ => return None,
    };

    if !matches!(visibility_node.kind(), "call" | "command" | "command_call") {
        return None;
    }

    let keyword_node = visibility_node
        .child_by_field_name("name")
        .or_else(|| visibility_node.child_by_field_name("method"))?;
    let keyword = node_text_raw(keyword_node, content)?;
    Visibility::from_keyword(keyword.trim())
}

/// Extracts UTF-8 text for an AST node with error handling.
///
/// Trims whitespace and converts UTF-8 errors to `GraphBuilderError`.
///
/// # Arguments
/// * `node` - AST node to extract text from
/// * `content` - Source file bytes
///
/// # Returns
/// Trimmed text content or error if UTF-8 decoding fails
fn node_text(node: Node<'_>, content: &[u8]) -> Result<String, GraphBuilderError> {
    node.utf8_text(content)
        .map(|s| s.trim().to_string())
        .map_err(|err| builder_parse_error(node, &format!("utf8 error: {err}")))
}

/// Raw text extraction without `GraphBuilderError` conversion
fn node_text_raw(node: Node<'_>, content: &[u8]) -> Option<String> {
    node.utf8_text(content)
        .ok()
        .map(std::string::ToString::to_string)
}

/// Creates a `GraphBuilderError::ParseError` with span information.
///
/// # Arguments
/// * `node` - AST node where error occurred
/// * `reason` - Human-readable error description
///
/// # Returns
/// `ParseError` with node's span and reason message
fn builder_parse_error(node: Node<'_>, reason: &str) -> GraphBuilderError {
    GraphBuilderError::ParseError {
        span: span_from_node(node),
        reason: reason.to_string(),
    }
}

/// Extract parameter signature from `method_parameters` node.
///
/// Handles all Ruby parameter types:
/// - Simple: `x`
/// - Optional: `x = 10`
/// - Splat: `*args`
/// - Keyword: `x:`, `x: 10`
/// - Hash splat: `**kwargs`
/// - Block: `&block`
///
/// # Arguments
/// * `params_node` - The `method_parameters` AST node
/// * `content` - Source file content
///
/// # Returns
/// Comma-separated parameter string, or None if no parameters
#[allow(clippy::match_same_arms)]
fn extract_method_parameters(params_node: Node<'_>, content: &[u8]) -> Option<String> {
    let mut params = Vec::new();
    let mut cursor = params_node.walk();

    for child in params_node.named_children(&mut cursor) {
        match child.kind() {
            // Simple parameter: def foo(x)
            // Optional parameter: def foo(x = 10)
            "identifier" | "optional_parameter" => {
                if let Ok(text) = child.utf8_text(content) {
                    params.push(text.to_string());
                }
            }
            // Splat: def foo(*args)
            "splat_parameter" => {
                if let Some(name_node) = child.child_by_field_name("name") {
                    if let Ok(name) = name_node.utf8_text(content) {
                        params.push(format!("*{name}"));
                    }
                } else if let Ok(text) = child.utf8_text(content) {
                    // Fallback: use full text if no name field
                    params.push(text.to_string());
                }
            }
            // Hash splat: def foo(**kwargs)
            "hash_splat_parameter" => {
                if let Some(name_node) = child.child_by_field_name("name") {
                    if let Ok(name) = name_node.utf8_text(content) {
                        params.push(format!("**{name}"));
                    }
                } else if let Ok(text) = child.utf8_text(content) {
                    // Fallback: use full text if no name field
                    params.push(text.to_string());
                }
            }
            // Block: def foo(&block)
            "block_parameter" => {
                if let Some(name_node) = child.child_by_field_name("name") {
                    if let Ok(name) = name_node.utf8_text(content) {
                        params.push(format!("&{name}"));
                    }
                } else if let Ok(text) = child.utf8_text(content) {
                    // Fallback: use full text if no name field
                    params.push(text.to_string());
                }
            }
            // Keyword parameter: def foo(x:, y: 10)
            "keyword_parameter" => {
                if let Ok(text) = child.utf8_text(content) {
                    params.push(text.to_string());
                }
            }
            // Destructured parameter: def foo((a, b))
            "destructured_parameter" => {
                if let Ok(text) = child.utf8_text(content) {
                    params.push(text.to_string());
                }
            }
            // Forward parameter: def foo(...)
            "forward_parameter" => {
                params.push("...".to_string());
            }
            // Hash splat nil: def foo(**nil)
            "hash_splat_nil" => {
                params.push("**nil".to_string());
            }
            _ => {
                // Ignore other node types (e.g., punctuation)
            }
        }
    }

    if params.is_empty() {
        None
    } else {
        Some(params.join(", "))
    }
}

/// Extract return type from method definition.
///
/// Attempts to parse return type annotations from:
/// 1. Sorbet sig blocks: `sig { returns(Type) }`
/// 2. RBS inline comments: `#: (...) -> Type`
/// 3. YARD documentation: `@return [Type]`
///
/// # Arguments
/// * `method_node` - The method definition AST node
/// * `content` - Source file content
///
/// # Returns
/// Return type string if found, None otherwise
fn extract_return_type(method_node: Node<'_>, content: &[u8]) -> Option<String> {
    // Try Sorbet first
    if let Some(return_type) = extract_sorbet_return_type(method_node, content) {
        return Some(return_type);
    }

    // Try RBS inline comment
    if let Some(return_type) = extract_rbs_return_type(method_node, content) {
        return Some(return_type);
    }

    // Try YARD documentation
    if let Some(return_type) = extract_yard_return_type(method_node, content) {
        return Some(return_type);
    }

    None
}

/// Extract return type from Sorbet sig block.
///
/// Looks for `sig { returns(Type) }` before method definition.
///
/// # Arguments
/// * `method_node` - The method definition AST node
/// * `content` - Source file content
///
/// # Returns
/// Return type if found in sig block
fn extract_sorbet_return_type(method_node: Node<'_>, content: &[u8]) -> Option<String> {
    // Look for previous sibling that is a call to 'sig'
    let mut sibling = method_node.prev_sibling()?;

    // Skip comments and whitespace
    while sibling.kind() == "comment" {
        sibling = sibling.prev_sibling()?;
    }

    // Check if this is a sig call
    if sibling.kind() == "call"
        && let Some(method_name) = sibling.child_by_field_name("method")
        && let Ok(name_text) = method_name.utf8_text(content)
        && name_text == "sig"
    {
        // Look for block with returns call
        if let Some(block_node) = sibling.child_by_field_name("block") {
            return extract_returns_from_sig_block(block_node, content);
        }
    }

    None
}

/// Extract return type from sig block's `returns()` call.
fn extract_returns_from_sig_block(block_node: Node<'_>, content: &[u8]) -> Option<String> {
    let mut cursor = block_node.walk();

    for child in block_node.named_children(&mut cursor) {
        if child.kind() == "call"
            && let Some(method_name) = child.child_by_field_name("method")
            && let Ok(name_text) = method_name.utf8_text(content)
            && name_text == "returns"
        {
            // Get the argument to returns()
            if let Some(args) = child.child_by_field_name("arguments") {
                let mut args_cursor = args.walk();
                for arg in args.named_children(&mut args_cursor) {
                    if arg.kind() != ","
                        && let Ok(type_text) = arg.utf8_text(content)
                    {
                        return Some(type_text.to_string());
                    }
                }
            }
        }
        // Recursively search in nested structures
        if let Some(nested_type) = extract_returns_from_sig_block(child, content) {
            return Some(nested_type);
        }
    }

    None
}

/// Extract return type from RBS inline comment.
///
/// Looks for `#: (...) -> Type` pattern as a child of method node.
///
/// # Arguments
/// * `method_node` - The method definition AST node
/// * `content` - Source file content
///
/// # Returns
/// Return type if found in RBS comment
fn extract_rbs_return_type(method_node: Node<'_>, content: &[u8]) -> Option<String> {
    // RBS comments are children of the method node
    let mut cursor = method_node.walk();
    for child in method_node.children(&mut cursor) {
        if child.kind() == "comment"
            && let Ok(comment_text) = child.utf8_text(content)
        {
            // Parse RBS inline comment: #: (...) -> Type
            // Require #: prefix to avoid false positives from regular comments
            if comment_text.trim_start().starts_with("#:") {
                // Find the top-level arrow (not nested inside parens/brackets/braces)
                if let Some(arrow_pos) = find_top_level_arrow(comment_text) {
                    let return_part = &comment_text[arrow_pos + 2..];
                    let return_type = return_part.trim().to_string();
                    if !return_type.is_empty() {
                        return Some(return_type);
                    }
                }
            }
        }
    }

    None
}

/// Find the position of the top-level `->` arrow (not nested in parens/brackets/braces).
///
/// Tracks depth of (), [], and {} to avoid selecting arrows inside nested types like proc types.
fn find_top_level_arrow(text: &str) -> Option<usize> {
    let chars: Vec<char> = text.chars().collect();
    let mut depth: i32 = 0;
    let mut i = 0;

    while i < chars.len() {
        match chars[i] {
            '(' | '[' | '{' => depth += 1,
            ')' | ']' | '}' => depth = depth.saturating_sub(1),
            '-' if i + 1 < chars.len() && chars[i + 1] == '>' && depth == 0 => {
                return Some(i);
            }
            _ => {}
        }
        i += 1;
    }

    None
}

/// Extract return type from YARD documentation.
///
/// Looks for `@return [Type]` in comment block before method.
///
/// # Arguments
/// * `method_node` - The method definition AST node
/// * `content` - Source file content
///
/// # Returns
/// Return type if found in YARD comment
fn extract_yard_return_type(method_node: Node<'_>, content: &[u8]) -> Option<String> {
    // Look for comment block before method
    let mut sibling_opt = method_node.prev_sibling();
    let method_start_row = method_node.start_position().row;

    // Collect all preceding comments that are adjacent to the method
    let mut comments = Vec::new();
    let mut expected_row = method_start_row;

    while let Some(sibling) = sibling_opt {
        if sibling.kind() == "comment" {
            let comment_end_row = sibling.end_position().row;

            // Check adjacency: comment should end on the line right before expected row
            // Allow at most 1 line gap (expected_row - 1 or expected_row)
            if comment_end_row + 1 >= expected_row {
                if let Ok(comment_text) = sibling.utf8_text(content) {
                    comments.push(comment_text);
                }
                expected_row = sibling.start_position().row;
                sibling_opt = sibling.prev_sibling();
            } else {
                // Gap too large, stop collecting
                break;
            }
        } else {
            break;
        }
    }

    // Search for @return [Type] pattern (reverse order since we collected backwards)
    for comment in comments.iter().rev() {
        if let Some(return_pos) = comment.find("@return") {
            let after_return = &comment[return_pos + 7..];
            // Find [Type] pattern
            if let Some(start_bracket) = after_return.find('[')
                && let Some(end_bracket) = after_return.find(']')
                && end_bracket > start_bracket
            {
                let return_type = &after_return[start_bracket + 1..end_bracket];
                return Some(return_type.trim().to_string());
            }
        }
    }

    None
}

/// Converts a tree-sitter Node to a sqry Span.
///
/// # Arguments
/// * `node` - AST node
///
/// # Returns
/// Span with start/end positions
fn span_from_node(node: Node<'_>) -> Span {
    span_from_points(node.start_position(), node.end_position())
}

/// Converts tree-sitter Points to a sqry Span.
///
/// # Arguments
/// * `start` - Start position (row, column)
/// * `end` - End position (row, column)
///
/// # Returns
/// Span covering the range
fn span_from_points(start: Point, end: Point) -> Span {
    Span::new(
        Position::new(start.row, start.column),
        Position::new(end.row, end.column),
    )
}

/// Checks if a node kind is a literal syntax delimiter.
///
/// Delimiters (parentheses, commas, brackets) are filtered when counting arguments.
///
/// # Arguments
/// * `kind` - Node kind string
///
/// # Returns
/// true if kind is a delimiter
fn is_literal_delimiter(kind: &str) -> bool {
    matches!(kind, "," | "(" | ")" | "[" | "]")
}

/// Parse controller DSL arguments using simple heuristics.
/// Returns (callbacks, only, except).
fn parse_controller_dsl_args(
    text: &str,
) -> (Vec<String>, Option<Vec<String>>, Option<Vec<String>>) {
    // Split callbacks head from kwargs tail (only:/except:)
    let mut head = text;
    let mut tail = "";
    if let Some(idx) = text.find("only:") {
        head = &text[..idx];
        tail = &text[idx..];
    } else if let Some(idx) = text.find("except:") {
        head = &text[..idx];
        tail = &text[idx..];
    }
    let callbacks = extract_symbol_list_from_args(head);
    let only = extract_kw_symbol_list(tail, "only:");
    let except = extract_kw_symbol_list(tail, "except:");
    (callbacks, only, except)
}

fn extract_symbol_list_from_args(text: &str) -> Vec<String> {
    let mut out = Vec::new();
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b':' {
            let start = i + 1;
            let mut j = start;
            while j < bytes.len() {
                let c = bytes[j] as char;
                if c.is_ascii_alphanumeric() || c == '_' {
                    j += 1;
                } else {
                    break;
                }
            }
            if j > start {
                out.push(text[start..j].to_string());
                i = j;
                continue;
            }
        }
        i += 1;
    }
    out
}

fn extract_kw_symbol_list(text: &str, kw: &str) -> Option<Vec<String>> {
    let pos = text.find(kw)?;
    let mut after = &text[pos + kw.len()..];
    // trim leading whitespace and commas
    after = after.trim_start_matches(|c: char| c.is_whitespace() || c == ',');
    if after.starts_with('[')
        && let Some(end) = after.find(']')
    {
        return Some(extract_symbol_list_from_args(&after[..=end]));
    }
    // single symbol
    if let Some(colon) = after.find(':') {
        let mut j = colon + 1;
        while j < after.len() {
            let ch = after.as_bytes()[j] as char;
            if ch.is_ascii_alphanumeric() || ch == '_' {
                j += 1;
            } else {
                break;
            }
        }
        if j > colon + 1 {
            return Some(vec![after[colon + 1..j].to_string()]);
        }
    }
    None
}

fn extract_symbols_from_node(node: Node<'_>, content: &[u8]) -> Vec<String> {
    let mut out = Vec::new();
    match node.kind() {
        "symbol" | "simple_symbol" => {
            if let Ok(t) = node_text(node, content) {
                out.push(t.trim_start_matches(':').to_string());
            }
        }
        "array" => {
            let mut c = node.walk();
            for ch in node.children(&mut c) {
                if matches!(ch.kind(), "symbol" | "simple_symbol")
                    && let Ok(t) = node_text(ch, content)
                {
                    out.push(t.trim_start_matches(':').to_string());
                }
            }
        }
        _ => {
            // For other nodes, fall back to text scan
            if let Some(txt) = node_text_raw(node, content) {
                out = extract_symbol_list_from_args(&txt);
            }
        }
    }
    out
}

/// Extract the module name from require arguments
fn extract_require_module_name(arguments: Node<'_>, content: &[u8]) -> Option<String> {
    let mut cursor = arguments.walk();
    for child in arguments.children(&mut cursor) {
        if !child.is_named() {
            continue;
        }
        if let Some(s) = extract_string_content(child, content) {
            return Some(s);
        }
    }
    None
}

/// Extract string content from a string node (handles quotes)
fn extract_string_content(node: Node<'_>, content: &[u8]) -> Option<String> {
    let text = node.utf8_text(content).ok()?;
    let trimmed = text.trim();

    // Handle quoted strings
    if ((trimmed.starts_with('"') && trimmed.ends_with('"'))
        || (trimmed.starts_with('\'') && trimmed.ends_with('\'')))
        && trimmed.len() >= 2
    {
        return Some(trimmed[1..trimmed.len() - 1].to_string());
    }

    // For string nodes, look for string_content child
    if matches!(node.kind(), "string" | "chained_string") {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "string_content"
                && let Ok(s) = child.utf8_text(content)
            {
                return Some(s.to_string());
            }
        }
    }

    None
}

/// Resolve Ruby require path to canonical identifier
///
/// For `require_relative`, incorporates the source file's directory to produce
/// a unique, stable identifier that won't collide across different directories.
///
/// # Examples
///
/// - `a/file.rb` with `require_relative "util"` -> `a::util`
/// - `b/file.rb` with `require_relative "util"` -> `b::util`
/// - `require "json"` -> `json`
pub(crate) fn resolve_ruby_require(
    module_name: &str,
    is_relative: bool,
    source_file: &str,
) -> String {
    if is_relative {
        // For require_relative, resolve relative to the source file's directory.
        // This ensures `a/file.rb: require_relative "util"` and `b/file.rb: require_relative "util"`
        // produce distinct identifiers `a::util` and `b::util`.
        let source_path = std::path::Path::new(source_file);
        let source_dir = source_path.parent().unwrap_or(std::path::Path::new(""));

        // Join the source directory with the relative path
        let relative_path = std::path::Path::new(module_name);
        let resolved = source_dir.join(relative_path);

        // Normalize to collapse `.` and `..` components
        let normalized = normalize_path(&resolved);

        // Convert to canonical identifier using :: separators
        // Handle both Unix (/) and Windows (\) path separators
        let path_str = normalized.to_string_lossy();
        let separators: &[char] = &['/', '\\'];
        path_str
            .split(separators)
            .filter(|s| !s.is_empty())
            .collect::<Vec<_>>()
            .join("::")
    } else {
        // require 'json' -> json
        // require 'active_support/core_ext' -> active_support::core_ext
        module_name.replace('/', "::")
    }
}

/// Normalize a path by resolving `.` and `..` components without filesystem access
fn normalize_path(path: &std::path::Path) -> std::path::PathBuf {
    let mut components = Vec::new();

    for component in path.components() {
        match component {
            std::path::Component::CurDir => {
                // Skip `.` components
            }
            std::path::Component::ParentDir => {
                // Pop for `..` if there's something to pop (and it's not another ..)
                if components
                    .last()
                    .is_some_and(|c| *c != std::path::Component::ParentDir)
                {
                    components.pop();
                } else {
                    components.push(component);
                }
            }
            _ => {
                components.push(component);
            }
        }
    }

    components.iter().collect()
}

// ============================================================================
// YARD Annotation Processing - TypeOf and Reference Edges
// ============================================================================

/// Process YARD annotations for `TypeOf` and Reference edges
/// Recursively walks the tree looking for nodes with YARD comments
fn process_yard_annotations(
    node: Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
) -> GraphResult<()> {
    match node.kind() {
        "method" => {
            process_method_yard(node, content, helper)?;
        }
        "singleton_method" => {
            process_singleton_method_yard(node, content, helper)?;
        }
        "call" | "command" | "command_call" => {
            // Check if this is an attr_reader/attr_writer/attr_accessor call
            if is_attr_call(node, content) {
                process_attr_yard(node, content, helper)?;
            }
        }
        "assignment" => {
            // Check if this is an instance variable assignment
            if is_instance_variable_assignment(node, content) {
                process_assignment_yard(node, content, helper)?;
            }
        }
        _ => {}
    }

    // Recurse into children
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        process_yard_annotations(child, content, helper)?;
    }

    Ok(())
}

/// Process YARD for method definitions
fn process_method_yard(
    method_node: Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
) -> GraphResult<()> {
    // Extract YARD comment
    let Some(yard_text) = extract_yard_comment(method_node, content) else {
        return Ok(());
    };

    // Parse YARD tags
    let tags = parse_yard_tags(&yard_text);

    // Get method name
    let Some(name_node) = method_node.child_by_field_name("name") else {
        return Ok(());
    };

    let method_name = name_node
        .utf8_text(content)
        .map_err(|_| GraphBuilderError::ParseError {
            span: span_from_node(method_node),
            reason: "failed to read method name".to_string(),
        })?
        .trim()
        .to_string();

    if method_name.is_empty() {
        return Ok(());
    }

    // Find the class name by walking up the tree
    let class_name = get_enclosing_class_name(method_node, content);

    // Create qualified method name
    let qualified_name = if let Some(class_name) = class_name {
        format!("{class_name}#{method_name}")
    } else {
        method_name.clone()
    };

    // Get or create method node
    let method_node_id = helper.ensure_method(&qualified_name, None, false, false);

    // Process @param tags
    for (param_idx, param_tag) in tags.params.iter().enumerate() {
        // Create TypeOf edge: method -> parameter type
        let canonical_type = canonical_type_string(&param_tag.type_str);
        let type_node_id = helper.add_type(&canonical_type, None);
        helper.add_typeof_edge_with_context(
            method_node_id,
            type_node_id,
            Some(TypeOfContext::Parameter),
            param_idx.try_into().ok(),
            Some(&param_tag.name),
        );

        // Create Reference edges: method -> each referenced type
        let type_names = extract_type_names(&param_tag.type_str);
        for type_name in type_names {
            let ref_type_id = helper.add_type(&type_name, None);
            helper.add_reference_edge(method_node_id, ref_type_id);
        }
    }

    // Process @return tag
    if let Some(return_type) = &tags.returns {
        let canonical_type = canonical_type_string(return_type);
        let type_node_id = helper.add_type(&canonical_type, None);
        helper.add_typeof_edge_with_context(
            method_node_id,
            type_node_id,
            Some(TypeOfContext::Return),
            Some(0),
            None,
        );

        // Create Reference edges for return type
        let type_names = extract_type_names(return_type);
        for type_name in type_names {
            let ref_type_id = helper.add_type(&type_name, None);
            helper.add_reference_edge(method_node_id, ref_type_id);
        }
    }

    Ok(())
}

/// Process YARD for singleton method definitions (class methods)
fn process_singleton_method_yard(
    method_node: Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
) -> GraphResult<()> {
    // Extract YARD comment
    let Some(yard_text) = extract_yard_comment(method_node, content) else {
        return Ok(());
    };

    // Parse YARD tags
    let tags = parse_yard_tags(&yard_text);

    // Get method name
    let Some(name_node) = method_node.child_by_field_name("name") else {
        return Ok(());
    };

    let method_name = name_node
        .utf8_text(content)
        .map_err(|_| GraphBuilderError::ParseError {
            span: span_from_node(method_node),
            reason: "failed to read method name".to_string(),
        })?
        .trim()
        .to_string();

    if method_name.is_empty() {
        return Ok(());
    }

    // Find the class name
    let class_name = get_enclosing_class_name(method_node, content);

    // Create qualified method name (singleton methods use . separator)
    let qualified_name = if let Some(class_name) = class_name {
        format!("{class_name}.{method_name}")
    } else {
        method_name.clone()
    };

    // Get or create method node (singleton method)
    let method_node_id = helper.ensure_method(&qualified_name, None, false, true);

    // Process @param tags
    for (param_idx, param_tag) in tags.params.iter().enumerate() {
        // Create TypeOf edge: method -> parameter type
        let canonical_type = canonical_type_string(&param_tag.type_str);
        let type_node_id = helper.add_type(&canonical_type, None);
        helper.add_typeof_edge_with_context(
            method_node_id,
            type_node_id,
            Some(TypeOfContext::Parameter),
            param_idx.try_into().ok(),
            Some(&param_tag.name),
        );

        // Create Reference edges: method -> each referenced type
        let type_names = extract_type_names(&param_tag.type_str);
        for type_name in type_names {
            let ref_type_id = helper.add_type(&type_name, None);
            helper.add_reference_edge(method_node_id, ref_type_id);
        }
    }

    // Process @return tag
    if let Some(return_type) = &tags.returns {
        let canonical_type = canonical_type_string(return_type);
        let type_node_id = helper.add_type(&canonical_type, None);
        helper.add_typeof_edge_with_context(
            method_node_id,
            type_node_id,
            Some(TypeOfContext::Return),
            Some(0),
            None,
        );

        // Create Reference edges for return type
        let type_names = extract_type_names(return_type);
        for type_name in type_names {
            let ref_type_id = helper.add_type(&type_name, None);
            helper.add_reference_edge(method_node_id, ref_type_id);
        }
    }

    Ok(())
}

/// Process YARD for `attr_reader/attr_writer/attr_accessor` declarations
#[allow(clippy::unnecessary_wraps)]
fn process_attr_yard(
    attr_node: Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
) -> GraphResult<()> {
    // Extract YARD comment
    let Some(yard_text) = extract_yard_comment(attr_node, content) else {
        return Ok(());
    };

    // Parse YARD tags
    let tags = parse_yard_tags(&yard_text);

    // Only process @return tags for attr declarations
    let Some(var_type) = &tags.returns else {
        return Ok(());
    };

    // Extract attribute names from the call arguments
    let attr_names = extract_attr_names(attr_node, content);

    if attr_names.is_empty() {
        return Ok(());
    }

    // Find the class name
    let class_name = get_enclosing_class_name(attr_node, content);

    // Process each attribute name
    for attr_name in attr_names {
        // Create qualified name for the attribute
        let qualified_name = if let Some(ref class) = class_name {
            format!("{class}#{attr_name}")
        } else {
            attr_name.clone()
        };

        // Create variable node for the attribute
        let attr_node_id = helper.add_variable(&qualified_name, None);

        // Create TypeOf edge: variable -> type
        let canonical_type = canonical_type_string(var_type);
        let type_node_id = helper.add_type(&canonical_type, None);
        helper.add_typeof_edge_with_context(
            attr_node_id,
            type_node_id,
            Some(TypeOfContext::Field),
            None,
            Some(&attr_name),
        );

        // Create Reference edges: variable -> each referenced type
        let type_names = extract_type_names(var_type);
        for type_name in type_names {
            let ref_type_id = helper.add_type(&type_name, None);
            helper.add_reference_edge(attr_node_id, ref_type_id);
        }
    }

    Ok(())
}

/// Process YARD for instance variable assignments
fn process_assignment_yard(
    assignment_node: Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
) -> GraphResult<()> {
    // Extract YARD comment
    let Some(yard_text) = extract_yard_comment(assignment_node, content) else {
        return Ok(());
    };

    // Parse YARD tags
    let tags = parse_yard_tags(&yard_text);

    // Only process @type tags for assignments
    let Some(var_type) = &tags.type_annotation else {
        return Ok(());
    };

    // Get the variable name (instance variable like @username)
    let Some(left_node) = assignment_node.child_by_field_name("left") else {
        return Ok(());
    };

    if left_node.kind() != "instance_variable" {
        return Ok(());
    }

    let var_name = left_node
        .utf8_text(content)
        .map_err(|_| GraphBuilderError::ParseError {
            span: span_from_node(assignment_node),
            reason: "failed to read variable name".to_string(),
        })?
        .trim()
        .to_string();

    if var_name.is_empty() {
        return Ok(());
    }

    // Find the class name
    let class_name = get_enclosing_class_name(assignment_node, content);

    // Create qualified name for the instance variable
    let qualified_name = if let Some(class) = class_name {
        format!("{class}#{var_name}")
    } else {
        var_name.clone()
    };

    // Create variable node
    let var_node_id = helper.add_variable(&qualified_name, None);

    // Create TypeOf edge: variable -> type
    let canonical_type = canonical_type_string(var_type);
    let type_node_id = helper.add_type(&canonical_type, None);
    helper.add_typeof_edge_with_context(
        var_node_id,
        type_node_id,
        Some(TypeOfContext::Variable),
        None,
        Some(&var_name),
    );

    // Create Reference edges: variable -> each referenced type
    let type_names = extract_type_names(var_type);
    for type_name in type_names {
        let ref_type_id = helper.add_type(&type_name, None);
        helper.add_reference_edge(var_node_id, ref_type_id);
    }

    Ok(())
}

/// Check if a node is an `attr_reader/attr_writer/attr_accessor` call
fn is_attr_call(node: Node, content: &[u8]) -> bool {
    let method_name = match node.kind() {
        "command" => node
            .child_by_field_name("name")
            .and_then(|n| n.utf8_text(content).ok()),
        "call" | "command_call" => node
            .child_by_field_name("method")
            .and_then(|n| n.utf8_text(content).ok()),
        _ => None,
    };

    method_name
        .is_some_and(|name| matches!(name.trim(), "attr_reader" | "attr_writer" | "attr_accessor"))
}

/// Check if an assignment is to an instance variable
fn is_instance_variable_assignment(node: Node, _content: &[u8]) -> bool {
    if let Some(left_node) = node.child_by_field_name("left") {
        left_node.kind() == "instance_variable"
    } else {
        false
    }
}

/// Extract attribute names from `attr_reader/attr_writer/attr_accessor` arguments
/// Supports both symbol and string arguments
fn extract_attr_names(attr_node: Node, content: &[u8]) -> Vec<String> {
    let mut names = Vec::new();

    // Get arguments (could be inline for command nodes)
    let arguments = attr_node.child_by_field_name("arguments");

    if let Some(args) = arguments {
        // Process argument list
        let mut cursor = args.walk();
        for child in args.children(&mut cursor) {
            if matches!(child.kind(), "symbol" | "simple_symbol")
                && let Ok(text) = child.utf8_text(content)
            {
                let cleaned = text.trim().trim_start_matches(':');
                if !cleaned.is_empty() {
                    names.push(cleaned.to_string());
                }
            } else if child.kind() == "string"
                && let Ok(text) = child.utf8_text(content)
            {
                // Handle string arguments: attr_reader "name"
                let cleaned = text
                    .trim()
                    .trim_start_matches(['\'', '"'])
                    .trim_end_matches(['\'', '"']);
                if !cleaned.is_empty() {
                    names.push(cleaned.to_string());
                }
            }
        }
    } else if matches!(attr_node.kind(), "command" | "command_call") {
        // For command/command_call nodes, symbols/strings might be direct children
        let mut cursor = attr_node.walk();
        let mut found_method = false;
        for child in attr_node.children(&mut cursor) {
            if !child.is_named() {
                continue;
            }
            // Skip the method name itself
            if !found_method {
                found_method = true;
                continue;
            }
            // Extract symbol arguments
            if matches!(child.kind(), "symbol" | "simple_symbol")
                && let Ok(text) = child.utf8_text(content)
            {
                let cleaned = text.trim().trim_start_matches(':');
                if !cleaned.is_empty() {
                    names.push(cleaned.to_string());
                }
            } else if child.kind() == "string"
                && let Ok(text) = child.utf8_text(content)
            {
                // Handle string arguments: attr_reader "name"
                let cleaned = text
                    .trim()
                    .trim_start_matches(['\'', '"'])
                    .trim_end_matches(['\'', '"']);
                if !cleaned.is_empty() {
                    names.push(cleaned.to_string());
                }
            }
        }
    }

    names
}

/// Get the fully qualified enclosing class/module name for a node
/// Returns the full namespace path (e.g., "`MyModule::MyClass`") by walking
/// up the parent chain and collecting all enclosing module/class names.
/// Handles absolute constants (e.g., `class ::Foo`) by detecting leading `::`.
fn get_enclosing_class_name(node: Node, content: &[u8]) -> Option<String> {
    let mut current = node;
    let mut namespace_parts = Vec::new();

    // Walk up the tree to collect all enclosing class/module names
    while let Some(parent) = current.parent() {
        if matches!(parent.kind(), "class" | "module") {
            // Extract the name of this class/module
            if let Some(name_node) = parent.child_by_field_name("name")
                && let Ok(name_text) = name_node.utf8_text(content)
            {
                let trimmed = name_text.trim();
                // Check for absolute constant (starts with ::)
                if trimmed.starts_with("::") {
                    // Absolute constant - stop accumulating parents
                    namespace_parts.clear();
                    namespace_parts.push(trimmed.trim_start_matches("::").to_string());
                    break;
                }
                // Add to the beginning of the list (outermost first)
                namespace_parts.insert(0, trimmed.to_string());
            }
        }
        current = parent;
    }

    // Join all namespace parts with "::"
    if namespace_parts.is_empty() {
        None
    } else {
        Some(namespace_parts.join("::"))
    }
}