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
//! C# parser implementation using tree-sitter
//!
//! This module provides comprehensive C# language parsing with support for:
//! - Symbol extraction (classes, interfaces, structs, enums, methods, properties, fields, events)
//! - Method call detection with proper caller context tracking
//! - Interface implementation tracking
//! - Using directive (import) tracking
//! - Visibility modifier handling (public, private, internal, protected)
//! - Signature extraction for methods and types
//! - Namespace/module path tracking
//!
//! **Tree-sitter ABI Version**: ABI-14 (tree-sitter-c-sharp 0.23.1)
//! **Total supported node types**: 503
//!
//! # Architecture
//!
//! The parser maintains scope context while traversing the AST to correctly
//! identify which method/class is making each call. This is critical for
//! relationship resolution.
//!
//! # Limitations
//!
//! - Type usage tracking (find_uses) is not yet implemented
//! - Define relationships (containment) are not yet implemented
//! - External framework references (e.g., System.Console) require special handling
use crate::parsing::Import;
use crate::parsing::parser::check_recursion_depth;
use crate::parsing::{
HandledNode, LanguageParser, MethodCall, NodeTracker, NodeTrackingState, ParserContext,
ScopeType,
};
use crate::types::SymbolCounter;
use crate::{FileId, Range, Symbol, SymbolKind, Visibility};
use std::any::Any;
use std::collections::HashSet;
use tree_sitter::{Language, Node, Parser};
/// C# language parser using tree-sitter
///
/// This parser traverses C# Abstract Syntax Trees (AST) to extract symbols,
/// relationships, and other code intelligence data.
///
/// # Fields
///
/// - `parser`: The underlying tree-sitter parser configured for C#
/// - `context`: Tracks current scope (class, method) during traversal for proper caller identification
/// - `node_tracker`: Prevents duplicate processing of tree-sitter nodes
///
/// # Example Usage
///
/// ```no_run
/// use codanna::parsing::csharp::parser::CSharpParser;
/// use codanna::parsing::LanguageParser;
///
/// let mut parser = CSharpParser::new().expect("Failed to create parser");
/// let code = "class Foo { void Bar() { } }";
/// // Parse and extract symbols...
/// ```
pub struct CSharpParser {
parser: Parser,
context: ParserContext,
node_tracker: NodeTrackingState,
}
impl CSharpParser {
/// Helper to create a symbol with all optional fields
fn create_symbol(
&self,
id: crate::types::SymbolId,
name: String,
kind: SymbolKind,
file_id: FileId,
range: Range,
signature: Option<String>,
doc_comment: Option<String>,
module_path: &str,
visibility: Visibility,
) -> Symbol {
let mut symbol = Symbol::new(id, name, kind, file_id, range);
if let Some(sig) = signature {
symbol = symbol.with_signature(sig);
}
if let Some(doc) = doc_comment {
symbol = symbol.with_doc(doc);
}
if !module_path.is_empty() {
symbol = symbol.with_module_path(module_path);
}
symbol = symbol.with_visibility(visibility);
// Set scope context based on parser's current scope
symbol.scope_context = Some(self.context.current_scope_context());
symbol
}
/// Parse C# source code and extract all symbols
pub fn parse(
&mut self,
code: &str,
file_id: FileId,
symbol_counter: &mut SymbolCounter,
) -> Vec<Symbol> {
// Reset context for each file
self.context = ParserContext::new();
let mut symbols = Vec::new();
match self.parser.parse(code, None) {
Some(tree) => {
let root_node = tree.root_node();
self.extract_symbols_from_node(
root_node,
code,
file_id,
symbol_counter,
&mut symbols,
"", // Module path will be determined by behavior
0,
);
}
None => {
eprintln!("Failed to parse C# file");
}
}
symbols
}
/// Create a new C# parser
pub fn new() -> Result<Self, String> {
let mut parser = Parser::new();
let language: Language = tree_sitter_c_sharp::LANGUAGE.into();
parser
.set_language(&language)
.map_err(|e| format!("Failed to set C# language: {e}"))?;
Ok(Self {
parser,
context: ParserContext::new(),
node_tracker: NodeTrackingState::new(),
})
}
/// Extract symbols from a C# node
fn extract_symbols_from_node(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
depth: usize,
) {
// Guard against stack overflow
if !check_recursion_depth(depth, node) {
return;
}
match node.kind() {
// Namespace declarations
"namespace_declaration" | "file_scoped_namespace_declaration" => {
self.register_handled_node(node.kind(), node.kind_id());
if let Some(namespace_path) = self.extract_namespace_name(node, code) {
// Process all children in this namespace context
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
&namespace_path,
depth + 1,
);
}
}
}
// Class declarations
"class_declaration" => {
// Register ALL child nodes for audit tracking
self.register_node_recursively(node);
let class_name = self.extract_type_name(node, code);
if let Some(symbol) = self.process_class(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
// Enter class scope for processing members
self.context.enter_scope(ScopeType::Class);
let saved_class = self.context.current_class().map(|s| s.to_string());
self.context.set_current_class(class_name.clone());
// Extract class members
self.extract_class_members(
node,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
self.context.exit_scope();
self.context.set_current_class(saved_class);
}
}
// Interface declarations
"interface_declaration" => {
// Register ALL child nodes for audit tracking
self.register_node_recursively(node);
if let Some(symbol) =
self.process_interface(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
// Process interface members
self.context.enter_scope(ScopeType::Class);
self.extract_interface_members(
node,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
self.context.exit_scope();
}
}
// Struct declarations
"struct_declaration" => {
// Register ALL child nodes for audit tracking
self.register_node_recursively(node);
if let Some(symbol) = self.process_struct(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
// Process struct members
self.context.enter_scope(ScopeType::Class);
self.extract_class_members(
node,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
self.context.exit_scope();
}
}
// Enum declarations
"enum_declaration" => {
// Register ALL child nodes for audit tracking
self.register_node_recursively(node);
if let Some(symbol) = self.process_enum(node, code, file_id, counter, module_path) {
symbols.push(symbol);
// Process enum members
self.extract_enum_members(node, code, file_id, counter, symbols, module_path);
}
}
// Record declarations (C# 9+)
"record_declaration" => {
// Register ALL child nodes for audit tracking
self.register_node_recursively(node);
if let Some(symbol) = self.process_record(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
// Process record members
self.context.enter_scope(ScopeType::Class);
self.extract_class_members(
node,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
self.context.exit_scope();
}
}
// Delegate declarations
"delegate_declaration" => {
// Register ALL child nodes for audit tracking
self.register_node_recursively(node);
if let Some(symbol) =
self.process_delegate(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
// Method declarations (standalone or in classes)
"method_declaration" => {
// Register ALL child nodes for audit tracking
self.register_node_recursively(node);
if let Some(symbol) = self.process_method(node, code, file_id, counter, module_path)
{
let method_name = symbol.name.to_string();
symbols.push(symbol);
// Process method body for local functions with proper caller context
self.context
.enter_scope(ScopeType::Function { hoisting: false });
self.context.set_current_function(Some(method_name));
self.extract_method_body(node, code, file_id, counter, symbols, module_path);
self.context.exit_scope();
}
}
// Local function statements
"local_function_statement" => {
self.register_handled_node(node.kind(), node.kind_id());
if let Some(symbol) =
self.process_local_function(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
// Field declarations
"field_declaration" => {
self.register_handled_node(node.kind(), node.kind_id());
self.process_field_declaration(node, code, file_id, counter, symbols, module_path);
}
// Property declarations
"property_declaration" => {
self.register_handled_node(node.kind(), node.kind_id());
if let Some(symbol) =
self.process_property(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
// Event declarations
"event_declaration" | "event_field_declaration" => {
self.register_handled_node(node.kind(), node.kind_id());
if let Some(symbol) = self.process_event(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
// Constructor declarations
"constructor_declaration" => {
self.register_handled_node(node.kind(), node.kind_id());
if let Some(symbol) =
self.process_constructor(node, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
// Variable declarations
"variable_declaration" | "local_declaration_statement" => {
self.register_handled_node(node.kind(), node.kind_id());
self.process_variable_declaration(
node,
code,
file_id,
counter,
symbols,
module_path,
);
}
// Default case: recursively process children
_ => {
self.register_handled_node(node.kind(), node.kind_id());
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
}
}
}
/// Extract namespace name from namespace declaration
fn extract_namespace_name(&self, node: Node, code: &str) -> Option<String> {
if let Some(name_node) = node.child_by_field_name("name") {
Some(code[name_node.byte_range()].to_string())
} else {
// Fallback: look for qualified_name child
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "qualified_name" || child.kind() == "identifier" {
return Some(code[child.byte_range()].to_string());
}
}
None
}
}
/// Extract type name (for classes, interfaces, structs, etc.)
fn extract_type_name(&self, node: Node, code: &str) -> Option<String> {
if let Some(name_node) = node.child_by_field_name("name") {
Some(code[name_node.byte_range()].to_string())
} else {
// Fallback: look for identifier child
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "identifier" {
return Some(code[child.byte_range()].to_string());
}
}
None
}
}
/// Process class declaration
fn process_class(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_class_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Class,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Process interface declaration
fn process_interface(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_interface_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Interface,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Process struct declaration
fn process_struct(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_struct_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Struct,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Process enum declaration
fn process_enum(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_enum_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Enum,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Process record declaration (C# 9+)
fn process_record(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_record_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Class, // Records are class-like in C#
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Process delegate declaration
fn process_delegate(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_delegate_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Function, // Delegates are function types
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
/// Extract class signature (including generics, base classes, interfaces)
fn extract_class_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "class_body")
}
/// Extract interface signature
fn extract_interface_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "interface_body")
}
/// Extract struct signature
fn extract_struct_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "struct_body")
}
/// Extract enum signature
fn extract_enum_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "enum_body")
}
/// Extract record signature
fn extract_record_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "record_body")
}
/// Extract delegate signature
fn extract_delegate_signature(&self, node: Node, code: &str) -> String {
// Delegates don't have bodies, so extract the full node
code[node.byte_range()].trim().to_string()
}
/// Extract method signature (including return type, parameters, generics)
fn extract_method_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "method_body")
}
/// Extract property signature (including type and accessors)
fn extract_property_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "accessor_list")
}
/// Extract constructor signature
fn extract_constructor_signature(&self, node: Node, code: &str) -> String {
self.extract_signature_excluding_body(node, code, "constructor_body")
}
/// Extract field signature
fn extract_field_signature(&self, node: Node, code: &str) -> String {
// Field declarations don't have bodies, but we want just the declaration part
code[node.byte_range()].trim().to_string()
}
/// Extract enum member signature
fn extract_enum_member_signature(&self, node: Node, code: &str) -> String {
// Enum members can have values like "Red = 1" or just "Red"
code[node.byte_range()].trim().to_string()
}
/// Extract event signature
fn extract_event_signature(&self, node: Node, code: &str) -> String {
// Events can have custom add/remove accessors, but often are just simple declarations
self.extract_signature_excluding_body(node, code, "accessor_list")
}
/// Extract variable signature
fn extract_variable_signature(&self, node: Node, code: &str) -> String {
// Variables are declared like "int x = 5;" or "var name = value;"
code[node.byte_range()].trim().to_string()
}
/// Extract calls recursively with function context tracking (TypeScript pattern)
fn extract_calls_recursive<'a>(
node: &Node,
code: &'a str,
current_function: Option<&'a str>,
calls: &mut Vec<(&'a str, &'a str, Range)>,
) {
// Handle function context - track which function we're inside
let function_context = if matches!(
node.kind(),
"method_declaration"
| "constructor_declaration"
| "property_declaration"
| "local_function_statement"
) {
// Extract function name
node.child_by_field_name("name")
.or_else(|| {
// Fallback: find first identifier child
let mut cursor = node.walk();
node.children(&mut cursor)
.find(|n| n.kind() == "identifier")
})
.map(|name_node| &code[name_node.byte_range()])
} else {
// Not a function, inherit current context
current_function
};
// Handle invocation expressions with proper caller context
if node.kind() == "invocation_expression" {
if let Some(expression_node) = node.child(0) {
let caller = function_context.unwrap_or("");
let callee = match expression_node.kind() {
"member_access_expression" => {
// obj.Method() - extract just method name for resolution
// The receiver info is captured by find_method_calls() for richer context
expression_node
.child_by_field_name("name")
.map(|n| &code[n.byte_range()])
.unwrap_or(&code[expression_node.byte_range()])
}
"identifier" => {
// Simple method call like "DoSomething()"
&code[expression_node.byte_range()]
}
_ => &code[expression_node.byte_range()],
};
let range = Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
);
calls.push((caller, callee, range));
}
}
// Recursively process children with inherited or updated context
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
Self::extract_calls_recursive(&child, code, function_context, calls);
}
}
/// Extract method calls from a node tree with proper caller context
///
/// This method traverses the AST and identifies method invocations while maintaining
/// proper scope context. It tracks which class and method the call originates from,
/// which is essential for relationship resolution.
///
/// # Key Features
///
/// - Maintains scope stack (class -> method) during traversal
/// - Correctly identifies caller for each method invocation
/// - Handles both member access (`obj.Method()`) and simple calls (`Method()`)
/// - Extracts receiver information for member access patterns
///
/// # Arguments
///
/// - `node`: Current AST node being processed
/// - `code`: Source code string for extracting text
/// - `method_calls`: Output vector to collect method calls
fn extract_method_calls_from_node(
&mut self,
node: Node,
code: &str,
method_calls: &mut Vec<MethodCall>,
) {
match node.kind() {
// Track scope changes to maintain caller context
"class_declaration"
| "struct_declaration"
| "record_declaration"
| "interface_declaration" => {
// Extract class/struct name
let type_name = node
.children(&mut node.walk())
.find(|child| child.kind() == "identifier")
.map(|child| code[child.byte_range()].to_string())
.unwrap_or_else(|| "Unknown".to_string());
self.context.enter_scope(ScopeType::Class);
self.context.set_current_class(Some(type_name));
// Recursively process children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_method_calls_from_node(child, code, method_calls);
}
self.context.exit_scope();
}
"method_declaration" | "constructor_declaration" | "property_declaration" => {
// Extract method name
let method_name = node
.children(&mut node.walk())
.find(|child| child.kind() == "identifier")
.map(|child| code[child.byte_range()].to_string())
.unwrap_or_else(|| "Unknown".to_string());
self.context.enter_scope(ScopeType::function());
self.context.set_current_function(Some(method_name));
// Recursively process children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_method_calls_from_node(child, code, method_calls);
}
self.context.exit_scope();
}
"invocation_expression" => {
// Get caller from current scope context
let caller = self
.context
.current_function()
.or_else(|| self.context.current_class())
.unwrap_or("unknown");
if let Some(expression_node) = node.child(0) {
match expression_node.kind() {
"member_access_expression" => {
// obj.Method() calls
if let Some(object_node) =
expression_node.child_by_field_name("expression")
{
if let Some(name_node) = expression_node.child_by_field_name("name")
{
let receiver = code[object_node.byte_range()].to_string();
let method = code[name_node.byte_range()].to_string();
let range = Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
);
method_calls.push(
MethodCall::new(caller, &method, range)
.with_receiver(&receiver),
);
}
}
}
"identifier" => {
// Simple method calls like Method()
let method = code[expression_node.byte_range()].to_string();
let range = Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
);
method_calls.push(
MethodCall::new(caller, &method, range).with_receiver("this"),
);
}
_ => {}
}
}
}
_ => {
// Recursively check children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.extract_method_calls_from_node(child, code, method_calls);
}
}
}
}
/// Extract interface implementations from a node tree
fn extract_implementations_from_node<'a>(
node: Node,
code: &'a str,
implementations: &mut Vec<(&'a str, &'a str, Range)>,
) {
match node.kind() {
"class_declaration" | "struct_declaration" | "record_declaration" => {
// Find class name first (identifier child of the class declaration)
let mut class_name = "";
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "identifier" {
class_name = &code[child.byte_range()];
break;
}
}
// Find base_list
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "base_list" {
// Extract interfaces from base list
let mut base_cursor = child.walk();
for base_child in child.children(&mut base_cursor) {
if base_child.kind() == "identifier"
|| base_child.kind() == "generic_name"
{
let interface_name = &code[base_child.byte_range()];
// Filter out base classes (heuristic: interfaces start with 'I')
if interface_name.starts_with('I') && interface_name.len() > 1 {
let range = Range::new(
base_child.start_position().row as u32,
base_child.start_position().column as u16,
base_child.end_position().row as u32,
base_child.end_position().column as u16,
);
implementations.push((class_name, interface_name, range));
}
}
}
break;
}
}
}
_ => {
// Recursively check children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
Self::extract_implementations_from_node(child, code, implementations);
}
}
}
}
/// Extract imports from a node tree
fn extract_imports_from_node(
node: Node,
code: &str,
file_id: FileId,
imports: &mut Vec<Import>,
) {
match node.kind() {
"using_directive" => {
// Try standard field extraction first
if let Some(name_node) = node.child_by_field_name("name") {
let import_path = code[name_node.byte_range()].to_string();
imports.push(Import {
path: import_path,
alias: None,
file_id,
is_glob: false,
is_type_only: false,
});
} else {
// Fallback: tree-sitter-c-sharp doesn't consistently expose "name" field
// for using_directive nodes. Iterate child nodes to find qualified_name
// or identifier nodes directly.
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "qualified_name" || child.kind() == "identifier" {
let import_path = code[child.byte_range()].to_string();
imports.push(Import {
path: import_path,
alias: None,
file_id,
is_glob: false,
is_type_only: false,
});
break;
}
}
}
}
_ => {
// Recursively check children
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
Self::extract_imports_from_node(child, code, file_id, imports);
}
}
}
}
/// Helper to extract signature excluding the body
fn extract_signature_excluding_body(&self, node: Node, code: &str, body_kind: &str) -> String {
let start = node.start_byte();
let mut end = node.end_byte();
// Find the body and stop before it
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == body_kind {
end = child.start_byte();
break;
}
}
code[start..end].trim().to_string()
}
/// Extract variable type declarations from a node tree (recursive helper)
///
/// This method recursively traverses the syntax tree looking for variable declarations
/// and extracts variable→type mappings. These mappings are crucial for resolving
/// method calls on local variables (e.g., `helper.DoWork()` where `helper` is a local variable).
///
/// ## Tree-sitter C# Grammar
///
/// The C# tree-sitter grammar represents variable declarations as either:
/// - `local_declaration_statement` - for local variables inside methods
/// - `variable_declaration` - the actual declaration node containing type and declarators
///
/// Example AST structure for `var helper = new Helper();`:
/// ```text
/// local_declaration_statement
/// └── variable_declaration
/// ├── implicit_type ("var")
/// └── variable_declarator
/// ├── identifier ("helper")
/// ├── =
/// └── object_creation_expression ("new Helper()")
/// ```
///
/// ## Supported Patterns
///
/// - `var x = new Type()` - Infers type from initializer
/// - `Type x = new Type()` - Uses explicit type annotation
/// - `var x = expr` - For qualified types (when expr type is explicit)
///
/// ## Parameters
///
/// * `node` - Current AST node being processed
/// * `code` - Source code as string slice
/// * `bindings` - Accumulated list of (variable_name, type_name, range) tuples
///
/// ## Returns
///
/// Returns tuples of (variable_name, type_name, range) via the `bindings` parameter
fn find_variable_types_in_node<'a>(
&self,
node: &Node,
code: &'a str,
bindings: &mut Vec<(&'a str, &'a str, Range)>,
) {
// Match both node types directly (same pattern as extract_symbols_from_node:304)
// We need to handle both because the tree structure can vary
if node.kind() == "variable_declaration" || node.kind() == "local_declaration_statement" {
self.extract_variable_bindings(node, code, bindings);
}
// Recurse into all children to find nested variable declarations
// (e.g., variables inside nested blocks, loops, etc.)
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.find_variable_types_in_node(&child, code, bindings);
}
}
/// Extract variable bindings from a variable_declaration node
///
/// This method processes a single variable declaration and extracts all variable→type
/// mappings from it. A single declaration can contain multiple variables (e.g.,
/// `var x = new A(), y = new B();`).
///
/// ## Strategy
///
/// 1. **Type from initializer** (preferred): If the variable has a `new Type()` initializer,
/// extract the type from there. This handles the `var` keyword case.
/// 2. **Explicit type** (fallback): If no initializer or not a `new` expression, use the
/// explicit type annotation (but skip "var" since we can't infer the type without initializer).
///
/// ## Examples
///
/// - `var helper = new Helper()` → (helper, Helper) - type inferred from initializer
/// - `Helper helper = new Helper()` → (helper, Helper) - explicit type used
/// - `IService service = factory.Create()` → (service, IService) - explicit type used (can't infer from method call)
/// - `var x = 5` → skipped (no type info available for primitives without full type inference)
///
/// ## Parameters
///
/// * `var_decl` - The variable_declaration or local_declaration_statement node
/// * `code` - Source code as string slice
/// * `bindings` - Output list of (variable_name, type_name, range) tuples
fn extract_variable_bindings<'a>(
&self,
var_decl: &Node,
code: &'a str,
bindings: &mut Vec<(&'a str, &'a str, Range)>,
) {
// Variable declaration structure in tree-sitter C#:
// variable_declaration has:
// - type field: could be "implicit_type" (var) or explicit type like "Helper", "List<T>", etc.
// - variable_declarator children: one or more declarators (the actual variables)
let type_node = var_decl.child_by_field_name("type");
let mut cursor = var_decl.walk();
for child in var_decl.children(&mut cursor) {
if child.kind() == "variable_declarator" {
// Each variable_declarator represents one variable in the declaration
// Structure: identifier = initializer
// Example: in "var x = new A(), y = new B()", there are two variable_declarators
if let Some(name_node) = child.child_by_field_name("name") {
// Ensure the name is a simple identifier (not a pattern)
if name_node.kind() != "identifier" {
continue;
}
let var_name = &code[name_node.byte_range()];
// Strategy 1: Try to extract type from initializer (handles "var" keyword)
// In tree-sitter C#, object_creation_expression is a direct child of variable_declarator
// Example structure: variable_declarator -> [identifier, "=", object_creation_expression]
let mut init_expr = None;
let mut sub_cursor = child.walk();
for vchild in child.children(&mut sub_cursor) {
if vchild.kind() == "object_creation_expression" {
init_expr = Some(vchild);
break;
}
}
if let Some(init_node) = init_expr {
// Found a "new Type()" expression - extract the type
if let Some(type_name) =
self.extract_type_from_initializer(&init_node, code)
{
let range = Range::new(
child.start_position().row as u32,
child.start_position().column as u16,
child.end_position().row as u32,
child.end_position().column as u16,
);
bindings.push((var_name, type_name, range));
continue; // Successfully extracted, move to next variable
}
}
// Strategy 2: Fall back to explicit type annotation
// This handles cases like "Helper helper = ..." or "IService service = factory.Create()"
if let Some(type_node) = type_node {
let type_str = &code[type_node.byte_range()];
// Skip "var" keyword - we can't infer type without analyzing the full expression
// (which would require complex type inference beyond current scope)
if type_str != "var" {
let range = Range::new(
child.start_position().row as u32,
child.start_position().column as u16,
child.end_position().row as u32,
child.end_position().column as u16,
);
bindings.push((var_name, type_str, range));
}
}
}
}
}
}
/// Extract type name from an initializer expression
///
/// Handles:
/// - `new Type()` → Some("Type")
/// - `new Generic<T>()` → Some("Generic")
/// - `new Namespace.Type()` → Some("Type")
fn extract_type_from_initializer<'a>(
&self,
init_node: &Node,
code: &'a str,
) -> Option<&'a str> {
// Look for object_creation_expression
if init_node.kind() == "object_creation_expression" {
// object_creation_expression has a 'type' field
if let Some(type_node) = init_node.child_by_field_name("type") {
return Some(self.extract_simple_type_name(&type_node, code));
}
}
None
}
/// Extract simple type name from a type node, handling qualified names and generics
///
/// Examples:
/// - `Helper` → "Helper"
/// - `List<T>` → "List"
/// - `System.Collections.List` → "List"
fn extract_simple_type_name<'a>(&self, type_node: &Node, code: &'a str) -> &'a str {
match type_node.kind() {
"identifier" => &code[type_node.byte_range()],
"generic_name" => {
// Generic name has an identifier child
if let Some(ident) = type_node.child_by_field_name("name") {
&code[ident.byte_range()]
} else {
&code[type_node.byte_range()]
}
}
"qualified_name" => {
// Take the last identifier (rightmost part)
let mut cursor = type_node.walk();
let mut last_ident = None;
for child in type_node.children(&mut cursor) {
if child.kind() == "identifier" {
last_ident = Some(&code[child.byte_range()]);
}
}
last_ident.unwrap_or(&code[type_node.byte_range()])
}
_ => &code[type_node.byte_range()],
}
}
/// Determine visibility from modifiers
fn determine_visibility(&self, node: Node, code: &str) -> Visibility {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "modifier" {
let modifier_text = &code[child.byte_range()];
if modifier_text.contains("public") {
return Visibility::Public;
} else if modifier_text.contains("private") {
return Visibility::Private;
} else if modifier_text.contains("protected") {
return Visibility::Module; // Closest approximation
} else if modifier_text.contains("internal") {
return Visibility::Module;
}
}
}
// Default C# visibility rules
match self.context.current_scope_context() {
crate::symbol::ScopeContext::ClassMember { .. } => Visibility::Private, // Class members are private by default
_ => Visibility::Module, // Top-level types are internal by default
}
}
/// Extract documentation comment
fn extract_doc_comment(&self, node: &Node, code: &str) -> Option<String> {
// Collect all consecutive /// comments immediately before this node
let mut doc_lines = Vec::new();
let mut current = node.prev_sibling();
// Walk backwards through siblings, collecting /// comments
while let Some(sibling) = current {
if sibling.kind() == "comment" {
let comment_text = &code[sibling.byte_range()];
// C# XML documentation comments start with ///
if comment_text.starts_with("///") {
doc_lines.push(comment_text.to_string());
} else {
// Non-doc comment stops the sequence
break;
}
} else {
// Non-comment node stops the sequence
break;
}
current = sibling.prev_sibling();
}
if doc_lines.is_empty() {
None
} else {
// Reverse to restore original order (we walked backwards)
doc_lines.reverse();
Some(doc_lines.join("\n"))
}
}
// Placeholder implementations for member extraction methods
// These would be implemented similarly to the main symbol extraction
fn extract_class_members(
&mut self,
class_node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
depth: usize,
) {
// Find the class body
if let Some(body_node) = class_node.child_by_field_name("body") {
let mut cursor = body_node.walk();
for child in body_node.children(&mut cursor) {
match child.kind() {
"method_declaration" => {
if let Some(symbol) =
self.process_method(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
"property_declaration" => {
if let Some(symbol) =
self.process_property(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
"field_declaration" => {
self.process_field_declaration(
child,
code,
file_id,
counter,
symbols,
module_path,
);
}
"constructor_declaration" => {
if let Some(symbol) =
self.process_constructor(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
"event_declaration" | "event_field_declaration" => {
if let Some(symbol) =
self.process_event(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
// Nested types
"class_declaration"
| "interface_declaration"
| "struct_declaration"
| "enum_declaration" => {
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
_ => {
// Continue processing other nodes recursively
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
}
}
}
}
fn extract_interface_members(
&mut self,
interface_node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
depth: usize,
) {
// Find the interface body
if let Some(body_node) = interface_node.child_by_field_name("body") {
let mut cursor = body_node.walk();
for child in body_node.children(&mut cursor) {
match child.kind() {
"method_declaration" => {
if let Some(symbol) =
self.process_method(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
"property_declaration" => {
if let Some(symbol) =
self.process_property(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
"event_declaration" => {
if let Some(symbol) =
self.process_event(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
_ => {
// Continue processing other nodes recursively
self.extract_symbols_from_node(
child,
code,
file_id,
counter,
symbols,
module_path,
depth + 1,
);
}
}
}
}
}
fn extract_enum_members(
&mut self,
enum_node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
) {
// Find the enum body
if let Some(body_node) = enum_node.child_by_field_name("body") {
let mut cursor = body_node.walk();
for child in body_node.children(&mut cursor) {
if child.kind() == "enum_member_declaration" {
if let Some(name_node) = child.child_by_field_name("name") {
let name = code[name_node.byte_range()].to_string();
let signature = self.extract_enum_member_signature(child, code);
let doc_comment = self.extract_doc_comment(&child, code);
let symbol = self.create_symbol(
counter.next_id(),
name,
SymbolKind::Constant, // Enum members are constant values
file_id,
Range::new(
child.start_position().row as u32,
child.start_position().column as u16,
child.end_position().row as u32,
child.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
Visibility::Public, // Enum members are always public
);
symbols.push(symbol);
}
}
}
}
}
fn extract_method_body(
&mut self,
method_node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
) {
// Look for local functions and variable declarations within method body
if let Some(body_node) = method_node.child_by_field_name("body") {
let mut cursor = body_node.walk();
for child in body_node.children(&mut cursor) {
match child.kind() {
"local_function_statement" => {
if let Some(symbol) =
self.process_local_function(child, code, file_id, counter, module_path)
{
symbols.push(symbol);
}
}
"local_declaration_statement" => {
self.process_variable_declaration(
child,
code,
file_id,
counter,
symbols,
module_path,
);
}
_ => {
// Continue recursively for nested blocks
self.extract_method_body(
child,
code,
file_id,
counter,
symbols,
module_path,
);
}
}
}
}
}
fn process_method(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_method_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Method,
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
fn process_local_function(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_method_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Function, // Local functions are more like standalone functions
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
Visibility::Private, // Local functions are always private to their containing method
))
}
fn process_field_declaration(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
) {
// Field declarations can contain multiple variables
// e.g., "public int x, y, z;"
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "variable_declaration" {
// Extract each variable declarator
let mut var_cursor = child.walk();
for var_child in child.children(&mut var_cursor) {
if var_child.kind() == "variable_declarator" {
if let Some(name_node) = var_child.child_by_field_name("name") {
let name = code[name_node.byte_range()].to_string();
let signature = self.extract_field_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
let symbol = self.create_symbol(
counter.next_id(),
name,
SymbolKind::Variable,
file_id,
Range::new(
var_child.start_position().row as u32,
var_child.start_position().column as u16,
var_child.end_position().row as u32,
var_child.end_position().column as u16,
),
Some(signature.clone()),
doc_comment.clone(),
module_path,
visibility,
);
symbols.push(symbol);
}
}
}
}
}
}
fn process_property(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_property_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Field, // Properties are field-like in the symbol system
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
fn process_event(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_event_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Field, // Events are field-like (similar to properties)
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
fn process_constructor(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
module_path: &str,
) -> Option<Symbol> {
let name = self.extract_type_name(node, code)?;
let signature = self.extract_constructor_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let visibility = self.determine_visibility(node, code);
Some(self.create_symbol(
counter.next_id(),
name,
SymbolKind::Method, // Constructors are method-like
file_id,
Range::new(
node.start_position().row as u32,
node.start_position().column as u16,
node.end_position().row as u32,
node.end_position().column as u16,
),
Some(signature),
doc_comment,
module_path,
visibility,
))
}
fn process_variable_declaration(
&mut self,
node: Node,
code: &str,
file_id: FileId,
counter: &mut SymbolCounter,
symbols: &mut Vec<Symbol>,
module_path: &str,
) {
// Look for variable_declarator nodes within the declaration
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "variable_declarator" {
if let Some(name_node) = child.child_by_field_name("name") {
let name = code[name_node.byte_range()].to_string();
let signature = self.extract_variable_signature(node, code);
let doc_comment = self.extract_doc_comment(&node, code);
let symbol = self.create_symbol(
counter.next_id(),
name,
SymbolKind::Variable,
file_id,
Range::new(
child.start_position().row as u32,
child.start_position().column as u16,
child.end_position().row as u32,
child.end_position().column as u16,
),
Some(signature.clone()),
doc_comment.clone(),
module_path,
Visibility::Private, // Local variables are private
);
symbols.push(symbol);
}
}
}
}
/// Recursively register all nodes in the tree for audit tracking
///
/// This ensures the audit system can see which AST nodes we're actually handling,
/// making it easier to identify gaps in implementation.
fn register_node_recursively(&mut self, node: Node) {
self.register_handled_node(node.kind(), node.kind_id());
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.register_node_recursively(child);
}
}
}
impl NodeTracker for CSharpParser {
fn register_handled_node(&mut self, kind: &str, kind_id: u16) {
self.node_tracker.register_handled_node(kind, kind_id);
}
fn get_handled_nodes(&self) -> &HashSet<HandledNode> {
self.node_tracker.get_handled_nodes()
}
}
impl LanguageParser for CSharpParser {
fn parse(&mut self, code: &str, file_id: FileId, counter: &mut SymbolCounter) -> Vec<Symbol> {
self.parse(code, file_id, counter)
}
fn find_calls<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
let tree = match self.parser.parse(code, None) {
Some(tree) => tree,
None => {
eprintln!("Failed to parse C# file for calls");
return Vec::new();
}
};
let root_node = tree.root_node();
let mut calls = Vec::new();
// Use recursive extraction with function context tracking (like TypeScript)
Self::extract_calls_recursive(&root_node, code, None, &mut calls);
calls
}
fn find_method_calls(&mut self, code: &str) -> Vec<MethodCall> {
let mut method_calls = Vec::new();
// Reset context to ensure clean state
self.context = ParserContext::new();
match self.parser.parse(code, None) {
Some(tree) => {
let root_node = tree.root_node();
self.extract_method_calls_from_node(root_node, code, &mut method_calls);
}
None => {
eprintln!("Failed to parse C# file for method calls");
}
}
method_calls
}
fn find_implementations<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
let mut implementations = Vec::new();
match self.parser.parse(code, None) {
Some(tree) => {
let root_node = tree.root_node();
Self::extract_implementations_from_node(root_node, code, &mut implementations);
}
None => {
eprintln!("Failed to parse C# file for implementations");
}
}
implementations
}
fn find_uses<'a>(&mut self, _code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
// TODO: Implement proper type usage tracking for C#
//
// This should track where types are referenced/used, for example:
// - Method parameter types: `void DoWork(Helper helper)`
// - Return types: `Helper GetHelper()`
// - Field/property types: `private Helper _helper;`
// - Base classes/interfaces: `class Foo : IBar`
//
// Previously disabled because it was creating invalid relationships with "use" as context
// instead of proper semantic relationships.
//
// Implementation approach:
// 1. Traverse AST looking for type references
// 2. Extract (user_symbol, used_type, range) tuples
// 3. Filter out primitive types (int, string, etc.)
// 4. Ensure proper context (method names, not node kinds)
Vec::new()
}
fn find_defines<'a>(&mut self, _code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
// TODO: Implement proper defines tracking for C#
//
// This should track definition relationships, for example:
// - Variable definitions: `var x = 5;` → (containing_method, "x", range)
// - Field definitions: `private int _count;` → (containing_class, "_count", range)
// - Property definitions: `public string Name { get; set; }` → (containing_class, "Name", range)
//
// Previously disabled because it was using node.kind() instead of actual definer names,
// creating relationships like "variable_declaration defines x" instead of "Method defines x".
//
// Implementation approach:
// 1. Track current scope context (class, method, etc.)
// 2. For each definition, extract (definer_name, defined_symbol, range)
// 3. Ensure definer_name is the actual symbol name, not AST node type
Vec::new()
}
/// Extract variable type bindings from C# code
///
/// This method implements variable type tracking for C#, which is essential for
/// resolving method calls on local variables. Without this, codanna cannot resolve
/// relationships like `var service = new MyService(); service.DoWork();` because
/// it doesn't know that `service` is of type `MyService`.
///
/// ## How It Works
///
/// 1. Parse the C# file into an AST using tree-sitter-c-sharp
/// 2. Recursively traverse the tree looking for variable declarations
/// 3. For each variable, extract the type either from:
/// - The initializer expression (`new Type()`)
/// - The explicit type annotation
/// 4. Return list of (variable_name, type_name, source_location) tuples
///
/// ## Example
///
/// ```csharp
/// public void Example() {
/// var helper = new Helper(); // → ("helper", "Helper", Range)
/// helper.DoWork(); // Now codanna can resolve DoWork() on Helper type
/// }
/// ```
///
/// ## Limitations
///
/// - Only tracks variables with explicit type or `new Type()` initializers
/// - Does not perform full type inference (e.g., `var x = 5` is not tracked)
/// - Does not track method return types without explicit annotation
///
/// ## Returns
///
/// Vector of tuples: (variable_name, type_name, source_range)
/// where type_name is a string slice pointing into the original source code (zero-copy)
fn find_variable_types<'a>(&mut self, code: &'a str) -> Vec<(&'a str, &'a str, Range)> {
let mut bindings = Vec::new();
if let Some(tree) = self.parser.parse(code, None) {
let root = tree.root_node();
self.find_variable_types_in_node(&root, code, &mut bindings);
}
bindings
}
fn find_imports(&mut self, code: &str, file_id: FileId) -> Vec<Import> {
let mut imports = Vec::new();
match self.parser.parse(code, None) {
Some(tree) => {
let root_node = tree.root_node();
Self::extract_imports_from_node(root_node, code, file_id, &mut imports);
}
None => {
eprintln!("Failed to parse C# file for imports");
}
}
imports
}
fn extract_doc_comment(&self, node: &Node, code: &str) -> Option<String> {
self.extract_doc_comment(node, code)
}
fn language(&self) -> crate::parsing::Language {
crate::parsing::Language::CSharp
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{FileId, SymbolCounter};
#[test]
fn test_csharp_interface_implementation_tracking() {
let mut parser = CSharpParser::new().unwrap();
let code = r#"
public interface ILogger {
void Log(string message);
}
public class ConsoleLogger : ILogger {
public void Log(string message) {
Console.WriteLine(message);
}
}
"#;
let implementations = parser.find_implementations(code);
// Should find ConsoleLogger implements ILogger
assert!(
implementations
.iter()
.any(|(from, to, _)| *from == "ConsoleLogger" && *to == "ILogger"),
"Should detect ConsoleLogger implements ILogger. Found: {implementations:?}"
);
}
#[test]
fn test_csharp_method_call_tracking_with_context() {
let mut parser = CSharpParser::new().unwrap();
let code = r#"
public class Calculator {
private int Add(int a, int b) { return a + b; }
public int Calculate() {
return Add(5, 10);
}
}
"#;
let calls = parser.find_calls(code);
// Should find Calculate -> Add with proper caller context
assert!(
calls
.iter()
.any(|(from, to, _)| *from == "Calculate" && *to == "Add"),
"Should detect Calculate -> Add with caller context. Found: {:?}",
calls
.iter()
.map(|(f, t, _)| format!("{f} -> {t}"))
.collect::<Vec<_>>()
);
}
#[test]
fn test_csharp_enum_extraction() {
let mut parser = CSharpParser::new().unwrap();
let code = r#"
public enum Status {
Active,
Inactive,
Pending = 5
}
"#;
let file_id = FileId::new(1).unwrap();
let mut counter = SymbolCounter::new();
let symbols = parser.parse(code, file_id, &mut counter);
// Should extract enum and its members
assert!(
symbols
.iter()
.any(|s| s.name.as_ref() == "Status" && s.kind == SymbolKind::Enum)
);
assert!(symbols.iter().any(|s| s.name.as_ref() == "Active"));
assert!(symbols.iter().any(|s| s.name.as_ref() == "Pending"));
}
#[test]
fn test_csharp_multiline_doc_comment_extraction() {
let mut parser = CSharpParser::new().unwrap();
let code = r#"
/// <summary>
/// This is a multi-line
/// XML documentation comment
/// </summary>
public class DocumentedClass {
}
"#;
let file_id = FileId::new(1).unwrap();
let mut counter = SymbolCounter::new();
let symbols = parser.parse(code, file_id, &mut counter);
let class_symbol = symbols
.iter()
.find(|s| s.name.as_ref() == "DocumentedClass")
.unwrap();
let doc = class_symbol.doc_comment.as_ref().unwrap();
// Should capture all lines of XML documentation
assert!(doc.contains("<summary>"));
assert!(doc.contains("multi-line"));
assert!(doc.contains("</summary>"));
}
#[test]
fn test_csharp_method_calls_in_method() {
let mut parser = CSharpParser::new().unwrap();
let code = r#"
public class Service {
public void Process() {
Validate();
Transform();
Save();
}
private void Validate() { }
private void Transform() { }
private void Save() { }
}
"#;
let method_calls = parser.find_method_calls(code);
// Should find all three calls from Process method
assert!(
method_calls
.iter()
.any(|c| c.caller == "Process" && c.method_name == "Validate")
);
assert!(
method_calls
.iter()
.any(|c| c.caller == "Process" && c.method_name == "Transform")
);
assert!(
method_calls
.iter()
.any(|c| c.caller == "Process" && c.method_name == "Save")
);
}
#[test]
fn test_csharp_using_directive_extraction() {
let mut parser = CSharpParser::new().unwrap();
let code = r#"
using System;
using System.Collections.Generic;
using MyApp.Services;
namespace TestNamespace {
public class TestClass { }
}
"#;
let file_id = FileId::new(1).unwrap();
let imports = parser.find_imports(code, file_id);
// Should extract all using directives
assert!(
imports.len() >= 3,
"Should find at least 3 imports, found: {}",
imports.len()
);
assert!(imports.iter().any(|i| i.path == "System"));
assert!(
imports
.iter()
.any(|i| i.path == "System.Collections.Generic")
);
assert!(imports.iter().any(|i| i.path == "MyApp.Services"));
}
}