codegraphcontext-rust 0.5.5__cp311-cp311-win_amd64.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.
- codegraphcontext/__init__.py +2 -0
- codegraphcontext/__main__.py +5 -0
- codegraphcontext/_cgc_rust.cp311-win_amd64.pyd +0 -0
- codegraphcontext/cli/__init__.py +1 -0
- codegraphcontext/cli/cli_helpers.py +774 -0
- codegraphcontext/cli/config_manager.py +1051 -0
- codegraphcontext/cli/main.py +2386 -0
- codegraphcontext/cli/registry_commands.py +404 -0
- codegraphcontext/cli/setup_macos.py +93 -0
- codegraphcontext/cli/setup_wizard.py +991 -0
- codegraphcontext/cli/visualizer.py +50 -0
- codegraphcontext/core/__init__.py +165 -0
- codegraphcontext/core/bundle_registry.py +181 -0
- codegraphcontext/core/cgc_bundle.py +858 -0
- codegraphcontext/core/cgcignore.py +118 -0
- codegraphcontext/core/database.py +273 -0
- codegraphcontext/core/database_falkordb.py +480 -0
- codegraphcontext/core/database_falkordb_remote.py +199 -0
- codegraphcontext/core/database_kuzu.py +636 -0
- codegraphcontext/core/falkor_worker.py +132 -0
- codegraphcontext/core/jobs.py +132 -0
- codegraphcontext/core/watcher.py +260 -0
- codegraphcontext/prompts.py +125 -0
- codegraphcontext/server.py +471 -0
- codegraphcontext/tool_definitions.py +220 -0
- codegraphcontext/tools/__init__.py +0 -0
- codegraphcontext/tools/advanced_language_query_tool.py +103 -0
- codegraphcontext/tools/code_finder.py +1160 -0
- codegraphcontext/tools/graph_builder.py +337 -0
- codegraphcontext/tools/handlers/analysis_handlers.py +114 -0
- codegraphcontext/tools/handlers/indexing_handlers.py +116 -0
- codegraphcontext/tools/handlers/management_handlers.py +339 -0
- codegraphcontext/tools/handlers/query_handlers.py +83 -0
- codegraphcontext/tools/handlers/watcher_handlers.py +83 -0
- codegraphcontext/tools/indexing/__init__.py +1 -0
- codegraphcontext/tools/indexing/constants.py +25 -0
- codegraphcontext/tools/indexing/discovery.py +64 -0
- codegraphcontext/tools/indexing/engine.py +109 -0
- codegraphcontext/tools/indexing/persistence/__init__.py +3 -0
- codegraphcontext/tools/indexing/persistence/parquet_loader.py +643 -0
- codegraphcontext/tools/indexing/persistence/writer.py +1114 -0
- codegraphcontext/tools/indexing/pipeline.py +171 -0
- codegraphcontext/tools/indexing/pre_scan.py +105 -0
- codegraphcontext/tools/indexing/resolution/__init__.py +8 -0
- codegraphcontext/tools/indexing/resolution/calls.py +253 -0
- codegraphcontext/tools/indexing/resolution/inheritance.py +91 -0
- codegraphcontext/tools/indexing/sanitize.py +41 -0
- codegraphcontext/tools/indexing/schema.py +79 -0
- codegraphcontext/tools/indexing/schema_contract.py +44 -0
- codegraphcontext/tools/indexing/scip_pipeline.py +140 -0
- codegraphcontext/tools/languages/c.py +563 -0
- codegraphcontext/tools/languages/cpp.py +616 -0
- codegraphcontext/tools/languages/csharp.py +551 -0
- codegraphcontext/tools/languages/dart.py +378 -0
- codegraphcontext/tools/languages/elixir.py +460 -0
- codegraphcontext/tools/languages/go.py +499 -0
- codegraphcontext/tools/languages/haskell.py +426 -0
- codegraphcontext/tools/languages/java.py +500 -0
- codegraphcontext/tools/languages/javascript.py +589 -0
- codegraphcontext/tools/languages/kotlin.py +640 -0
- codegraphcontext/tools/languages/perl.py +260 -0
- codegraphcontext/tools/languages/php.py +519 -0
- codegraphcontext/tools/languages/python.py +576 -0
- codegraphcontext/tools/languages/ruby.py +537 -0
- codegraphcontext/tools/languages/rust.py +295 -0
- codegraphcontext/tools/languages/scala.py +519 -0
- codegraphcontext/tools/languages/swift.py +491 -0
- codegraphcontext/tools/languages/typescript.py +575 -0
- codegraphcontext/tools/languages/typescriptjsx.py +151 -0
- codegraphcontext/tools/package_resolver.py +472 -0
- codegraphcontext/tools/query_tool_languages/c_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/cpp_toolkit.py +87 -0
- codegraphcontext/tools/query_tool_languages/csharp_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/dart_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/go_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/haskell_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/java_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/javascript_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/perl_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/python_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/ruby_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/rust_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/scala_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/swift_toolkit.py +5 -0
- codegraphcontext/tools/query_tool_languages/typescript_toolkit.py +5 -0
- codegraphcontext/tools/scip_indexer.py +467 -0
- codegraphcontext/tools/scip_pb2.py +2455 -0
- codegraphcontext/tools/system.py +133 -0
- codegraphcontext/tools/tree_sitter_parser.py +103 -0
- codegraphcontext/utils/debug_log.py +90 -0
- codegraphcontext/utils/path_ignore.py +55 -0
- codegraphcontext/utils/repo_path.py +27 -0
- codegraphcontext/utils/tree_sitter_manager.py +285 -0
- codegraphcontext/utils/visualize_graph.py +0 -0
- codegraphcontext/viz/server.py +282 -0
- codegraphcontext_rust-0.5.5.dist-info/METADATA +537 -0
- codegraphcontext_rust-0.5.5.dist-info/RECORD +101 -0
- codegraphcontext_rust-0.5.5.dist-info/WHEEL +4 -0
- codegraphcontext_rust-0.5.5.dist-info/entry_points.txt +3 -0
- codegraphcontext_rust-0.5.5.dist-info/licenses/LICENSE +21 -0
- codegraphcontext_rust-0.5.5.dist-info/sboms/cgc-python.cyclonedx.json +2333 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Any, Dict, Optional, Tuple
|
|
3
|
+
import re
|
|
4
|
+
from codegraphcontext.utils.debug_log import debug_log, info_logger, error_logger, warning_logger
|
|
5
|
+
from codegraphcontext.utils.tree_sitter_manager import execute_query
|
|
6
|
+
|
|
7
|
+
KOTLIN_QUERIES = {
|
|
8
|
+
"functions": """
|
|
9
|
+
(function_declaration
|
|
10
|
+
(simple_identifier) @name
|
|
11
|
+
(function_value_parameters) @params
|
|
12
|
+
) @function_node
|
|
13
|
+
""",
|
|
14
|
+
"classes": """
|
|
15
|
+
[
|
|
16
|
+
(class_declaration (type_identifier) @name)
|
|
17
|
+
(object_declaration (type_identifier) @name)
|
|
18
|
+
(companion_object (type_identifier)? @name)
|
|
19
|
+
] @class
|
|
20
|
+
""",
|
|
21
|
+
"imports": """
|
|
22
|
+
(import_header) @import
|
|
23
|
+
""",
|
|
24
|
+
"calls": """
|
|
25
|
+
(call_expression) @call_node
|
|
26
|
+
""",
|
|
27
|
+
"variables": """
|
|
28
|
+
(property_declaration
|
|
29
|
+
(variable_declaration
|
|
30
|
+
(simple_identifier) @name
|
|
31
|
+
)
|
|
32
|
+
) @variable
|
|
33
|
+
""",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class KotlinTreeSitterParser:
|
|
37
|
+
def __init__(self, generic_parser_wrapper: Any):
|
|
38
|
+
self.generic_parser_wrapper = generic_parser_wrapper
|
|
39
|
+
self.language_name = "kotlin"
|
|
40
|
+
self.language = generic_parser_wrapper.language
|
|
41
|
+
self.parser = generic_parser_wrapper.parser
|
|
42
|
+
|
|
43
|
+
def parse(self, path: Path, is_dependency: bool = False, index_source: bool = False) -> Dict[str, Any]:
|
|
44
|
+
try:
|
|
45
|
+
self.index_source = index_source
|
|
46
|
+
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
|
|
47
|
+
source_code = f.read()
|
|
48
|
+
|
|
49
|
+
if not source_code.strip():
|
|
50
|
+
warning_logger(f"Empty or whitespace-only file: {path}")
|
|
51
|
+
return {
|
|
52
|
+
"path": str(path),
|
|
53
|
+
"functions": [],
|
|
54
|
+
"classes": [],
|
|
55
|
+
"variables": [],
|
|
56
|
+
"imports": [],
|
|
57
|
+
"function_calls": [],
|
|
58
|
+
"is_dependency": is_dependency,
|
|
59
|
+
"lang": self.language_name,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
tree = self.parser.parse(bytes(source_code, "utf8"))
|
|
63
|
+
|
|
64
|
+
parsed_functions = []
|
|
65
|
+
parsed_classes = []
|
|
66
|
+
parsed_variables = []
|
|
67
|
+
parsed_imports = []
|
|
68
|
+
parsed_calls = []
|
|
69
|
+
|
|
70
|
+
# Parse Variables first to populate for inference
|
|
71
|
+
if 'variables' in KOTLIN_QUERIES:
|
|
72
|
+
results = execute_query(self.language, KOTLIN_QUERIES['variables'], tree.root_node)
|
|
73
|
+
parsed_variables = self._parse_variables(results, source_code, path)
|
|
74
|
+
|
|
75
|
+
for capture_name, query in KOTLIN_QUERIES.items():
|
|
76
|
+
if capture_name == 'variables': continue # Already done
|
|
77
|
+
results = execute_query(self.language, query, tree.root_node)
|
|
78
|
+
|
|
79
|
+
if capture_name == "functions":
|
|
80
|
+
parsed_functions.extend(self._parse_functions(results, source_code, path))
|
|
81
|
+
elif capture_name == "classes":
|
|
82
|
+
parsed_classes.extend(self._parse_classes(results, source_code, path))
|
|
83
|
+
elif capture_name == "imports":
|
|
84
|
+
parsed_imports.extend(self._parse_imports(results, source_code))
|
|
85
|
+
elif capture_name == "calls":
|
|
86
|
+
parsed_calls.extend(self._parse_calls(results, source_code, path, parsed_variables))
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
"path": str(path),
|
|
90
|
+
"functions": parsed_functions,
|
|
91
|
+
"classes": parsed_classes,
|
|
92
|
+
"variables": parsed_variables,
|
|
93
|
+
"imports": parsed_imports,
|
|
94
|
+
"function_calls": parsed_calls,
|
|
95
|
+
"is_dependency": is_dependency,
|
|
96
|
+
"lang": self.language_name,
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
except Exception as e:
|
|
100
|
+
error_logger(f"Error parsing Kotlin file {path}: {e}")
|
|
101
|
+
return {
|
|
102
|
+
"path": str(path),
|
|
103
|
+
"functions": [],
|
|
104
|
+
"classes": [],
|
|
105
|
+
"variables": [],
|
|
106
|
+
"imports": [],
|
|
107
|
+
"function_calls": [],
|
|
108
|
+
"is_dependency": is_dependency,
|
|
109
|
+
"lang": self.language_name,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
def _get_parent_context(self, node: Any) -> Tuple[Optional[str], Optional[str], Optional[int]]:
|
|
113
|
+
curr = node.parent
|
|
114
|
+
while curr:
|
|
115
|
+
if curr.type in ("function_declaration",):
|
|
116
|
+
name_node = None
|
|
117
|
+
for child in curr.children:
|
|
118
|
+
if child.type == "simple_identifier":
|
|
119
|
+
name_node = child
|
|
120
|
+
break
|
|
121
|
+
return (
|
|
122
|
+
self._get_node_text(name_node) if name_node else None,
|
|
123
|
+
curr.type,
|
|
124
|
+
curr.start_point[0] + 1,
|
|
125
|
+
)
|
|
126
|
+
if curr.type in ("class_declaration", "object_declaration"):
|
|
127
|
+
for child in curr.children:
|
|
128
|
+
if child.type in ("simple_identifier", "type_identifier"):
|
|
129
|
+
return (
|
|
130
|
+
self._get_node_text(child),
|
|
131
|
+
curr.type,
|
|
132
|
+
curr.start_point[0] + 1,
|
|
133
|
+
)
|
|
134
|
+
# Check for secondary constructors
|
|
135
|
+
if curr.type == "secondary_constructor":
|
|
136
|
+
return (
|
|
137
|
+
"constructor",
|
|
138
|
+
curr.type,
|
|
139
|
+
curr.start_point[0] + 1
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
if curr.type == "companion_object":
|
|
143
|
+
name = "Companion"
|
|
144
|
+
for child in curr.children:
|
|
145
|
+
if child.type in ("simple_identifier", "type_identifier"):
|
|
146
|
+
name = self._get_node_text(child)
|
|
147
|
+
break
|
|
148
|
+
return (
|
|
149
|
+
name,
|
|
150
|
+
curr.type,
|
|
151
|
+
curr.start_point[0] + 1,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
# Handle anonymous objects (object_literal)
|
|
155
|
+
if curr.type == "object_literal":
|
|
156
|
+
# checking if it is assigned to a variable to get a name?
|
|
157
|
+
# or simply "AnonymousObject"
|
|
158
|
+
# It's usually hard to name them without variable context.
|
|
159
|
+
# We can check if parent is property/variable declaration
|
|
160
|
+
name = "AnonymousObject"
|
|
161
|
+
return (
|
|
162
|
+
name,
|
|
163
|
+
curr.type,
|
|
164
|
+
curr.start_point[0] + 1
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
curr = curr.parent
|
|
168
|
+
return None, None, None
|
|
169
|
+
|
|
170
|
+
def _get_node_text(self, node: Any) -> str:
|
|
171
|
+
if not node: return ""
|
|
172
|
+
return node.text.decode("utf-8")
|
|
173
|
+
|
|
174
|
+
def _parse_functions(self, captures: list, source_code: str, path: Path) -> list[Dict[str, Any]]:
|
|
175
|
+
functions = []
|
|
176
|
+
seen_nodes = set()
|
|
177
|
+
|
|
178
|
+
for node, capture_name in captures:
|
|
179
|
+
if capture_name == "function_node":
|
|
180
|
+
node_id = (node.start_byte, node.end_byte, node.type)
|
|
181
|
+
if node_id in seen_nodes:
|
|
182
|
+
continue
|
|
183
|
+
seen_nodes.add(node_id)
|
|
184
|
+
|
|
185
|
+
try:
|
|
186
|
+
start_line = node.start_point[0] + 1
|
|
187
|
+
end_line = node.end_point[0] + 1
|
|
188
|
+
|
|
189
|
+
# Manual child lookup
|
|
190
|
+
name_node = None
|
|
191
|
+
for child in node.children:
|
|
192
|
+
if child.type == "simple_identifier":
|
|
193
|
+
name_node = child
|
|
194
|
+
break
|
|
195
|
+
|
|
196
|
+
if name_node:
|
|
197
|
+
func_name = self._get_node_text(name_node)
|
|
198
|
+
|
|
199
|
+
params_node = None
|
|
200
|
+
for child in node.children:
|
|
201
|
+
if child.type == "function_value_parameters":
|
|
202
|
+
params_node = child
|
|
203
|
+
break
|
|
204
|
+
|
|
205
|
+
parameters = []
|
|
206
|
+
if params_node:
|
|
207
|
+
params_text = self._get_node_text(params_node)
|
|
208
|
+
parameters = self._extract_parameter_names(params_text)
|
|
209
|
+
|
|
210
|
+
source_text = self._get_node_text(node)
|
|
211
|
+
|
|
212
|
+
context_name, context_type, context_line = self._get_parent_context(node)
|
|
213
|
+
|
|
214
|
+
func_data = {
|
|
215
|
+
"name": func_name,
|
|
216
|
+
"args": parameters,
|
|
217
|
+
"line_number": start_line,
|
|
218
|
+
"end_line": end_line,
|
|
219
|
+
"path": str(path),
|
|
220
|
+
"lang": self.language_name,
|
|
221
|
+
"context": context_name,
|
|
222
|
+
"class_context": context_name if context_type and ("class" in context_type or "object" in context_type) else None
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if self.index_source:
|
|
226
|
+
func_data["source"] = source_text
|
|
227
|
+
|
|
228
|
+
functions.append(func_data)
|
|
229
|
+
|
|
230
|
+
except Exception as e:
|
|
231
|
+
error_logger(f"Error parsing function in {path}: {e}")
|
|
232
|
+
continue
|
|
233
|
+
|
|
234
|
+
return functions
|
|
235
|
+
|
|
236
|
+
def _parse_classes(self, captures: list, source_code: str, path: Path) -> list[Dict[str, Any]]:
|
|
237
|
+
classes = []
|
|
238
|
+
seen_nodes = set()
|
|
239
|
+
|
|
240
|
+
for node, capture_name in captures:
|
|
241
|
+
if capture_name == "class":
|
|
242
|
+
node_id = (node.start_byte, node.end_byte, node.type)
|
|
243
|
+
if node_id in seen_nodes:
|
|
244
|
+
continue
|
|
245
|
+
seen_nodes.add(node_id)
|
|
246
|
+
|
|
247
|
+
try:
|
|
248
|
+
start_line = node.start_point[0] + 1
|
|
249
|
+
end_line = node.end_point[0] + 1
|
|
250
|
+
|
|
251
|
+
# Find name child (type_identifier or simple_identifier)
|
|
252
|
+
class_name = "Anonymous"
|
|
253
|
+
if node.type == "companion_object":
|
|
254
|
+
class_name = "Companion" # Default name
|
|
255
|
+
|
|
256
|
+
for child in node.children:
|
|
257
|
+
if child.type in ("type_identifier", "simple_identifier"):
|
|
258
|
+
class_name = self._get_node_text(child)
|
|
259
|
+
break
|
|
260
|
+
|
|
261
|
+
source_text = self._get_node_text(node)
|
|
262
|
+
|
|
263
|
+
bases = []
|
|
264
|
+
# Check for delegation specifiers
|
|
265
|
+
# class_declaration -> delegation_specifier
|
|
266
|
+
|
|
267
|
+
for child in node.children:
|
|
268
|
+
if child.type == "delegation_specifier":
|
|
269
|
+
# children: constructor_invocation or user_type
|
|
270
|
+
for specifier in child.children:
|
|
271
|
+
# constructor_invocation -> user_type -> type_identifier
|
|
272
|
+
# user_type -> type_identifier
|
|
273
|
+
|
|
274
|
+
# We want the text of the type
|
|
275
|
+
if specifier.type == "constructor_invocation":
|
|
276
|
+
# child 0 is typically user_type
|
|
277
|
+
for sub in specifier.children:
|
|
278
|
+
if sub.type == "user_type":
|
|
279
|
+
bases.append(self._get_node_text(sub))
|
|
280
|
+
break
|
|
281
|
+
elif specifier.type == "user_type":
|
|
282
|
+
bases.append(self._get_node_text(specifier))
|
|
283
|
+
elif specifier.type == "explicit_delegation":
|
|
284
|
+
# Not handling simple yet, uses 'by'
|
|
285
|
+
pass
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class_data = {
|
|
289
|
+
"name": class_name,
|
|
290
|
+
"line_number": start_line,
|
|
291
|
+
"end_line": end_line,
|
|
292
|
+
"bases": bases,
|
|
293
|
+
"path": str(path),
|
|
294
|
+
"lang": self.language_name,
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if self.index_source:
|
|
298
|
+
class_data["source"] = source_text
|
|
299
|
+
|
|
300
|
+
classes.append(class_data)
|
|
301
|
+
|
|
302
|
+
except Exception as e:
|
|
303
|
+
error_logger(f"Error parsing class in {path}: {e}")
|
|
304
|
+
continue
|
|
305
|
+
|
|
306
|
+
return classes
|
|
307
|
+
|
|
308
|
+
def _parse_variables(self, captures: list, source_code: str, path: Path) -> list[Dict[str, Any]]:
|
|
309
|
+
variables = []
|
|
310
|
+
seen_vars = set()
|
|
311
|
+
|
|
312
|
+
for node, capture_name in captures:
|
|
313
|
+
if capture_name == "variable":
|
|
314
|
+
try:
|
|
315
|
+
start_line = node.start_point[0] + 1
|
|
316
|
+
ctx_name, ctx_type, ctx_line = self._get_parent_context(node)
|
|
317
|
+
|
|
318
|
+
# Destructuring declaration
|
|
319
|
+
if "destructuring" in node.type:
|
|
320
|
+
pass
|
|
321
|
+
|
|
322
|
+
# Regular property/variable
|
|
323
|
+
var_name = "unknown"
|
|
324
|
+
var_type = "Unknown"
|
|
325
|
+
|
|
326
|
+
var_decl = None
|
|
327
|
+
for child in node.children:
|
|
328
|
+
if child.type == "variable_declaration":
|
|
329
|
+
var_decl = child
|
|
330
|
+
break
|
|
331
|
+
|
|
332
|
+
if var_decl:
|
|
333
|
+
# Check for name and type in variable_declaration
|
|
334
|
+
for child in var_decl.children:
|
|
335
|
+
if child.type == "simple_identifier":
|
|
336
|
+
var_name = self._get_node_text(child)
|
|
337
|
+
|
|
338
|
+
if child.type == "user_type":
|
|
339
|
+
var_type = self._get_node_text(child)
|
|
340
|
+
|
|
341
|
+
# Attempt inference from initializer if type is unknown
|
|
342
|
+
if var_type == "Unknown":
|
|
343
|
+
# property_declaration -> expression (e.g. call_expression)
|
|
344
|
+
for child in node.children:
|
|
345
|
+
if child.type == "call_expression":
|
|
346
|
+
# call_expression -> simple_identifier (constructor)
|
|
347
|
+
for sub in child.children:
|
|
348
|
+
if sub.type == "simple_identifier":
|
|
349
|
+
var_type = self._get_node_text(sub)
|
|
350
|
+
break
|
|
351
|
+
if var_type != "Unknown": break
|
|
352
|
+
|
|
353
|
+
if var_name != "unknown":
|
|
354
|
+
variables.append({
|
|
355
|
+
"name": var_name,
|
|
356
|
+
"type": var_type,
|
|
357
|
+
"line_number": start_line,
|
|
358
|
+
"path": str(path),
|
|
359
|
+
"lang": self.language_name,
|
|
360
|
+
"context": ctx_name,
|
|
361
|
+
"class_context": ctx_name if ctx_type and ("class" in ctx_type or "object" in ctx_type) else None
|
|
362
|
+
})
|
|
363
|
+
except Exception as e:
|
|
364
|
+
continue
|
|
365
|
+
|
|
366
|
+
return variables
|
|
367
|
+
|
|
368
|
+
def _parse_imports(self, captures: list, source_code: str) -> list[dict]:
|
|
369
|
+
imports = []
|
|
370
|
+
|
|
371
|
+
for node, capture_name in captures:
|
|
372
|
+
if capture_name == "import":
|
|
373
|
+
try:
|
|
374
|
+
# import_header -> "import" identifier (import_alias)?
|
|
375
|
+
text = self._get_node_text(node)
|
|
376
|
+
# remove 'import '
|
|
377
|
+
path = text.replace('import ', '').strip().split(' as ')[0].strip()
|
|
378
|
+
alias = None
|
|
379
|
+
if ' as ' in text:
|
|
380
|
+
alias = text.split(' as ')[1].strip()
|
|
381
|
+
|
|
382
|
+
imports.append({
|
|
383
|
+
"name": path,
|
|
384
|
+
"full_import_name": path,
|
|
385
|
+
"line_number": node.start_point[0] + 1,
|
|
386
|
+
"alias": alias,
|
|
387
|
+
"context": (None, None),
|
|
388
|
+
"lang": self.language_name,
|
|
389
|
+
"is_dependency": False,
|
|
390
|
+
})
|
|
391
|
+
except Exception as e:
|
|
392
|
+
continue
|
|
393
|
+
|
|
394
|
+
return imports
|
|
395
|
+
|
|
396
|
+
def _parse_calls(self, captures: list, source_code: str, path: Path, variables: list[Dict[str, Any]] = []) -> list[Dict[str, Any]]:
|
|
397
|
+
calls = []
|
|
398
|
+
seen_calls = set()
|
|
399
|
+
|
|
400
|
+
# Index variables for fast lookup: (name, context) -> type
|
|
401
|
+
var_map = {}
|
|
402
|
+
for v in variables:
|
|
403
|
+
key = (v['name'], v['context'])
|
|
404
|
+
var_map[key] = v['type']
|
|
405
|
+
# Fallback for null context or partial match could be added
|
|
406
|
+
# For class props: (name, class_context) might work if local lookup fails?
|
|
407
|
+
|
|
408
|
+
for node, capture_name in captures:
|
|
409
|
+
if capture_name == "call_node":
|
|
410
|
+
try:
|
|
411
|
+
# navigation_expression check
|
|
412
|
+
|
|
413
|
+
start_line = node.start_point[0] + 1
|
|
414
|
+
|
|
415
|
+
call_name = "unknown"
|
|
416
|
+
base_obj = None
|
|
417
|
+
|
|
418
|
+
# call_expression usually has children:
|
|
419
|
+
# simple_identifier (func name)
|
|
420
|
+
# or navigation_expression (obj.method)
|
|
421
|
+
|
|
422
|
+
# Heuristic for base object:
|
|
423
|
+
# If navigation_expression -> child[0] is base, child[1] is suffix (method)
|
|
424
|
+
|
|
425
|
+
# We need to look deeper into the call_expression structure.
|
|
426
|
+
# call_expression -> (simple_identifier)
|
|
427
|
+
# OR call_expression -> (navigation_expression (simple_identifier) (navigation_suffix (simple_identifier) ...))
|
|
428
|
+
# OR call_expression -> (navigation_expression (call_expression) ...) (chained)
|
|
429
|
+
|
|
430
|
+
# Simplified traversal to find the "function name" and "receiver"
|
|
431
|
+
|
|
432
|
+
# If it's a direct call: foo()
|
|
433
|
+
# If it's a method call: x.foo()
|
|
434
|
+
|
|
435
|
+
# Tree-sitter struct:
|
|
436
|
+
# (call_expression (simple_identifier) (call_suffix ...)) -> name = simple_identifier
|
|
437
|
+
# (call_expression (navigation_expression (simple_identifier) (navigation_suffix (simple_identifier))) (call_suffix))
|
|
438
|
+
# -> name = 2nd simple_identifier, base = 1st simple_identifier
|
|
439
|
+
|
|
440
|
+
# Let's verify children
|
|
441
|
+
children = node.children
|
|
442
|
+
first_child = children[0]
|
|
443
|
+
|
|
444
|
+
if first_child.type == "simple_identifier":
|
|
445
|
+
call_name = self._get_node_text(first_child)
|
|
446
|
+
# No explicit base object
|
|
447
|
+
elif first_child.type == "navigation_expression":
|
|
448
|
+
# x.foo
|
|
449
|
+
# children: operand (x), operator (.), suffix (foo)
|
|
450
|
+
# Usually 3 children?
|
|
451
|
+
# Let's inspect nav expression children
|
|
452
|
+
nav_children = first_child.children
|
|
453
|
+
if len(nav_children) >= 2:
|
|
454
|
+
# operand is 0
|
|
455
|
+
operand = nav_children[0]
|
|
456
|
+
# last one is suffix?
|
|
457
|
+
suffix = nav_children[-1]
|
|
458
|
+
|
|
459
|
+
# Suffix usually contains the method name in a navigation_suffix node or directly?
|
|
460
|
+
# Suffix is (navigation_suffix (simple_identifier)) usually.
|
|
461
|
+
|
|
462
|
+
if suffix.type == "navigation_suffix":
|
|
463
|
+
# (navigation_suffix (simple_identifier))
|
|
464
|
+
for c in suffix.children:
|
|
465
|
+
if c.type == "simple_identifier":
|
|
466
|
+
call_name = self._get_node_text(c)
|
|
467
|
+
break
|
|
468
|
+
elif suffix.type == "simple_identifier":
|
|
469
|
+
call_name = self._get_node_text(suffix)
|
|
470
|
+
|
|
471
|
+
# Base object
|
|
472
|
+
base_obj = self._get_node_text(operand)
|
|
473
|
+
|
|
474
|
+
if call_name == "unknown":
|
|
475
|
+
continue
|
|
476
|
+
|
|
477
|
+
full_name = f"{base_obj}.{call_name}" if base_obj else call_name
|
|
478
|
+
|
|
479
|
+
ctx_name, ctx_type, ctx_line = self._get_parent_context(node)
|
|
480
|
+
|
|
481
|
+
# Inference
|
|
482
|
+
inferred_type = None
|
|
483
|
+
if base_obj:
|
|
484
|
+
# Lookup base_obj in variables
|
|
485
|
+
# Try exact context
|
|
486
|
+
inferred_type = var_map.get((base_obj, ctx_name))
|
|
487
|
+
if not inferred_type:
|
|
488
|
+
# Try class context if we are in a method
|
|
489
|
+
# This logic is approximate.
|
|
490
|
+
# If we are in method 'foo' of 'ClassA', and 'base_obj' refers to a property of 'ClassA',
|
|
491
|
+
# var_map entry would have context 'ClassA'.
|
|
492
|
+
# But our 'variables' parsing puts context as 'ClassA' for props.
|
|
493
|
+
# But 'ctx_name' here is 'foo'.
|
|
494
|
+
# We need to know 'foo' is in 'ClassA'.
|
|
495
|
+
# 'get_parent_context' returns immediate parent.
|
|
496
|
+
pass
|
|
497
|
+
# Fallback: check global/file scope (context=None)
|
|
498
|
+
if not inferred_type:
|
|
499
|
+
inferred_type = var_map.get((base_obj, None))
|
|
500
|
+
|
|
501
|
+
# Fallback: check if any variable named base_obj exists (loose match)
|
|
502
|
+
if not inferred_type:
|
|
503
|
+
for (vname, vctx), vtype in var_map.items():
|
|
504
|
+
if vname == base_obj:
|
|
505
|
+
inferred_type = vtype
|
|
506
|
+
break
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
calls.append({
|
|
510
|
+
"name": call_name,
|
|
511
|
+
"full_name": full_name,
|
|
512
|
+
"line_number": start_line,
|
|
513
|
+
"args": [], # Simplified
|
|
514
|
+
"inferred_obj_type": inferred_type,
|
|
515
|
+
"context": [None, ctx_type, ctx_line], # Keeping format compatible
|
|
516
|
+
"class_context": [None, None],
|
|
517
|
+
"lang": self.language_name,
|
|
518
|
+
"is_dependency": False
|
|
519
|
+
})
|
|
520
|
+
except Exception as e:
|
|
521
|
+
continue
|
|
522
|
+
return calls
|
|
523
|
+
|
|
524
|
+
def _extract_parameter_names(self, params_text: str) -> list[str]:
|
|
525
|
+
"""
|
|
526
|
+
Extracts parameter names from a Kotlin parameter list string.
|
|
527
|
+
Handles nested generics like Map<String, Int>.
|
|
528
|
+
|
|
529
|
+
Args:
|
|
530
|
+
params_text (str): The text content of function_value_parameters node, e.g. "(a: Int, b: Map<String, Int>)"
|
|
531
|
+
|
|
532
|
+
Returns:
|
|
533
|
+
list[str]: List of parameter names.
|
|
534
|
+
"""
|
|
535
|
+
params = []
|
|
536
|
+
if not params_text: return params
|
|
537
|
+
|
|
538
|
+
# Remove outer parentheses
|
|
539
|
+
clean = params_text.strip()
|
|
540
|
+
if clean.startswith('(') and clean.endswith(')'):
|
|
541
|
+
clean = clean[1:-1]
|
|
542
|
+
|
|
543
|
+
if not clean.strip(): return params
|
|
544
|
+
|
|
545
|
+
# Robust splitting by comma, respecting brackets <>, (), [], {}
|
|
546
|
+
current_param = []
|
|
547
|
+
depth_angle = 0 # < >
|
|
548
|
+
depth_round = 0 # ( )
|
|
549
|
+
depth_square = 0 # [ ]
|
|
550
|
+
depth_curly = 0 # { }
|
|
551
|
+
|
|
552
|
+
raw_params = []
|
|
553
|
+
|
|
554
|
+
for char in clean:
|
|
555
|
+
if char == '<': depth_angle += 1
|
|
556
|
+
elif char == '>': depth_angle -= 1
|
|
557
|
+
elif char == '(': depth_round += 1
|
|
558
|
+
elif char == ')': depth_round -= 1
|
|
559
|
+
elif char == '[': depth_square += 1
|
|
560
|
+
elif char == ']': depth_square -= 1
|
|
561
|
+
elif char == '{': depth_curly += 1
|
|
562
|
+
elif char == '}': depth_curly -= 1
|
|
563
|
+
|
|
564
|
+
if char == ',' and depth_angle == 0 and depth_round == 0 and depth_square == 0 and depth_curly == 0:
|
|
565
|
+
raw_params.append("".join(current_param).strip())
|
|
566
|
+
current_param = []
|
|
567
|
+
else:
|
|
568
|
+
current_param.append(char)
|
|
569
|
+
|
|
570
|
+
if current_param:
|
|
571
|
+
raw_params.append("".join(current_param).strip())
|
|
572
|
+
|
|
573
|
+
# Process each raw parameter string to extract name
|
|
574
|
+
# Format: "val x: Int", "override var y: String", "@Ann z: Int", "a: Int = 5"
|
|
575
|
+
for p in raw_params:
|
|
576
|
+
if not p: continue
|
|
577
|
+
|
|
578
|
+
# Remove default value if present
|
|
579
|
+
# Be careful with '=' inside strings or generic defaults, but usually param defaults are at top level
|
|
580
|
+
# A simple split by '=' might be risky if default value has '=', but for name extraction it's usually safe
|
|
581
|
+
# as the name is on the LHS.
|
|
582
|
+
# But wait, "val x: Type = ..." -> name is before ':'
|
|
583
|
+
|
|
584
|
+
# Split by ':' to separate name/modifiers from Type
|
|
585
|
+
# Using the first ':' usually works, assuming name doesn't contain ':'
|
|
586
|
+
colon_index = p.find(':')
|
|
587
|
+
if colon_index != -1:
|
|
588
|
+
lhs = p[:colon_index].strip()
|
|
589
|
+
else:
|
|
590
|
+
# Could be a parameter without type? (not common in Kotlin unless lambda destructuring)
|
|
591
|
+
# Or "var x = 5" (unlikely in func params)
|
|
592
|
+
# Just take the whole string if no colon?
|
|
593
|
+
lhs = p.strip()
|
|
594
|
+
|
|
595
|
+
# LHS contains keywords (val, var), annotations (@Foo), modifiers (crossinline, noinline, vararg)
|
|
596
|
+
# and the parameter name. The parameter name is usually the LAST identifier.
|
|
597
|
+
|
|
598
|
+
if not lhs: continue
|
|
599
|
+
|
|
600
|
+
tokens = lhs.split()
|
|
601
|
+
if tokens:
|
|
602
|
+
# The name is the last token
|
|
603
|
+
params.append(tokens[-1])
|
|
604
|
+
|
|
605
|
+
return params
|
|
606
|
+
|
|
607
|
+
def pre_scan_kotlin(files: list[Path], parser_wrapper) -> dict:
|
|
608
|
+
name_to_files = {}
|
|
609
|
+
for path in files:
|
|
610
|
+
try:
|
|
611
|
+
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
|
|
612
|
+
content = f.read()
|
|
613
|
+
|
|
614
|
+
# 1. Extract package
|
|
615
|
+
# package com.example.project
|
|
616
|
+
package_name = ""
|
|
617
|
+
pkg_match = re.search(r'^\s*package\s+([\w\.]+)', content, re.MULTILINE)
|
|
618
|
+
if pkg_match:
|
|
619
|
+
package_name = pkg_match.group(1)
|
|
620
|
+
|
|
621
|
+
# 2. Extract classes/objects/interfaces/typealiases
|
|
622
|
+
matches = re.finditer(r'\b(class|interface|object|typealias)\s+(\w+)', content)
|
|
623
|
+
|
|
624
|
+
for match in matches:
|
|
625
|
+
name = match.group(2)
|
|
626
|
+
# Map simple name
|
|
627
|
+
if name not in name_to_files:
|
|
628
|
+
name_to_files[name] = []
|
|
629
|
+
name_to_files[name].append(str(path))
|
|
630
|
+
|
|
631
|
+
# If package exists, map FQN
|
|
632
|
+
if package_name:
|
|
633
|
+
fqn = f"{package_name}.{name}"
|
|
634
|
+
if fqn not in name_to_files:
|
|
635
|
+
name_to_files[fqn] = []
|
|
636
|
+
name_to_files[fqn].append(str(path))
|
|
637
|
+
|
|
638
|
+
except Exception:
|
|
639
|
+
pass
|
|
640
|
+
return name_to_files
|