blastdiff 0.1.0__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.
- blastdiff/__init__.py +3 -0
- blastdiff/__main__.py +8 -0
- blastdiff/cli.py +448 -0
- blastdiff/core/__init__.py +5 -0
- blastdiff/core/blast_radius.py +456 -0
- blastdiff/core/diff_parser.py +368 -0
- blastdiff/core/languages/__init__.py +17 -0
- blastdiff/core/languages/base.py +111 -0
- blastdiff/core/languages/go_lang.py +219 -0
- blastdiff/core/languages/python_lang.py +226 -0
- blastdiff/core/languages/rust_lang.py +230 -0
- blastdiff/core/languages/ts_lang.py +406 -0
- blastdiff/core/models.py +281 -0
- blastdiff/core/proof.py +257 -0
- blastdiff/core/story.py +566 -0
- blastdiff/core/symbol_impact.py +698 -0
- blastdiff/engine.py +398 -0
- blastdiff/export.py +160 -0
- blastdiff/git_adapter.py +503 -0
- blastdiff/mcp_server.py +589 -0
- blastdiff/server.py +438 -0
- blastdiff/store.py +502 -0
- blastdiff/web/app.js +2387 -0
- blastdiff/web/index.html +272 -0
- blastdiff/web/styles.css +2092 -0
- blastdiff-0.1.0.dist-info/METADATA +186 -0
- blastdiff-0.1.0.dist-info/RECORD +31 -0
- blastdiff-0.1.0.dist-info/WHEEL +5 -0
- blastdiff-0.1.0.dist-info/entry_points.txt +2 -0
- blastdiff-0.1.0.dist-info/licenses/LICENSE +21 -0
- blastdiff-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"""Tree-sitter based Go AST analyzer."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
import tree_sitter
|
|
8
|
+
import tree_sitter_go
|
|
9
|
+
|
|
10
|
+
from blastdiff.core.models import CallSite, FileSymbols, ImportRef, SymbolDef
|
|
11
|
+
|
|
12
|
+
_WS_RE = re.compile(r"\s+")
|
|
13
|
+
_MAX_SNIPPET_LEN = 160
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _collapse_ws(text: str) -> str:
|
|
17
|
+
collapsed = _WS_RE.sub(" ", text).strip()
|
|
18
|
+
collapsed = re.sub(r"\(\s+", "(", collapsed)
|
|
19
|
+
collapsed = re.sub(r"\s+\)", ")", collapsed)
|
|
20
|
+
collapsed = re.sub(r",\s*\)", ")", collapsed)
|
|
21
|
+
return collapsed
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _node_text(node: tree_sitter.Node | None, source_bytes: bytes) -> str:
|
|
25
|
+
if node is None:
|
|
26
|
+
return ""
|
|
27
|
+
return source_bytes[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _strip_quotes(raw: str) -> str:
|
|
31
|
+
raw = raw.strip()
|
|
32
|
+
if len(raw) >= 2 and raw[0] == raw[-1] and raw[0] in ('"', "`"):
|
|
33
|
+
return raw[1:-1]
|
|
34
|
+
return raw
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _extract_receiver_type(receiver_node: tree_sitter.Node | None, source_bytes: bytes) -> str:
|
|
38
|
+
if receiver_node is None:
|
|
39
|
+
return ""
|
|
40
|
+
for child in receiver_node.children:
|
|
41
|
+
if child.type == "parameter_declaration":
|
|
42
|
+
type_node = child.child_by_field_name("type")
|
|
43
|
+
if type_node is not None:
|
|
44
|
+
raw = _node_text(type_node, source_bytes).strip().lstrip("*")
|
|
45
|
+
# Strip generic type arguments like Box[T]
|
|
46
|
+
return raw.split("[", 1)[0].strip()
|
|
47
|
+
return ""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class GoAnalyzer:
|
|
51
|
+
"""Extracts Go symbol definitions, imports, and call sites via tree-sitter."""
|
|
52
|
+
|
|
53
|
+
language: str = "go"
|
|
54
|
+
extensions: tuple[str, ...] = (".go",)
|
|
55
|
+
|
|
56
|
+
def __init__(self) -> None:
|
|
57
|
+
self._language = tree_sitter.Language(tree_sitter_go.language())
|
|
58
|
+
self._parser = tree_sitter.Parser(self._language)
|
|
59
|
+
|
|
60
|
+
def analyze_file(self, file_path: str, source: str) -> FileSymbols:
|
|
61
|
+
if not source:
|
|
62
|
+
return FileSymbols(file_path=file_path, language=self.language)
|
|
63
|
+
|
|
64
|
+
source_bytes = source.encode("utf-8", errors="replace")
|
|
65
|
+
source_lines = source.splitlines()
|
|
66
|
+
tree = self._parser.parse(source_bytes)
|
|
67
|
+
|
|
68
|
+
definitions: list[SymbolDef] = []
|
|
69
|
+
imports: list[ImportRef] = []
|
|
70
|
+
calls: list[CallSite] = []
|
|
71
|
+
|
|
72
|
+
def snippet_for_line(lineno: int) -> str:
|
|
73
|
+
if 1 <= lineno <= len(source_lines):
|
|
74
|
+
return source_lines[lineno - 1].strip()[:_MAX_SNIPPET_LEN]
|
|
75
|
+
return ""
|
|
76
|
+
|
|
77
|
+
def extract_callee_name(fn_node: tree_sitter.Node | None) -> str:
|
|
78
|
+
if fn_node is None:
|
|
79
|
+
return ""
|
|
80
|
+
if fn_node.type == "identifier":
|
|
81
|
+
return _node_text(fn_node, source_bytes).strip()
|
|
82
|
+
if fn_node.type == "selector_expression":
|
|
83
|
+
field_node = fn_node.child_by_field_name("field")
|
|
84
|
+
return _node_text(field_node, source_bytes).strip()
|
|
85
|
+
if fn_node.type == "parenthesized_expression":
|
|
86
|
+
for c in fn_node.named_children:
|
|
87
|
+
return extract_callee_name(c)
|
|
88
|
+
return ""
|
|
89
|
+
|
|
90
|
+
def visit(node: tree_sitter.Node, enclosing_symbol: str | None) -> None:
|
|
91
|
+
ntype = node.type
|
|
92
|
+
|
|
93
|
+
if ntype == "import_spec":
|
|
94
|
+
line = node.start_point.row + 1
|
|
95
|
+
path_node = node.child_by_field_name("path")
|
|
96
|
+
name_node = node.child_by_field_name("name")
|
|
97
|
+
mod_path = _strip_quotes(_node_text(path_node, source_bytes))
|
|
98
|
+
alias = _node_text(name_node, source_bytes).strip()
|
|
99
|
+
names = [alias] if alias and alias not in (".", "_") else []
|
|
100
|
+
if mod_path:
|
|
101
|
+
imports.append(
|
|
102
|
+
ImportRef(module=mod_path, names=names, line=line, file_path=file_path)
|
|
103
|
+
)
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
if ntype == "type_spec":
|
|
107
|
+
name_node = node.child_by_field_name("name")
|
|
108
|
+
type_node = node.child_by_field_name("type")
|
|
109
|
+
name = _node_text(name_node, source_bytes).strip()
|
|
110
|
+
if name:
|
|
111
|
+
if type_node is not None and type_node.type == "struct_type":
|
|
112
|
+
kind = "class"
|
|
113
|
+
sig = f"type {name} struct"
|
|
114
|
+
elif type_node is not None and type_node.type == "interface_type":
|
|
115
|
+
kind = "interface"
|
|
116
|
+
sig = f"type {name} interface"
|
|
117
|
+
else:
|
|
118
|
+
kind = "type"
|
|
119
|
+
sig = _collapse_ws(f"type {name} {_node_text(type_node, source_bytes)}")[:140]
|
|
120
|
+
definitions.append(
|
|
121
|
+
SymbolDef(
|
|
122
|
+
name=name,
|
|
123
|
+
qualified_name=name,
|
|
124
|
+
kind=kind, # type: ignore[arg-type]
|
|
125
|
+
signature=sig,
|
|
126
|
+
start_line=node.start_point.row + 1,
|
|
127
|
+
end_line=node.end_point.row + 1,
|
|
128
|
+
exported=bool(name and name[0].isupper()),
|
|
129
|
+
file_path=file_path,
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
return
|
|
133
|
+
|
|
134
|
+
if ntype == "function_declaration":
|
|
135
|
+
name_node = node.child_by_field_name("name")
|
|
136
|
+
params_node = node.child_by_field_name("parameters")
|
|
137
|
+
result_node = node.child_by_field_name("result")
|
|
138
|
+
body_node = node.child_by_field_name("body")
|
|
139
|
+
name = _node_text(name_node, source_bytes).strip()
|
|
140
|
+
if name:
|
|
141
|
+
params_txt = _node_text(params_node, source_bytes) or "()"
|
|
142
|
+
result_txt = _node_text(result_node, source_bytes).strip()
|
|
143
|
+
sig_raw = f"func {name}{params_txt} {result_txt}" if result_txt else f"func {name}{params_txt}"
|
|
144
|
+
definitions.append(
|
|
145
|
+
SymbolDef(
|
|
146
|
+
name=name,
|
|
147
|
+
qualified_name=name,
|
|
148
|
+
kind="function",
|
|
149
|
+
signature=_collapse_ws(sig_raw),
|
|
150
|
+
start_line=node.start_point.row + 1,
|
|
151
|
+
end_line=node.end_point.row + 1,
|
|
152
|
+
exported=bool(name and name[0].isupper()),
|
|
153
|
+
file_path=file_path,
|
|
154
|
+
)
|
|
155
|
+
)
|
|
156
|
+
if body_node is not None:
|
|
157
|
+
visit(body_node, name)
|
|
158
|
+
return
|
|
159
|
+
|
|
160
|
+
if ntype == "method_declaration":
|
|
161
|
+
recv_node = node.child_by_field_name("receiver")
|
|
162
|
+
name_node = node.child_by_field_name("name")
|
|
163
|
+
params_node = node.child_by_field_name("parameters")
|
|
164
|
+
result_node = node.child_by_field_name("result")
|
|
165
|
+
body_node = node.child_by_field_name("body")
|
|
166
|
+
name = _node_text(name_node, source_bytes).strip()
|
|
167
|
+
if name:
|
|
168
|
+
recv_type = _extract_receiver_type(recv_node, source_bytes)
|
|
169
|
+
qualified = f"{recv_type}.{name}" if recv_type else name
|
|
170
|
+
recv_txt = _node_text(recv_node, source_bytes)
|
|
171
|
+
params_txt = _node_text(params_node, source_bytes) or "()"
|
|
172
|
+
result_txt = _node_text(result_node, source_bytes).strip()
|
|
173
|
+
sig_raw = (
|
|
174
|
+
f"func {recv_txt} {name}{params_txt} {result_txt}"
|
|
175
|
+
if result_txt
|
|
176
|
+
else f"func {recv_txt} {name}{params_txt}"
|
|
177
|
+
)
|
|
178
|
+
definitions.append(
|
|
179
|
+
SymbolDef(
|
|
180
|
+
name=name,
|
|
181
|
+
qualified_name=qualified,
|
|
182
|
+
kind="method",
|
|
183
|
+
signature=_collapse_ws(sig_raw),
|
|
184
|
+
start_line=node.start_point.row + 1,
|
|
185
|
+
end_line=node.end_point.row + 1,
|
|
186
|
+
exported=bool(name and name[0].isupper()),
|
|
187
|
+
file_path=file_path,
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
if body_node is not None:
|
|
191
|
+
visit(body_node, qualified)
|
|
192
|
+
return
|
|
193
|
+
|
|
194
|
+
if ntype == "call_expression":
|
|
195
|
+
fn_node = node.child_by_field_name("function")
|
|
196
|
+
callee = extract_callee_name(fn_node)
|
|
197
|
+
if callee:
|
|
198
|
+
line = node.start_point.row + 1
|
|
199
|
+
calls.append(
|
|
200
|
+
CallSite(
|
|
201
|
+
callee_name=callee,
|
|
202
|
+
line=line,
|
|
203
|
+
enclosing_symbol=enclosing_symbol,
|
|
204
|
+
file_path=file_path,
|
|
205
|
+
snippet=snippet_for_line(line),
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
for child in node.children:
|
|
210
|
+
visit(child, enclosing_symbol)
|
|
211
|
+
|
|
212
|
+
visit(tree.root_node, None)
|
|
213
|
+
return FileSymbols(
|
|
214
|
+
file_path=file_path,
|
|
215
|
+
language=self.language,
|
|
216
|
+
definitions=definitions,
|
|
217
|
+
imports=imports,
|
|
218
|
+
calls=calls,
|
|
219
|
+
)
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""Tree-sitter based Python AST analyzer."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
import tree_sitter
|
|
8
|
+
import tree_sitter_python
|
|
9
|
+
|
|
10
|
+
from blastdiff.core.models import CallSite, FileSymbols, ImportRef, SymbolDef
|
|
11
|
+
|
|
12
|
+
_WS_RE = re.compile(r"\s+")
|
|
13
|
+
_MAX_SNIPPET_LEN = 160
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _collapse_ws(text: str) -> str:
|
|
17
|
+
collapsed = _WS_RE.sub(" ", text).strip()
|
|
18
|
+
collapsed = re.sub(r"\(\s+", "(", collapsed)
|
|
19
|
+
collapsed = re.sub(r"\s+\)", ")", collapsed)
|
|
20
|
+
collapsed = re.sub(r",\s*\)", ")", collapsed)
|
|
21
|
+
return collapsed
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _node_text(node: tree_sitter.Node | None, source_bytes: bytes) -> str:
|
|
25
|
+
if node is None:
|
|
26
|
+
return ""
|
|
27
|
+
return source_bytes[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class PythonAnalyzer:
|
|
31
|
+
"""Extracts Python symbol definitions, imports, and call sites via tree-sitter."""
|
|
32
|
+
|
|
33
|
+
language: str = "python"
|
|
34
|
+
extensions: tuple[str, ...] = (".py", ".pyi")
|
|
35
|
+
|
|
36
|
+
def __init__(self) -> None:
|
|
37
|
+
self._language = tree_sitter.Language(tree_sitter_python.language())
|
|
38
|
+
self._parser = tree_sitter.Parser(self._language)
|
|
39
|
+
|
|
40
|
+
def analyze_file(self, file_path: str, source: str) -> FileSymbols:
|
|
41
|
+
if not source:
|
|
42
|
+
return FileSymbols(file_path=file_path, language=self.language)
|
|
43
|
+
|
|
44
|
+
source_bytes = source.encode("utf-8", errors="replace")
|
|
45
|
+
source_lines = source.splitlines()
|
|
46
|
+
tree = self._parser.parse(source_bytes)
|
|
47
|
+
|
|
48
|
+
definitions: list[SymbolDef] = []
|
|
49
|
+
imports: list[ImportRef] = []
|
|
50
|
+
calls: list[CallSite] = []
|
|
51
|
+
|
|
52
|
+
def snippet_for_line(lineno: int) -> str:
|
|
53
|
+
if 1 <= lineno <= len(source_lines):
|
|
54
|
+
return source_lines[lineno - 1].strip()[:_MAX_SNIPPET_LEN]
|
|
55
|
+
return ""
|
|
56
|
+
|
|
57
|
+
def visit(
|
|
58
|
+
node: tree_sitter.Node,
|
|
59
|
+
class_stack: list[str],
|
|
60
|
+
enclosing_symbol: str | None,
|
|
61
|
+
inside_function: bool,
|
|
62
|
+
) -> None:
|
|
63
|
+
ntype = node.type
|
|
64
|
+
|
|
65
|
+
if ntype == "import_statement":
|
|
66
|
+
line = node.start_point.row + 1
|
|
67
|
+
for child in node.children:
|
|
68
|
+
if child.type == "dotted_name":
|
|
69
|
+
mod = _node_text(child, source_bytes).strip()
|
|
70
|
+
if mod:
|
|
71
|
+
imports.append(
|
|
72
|
+
ImportRef(module=mod, names=[], line=line, file_path=file_path)
|
|
73
|
+
)
|
|
74
|
+
elif child.type == "aliased_import":
|
|
75
|
+
name_node = child.child_by_field_name("name")
|
|
76
|
+
alias_node = child.child_by_field_name("alias")
|
|
77
|
+
mod = _node_text(name_node, source_bytes).strip()
|
|
78
|
+
alias = _node_text(alias_node, source_bytes).strip()
|
|
79
|
+
if mod:
|
|
80
|
+
imports.append(
|
|
81
|
+
ImportRef(
|
|
82
|
+
module=mod,
|
|
83
|
+
names=[alias] if alias else [],
|
|
84
|
+
line=line,
|
|
85
|
+
file_path=file_path,
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
return
|
|
89
|
+
|
|
90
|
+
if ntype == "import_from_statement":
|
|
91
|
+
line = node.start_point.row + 1
|
|
92
|
+
mod_node = node.child_by_field_name("module_name")
|
|
93
|
+
module_name = _node_text(mod_node, source_bytes).strip()
|
|
94
|
+
imported_names: list[str] = []
|
|
95
|
+
seen_import_kw = False
|
|
96
|
+
for child in node.children:
|
|
97
|
+
if child.type == "import":
|
|
98
|
+
seen_import_kw = True
|
|
99
|
+
continue
|
|
100
|
+
if not seen_import_kw:
|
|
101
|
+
continue
|
|
102
|
+
if child.type == "dotted_name":
|
|
103
|
+
name_txt = _node_text(child, source_bytes).strip()
|
|
104
|
+
if name_txt:
|
|
105
|
+
imported_names.append(name_txt)
|
|
106
|
+
elif child.type == "aliased_import":
|
|
107
|
+
orig_node = child.child_by_field_name("name")
|
|
108
|
+
orig_txt = _node_text(orig_node, source_bytes).strip()
|
|
109
|
+
if orig_txt:
|
|
110
|
+
imported_names.append(orig_txt)
|
|
111
|
+
elif child.type == "wildcard_import":
|
|
112
|
+
imported_names.append("*")
|
|
113
|
+
if module_name or imported_names:
|
|
114
|
+
imports.append(
|
|
115
|
+
ImportRef(
|
|
116
|
+
module=module_name,
|
|
117
|
+
names=imported_names,
|
|
118
|
+
line=line,
|
|
119
|
+
file_path=file_path,
|
|
120
|
+
)
|
|
121
|
+
)
|
|
122
|
+
return
|
|
123
|
+
|
|
124
|
+
if ntype == "class_definition":
|
|
125
|
+
name_node = node.child_by_field_name("name")
|
|
126
|
+
name = _node_text(name_node, source_bytes).strip()
|
|
127
|
+
if name:
|
|
128
|
+
qualified = ".".join([*class_stack, name]) if class_stack else name
|
|
129
|
+
super_node = node.child_by_field_name("superclasses")
|
|
130
|
+
super_txt = _node_text(super_node, source_bytes).strip()
|
|
131
|
+
sig = _collapse_ws(f"class {name}{super_txt}" if super_txt else f"class {name}")
|
|
132
|
+
definitions.append(
|
|
133
|
+
SymbolDef(
|
|
134
|
+
name=name,
|
|
135
|
+
qualified_name=qualified,
|
|
136
|
+
kind="class",
|
|
137
|
+
signature=sig,
|
|
138
|
+
start_line=node.start_point.row + 1,
|
|
139
|
+
end_line=node.end_point.row + 1,
|
|
140
|
+
exported=(not inside_function) and (not name.startswith("_")),
|
|
141
|
+
file_path=file_path,
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
body = node.child_by_field_name("body")
|
|
145
|
+
if body is not None:
|
|
146
|
+
next_classes = [*class_stack, name]
|
|
147
|
+
for child in body.children:
|
|
148
|
+
visit(child, next_classes, qualified, inside_function)
|
|
149
|
+
return
|
|
150
|
+
|
|
151
|
+
if ntype == "function_definition":
|
|
152
|
+
name_node = node.child_by_field_name("name")
|
|
153
|
+
name = _node_text(name_node, source_bytes).strip()
|
|
154
|
+
if name:
|
|
155
|
+
is_method = bool(class_stack) and not inside_function
|
|
156
|
+
kind = "method" if is_method else "function"
|
|
157
|
+
if is_method:
|
|
158
|
+
qualified = f"{'.'.join(class_stack)}.{name}"
|
|
159
|
+
elif enclosing_symbol and inside_function:
|
|
160
|
+
qualified = f"{enclosing_symbol}.{name}"
|
|
161
|
+
else:
|
|
162
|
+
qualified = name
|
|
163
|
+
|
|
164
|
+
is_async = any(c.type == "async" for c in node.children)
|
|
165
|
+
prefix = "async def " if is_async else "def "
|
|
166
|
+
params_node = node.child_by_field_name("parameters")
|
|
167
|
+
params_txt = _node_text(params_node, source_bytes).strip() or "()"
|
|
168
|
+
ret_node = node.child_by_field_name("return_type")
|
|
169
|
+
ret_txt = _node_text(ret_node, source_bytes).strip()
|
|
170
|
+
raw_sig = f"{prefix}{name}{params_txt}"
|
|
171
|
+
if ret_txt:
|
|
172
|
+
raw_sig = f"{raw_sig} -> {ret_txt}"
|
|
173
|
+
sig = _collapse_ws(raw_sig)
|
|
174
|
+
|
|
175
|
+
exported = (not inside_function) and (not name.startswith("_"))
|
|
176
|
+
definitions.append(
|
|
177
|
+
SymbolDef(
|
|
178
|
+
name=name,
|
|
179
|
+
qualified_name=qualified,
|
|
180
|
+
kind=kind,
|
|
181
|
+
signature=sig,
|
|
182
|
+
start_line=node.start_point.row + 1,
|
|
183
|
+
end_line=node.end_point.row + 1,
|
|
184
|
+
exported=exported,
|
|
185
|
+
file_path=file_path,
|
|
186
|
+
)
|
|
187
|
+
)
|
|
188
|
+
body = node.child_by_field_name("body")
|
|
189
|
+
if body is not None:
|
|
190
|
+
for child in body.children:
|
|
191
|
+
visit(child, class_stack, qualified, True)
|
|
192
|
+
return
|
|
193
|
+
|
|
194
|
+
if ntype == "call":
|
|
195
|
+
func_node = node.child_by_field_name("function")
|
|
196
|
+
callee_name = ""
|
|
197
|
+
if func_node is not None:
|
|
198
|
+
if func_node.type == "identifier":
|
|
199
|
+
callee_name = _node_text(func_node, source_bytes).strip()
|
|
200
|
+
elif func_node.type == "attribute":
|
|
201
|
+
attr_node = func_node.child_by_field_name("attribute")
|
|
202
|
+
callee_name = _node_text(attr_node, source_bytes).strip()
|
|
203
|
+
if callee_name:
|
|
204
|
+
line = node.start_point.row + 1
|
|
205
|
+
calls.append(
|
|
206
|
+
CallSite(
|
|
207
|
+
callee_name=callee_name,
|
|
208
|
+
line=line,
|
|
209
|
+
enclosing_symbol=enclosing_symbol,
|
|
210
|
+
file_path=file_path,
|
|
211
|
+
snippet=snippet_for_line(line),
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
for child in node.children:
|
|
216
|
+
visit(child, class_stack, enclosing_symbol, inside_function)
|
|
217
|
+
|
|
218
|
+
visit(tree.root_node, [], None, False)
|
|
219
|
+
|
|
220
|
+
return FileSymbols(
|
|
221
|
+
file_path=file_path,
|
|
222
|
+
language=self.language,
|
|
223
|
+
definitions=definitions,
|
|
224
|
+
imports=imports,
|
|
225
|
+
calls=calls,
|
|
226
|
+
)
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"""Tree-sitter based Rust AST analyzer."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
import tree_sitter
|
|
8
|
+
import tree_sitter_rust
|
|
9
|
+
|
|
10
|
+
from blastdiff.core.models import CallSite, FileSymbols, ImportRef, SymbolDef
|
|
11
|
+
|
|
12
|
+
_WS_RE = re.compile(r"\s+")
|
|
13
|
+
_MAX_SNIPPET_LEN = 160
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _collapse_ws(text: str) -> str:
|
|
17
|
+
collapsed = _WS_RE.sub(" ", text).strip()
|
|
18
|
+
collapsed = re.sub(r"\(\s+", "(", collapsed)
|
|
19
|
+
collapsed = re.sub(r"\s+\)", ")", collapsed)
|
|
20
|
+
collapsed = re.sub(r",\s*\)", ")", collapsed)
|
|
21
|
+
return collapsed
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _node_text(node: tree_sitter.Node | None, source_bytes: bytes) -> str:
|
|
25
|
+
if node is None:
|
|
26
|
+
return ""
|
|
27
|
+
return source_bytes[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _has_pub_visibility(node: tree_sitter.Node, source_bytes: bytes) -> bool:
|
|
31
|
+
for child in node.children:
|
|
32
|
+
if child.type == "visibility_modifier":
|
|
33
|
+
txt = _node_text(child, source_bytes).strip()
|
|
34
|
+
return txt.startswith("pub")
|
|
35
|
+
return False
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _parse_use_clause(raw_use: str) -> list[tuple[str, list[str]]]:
|
|
39
|
+
"""Parse Rust `use crate::foo::{a, b};` into `(module_path, imported_names)` pairs."""
|
|
40
|
+
cleaned = raw_use.strip()
|
|
41
|
+
if cleaned.startswith("pub "):
|
|
42
|
+
cleaned = re.sub(r"^pub(?:\([^)]*\))?\s+", "", cleaned)
|
|
43
|
+
if cleaned.startswith("use "):
|
|
44
|
+
cleaned = cleaned[4:].strip()
|
|
45
|
+
cleaned = cleaned.rstrip(";").strip()
|
|
46
|
+
if not cleaned:
|
|
47
|
+
return []
|
|
48
|
+
|
|
49
|
+
if "{" in cleaned and "}" in cleaned:
|
|
50
|
+
prefix, rest = cleaned.split("{", 1)
|
|
51
|
+
mod = prefix.rstrip(":").strip().replace("::", "/")
|
|
52
|
+
inner = rest.rsplit("}", 1)[0]
|
|
53
|
+
names: list[str] = []
|
|
54
|
+
for item in inner.split(","):
|
|
55
|
+
part = item.strip()
|
|
56
|
+
if not part or part == "self":
|
|
57
|
+
continue
|
|
58
|
+
part = part.split(" as ", 1)[0].strip()
|
|
59
|
+
leaf = part.rsplit("::", 1)[-1].strip()
|
|
60
|
+
if leaf:
|
|
61
|
+
names.append(leaf)
|
|
62
|
+
return [(mod, names)]
|
|
63
|
+
|
|
64
|
+
base = cleaned.split(" as ", 1)[0].strip()
|
|
65
|
+
if "::" in base:
|
|
66
|
+
mod_part, leaf = base.rsplit("::", 1)
|
|
67
|
+
return [(mod_part.replace("::", "/"), [leaf] if leaf != "*" else ["*"])]
|
|
68
|
+
return [(base, [])]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class RustAnalyzer:
|
|
72
|
+
"""Extracts Rust symbol definitions, imports, and call sites via tree-sitter."""
|
|
73
|
+
|
|
74
|
+
language: str = "rust"
|
|
75
|
+
extensions: tuple[str, ...] = (".rs",)
|
|
76
|
+
|
|
77
|
+
def __init__(self) -> None:
|
|
78
|
+
self._language = tree_sitter.Language(tree_sitter_rust.language())
|
|
79
|
+
self._parser = tree_sitter.Parser(self._language)
|
|
80
|
+
|
|
81
|
+
def analyze_file(self, file_path: str, source: str) -> FileSymbols:
|
|
82
|
+
if not source:
|
|
83
|
+
return FileSymbols(file_path=file_path, language=self.language)
|
|
84
|
+
|
|
85
|
+
source_bytes = source.encode("utf-8", errors="replace")
|
|
86
|
+
source_lines = source.splitlines()
|
|
87
|
+
tree = self._parser.parse(source_bytes)
|
|
88
|
+
|
|
89
|
+
definitions: list[SymbolDef] = []
|
|
90
|
+
imports: list[ImportRef] = []
|
|
91
|
+
calls: list[CallSite] = []
|
|
92
|
+
|
|
93
|
+
def snippet_for_line(lineno: int) -> str:
|
|
94
|
+
if 1 <= lineno <= len(source_lines):
|
|
95
|
+
return source_lines[lineno - 1].strip()[:_MAX_SNIPPET_LEN]
|
|
96
|
+
return ""
|
|
97
|
+
|
|
98
|
+
def extract_callee_name(fn_node: tree_sitter.Node | None) -> str:
|
|
99
|
+
if fn_node is None:
|
|
100
|
+
return ""
|
|
101
|
+
if fn_node.type == "identifier":
|
|
102
|
+
return _node_text(fn_node, source_bytes).strip()
|
|
103
|
+
if fn_node.type == "scoped_identifier":
|
|
104
|
+
name_node = fn_node.child_by_field_name("name")
|
|
105
|
+
return _node_text(name_node, source_bytes).strip()
|
|
106
|
+
if fn_node.type == "field_expression":
|
|
107
|
+
field_node = fn_node.child_by_field_name("field")
|
|
108
|
+
return _node_text(field_node, source_bytes).strip()
|
|
109
|
+
if fn_node.type == "generic_function":
|
|
110
|
+
inner = fn_node.child_by_field_name("function")
|
|
111
|
+
return extract_callee_name(inner)
|
|
112
|
+
return ""
|
|
113
|
+
|
|
114
|
+
def visit(
|
|
115
|
+
node: tree_sitter.Node,
|
|
116
|
+
impl_type: str | None,
|
|
117
|
+
enclosing_symbol: str | None,
|
|
118
|
+
) -> None:
|
|
119
|
+
ntype = node.type
|
|
120
|
+
|
|
121
|
+
if ntype == "use_declaration":
|
|
122
|
+
line = node.start_point.row + 1
|
|
123
|
+
raw = _node_text(node, source_bytes)
|
|
124
|
+
for mod, names in _parse_use_clause(raw):
|
|
125
|
+
imports.append(
|
|
126
|
+
ImportRef(module=mod, names=names, line=line, file_path=file_path)
|
|
127
|
+
)
|
|
128
|
+
return
|
|
129
|
+
|
|
130
|
+
if ntype in ("struct_item", "enum_item", "trait_item", "type_item"):
|
|
131
|
+
name_node = node.child_by_field_name("name")
|
|
132
|
+
name = _node_text(name_node, source_bytes).strip()
|
|
133
|
+
if name:
|
|
134
|
+
kind_map = {
|
|
135
|
+
"struct_item": "class",
|
|
136
|
+
"enum_item": "type",
|
|
137
|
+
"trait_item": "interface",
|
|
138
|
+
"type_item": "type",
|
|
139
|
+
}
|
|
140
|
+
kw_map = {
|
|
141
|
+
"struct_item": "struct",
|
|
142
|
+
"enum_item": "enum",
|
|
143
|
+
"trait_item": "trait",
|
|
144
|
+
"type_item": "type",
|
|
145
|
+
}
|
|
146
|
+
exported = _has_pub_visibility(node, source_bytes)
|
|
147
|
+
prefix = "pub " if exported else ""
|
|
148
|
+
definitions.append(
|
|
149
|
+
SymbolDef(
|
|
150
|
+
name=name,
|
|
151
|
+
qualified_name=name,
|
|
152
|
+
kind=kind_map[ntype], # type: ignore[arg-type]
|
|
153
|
+
signature=f"{prefix}{kw_map[ntype]} {name}",
|
|
154
|
+
start_line=node.start_point.row + 1,
|
|
155
|
+
end_line=node.end_point.row + 1,
|
|
156
|
+
exported=exported,
|
|
157
|
+
file_path=file_path,
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
return
|
|
161
|
+
|
|
162
|
+
if ntype == "impl_item":
|
|
163
|
+
type_node = node.child_by_field_name("type")
|
|
164
|
+
type_name = _node_text(type_node, source_bytes).strip().split("<", 1)[0].strip()
|
|
165
|
+
body = node.child_by_field_name("body")
|
|
166
|
+
if body is not None:
|
|
167
|
+
for child in body.children:
|
|
168
|
+
visit(child, type_name or impl_type, enclosing_symbol)
|
|
169
|
+
return
|
|
170
|
+
|
|
171
|
+
if ntype == "function_item":
|
|
172
|
+
name_node = node.child_by_field_name("name")
|
|
173
|
+
params_node = node.child_by_field_name("parameters")
|
|
174
|
+
ret_node = node.child_by_field_name("return_type")
|
|
175
|
+
body_node = node.child_by_field_name("body")
|
|
176
|
+
name = _node_text(name_node, source_bytes).strip()
|
|
177
|
+
if name:
|
|
178
|
+
is_method = impl_type is not None
|
|
179
|
+
qualified = f"{impl_type}.{name}" if is_method else name
|
|
180
|
+
exported = _has_pub_visibility(node, source_bytes)
|
|
181
|
+
params_txt = _node_text(params_node, source_bytes) or "()"
|
|
182
|
+
ret_txt = _node_text(ret_node, source_bytes).strip()
|
|
183
|
+
pub_prefix = "pub " if exported else ""
|
|
184
|
+
sig_raw = (
|
|
185
|
+
f"{pub_prefix}fn {name}{params_txt} -> {ret_txt}"
|
|
186
|
+
if ret_txt
|
|
187
|
+
else f"{pub_prefix}fn {name}{params_txt}"
|
|
188
|
+
)
|
|
189
|
+
definitions.append(
|
|
190
|
+
SymbolDef(
|
|
191
|
+
name=name,
|
|
192
|
+
qualified_name=qualified,
|
|
193
|
+
kind="method" if is_method else "function",
|
|
194
|
+
signature=_collapse_ws(sig_raw),
|
|
195
|
+
start_line=node.start_point.row + 1,
|
|
196
|
+
end_line=node.end_point.row + 1,
|
|
197
|
+
exported=exported,
|
|
198
|
+
file_path=file_path,
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
if body_node is not None:
|
|
202
|
+
visit(body_node, impl_type, qualified)
|
|
203
|
+
return
|
|
204
|
+
|
|
205
|
+
if ntype == "call_expression":
|
|
206
|
+
fn_node = node.child_by_field_name("function")
|
|
207
|
+
callee = extract_callee_name(fn_node)
|
|
208
|
+
if callee:
|
|
209
|
+
line = node.start_point.row + 1
|
|
210
|
+
calls.append(
|
|
211
|
+
CallSite(
|
|
212
|
+
callee_name=callee,
|
|
213
|
+
line=line,
|
|
214
|
+
enclosing_symbol=enclosing_symbol,
|
|
215
|
+
file_path=file_path,
|
|
216
|
+
snippet=snippet_for_line(line),
|
|
217
|
+
)
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
for child in node.children:
|
|
221
|
+
visit(child, impl_type, enclosing_symbol)
|
|
222
|
+
|
|
223
|
+
visit(tree.root_node, None, None)
|
|
224
|
+
return FileSymbols(
|
|
225
|
+
file_path=file_path,
|
|
226
|
+
language=self.language,
|
|
227
|
+
definitions=definitions,
|
|
228
|
+
imports=imports,
|
|
229
|
+
calls=calls,
|
|
230
|
+
)
|