code-graph-rag 0.0.79__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,427 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import TYPE_CHECKING
5
+
6
+ from . import constants as cs
7
+ from .models import FQNSpec, LanguageSpec
8
+
9
+ if TYPE_CHECKING:
10
+ from tree_sitter import Node
11
+
12
+
13
+ def _python_get_name(node: Node) -> str | None:
14
+ name_node = node.child_by_field_name("name")
15
+ return (
16
+ name_node.text.decode(cs.ENCODING_UTF8)
17
+ if name_node and name_node.text
18
+ else None
19
+ )
20
+
21
+
22
+ def _python_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
23
+ try:
24
+ rel = file_path.relative_to(repo_root)
25
+ parts = list(rel.with_suffix("").parts)
26
+ if parts and parts[-1] == cs.INDEX_INIT:
27
+ parts = parts[:-1]
28
+ return parts
29
+ except ValueError:
30
+ return []
31
+
32
+
33
+ def _js_get_name(node: Node) -> str | None:
34
+ if node.type in cs.JS_NAME_NODE_TYPES:
35
+ name_node = node.child_by_field_name(cs.FIELD_NAME)
36
+ return (
37
+ name_node.text.decode(cs.ENCODING_UTF8)
38
+ if name_node and name_node.text
39
+ else None
40
+ )
41
+ return None
42
+
43
+
44
+ def _js_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
45
+ try:
46
+ rel = file_path.relative_to(repo_root)
47
+ parts = list(rel.with_suffix("").parts)
48
+ if parts and parts[-1] == cs.INDEX_INDEX:
49
+ parts = parts[:-1]
50
+ return parts
51
+ except ValueError:
52
+ return []
53
+
54
+
55
+ def _generic_get_name(node: Node) -> str | None:
56
+ name_node = node.child_by_field_name("name")
57
+ if name_node and name_node.text:
58
+ return name_node.text.decode(cs.ENCODING_UTF8)
59
+
60
+ for field_name in cs.NAME_FIELDS:
61
+ name_node = node.child_by_field_name(field_name)
62
+ if name_node and name_node.text:
63
+ return name_node.text.decode(cs.ENCODING_UTF8)
64
+
65
+ return None
66
+
67
+
68
+ def _generic_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
69
+ try:
70
+ rel = file_path.relative_to(repo_root)
71
+ return list(rel.with_suffix("").parts)
72
+ except ValueError:
73
+ return []
74
+
75
+
76
+ def _rust_get_name(node: Node) -> str | None:
77
+ if node.type in cs.RS_TYPE_NODE_TYPES:
78
+ name_node = node.child_by_field_name(cs.FIELD_NAME)
79
+ if name_node and name_node.type == cs.TS_TYPE_IDENTIFIER and name_node.text:
80
+ return name_node.text.decode(cs.ENCODING_UTF8)
81
+ elif node.type in cs.RS_IDENT_NODE_TYPES:
82
+ name_node = node.child_by_field_name(cs.FIELD_NAME)
83
+ if name_node and name_node.type == cs.TS_IDENTIFIER and name_node.text:
84
+ return name_node.text.decode(cs.ENCODING_UTF8)
85
+
86
+ return _generic_get_name(node)
87
+
88
+
89
+ def _rust_file_to_module(file_path: Path, repo_root: Path) -> list[str]:
90
+ try:
91
+ rel = file_path.relative_to(repo_root)
92
+ parts = list(rel.with_suffix("").parts)
93
+ if parts and parts[-1] == cs.INDEX_MOD:
94
+ parts = parts[:-1]
95
+ return parts
96
+ except ValueError:
97
+ return []
98
+
99
+
100
+ def _cpp_get_name(node: Node) -> str | None:
101
+ if node.type in cs.CPP_NAME_NODE_TYPES:
102
+ name_node = node.child_by_field_name(cs.FIELD_NAME)
103
+ if name_node and name_node.text:
104
+ return name_node.text.decode(cs.ENCODING_UTF8)
105
+ elif node.type == cs.TS_CPP_FUNCTION_DEFINITION:
106
+ declarator = node.child_by_field_name(cs.FIELD_DECLARATOR)
107
+ if declarator and declarator.type == cs.TS_CPP_FUNCTION_DECLARATOR:
108
+ name_node = declarator.child_by_field_name(cs.FIELD_DECLARATOR)
109
+ if name_node and name_node.type == cs.TS_IDENTIFIER and name_node.text:
110
+ return name_node.text.decode(cs.ENCODING_UTF8)
111
+
112
+ return _generic_get_name(node)
113
+
114
+
115
+ PYTHON_FQN_SPEC = FQNSpec(
116
+ scope_node_types=frozenset(cs.FQN_PY_SCOPE_TYPES),
117
+ function_node_types=frozenset(cs.FQN_PY_FUNCTION_TYPES),
118
+ get_name=_python_get_name,
119
+ file_to_module_parts=_python_file_to_module,
120
+ )
121
+
122
+ JS_FQN_SPEC = FQNSpec(
123
+ scope_node_types=frozenset(cs.FQN_JS_SCOPE_TYPES),
124
+ function_node_types=frozenset(cs.FQN_JS_FUNCTION_TYPES),
125
+ get_name=_js_get_name,
126
+ file_to_module_parts=_js_file_to_module,
127
+ )
128
+
129
+ TS_FQN_SPEC = FQNSpec(
130
+ scope_node_types=frozenset(cs.FQN_TS_SCOPE_TYPES),
131
+ function_node_types=frozenset(cs.FQN_TS_FUNCTION_TYPES),
132
+ get_name=_js_get_name,
133
+ file_to_module_parts=_js_file_to_module,
134
+ )
135
+
136
+ RUST_FQN_SPEC = FQNSpec(
137
+ scope_node_types=frozenset(cs.FQN_RS_SCOPE_TYPES),
138
+ function_node_types=frozenset(cs.FQN_RS_FUNCTION_TYPES),
139
+ get_name=_rust_get_name,
140
+ file_to_module_parts=_rust_file_to_module,
141
+ )
142
+
143
+ JAVA_FQN_SPEC = FQNSpec(
144
+ scope_node_types=frozenset(cs.FQN_JAVA_SCOPE_TYPES),
145
+ function_node_types=frozenset(cs.FQN_JAVA_FUNCTION_TYPES),
146
+ get_name=_generic_get_name,
147
+ file_to_module_parts=_generic_file_to_module,
148
+ )
149
+
150
+ CPP_FQN_SPEC = FQNSpec(
151
+ scope_node_types=frozenset(cs.FQN_CPP_SCOPE_TYPES),
152
+ function_node_types=frozenset(cs.FQN_CPP_FUNCTION_TYPES),
153
+ get_name=_cpp_get_name,
154
+ file_to_module_parts=_generic_file_to_module,
155
+ )
156
+
157
+ LUA_FQN_SPEC = FQNSpec(
158
+ scope_node_types=frozenset(cs.FQN_LUA_SCOPE_TYPES),
159
+ function_node_types=frozenset(cs.FQN_LUA_FUNCTION_TYPES),
160
+ get_name=_generic_get_name,
161
+ file_to_module_parts=_generic_file_to_module,
162
+ )
163
+
164
+ GO_FQN_SPEC = FQNSpec(
165
+ scope_node_types=frozenset(cs.FQN_GO_SCOPE_TYPES),
166
+ function_node_types=frozenset(cs.FQN_GO_FUNCTION_TYPES),
167
+ get_name=_generic_get_name,
168
+ file_to_module_parts=_generic_file_to_module,
169
+ )
170
+
171
+ SCALA_FQN_SPEC = FQNSpec(
172
+ scope_node_types=frozenset(cs.FQN_SCALA_SCOPE_TYPES),
173
+ function_node_types=frozenset(cs.FQN_SCALA_FUNCTION_TYPES),
174
+ get_name=_generic_get_name,
175
+ file_to_module_parts=_generic_file_to_module,
176
+ )
177
+
178
+ CSHARP_FQN_SPEC = FQNSpec(
179
+ scope_node_types=frozenset(cs.FQN_CS_SCOPE_TYPES),
180
+ function_node_types=frozenset(cs.FQN_CS_FUNCTION_TYPES),
181
+ get_name=_generic_get_name,
182
+ file_to_module_parts=_generic_file_to_module,
183
+ )
184
+
185
+ PHP_FQN_SPEC = FQNSpec(
186
+ scope_node_types=frozenset(cs.FQN_PHP_SCOPE_TYPES),
187
+ function_node_types=frozenset(cs.FQN_PHP_FUNCTION_TYPES),
188
+ get_name=_generic_get_name,
189
+ file_to_module_parts=_generic_file_to_module,
190
+ )
191
+
192
+ LANGUAGE_FQN_SPECS: dict[cs.SupportedLanguage, FQNSpec] = {
193
+ cs.SupportedLanguage.PYTHON: PYTHON_FQN_SPEC,
194
+ cs.SupportedLanguage.JS: JS_FQN_SPEC,
195
+ cs.SupportedLanguage.TS: TS_FQN_SPEC,
196
+ cs.SupportedLanguage.RUST: RUST_FQN_SPEC,
197
+ cs.SupportedLanguage.JAVA: JAVA_FQN_SPEC,
198
+ cs.SupportedLanguage.CPP: CPP_FQN_SPEC,
199
+ cs.SupportedLanguage.LUA: LUA_FQN_SPEC,
200
+ cs.SupportedLanguage.GO: GO_FQN_SPEC,
201
+ cs.SupportedLanguage.SCALA: SCALA_FQN_SPEC,
202
+ cs.SupportedLanguage.CSHARP: CSHARP_FQN_SPEC,
203
+ cs.SupportedLanguage.PHP: PHP_FQN_SPEC,
204
+ }
205
+
206
+
207
+ LANGUAGE_SPECS: dict[cs.SupportedLanguage, LanguageSpec] = {
208
+ cs.SupportedLanguage.PYTHON: LanguageSpec(
209
+ language=cs.SupportedLanguage.PYTHON,
210
+ file_extensions=cs.PY_EXTENSIONS,
211
+ function_node_types=cs.SPEC_PY_FUNCTION_TYPES,
212
+ class_node_types=cs.SPEC_PY_CLASS_TYPES,
213
+ module_node_types=cs.SPEC_PY_MODULE_TYPES,
214
+ call_node_types=cs.SPEC_PY_CALL_TYPES,
215
+ import_node_types=cs.SPEC_PY_IMPORT_TYPES,
216
+ import_from_node_types=cs.SPEC_PY_IMPORT_FROM_TYPES,
217
+ package_indicators=cs.SPEC_PY_PACKAGE_INDICATORS,
218
+ ),
219
+ cs.SupportedLanguage.JS: LanguageSpec(
220
+ language=cs.SupportedLanguage.JS,
221
+ file_extensions=cs.JS_EXTENSIONS,
222
+ function_node_types=cs.JS_TS_FUNCTION_NODES,
223
+ class_node_types=cs.JS_TS_CLASS_NODES,
224
+ module_node_types=cs.SPEC_JS_MODULE_TYPES,
225
+ call_node_types=cs.SPEC_JS_CALL_TYPES,
226
+ import_node_types=cs.JS_TS_IMPORT_NODES,
227
+ import_from_node_types=cs.JS_TS_IMPORT_NODES,
228
+ ),
229
+ cs.SupportedLanguage.TS: LanguageSpec(
230
+ language=cs.SupportedLanguage.TS,
231
+ file_extensions=cs.TS_EXTENSIONS,
232
+ function_node_types=cs.JS_TS_FUNCTION_NODES + (cs.TS_FUNCTION_SIGNATURE,),
233
+ class_node_types=cs.JS_TS_CLASS_NODES
234
+ + (
235
+ cs.TS_ABSTRACT_CLASS_DECLARATION,
236
+ cs.TS_ENUM_DECLARATION,
237
+ cs.TS_INTERFACE_DECLARATION,
238
+ cs.TS_TYPE_ALIAS_DECLARATION,
239
+ cs.TS_INTERNAL_MODULE,
240
+ ),
241
+ module_node_types=cs.SPEC_JS_MODULE_TYPES,
242
+ call_node_types=cs.SPEC_JS_CALL_TYPES,
243
+ import_node_types=cs.JS_TS_IMPORT_NODES,
244
+ import_from_node_types=cs.JS_TS_IMPORT_NODES,
245
+ ),
246
+ cs.SupportedLanguage.RUST: LanguageSpec(
247
+ language=cs.SupportedLanguage.RUST,
248
+ file_extensions=cs.RS_EXTENSIONS,
249
+ function_node_types=cs.SPEC_RS_FUNCTION_TYPES,
250
+ class_node_types=cs.SPEC_RS_CLASS_TYPES,
251
+ module_node_types=cs.SPEC_RS_MODULE_TYPES,
252
+ call_node_types=cs.SPEC_RS_CALL_TYPES,
253
+ import_node_types=cs.SPEC_RS_IMPORT_TYPES,
254
+ import_from_node_types=cs.SPEC_RS_IMPORT_FROM_TYPES,
255
+ package_indicators=cs.SPEC_RS_PACKAGE_INDICATORS,
256
+ function_query="""
257
+ (function_item
258
+ name: (identifier) @name) @function
259
+ (function_signature_item
260
+ name: (identifier) @name) @function
261
+ (closure_expression) @function
262
+ """,
263
+ class_query="""
264
+ (struct_item
265
+ name: (type_identifier) @name) @class
266
+ (enum_item
267
+ name: (type_identifier) @name) @class
268
+ (union_item
269
+ name: (type_identifier) @name) @class
270
+ (trait_item
271
+ name: (type_identifier) @name) @class
272
+ (type_item
273
+ name: (type_identifier) @name) @class
274
+ (impl_item) @class
275
+ (mod_item
276
+ name: (identifier) @name) @module
277
+ """,
278
+ call_query="""
279
+ (call_expression
280
+ function: (identifier) @name) @call
281
+ (call_expression
282
+ function: (field_expression
283
+ field: (field_identifier) @name)) @call
284
+ (call_expression
285
+ function: (scoped_identifier
286
+ "::"
287
+ name: (identifier) @name)) @call
288
+ (macro_invocation
289
+ macro: (identifier) @name) @call
290
+ """,
291
+ ),
292
+ cs.SupportedLanguage.GO: LanguageSpec(
293
+ language=cs.SupportedLanguage.GO,
294
+ file_extensions=cs.GO_EXTENSIONS,
295
+ function_node_types=cs.SPEC_GO_FUNCTION_TYPES,
296
+ class_node_types=cs.SPEC_GO_CLASS_TYPES,
297
+ module_node_types=cs.SPEC_GO_MODULE_TYPES,
298
+ call_node_types=cs.SPEC_GO_CALL_TYPES,
299
+ import_node_types=cs.SPEC_GO_IMPORT_TYPES,
300
+ import_from_node_types=cs.SPEC_GO_IMPORT_TYPES,
301
+ ),
302
+ cs.SupportedLanguage.SCALA: LanguageSpec(
303
+ language=cs.SupportedLanguage.SCALA,
304
+ file_extensions=cs.SCALA_EXTENSIONS,
305
+ function_node_types=cs.SPEC_SCALA_FUNCTION_TYPES,
306
+ class_node_types=cs.SPEC_SCALA_CLASS_TYPES,
307
+ module_node_types=cs.SPEC_SCALA_MODULE_TYPES,
308
+ call_node_types=cs.SPEC_SCALA_CALL_TYPES,
309
+ import_node_types=cs.SPEC_SCALA_IMPORT_TYPES,
310
+ import_from_node_types=cs.SPEC_SCALA_IMPORT_TYPES,
311
+ ),
312
+ cs.SupportedLanguage.JAVA: LanguageSpec(
313
+ language=cs.SupportedLanguage.JAVA,
314
+ file_extensions=cs.JAVA_EXTENSIONS,
315
+ function_node_types=cs.SPEC_JAVA_FUNCTION_TYPES,
316
+ class_node_types=cs.SPEC_JAVA_CLASS_TYPES,
317
+ module_node_types=cs.SPEC_JAVA_MODULE_TYPES,
318
+ call_node_types=cs.SPEC_JAVA_CALL_TYPES,
319
+ import_node_types=cs.SPEC_JAVA_IMPORT_TYPES,
320
+ import_from_node_types=cs.SPEC_JAVA_IMPORT_TYPES,
321
+ function_query="""
322
+ (method_declaration
323
+ name: (identifier) @name) @function
324
+ (constructor_declaration
325
+ name: (identifier) @name) @function
326
+ """,
327
+ class_query="""
328
+ (class_declaration
329
+ name: (identifier) @name) @class
330
+ (interface_declaration
331
+ name: (identifier) @name) @class
332
+ (enum_declaration
333
+ name: (identifier) @name) @class
334
+ (annotation_type_declaration
335
+ name: (identifier) @name) @class
336
+ (record_declaration
337
+ name: (identifier) @name) @class
338
+ """,
339
+ call_query="""
340
+ (method_invocation
341
+ name: (identifier) @name) @call
342
+ (object_creation_expression
343
+ type: (type_identifier) @name) @call
344
+ """,
345
+ ),
346
+ cs.SupportedLanguage.CPP: LanguageSpec(
347
+ language=cs.SupportedLanguage.CPP,
348
+ file_extensions=cs.CPP_EXTENSIONS,
349
+ function_node_types=cs.SPEC_CPP_FUNCTION_TYPES,
350
+ class_node_types=cs.SPEC_CPP_CLASS_TYPES,
351
+ module_node_types=cs.SPEC_CPP_MODULE_TYPES,
352
+ call_node_types=cs.SPEC_CPP_CALL_TYPES,
353
+ import_node_types=cs.CPP_IMPORT_NODES,
354
+ import_from_node_types=cs.CPP_IMPORT_NODES,
355
+ package_indicators=cs.SPEC_CPP_PACKAGE_INDICATORS,
356
+ function_query="""
357
+ (field_declaration) @function
358
+ (declaration) @function
359
+ (function_definition) @function
360
+ (template_declaration (function_definition)) @function
361
+ (lambda_expression) @function
362
+ """,
363
+ class_query="""
364
+ (class_specifier) @class
365
+ (struct_specifier) @class
366
+ (union_specifier) @class
367
+ (enum_specifier) @class
368
+ (template_declaration (class_specifier)) @class
369
+ (template_declaration (struct_specifier)) @class
370
+ (template_declaration (union_specifier)) @class
371
+ (template_declaration (enum_specifier)) @class
372
+ """,
373
+ call_query="""
374
+ (call_expression) @call
375
+ (binary_expression) @call
376
+ (unary_expression) @call
377
+ (update_expression) @call
378
+ (field_expression) @call
379
+ (subscript_expression) @call
380
+ (new_expression) @call
381
+ (delete_expression) @call
382
+ """,
383
+ ),
384
+ cs.SupportedLanguage.CSHARP: LanguageSpec(
385
+ language=cs.SupportedLanguage.CSHARP,
386
+ file_extensions=cs.CS_EXTENSIONS,
387
+ function_node_types=cs.SPEC_CS_FUNCTION_TYPES,
388
+ class_node_types=cs.SPEC_CS_CLASS_TYPES,
389
+ module_node_types=cs.SPEC_CS_MODULE_TYPES,
390
+ call_node_types=cs.SPEC_CS_CALL_TYPES,
391
+ import_node_types=cs.IMPORT_NODES_USING,
392
+ import_from_node_types=cs.IMPORT_NODES_USING,
393
+ ),
394
+ cs.SupportedLanguage.PHP: LanguageSpec(
395
+ language=cs.SupportedLanguage.PHP,
396
+ file_extensions=cs.PHP_EXTENSIONS,
397
+ function_node_types=cs.SPEC_PHP_FUNCTION_TYPES,
398
+ class_node_types=cs.SPEC_PHP_CLASS_TYPES,
399
+ module_node_types=cs.SPEC_PHP_MODULE_TYPES,
400
+ call_node_types=cs.SPEC_PHP_CALL_TYPES,
401
+ ),
402
+ cs.SupportedLanguage.LUA: LanguageSpec(
403
+ language=cs.SupportedLanguage.LUA,
404
+ file_extensions=cs.LUA_EXTENSIONS,
405
+ function_node_types=cs.SPEC_LUA_FUNCTION_TYPES,
406
+ class_node_types=cs.SPEC_LUA_CLASS_TYPES,
407
+ module_node_types=cs.SPEC_LUA_MODULE_TYPES,
408
+ call_node_types=cs.SPEC_LUA_CALL_TYPES,
409
+ import_node_types=cs.SPEC_LUA_IMPORT_TYPES,
410
+ ),
411
+ }
412
+
413
+ _EXTENSION_TO_SPEC: dict[str, LanguageSpec] = {}
414
+ for _config in LANGUAGE_SPECS.values():
415
+ for _ext in _config.file_extensions:
416
+ _EXTENSION_TO_SPEC[_ext] = _config
417
+
418
+
419
+ def get_language_spec(file_extension: str) -> LanguageSpec | None:
420
+ return _EXTENSION_TO_SPEC.get(file_extension)
421
+
422
+
423
+ def get_language_for_extension(file_extension: str) -> cs.SupportedLanguage | None:
424
+ spec = _EXTENSION_TO_SPEC.get(file_extension)
425
+ if spec and isinstance(spec.language, cs.SupportedLanguage):
426
+ return spec.language
427
+ return None