forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsharp.py
More file actions
551 lines (468 loc) · 22.1 KB
/
Copy pathcsharp.py
File metadata and controls
551 lines (468 loc) · 22.1 KB
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
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
import re
from codegraphcontext.utils.debug_log import debug_log, info_logger, error_logger, warning_logger
from codegraphcontext.utils.tree_sitter_manager import execute_query
CSHARP_QUERIES = {
"functions": """
(method_declaration
name: (identifier) @name
parameters: (parameter_list) @params
) @function_node
(constructor_declaration
name: (identifier) @name
parameters: (parameter_list) @params
) @function_node
(local_function_statement
name: (identifier) @name
parameters: (parameter_list) @params
) @function_node
""",
"classes": """
(class_declaration
name: (identifier) @name
(base_list)? @bases
) @class
""",
"interfaces": """
(interface_declaration
name: (identifier) @name
(base_list)? @bases
) @interface
""",
"structs": """
(struct_declaration
name: (identifier) @name
(base_list)? @bases
) @struct
""",
"enums": """
(enum_declaration
name: (identifier) @name
) @enum
""",
"records": """
(record_declaration
name: (identifier) @name
(base_list)? @bases
) @record
""",
"properties": """
(property_declaration
name: (identifier) @name
) @property
""",
"imports": """
(using_directive) @import
""",
"calls": """
(invocation_expression
function: [
(identifier) @name
(member_access_expression
name: (identifier) @name
)
]
)
(object_creation_expression
type: [
(identifier) @name
(qualified_name) @name
]
)
""",
}
class CSharpTreeSitterParser:
def __init__(self, generic_parser_wrapper: Any):
self.generic_parser_wrapper = generic_parser_wrapper
self.language_name = "c_sharp"
self.language = generic_parser_wrapper.language
self.parser = generic_parser_wrapper.parser
def parse(self, path: Path, is_dependency: bool = False, index_source: bool = False) -> Dict[str, Any]:
try:
self.index_source = index_source
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
source_code = f.read()
if not source_code.strip():
warning_logger(f"Empty or whitespace-only file: {path}")
return {
"path": str(path),
"functions": [],
"classes": [],
"interfaces": [],
"structs": [],
"enums": [],
"records": [],
"properties": [],
"variables": [],
"imports": [],
"function_calls": [],
"is_dependency": is_dependency,
"lang": self.language_name,
}
tree = self.parser.parse(bytes(source_code, "utf8"))
parsed_functions = []
parsed_classes = []
parsed_interfaces = []
parsed_structs = []
parsed_enums = []
parsed_records = []
parsed_properties = []
parsed_imports = []
parsed_calls = []
for capture_name, query_str in CSHARP_QUERIES.items():
captures = execute_query(self.language, query_str, tree.root_node)
if capture_name == "functions":
parsed_functions = self._parse_functions(captures, source_code, path, tree.root_node)
elif capture_name == "classes":
parsed_classes = self._parse_type_declarations(captures, source_code, path, "Class")
elif capture_name == "interfaces":
parsed_interfaces = self._parse_type_declarations(captures, source_code, path, "Interface")
elif capture_name == "structs":
parsed_structs = self._parse_type_declarations(captures, source_code, path, "Struct")
elif capture_name == "enums":
parsed_enums = self._parse_type_declarations(captures, source_code, path, "Enum")
elif capture_name == "records":
parsed_records = self._parse_type_declarations(captures, source_code, path, "Record")
elif capture_name == "properties":
parsed_properties = self._parse_properties(captures, source_code, path, tree.root_node)
elif capture_name == "imports":
parsed_imports = self._parse_imports(captures, source_code)
elif capture_name == "calls":
parsed_calls = self._parse_calls(captures, source_code)
return {
"path": str(path),
"functions": parsed_functions,
"classes": parsed_classes,
"interfaces": parsed_interfaces,
"structs": parsed_structs,
"enums": parsed_enums,
"records": parsed_records,
"properties": parsed_properties,
"variables": [],
"imports": parsed_imports,
"function_calls": parsed_calls,
"is_dependency": is_dependency,
"lang": self.language_name,
}
except Exception as e:
error_logger(f"Error parsing C# file {path}: {e}")
return {
"path": str(path),
"functions": [],
"classes": [],
"interfaces": [],
"structs": [],
"enums": [],
"records": [],
"properties": [],
"variables": [],
"imports": [],
"function_calls": [],
"is_dependency": is_dependency,
"lang": self.language_name,
}
def _parse_functions(self, captures: list, source_code: str, path: Path, root_node) -> list[Dict[str, Any]]:
functions = []
source_lines = source_code.splitlines()
for node, capture_name in captures:
if capture_name == "function_node":
try:
start_line = node.start_point[0] + 1
end_line = node.end_point[0] + 1
name_captures = [
(n, cn) for n, cn in captures
if cn == "name" and n.parent.id == node.id
]
if name_captures:
name_node = name_captures[0][0]
func_name = self._get_node_text(name_node)
params_captures = [
(n, cn) for n, cn in captures
if cn == "params" and n.parent.id == node.id
]
parameters = []
if params_captures:
params_node = params_captures[0][0]
parameters = self._extract_parameters(params_node)
# Extract attributes applied to this function
attributes = []
if node.parent and node.parent.type == "attribute_list":
attr_text = self._get_node_text(node.parent)
attributes.append(attr_text)
# Find containing class/struct/interface
class_context = self._find_containing_type(node, source_code)
source_text = self._get_node_text(node)
func_data = {
"name": func_name,
"args": parameters,
"attributes": attributes,
"line_number": start_line,
"end_line": end_line,
"path": str(path),
"lang": self.language_name,
}
# Add class context if found
if class_context:
func_data["class_context"] = class_context
if self.index_source:
func_data["source"] = source_text
functions.append(func_data)
except Exception as e:
error_logger(f"Error parsing function in {path}: {e}")
continue
return functions
def _parse_type_declarations(self, captures: list, source_code: str, path: Path, type_label: str) -> list[Dict[str, Any]]:
"""Parse class, interface, struct, enum, or record declarations with inheritance info."""
types = []
# Map capture names based on type
capture_map = {
"Class": "class",
"Interface": "interface",
"Struct": "struct",
"Enum": "enum",
"Record": "record"
}
expected_capture = capture_map.get(type_label, "class")
for node, capture_name in captures:
if capture_name == expected_capture:
try:
start_line = node.start_point[0] + 1
end_line = node.end_point[0] + 1
name_captures = [
(n, cn) for n, cn in captures
if cn == "name" and n.parent.id == node.id
]
if name_captures:
name_node = name_captures[0][0]
type_name = self._get_node_text(name_node)
# Extract base classes/interfaces
bases = []
bases_captures = [
(n, cn) for n, cn in captures
if cn == "bases" and n.parent.id == node.id
]
if bases_captures:
bases_node = bases_captures[0][0]
bases_text = self._get_node_text(bases_node)
# Parse base list: ": BaseClass, IInterface1, IInterface2"
bases_text = bases_text.strip().lstrip(':').strip()
if bases_text:
bases = [b.strip() for b in bases_text.split(',')]
source_text = self._get_node_text(node)
type_data = {
"name": type_name,
"line_number": start_line,
"end_line": end_line,
"path": str(path),
"lang": self.language_name,
}
# Add bases if found
if bases:
type_data["bases"] = bases
if self.index_source:
type_data["source"] = source_text
types.append(type_data)
except Exception as e:
error_logger(f"Error parsing {type_label} in {path}: {e}")
continue
return types
def _parse_imports(self, captures: list, source_code: str) -> list[dict]:
imports = []
for node, capture_name in captures:
if capture_name == "import":
try:
import_text = self._get_node_text(node)
# Match: using System.Collections.Generic; or using static System.Math;
import_match = re.search(r'using\s+(?:static\s+)?([^;]+)', import_text)
if import_match:
import_path = import_match.group(1).strip()
# Check for alias: using MyAlias = System.Collections.Generic.List<int>;
alias = None
if '=' in import_path:
parts = import_path.split('=')
alias = parts[0].strip()
import_path = parts[1].strip()
import_data = {
"name": import_path,
"full_import_name": import_path,
"line_number": node.start_point[0] + 1,
"alias": alias,
"context": (None, None),
"lang": self.language_name,
"is_dependency": False,
}
imports.append(import_data)
except Exception as e:
error_logger(f"Error parsing import: {e}")
continue
return imports
def _get_parent_context(self, node: Any, types: Tuple[str, ...] = ('class_declaration', 'struct_declaration', 'function_declaration', 'method_declaration')):
"""Find parent context for C# constructs."""
curr = node.parent
while curr:
if curr.type in types:
if curr.type in ('method_declaration', 'function_declaration'):
name_node = curr.child_by_field_name('name')
return self._get_node_text(name_node) if name_node else None, curr.type, curr.start_point[0] + 1
else:
# Classes, structs, etc.
name_node = curr.child_by_field_name('name')
return self._get_node_text(name_node) if name_node else None, curr.type, curr.start_point[0] + 1
curr = curr.parent
return None, None, None
def _get_node_text(self, node: Any) -> str:
if not node: return ""
return node.text.decode("utf-8")
def _parse_calls(self, captures: list, source_code: str) -> list[dict]:
calls = []
seen_calls = set()
for node, capture_name in captures:
if capture_name == "name":
try:
call_name = self._get_node_text(node)
line_number = node.start_point[0] + 1
# Avoid duplicates
call_key = f"{call_name}_{line_number}"
if call_key in seen_calls:
continue
seen_calls.add(call_key)
# Get context
context_name, context_type, context_line = self._get_parent_context(node)
class_context = context_name if context_type and 'class' in context_type else None
call_data = {
"name": call_name,
"full_name": call_name,
"line_number": line_number,
"args": [],
"inferred_obj_type": None,
"context": (context_name, context_type, context_line),
"class_context": class_context,
"lang": self.language_name,
"is_dependency": False,
}
calls.append(call_data)
except Exception as e:
error_logger(f"Error parsing call: {e}")
continue
return calls
def _extract_parameters(self, params_node) -> list[str]:
params = []
if not params_node:
return params
# Iterate over parameter nodes in the parameter list
for child in params_node.children:
if child.type == "parameter":
# find the identifier
name_node = child.child_by_field_name("name")
if name_node:
params.append(self._get_node_text(name_node))
else:
# Fallback: scan children for identifier if field name not present in this grammar version
for sub in child.children:
if sub.type == "identifier":
params.append(self._get_node_text(sub))
break
return params
def _find_containing_type(self, node, source_code):
"""Find the containing class, struct, interface, or record for a given node."""
current = node.parent
while current:
if current.type in ['class_declaration', 'struct_declaration', 'interface_declaration', 'record_declaration']:
# Find the name of this type
for child in current.children:
if child.type == 'identifier':
return self._get_node_text(child)
current = current.parent
return None
def _parse_properties(self, captures: list, source_code: str, path: Path, root_node) -> list[Dict[str, Any]]:
"""Parse C# properties."""
properties = []
for node, capture_name in captures:
if capture_name == "property":
try:
start_line = node.start_point[0] + 1
end_line = node.end_point[0] + 1
name_captures = [
(n, cn) for n, cn in captures
if cn == "name" and n.parent == node
]
if name_captures:
name_node = name_captures[0][0]
prop_name = self._get_node_text(name_node)
# Get property type from node children
prop_type = None
for child in node.children:
if child.type in ['predefined_type', 'identifier', 'generic_name', 'nullable_type', 'array_type']:
prop_type = self._get_node_text(child)
break
# Find containing class/struct
class_context = self._find_containing_type(node, source_code)
source_text = self._get_node_text(node)
prop_data = {
"name": prop_name,
"type": prop_type,
"line_number": start_line,
"end_line": end_line,
"path": str(path),
"lang": self.language_name,
}
if class_context:
prop_data["class_context"] = class_context
if self.index_source:
prop_data["source"] = source_text
properties.append(prop_data)
except Exception as e:
error_logger(f"Error parsing property in {path}: {e}")
continue
return properties
def pre_scan_csharp(files: list[Path], parser_wrapper) -> dict:
"""Pre-scan C# files to build a name-to-files mapping."""
name_to_files = {}
for path in files:
try:
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
# Match class declarations
class_matches = re.finditer(
r'\b(?:public\s+|private\s+|protected\s+|internal\s+)?(?:static\s+)?(?:abstract\s+)?(?:sealed\s+)?(?:partial\s+)?class\s+(\w+)',
content
)
for match in class_matches:
class_name = match.group(1)
if class_name not in name_to_files:
name_to_files[class_name] = []
name_to_files[class_name].append(str(path))
# Match interface declarations
interface_matches = re.finditer(
r'\b(?:public\s+|private\s+|protected\s+|internal\s+)?(?:partial\s+)?interface\s+(\w+)',
content
)
for match in interface_matches:
interface_name = match.group(1)
if interface_name not in name_to_files:
name_to_files[interface_name] = []
name_to_files[interface_name].append(str(path))
# Match struct declarations
struct_matches = re.finditer(
r'\b(?:public\s+|private\s+|protected\s+|internal\s+)?(?:readonly\s+)?(?:partial\s+)?struct\s+(\w+)',
content
)
for match in struct_matches:
struct_name = match.group(1)
if struct_name not in name_to_files:
name_to_files[struct_name] = []
name_to_files[struct_name].append(str(path))
# Match record declarations
record_matches = re.finditer(
r'\b(?:public\s+|private\s+|protected\s+|internal\s+)?(?:sealed\s+)?record\s+(?:class\s+)?(\w+)',
content
)
for match in record_matches:
record_name = match.group(1)
if record_name not in name_to_files:
name_to_files[record_name] = []
name_to_files[record_name].append(str(path))
except Exception as e:
error_logger(f"Error pre-scanning C# file {path}: {e}")
return name_to_files