forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelixir.py
More file actions
460 lines (409 loc) · 18.4 KB
/
Copy pathelixir.py
File metadata and controls
460 lines (409 loc) · 18.4 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
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
from codegraphcontext.utils.debug_log import warning_logger
# In Elixir's tree-sitter grammar, most constructs are `call` nodes
# with identifiers like `defmodule`, `def`, `defp`, etc.
ELIXIR_QUERIES = {
"modules": """
(call
target: (identifier) @keyword
(arguments (alias) @name)
(do_block)
) @module_node
""",
"functions": """
(call
target: (identifier) @keyword
(arguments
(call
target: (identifier) @name
)
)
) @function_node
""",
"imports": """
(call
target: (identifier) @keyword
(arguments (alias) @path)
) @import_node
""",
"calls": """
(call
target: (dot
left: (_) @receiver
right: (identifier) @name
)
(arguments) @args
) @call_node
""",
"simple_calls": """
(call
target: (identifier) @name
(arguments) @args
) @call_node
""",
"module_attributes": """
(unary_operator
operator: "@"
operand: (call
target: (identifier) @attr_name
(arguments (_) @attr_value)
)
) @attribute
""",
"comments": """
(comment) @comment
""",
}
# Keywords that define modules/namespaces
MODULE_KEYWORDS = {"defmodule", "defprotocol", "defimpl"}
# Keywords that define functions
FUNCTION_KEYWORDS = {"def", "defp", "defmacro", "defmacrop", "defguard", "defguardp", "defdelegate"}
# Keywords that represent imports/dependencies
IMPORT_KEYWORDS = {"use", "import", "alias", "require"}
# Keywords to exclude from general call detection
ELIXIR_KEYWORDS = MODULE_KEYWORDS | FUNCTION_KEYWORDS | IMPORT_KEYWORDS | {
"quote", "unquote", "case", "cond", "if", "unless", "for", "with",
"try", "receive", "raise", "reraise", "throw", "super",
}
class ElixirTreeSitterParser:
"""An Elixir-specific parser using tree-sitter."""
def __init__(self, generic_parser_wrapper: Any):
self.generic_parser_wrapper = generic_parser_wrapper
self.language_name = "elixir"
self.language = generic_parser_wrapper.language
self.parser = generic_parser_wrapper.parser
self.index_source = False
def _get_node_text(self, node: Any) -> str:
return node.text.decode("utf-8")
def _get_parent_context(self, node: Any):
"""Find parent module or function context."""
curr = node.parent
while curr:
if curr.type == 'call':
# Check if it's a defmodule/def/defp etc
for child in curr.children:
if child.type == 'identifier':
keyword = self._get_node_text(child)
if keyword in MODULE_KEYWORDS:
# Get module name from arguments
for arg_child in curr.children:
if arg_child.type == 'arguments':
for ac in arg_child.children:
if ac.type == 'alias':
return self._get_node_text(ac), 'module', curr.start_point[0] + 1
elif keyword in FUNCTION_KEYWORDS:
for arg_child in curr.children:
if arg_child.type == 'arguments':
for ac in arg_child.children:
if ac.type == 'call':
name_node = ac.child_by_field_name('target')
if name_node:
return self._get_node_text(name_node), 'function', curr.start_point[0] + 1
break
curr = curr.parent
return None, None, None
def _enclosing_module_name(self, node: Any) -> Optional[str]:
"""Find the enclosing module name."""
curr = node.parent
while curr:
if curr.type == 'call':
for child in curr.children:
if child.type == 'identifier':
keyword = self._get_node_text(child)
if keyword in MODULE_KEYWORDS:
for arg_child in curr.children:
if arg_child.type == 'arguments':
for ac in arg_child.children:
if ac.type == 'alias':
return self._get_node_text(ac)
break
curr = curr.parent
return None
def _calculate_complexity(self, node: Any) -> int:
"""Calculate cyclomatic complexity for Elixir constructs."""
complexity_keywords = {
"if", "unless", "case", "cond", "with", "for", "try",
"receive", "and", "or", "&&", "||", "when",
}
count = 1
def traverse(n):
nonlocal count
if n.type == 'identifier' and self._get_node_text(n) in complexity_keywords:
count += 1
elif n.type in ('binary_operator',):
op_text = self._get_node_text(n)
if '&&' in op_text or '||' in op_text or ' and ' in op_text or ' or ' in op_text:
count += 1
for child in n.children:
traverse(child)
traverse(node)
return count
def _get_docstring(self, node: Any) -> Optional[str]:
"""Extract @doc or @moduledoc attribute as docstring."""
prev = node.prev_sibling
while prev:
if prev.type == 'unary_operator':
text = self._get_node_text(prev)
if text.startswith('@doc') or text.startswith('@moduledoc'):
return text.strip()
elif prev.type == 'comment':
return self._get_node_text(prev).strip()
else:
break
prev = prev.prev_sibling
return None
def parse(self, path: Path, is_dependency: bool = False, index_source: bool = False) -> Dict[str, Any]:
"""Parses an Elixir file and returns its structure."""
self.index_source = index_source
with open(path, "r", encoding="utf-8", errors="ignore") as f:
source_code = f.read()
tree = self.parser.parse(bytes(source_code, "utf8"))
root_node = tree.root_node
functions = self._find_functions(root_node)
modules = self._find_modules(root_node)
imports = self._find_imports(root_node)
function_calls = self._find_calls(root_node)
variables = [] # Elixir uses pattern matching, not traditional assignments
return {
"path": str(path),
"functions": functions,
"classes": [], # Elixir doesn't have classes
"variables": variables,
"imports": imports,
"function_calls": function_calls,
"is_dependency": is_dependency,
"lang": self.language_name,
"modules": modules,
}
def _find_modules(self, root_node: Any) -> list[Dict[str, Any]]:
"""Find all defmodule, defprotocol, defimpl definitions."""
modules = []
self._find_modules_recursive(root_node, modules)
return modules
def _find_modules_recursive(self, node: Any, modules: list):
"""Recursively find module definitions."""
if node.type == 'call':
keyword = None
name = None
has_do_block = False
for child in node.children:
if child.type == 'identifier':
kw = self._get_node_text(child)
if kw in MODULE_KEYWORDS:
keyword = kw
elif child.type == 'arguments' and keyword:
for ac in child.children:
if ac.type == 'alias':
name = self._get_node_text(ac)
break
elif child.type == 'do_block':
has_do_block = True
if keyword and name and has_do_block:
module_data = {
"name": name,
"line_number": node.start_point[0] + 1,
"end_line": node.end_point[0] + 1,
"lang": self.language_name,
"is_dependency": False,
"type": keyword, # defmodule, defprotocol, defimpl
}
if self.index_source:
module_data["source"] = self._get_node_text(node)
modules.append(module_data)
for child in node.children:
self._find_modules_recursive(child, modules)
def _find_functions(self, root_node: Any) -> list[Dict[str, Any]]:
"""Find all def, defp, defmacro, etc. definitions."""
functions = []
self._find_functions_recursive(root_node, functions)
return functions
def _find_functions_recursive(self, node: Any, functions: list):
"""Recursively find function definitions."""
if node.type == 'call':
keyword = None
func_name = None
args = []
for child in node.children:
if child.type == 'identifier':
kw = self._get_node_text(child)
if kw in FUNCTION_KEYWORDS:
keyword = kw
elif child.type == 'arguments' and keyword:
for ac in child.children:
if ac.type == 'call':
# The function head: name(arg1, arg2)
target = ac.child_by_field_name('target')
if target:
func_name = self._get_node_text(target)
# Extract arguments
for acc in ac.children:
if acc.type == 'arguments':
for arg in acc.children:
if arg.type not in (',', '(', ')'):
args.append(self._get_node_text(arg))
elif ac.type == 'identifier' and not func_name:
# Zero-arity function: def my_func, do: ...
func_name = self._get_node_text(ac)
if keyword and func_name:
module_name = self._enclosing_module_name(node)
docstring = self._get_docstring(node)
func_data = {
"name": func_name,
"line_number": node.start_point[0] + 1,
"end_line": node.end_point[0] + 1,
"args": args,
"lang": self.language_name,
"is_dependency": False,
"visibility": "private" if keyword.endswith("p") else "public",
"type": keyword, # def, defp, defmacro, etc.
}
if self.index_source:
func_data["source"] = self._get_node_text(node)
func_data["docstring"] = docstring
if module_name:
func_data["context"] = module_name
func_data["context_type"] = "module"
func_data["class_context"] = module_name
functions.append(func_data)
for child in node.children:
self._find_functions_recursive(child, functions)
def _find_imports(self, root_node: Any) -> list[Dict[str, Any]]:
"""Find all use, import, alias, require statements."""
imports = []
self._find_imports_recursive(root_node, imports)
return imports
def _find_imports_recursive(self, node: Any, imports: list):
"""Recursively find import-like statements."""
if node.type == 'call':
keyword = None
path = None
for child in node.children:
if child.type == 'identifier':
kw = self._get_node_text(child)
if kw in IMPORT_KEYWORDS:
keyword = kw
elif child.type == 'arguments' and keyword:
for ac in child.children:
if ac.type == 'alias':
path = self._get_node_text(ac)
break
if keyword and path:
imports.append({
"name": path,
"full_import_name": f"{keyword} {path}",
"line_number": node.start_point[0] + 1,
"alias": path.split('.')[-1] if keyword == 'alias' else None,
"lang": self.language_name,
"is_dependency": False,
"import_type": keyword, # use, import, alias, require
})
for child in node.children:
self._find_imports_recursive(child, imports)
def _find_calls(self, root_node: Any) -> list[Dict[str, Any]]:
"""Find all function calls (excluding def/defmodule/etc keywords)."""
calls = []
self._find_calls_recursive(root_node, calls)
return calls
def _find_calls_recursive(self, node: Any, calls: list):
"""Recursively find function calls."""
if node.type == 'call':
target = None
receiver = None
name = None
args = []
for child in node.children:
if child.type == 'dot':
# Module.function() style call
left = child.child_by_field_name('left')
right = child.child_by_field_name('right')
if left:
receiver = self._get_node_text(left)
if right:
name = self._get_node_text(right)
elif child.type == 'identifier' and target is None:
target = self._get_node_text(child)
elif child.type == 'arguments':
for arg in child.children:
if arg.type not in (',', '(', ')'):
args.append(self._get_node_text(arg))
# Dot-call: Module.func(args)
if name and receiver:
full_name = f"{receiver}.{name}"
context_name, context_type, context_line = self._get_parent_context(node)
class_context = context_name if context_type == 'module' else None
if context_type == 'function':
class_context = self._enclosing_module_name(node)
calls.append({
"name": name,
"full_name": full_name,
"line_number": node.start_point[0] + 1,
"args": args,
"inferred_obj_type": receiver,
"context": (context_name, context_type, context_line),
"class_context": class_context,
"lang": self.language_name,
"is_dependency": False,
})
# Simple call: func(args), but skip Elixir keywords
elif target and target not in ELIXIR_KEYWORDS:
context_name, context_type, context_line = self._get_parent_context(node)
class_context = context_name if context_type == 'module' else None
if context_type == 'function':
class_context = self._enclosing_module_name(node)
calls.append({
"name": target,
"full_name": target,
"line_number": node.start_point[0] + 1,
"args": args,
"inferred_obj_type": None,
"context": (context_name, context_type, context_line),
"class_context": class_context,
"lang": self.language_name,
"is_dependency": False,
})
for child in node.children:
self._find_calls_recursive(child, calls)
def pre_scan_elixir(files: list[Path], parser_wrapper) -> dict:
"""Scans Elixir files to create a map of module/function names to their file paths."""
imports_map = {}
for path in files:
try:
with open(path, "r", encoding="utf-8") as f:
source = f.read()
tree = parser_wrapper.parser.parse(bytes(source, "utf8"))
_pre_scan_recursive(tree.root_node, path, imports_map)
except Exception as e:
warning_logger(f"Tree-sitter pre-scan failed for {path}: {e}")
return imports_map
def _pre_scan_recursive(node, path: Path, imports_map: dict):
"""Recursively scan for module and function names."""
if node.type == 'call':
for child in node.children:
if child.type == 'identifier':
keyword = child.text.decode('utf-8')
if keyword in MODULE_KEYWORDS:
# Get module name
for sib in node.children:
if sib.type == 'arguments':
for ac in sib.children:
if ac.type == 'alias':
name = ac.text.decode('utf-8')
if name not in imports_map:
imports_map[name] = []
imports_map[name].append(str(path.resolve()))
elif keyword in FUNCTION_KEYWORDS:
# Get function name
for sib in node.children:
if sib.type == 'arguments':
for ac in sib.children:
if ac.type == 'call':
target = ac.child_by_field_name('target')
if target:
name = target.text.decode('utf-8')
if name not in imports_map:
imports_map[name] = []
imports_map[name].append(str(path.resolve()))
break
for child in node.children:
_pre_scan_recursive(child, path, imports_map)