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
//! Tree-sitter parse + query execution → raw symbols and name-only edges.
use streaming_iterator::StreamingIterator;
use tree_sitter::{Parser, QueryCursor};
use crate::lang::support_for;
use crate::model::{EdgeKind, SymbolKind};
pub struct RawSymbol {
pub name: String,
pub kind: SymbolKind,
pub start_line: u32,
pub end_line: u32,
pub signature: Option<String>,
/// Enclosing type/class/trait name for a method (e.g. `ContextAssembler` for
/// `ContextAssembler::build`); `None` for free functions.
pub container: Option<String>,
}
pub struct RawEdge {
pub src_name: Option<String>,
pub dst_name: String,
pub kind: EdgeKind,
/// Enclosing container of the call site (e.g. the `impl`/module the call is
/// inside), used to disambiguate a call to a name defined more than once in
/// the same file. `None` when the call is at file scope.
pub src_container: Option<String>,
}
pub fn extract(lang: &str, source: &str) -> Option<(Vec<RawSymbol>, Vec<RawEdge>)> {
let support = support_for(lang)?;
let mut parser = Parser::new();
parser.set_language(&support.language()).ok()?;
let tree = parser.parse(source, None)?;
let query = support.query();
let mut symbols = Vec::new();
let mut edges = Vec::new();
walk(query, &tree, source.as_bytes(), &mut symbols, &mut edges);
Some((symbols, edges))
}
fn kind_from_suffix(suffix: &str) -> Option<SymbolKind> {
match suffix {
"function" => Some(SymbolKind::Function),
"method" => Some(SymbolKind::Method),
"class" => Some(SymbolKind::Class),
"struct" => Some(SymbolKind::Struct),
"enum" => Some(SymbolKind::Enum),
"trait" => Some(SymbolKind::Trait),
"interface" => Some(SymbolKind::Interface),
"module" => Some(SymbolKind::Module),
_ => None,
}
}
fn enclosing_symbol_name<'a>(
mut node: tree_sitter::Node<'a>,
query: &tree_sitter::Query,
source: &[u8],
) -> Option<String> {
// Walk up to the nearest node that a def.function or def.method capture matches.
// We detect this by checking if any capture on the query named def.function/def.method
// targets this node type. Instead of re-running the query, we walk parents and check
// node kinds that represent function/method definitions.
let def_kinds = [
"function_item", // rust
"function_definition", // python
"function_declaration", // ts/js
"method_definition", // ts/js
"arrow_function", // ts/js (anonymous, no name — skip)
];
// Get the capture names from the query so we can check if def.function / def.method exist
let cap_names: Vec<&str> = query.capture_names().iter().map(|s| s.as_ref()).collect();
let has_def_function = cap_names.contains(&"def.function");
let has_def_method = cap_names.contains(&"def.method");
if !has_def_function && !has_def_method {
return None;
}
while let Some(parent) = node.parent() {
let kind = parent.kind();
if def_kinds.contains(&kind) {
// Try to get child named "name"
if let Some(name_node) = parent.child_by_field_name("name") {
let name = name_node.utf8_text(source).ok()?.to_string();
return Some(name);
}
}
node = parent;
}
None
}
/// Walk up from a node to its nearest enclosing container (type/class/trait/
/// module) and return that container's name, or `None` at file scope.
///
/// Used two ways: for a *definition's* name node it gives the method's owning
/// type; for a *call site* it gives the scope the call lives in. Matching the two
/// disambiguates a name defined more than once in the same file. Covers Rust
/// `impl Type`/`impl Trait for Type` (the `type` field) and `mod name`, plus
/// class-like containers in ts/js/python (the `name` field).
fn container_of(name_node: tree_sitter::Node<'_>, source: &[u8]) -> Option<String> {
let mut node = name_node;
while let Some(parent) = node.parent() {
match parent.kind() {
// Rust: `impl ContextAssembler { ... }` / `impl Trait for T { ... }`.
// The `type` field is the type receiving the methods.
"impl_item" => {
if let Some(ty) = parent.child_by_field_name("type") {
return ty.utf8_text(source).ok().map(str::to_string);
}
}
// Rust module: `mod second { ... }`.
"mod_item" => {
if let Some(n) = parent.child_by_field_name("name") {
return n.utf8_text(source).ok().map(str::to_string);
}
}
// ts/js/python class-like containers name themselves via `name`.
"class_declaration" | "class_definition" | "class" | "interface_declaration" => {
if let Some(n) = parent.child_by_field_name("name") {
return n.utf8_text(source).ok().map(str::to_string);
}
}
_ => {}
}
node = parent;
}
None
}
fn walk(
query: &tree_sitter::Query,
tree: &tree_sitter::Tree,
source: &[u8],
symbols: &mut Vec<RawSymbol>,
edges: &mut Vec<RawEdge>,
) {
let mut cursor = QueryCursor::new();
let mut matches = cursor.matches(query, tree.root_node(), source);
while let Some(m) = matches.next() {
for cap in m.captures {
let cap_name = &query.capture_names()[cap.index as usize];
let node = cap.node;
if let Some(suffix) = cap_name.strip_prefix("def.") {
let Some(kind) = kind_from_suffix(suffix) else {
continue;
};
// The capture is on the name node; get the parent for line span.
let def_node = node.parent().unwrap_or(node);
let name = node.utf8_text(source).unwrap_or("").to_string();
if name.is_empty() {
continue;
}
// Signature: first line of the definition node
let start_byte = def_node.start_byte();
let end_byte = def_node.end_byte();
let def_text = std::str::from_utf8(&source[start_byte..end_byte]).unwrap_or("");
let signature = def_text.lines().next().map(|l| l.trim().to_string());
let start_line = def_node.start_position().row as u32 + 1;
let end_line = def_node.end_position().row as u32 + 1;
// A definition can match more than one query pattern (e.g. a Rust
// method inside an `impl` matches both `function_item` and the
// `impl_item`-method pattern). Dedup by span, keeping the more
// specific kind (Method over Function) so it is counted once.
if let Some(existing) = symbols
.iter_mut()
.find(|s: &&mut RawSymbol| s.name == name && s.start_line == start_line)
{
if kind == SymbolKind::Method && existing.kind == SymbolKind::Function {
existing.kind = SymbolKind::Method;
// Backfill the container now that we know it's a method:
// the earlier `function_item` capture set it to None.
existing.container = container_of(node, source);
}
continue;
}
// Every definition carries its nearest enclosing container: a
// method gets its `impl` type, a function inside a module gets the
// module, a file-scope function gets `None`. This qualified identity
// lets same-name defs in one file be told apart, and matches the
// container recorded on call-site edges for scope-aware resolution.
let container = container_of(node, source);
symbols.push(RawSymbol {
name,
kind,
start_line,
end_line,
signature,
container,
});
} else if cap_name.starts_with("call.") {
let dst_name = node.utf8_text(source).unwrap_or("").to_string();
if dst_name.is_empty() {
continue;
}
let src_name = enclosing_symbol_name(node, query, source);
let src_container = container_of(node, source);
edges.push(RawEdge {
src_name,
dst_name,
kind: EdgeKind::Calls,
src_container,
});
} else if cap_name.starts_with("import.") {
let dst_name = node.utf8_text(source).unwrap_or("").to_string();
if dst_name.is_empty() {
continue;
}
edges.push(RawEdge {
src_name: None,
dst_name,
kind: EdgeKind::Imports,
src_container: None,
});
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_rust_functions_and_calls() {
let src = "fn helper() {}\nfn main() { helper(); }\n";
let (symbols, edges) = extract("rust", src).expect("rust supported");
let names: Vec<_> = symbols.iter().map(|s| s.name.as_str()).collect();
assert!(names.contains(&"helper"));
assert!(names.contains(&"main"));
assert!(edges
.iter()
.any(|e| e.dst_name == "helper" && e.src_name.as_deref() == Some("main")));
}
#[test]
fn impl_methods_are_not_duplicated() {
// A method inside an impl block matches both the `function_item` and the
// `impl_item`-method query patterns; it must be extracted exactly once,
// and as a Method (the more specific kind), not a plain Function.
let src = "struct S;\nimpl S {\n fn method(&self) {}\n}\n";
let (symbols, _edges) = extract("rust", src).unwrap();
let methods: Vec<_> = symbols.iter().filter(|s| s.name == "method").collect();
assert_eq!(methods.len(), 1, "impl method must not be double-counted");
assert_eq!(methods[0].kind, SymbolKind::Method);
}
#[test]
fn extracts_python_defs() {
let src = "def a():\n b()\ndef b():\n pass\n";
let (symbols, edges) = extract("python", src).unwrap();
assert!(symbols.iter().any(|s| s.name == "a"));
assert!(edges
.iter()
.any(|e| e.dst_name == "b" && e.src_name.as_deref() == Some("a")));
}
#[test]
fn extracts_typescript_functions() {
let src = "function f() { g(); }\nfunction g() {}\n";
let (symbols, edges) = extract("typescript", src).unwrap();
assert!(symbols.iter().any(|s| s.name == "f"));
assert!(edges
.iter()
.any(|e| e.dst_name == "g" && e.src_name.as_deref() == Some("f")));
}
}