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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Per-language tree-sitter adapters: which AST nodes are definitions,
//! scopes, calls, and imports — and how to read docs/signatures off them.
use tree_sitter::Node;
use crate::extract::ImportRef;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lang {
Rust,
TypeScript,
Tsx,
Python,
Go,
Java,
Ruby,
C,
CSharp,
Sql,
}
impl Lang {
pub fn from_path(path: &str) -> Option<Lang> {
let ext = path.rsplit('.').next()?;
Some(match ext {
"rs" => Lang::Rust,
"ts" | "mts" | "cts" => Lang::TypeScript,
"tsx" | "jsx" | "js" | "mjs" | "cjs" => Lang::Tsx,
"py" | "pyi" => Lang::Python,
"go" => Lang::Go,
"java" => Lang::Java,
"rb" | "rake" => Lang::Ruby,
"c" | "h" => Lang::C,
"cs" | "csx" => Lang::CSharp,
"sql" => Lang::Sql,
_ => return None,
})
}
pub fn name(&self) -> &'static str {
match self {
Lang::Rust => "rust",
Lang::TypeScript | Lang::Tsx => "typescript",
Lang::Python => "python",
Lang::Go => "go",
Lang::Java => "java",
Lang::Ruby => "ruby",
Lang::C => "c",
Lang::CSharp => "csharp",
Lang::Sql => "sql",
}
}
pub fn language(&self) -> tree_sitter::Language {
match self {
Lang::Rust => tree_sitter_rust::LANGUAGE.into(),
Lang::TypeScript => tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
Lang::Tsx => tree_sitter_typescript::LANGUAGE_TSX.into(),
Lang::Python => tree_sitter_python::LANGUAGE.into(),
Lang::Go => tree_sitter_go::LANGUAGE.into(),
Lang::Java => tree_sitter_java::LANGUAGE.into(),
Lang::Ruby => tree_sitter_ruby::LANGUAGE.into(),
Lang::C => tree_sitter_c::LANGUAGE.into(),
Lang::CSharp => tree_sitter_c_sharp::LANGUAGE.into(),
Lang::Sql => tree_sitter_sequel_tsql::LANGUAGE.into(),
}
}
pub fn separator(&self) -> String {
"::".into()
}
/// Is this node a symbol definition? Returns (name, kind). The name may
/// be pre-qualified with `::` (Go methods carry their receiver).
pub fn definition(&self, node: Node, src: &str) -> Option<(String, &'static str)> {
let text = |n: Node| src[n.byte_range()].to_string();
match self {
Lang::Rust => match node.kind() {
"function_item" => Some((text(node.child_by_field_name("name")?), "function")),
"struct_item" => Some((text(node.child_by_field_name("name")?), "struct")),
"enum_item" => Some((text(node.child_by_field_name("name")?), "enum")),
"trait_item" => Some((text(node.child_by_field_name("name")?), "trait")),
"union_item" => Some((text(node.child_by_field_name("name")?), "struct")),
_ => None,
},
Lang::TypeScript | Lang::Tsx => match node.kind() {
"function_declaration" | "generator_function_declaration" => {
Some((text(node.child_by_field_name("name")?), "function"))
}
"class_declaration" => Some((text(node.child_by_field_name("name")?), "class")),
"method_definition" => {
let name = text(node.child_by_field_name("name")?);
if name == "constructor" {
return None;
}
Some((name, "method"))
}
"interface_declaration" => {
Some((text(node.child_by_field_name("name")?), "interface"))
}
"enum_declaration" => Some((text(node.child_by_field_name("name")?), "enum")),
"type_alias_declaration" => Some((text(node.child_by_field_name("name")?), "type")),
// const f = (..) => .. / const f = function(..) {..}
"variable_declarator" => {
let value = node.child_by_field_name("value")?;
if matches!(value.kind(), "arrow_function" | "function_expression") {
let name = node.child_by_field_name("name")?;
if name.kind() == "identifier" {
return Some((text(name), "function"));
}
}
None
}
_ => None,
},
Lang::Python => match node.kind() {
"function_definition" => {
Some((text(node.child_by_field_name("name")?), "function"))
}
"class_definition" => Some((text(node.child_by_field_name("name")?), "class")),
_ => None,
},
Lang::Go => match node.kind() {
"function_declaration" => {
Some((text(node.child_by_field_name("name")?), "function"))
}
"method_declaration" => {
let name = text(node.child_by_field_name("name")?);
let recv = node
.child_by_field_name("receiver")
.and_then(|r| receiver_type(r, src));
Some((
match recv {
Some(t) => format!("{t}::{name}"),
None => name,
},
"method",
))
}
"type_spec" => {
let name = text(node.child_by_field_name("name")?);
let kind = match node.child_by_field_name("type").map(|t| t.kind()) {
Some("struct_type") => "struct",
Some("interface_type") => "interface",
_ => "type",
};
Some((name, kind))
}
_ => None,
},
Lang::Java => match node.kind() {
"class_declaration" => Some((text(node.child_by_field_name("name")?), "class")),
"interface_declaration" => {
Some((text(node.child_by_field_name("name")?), "interface"))
}
"enum_declaration" => Some((text(node.child_by_field_name("name")?), "enum")),
"record_declaration" => Some((text(node.child_by_field_name("name")?), "class")),
"method_declaration" => Some((text(node.child_by_field_name("name")?), "method")),
"constructor_declaration" => {
Some((text(node.child_by_field_name("name")?), "method"))
}
_ => None,
},
Lang::Ruby => match node.kind() {
"class" => Some((const_name(node.child_by_field_name("name")?, src), "class")),
"module" => Some((const_name(node.child_by_field_name("name")?, src), "module")),
"method" => Some((text(node.child_by_field_name("name")?), "method")),
// def self.foo — a class method.
"singleton_method" => Some((text(node.child_by_field_name("name")?), "method")),
_ => None,
},
Lang::C => match node.kind() {
"function_definition" => Some((
c_declarator_name(node.child_by_field_name("declarator")?, src)?,
"function",
)),
"struct_specifier" => Some((text(node.child_by_field_name("name")?), "struct")),
"union_specifier" => Some((text(node.child_by_field_name("name")?), "struct")),
"enum_specifier" => Some((text(node.child_by_field_name("name")?), "enum")),
_ => None,
},
Lang::CSharp => match node.kind() {
// Both `namespace Foo { .. }` and file-scoped `namespace Foo;`
// — a scope that qualifies the types nested under it.
"namespace_declaration" | "file_scoped_namespace_declaration" => {
Some((text(node.child_by_field_name("name")?), "namespace"))
}
"class_declaration" => Some((text(node.child_by_field_name("name")?), "class")),
"interface_declaration" => {
Some((text(node.child_by_field_name("name")?), "interface"))
}
"struct_declaration" => Some((text(node.child_by_field_name("name")?), "struct")),
"enum_declaration" => Some((text(node.child_by_field_name("name")?), "enum")),
// Records are reference types by default; treat as a class.
"record_declaration" => Some((text(node.child_by_field_name("name")?), "class")),
"record_struct_declaration" => {
Some((text(node.child_by_field_name("name")?), "struct"))
}
"method_declaration" => Some((text(node.child_by_field_name("name")?), "method")),
"property_declaration" => {
Some((text(node.child_by_field_name("name")?), "property"))
}
// Constructor's name field is the enclosing type identifier.
"constructor_declaration" => {
Some((text(node.child_by_field_name("name")?), "constructor"))
}
_ => None,
},
Lang::Sql => match node.kind() {
"create_table" => sql_def_name(node, src).map(|n| (n, "table")),
"create_view" => sql_def_name(node, src).map(|n| (n, "view")),
"create_function" => sql_def_name(node, src).map(|n| (n, "function")),
"create_procedure" => sql_def_name(node, src).map(|n| (n, "procedure")),
_ => None,
},
}
}
/// Containers that qualify children without being symbols themselves.
pub fn scope_only(&self, node: Node, src: &str) -> Option<String> {
match self {
Lang::Rust => match node.kind() {
// impl Foo { .. } / impl Trait for Foo { .. } → scope "Foo"
"impl_item" => {
let ty = node.child_by_field_name("type")?;
Some(base_type_name(ty, src))
}
"mod_item" => Some(src[node.child_by_field_name("name")?.byte_range()].to_string()),
_ => None,
},
_ => None,
}
}
/// Field holding the body (cut point for signatures), per node kind.
pub fn body_field(&self) -> Option<&'static str> {
// All supported definition kinds use "body" except TS declarators,
// which signature_text handles via the generic fallback.
Some("body")
}
/// If this node is a call, return the bare callee name.
pub fn call(&self, node: Node, src: &str) -> Option<String> {
let text = |n: Node| src[n.byte_range()].to_string();
match self {
Lang::Rust => {
if node.kind() != "call_expression" {
return None;
}
let f = node.child_by_field_name("function")?;
match f.kind() {
"identifier" => Some(text(f)),
"field_expression" => f.child_by_field_name("field").map(text),
"scoped_identifier" => f.child_by_field_name("name").map(text),
"generic_function" => {
let inner = f.child_by_field_name("function")?;
match inner.kind() {
"identifier" => Some(text(inner)),
"scoped_identifier" => inner.child_by_field_name("name").map(text),
_ => None,
}
}
_ => None,
}
}
Lang::TypeScript | Lang::Tsx => {
if node.kind() != "call_expression" {
return None;
}
let f = node.child_by_field_name("function")?;
match f.kind() {
"identifier" => Some(text(f)),
"member_expression" => f.child_by_field_name("property").map(text),
_ => None,
}
}
Lang::Python => {
if node.kind() != "call" {
return None;
}
let f = node.child_by_field_name("function")?;
match f.kind() {
"identifier" => Some(text(f)),
"attribute" => f.child_by_field_name("attribute").map(text),
_ => None,
}
}
Lang::Go => {
if node.kind() != "call_expression" {
return None;
}
let f = node.child_by_field_name("function")?;
match f.kind() {
"identifier" => Some(text(f)),
"selector_expression" => f.child_by_field_name("field").map(text),
_ => None,
}
}
Lang::Java => {
if node.kind() != "method_invocation" {
return None;
}
node.child_by_field_name("name").map(text)
}
Lang::Ruby => {
// `foo(...)`, `obj.foo(...)`, `obj.foo` — the method name.
if node.kind() != "call" {
return None;
}
node.child_by_field_name("method").map(text)
}
Lang::C => {
if node.kind() != "call_expression" {
return None;
}
let f = node.child_by_field_name("function")?;
match f.kind() {
"identifier" => Some(text(f)),
_ => None,
}
}
Lang::CSharp => {
if node.kind() != "invocation_expression" {
return None;
}
let f = node.child_by_field_name("function")?;
match f.kind() {
"identifier" => Some(text(f)),
// obj.Method() / Type.Method() — the rightmost name.
"member_access_expression" => f.child_by_field_name("name").map(text),
_ => None,
}
}
Lang::Sql => {
// SQL has no calls; reuse the call edge for table dependencies.
// A table reference is an `object_reference` sitting in a query
// relation (FROM/JOIN), a DELETE/UPDATE `from` target, or a
// column's `REFERENCES` (foreign key) clause. The enclosing
// CREATE … is the caller, so the edge is view/proc/table → table.
if node.kind() != "object_reference" {
return None;
}
match node.parent()?.kind() {
"relation" | "from" | "column_definition" => {
Some(text(node.child_by_field_name("name").unwrap_or(node)))
}
_ => None,
}
}
}
}
/// Collect imports declared by this node.
pub fn imports(&self, node: Node, src: &str, out: &mut Vec<ImportRef>) {
let text = |n: Node| src[n.byte_range()].to_string();
match self {
Lang::Rust => {
if node.kind() == "use_declaration" {
if let Some(arg) = node.child_by_field_name("argument") {
rust_use_tree(arg, src, "", out);
}
}
}
Lang::TypeScript | Lang::Tsx => {
if node.kind() != "import_statement" {
return;
}
let Some(source) = node
.child_by_field_name("source")
.map(|s| text(s).trim_matches(['"', '\'']).to_string())
else {
return;
};
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() != "import_clause" {
continue;
}
let mut c2 = child.walk();
for part in child.children(&mut c2) {
match part.kind() {
"identifier" => out.push(ImportRef {
local: text(part),
source: source.clone(),
}),
"named_imports" => {
let mut c3 = part.walk();
for spec in part.children(&mut c3) {
if spec.kind() != "import_specifier" {
continue;
}
let local = spec
.child_by_field_name("alias")
.or_else(|| spec.child_by_field_name("name"))
.map(text);
if let Some(local) = local {
out.push(ImportRef {
local,
source: source.clone(),
});
}
}
}
"namespace_import" => {
// import * as ns from "x"
let mut c3 = part.walk();
for id in part.children(&mut c3) {
if id.kind() == "identifier" {
out.push(ImportRef {
local: text(id),
source: source.clone(),
});
}
}
}
_ => {}
}
}
}
}
Lang::Python => match node.kind() {
"import_statement" => {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"dotted_name" => out.push(ImportRef {
local: text(child)
.rsplit('.')
.next()
.unwrap_or_default()
.to_string(),
source: text(child),
}),
"aliased_import" => {
let name = child.child_by_field_name("name").map(text);
let alias = child.child_by_field_name("alias").map(text);
if let (Some(name), Some(alias)) = (name, alias) {
out.push(ImportRef {
local: alias,
source: name,
});
}
}
_ => {}
}
}
}
"import_from_statement" => {
let Some(module) = node.child_by_field_name("module_name").map(text) else {
return;
};
let mut cursor = node.walk();
let mut past_import = false;
for child in node.children(&mut cursor) {
if child.kind() == "import" {
past_import = true;
continue;
}
if !past_import {
continue;
}
match child.kind() {
"dotted_name" => out.push(ImportRef {
local: text(child),
source: module.clone(),
}),
"aliased_import" => {
if let Some(alias) = child.child_by_field_name("alias").map(text) {
out.push(ImportRef {
local: alias,
source: module.clone(),
});
}
}
_ => {}
}
}
}
_ => {}
},
Lang::Go => {
if node.kind() != "import_spec" {
return;
}
let Some(path) = node
.child_by_field_name("path")
.map(|p| text(p).trim_matches('"').to_string())
else {
return;
};
let local = node
.child_by_field_name("name")
.map(text)
.unwrap_or_else(|| path.rsplit('/').next().unwrap_or(&path).to_string());
out.push(ImportRef {
local,
source: path,
});
}
Lang::Java => {
// import a.b.C; / import static a.b.C.m; → bind the last segment.
if node.kind() != "import_declaration" {
return;
}
let mut cursor = node.walk();
let Some(scoped) = node
.children(&mut cursor)
.find(|c| c.kind() == "scoped_identifier")
else {
return;
};
let source = text(scoped);
let local = source.rsplit('.').next().unwrap_or(&source).to_string();
out.push(ImportRef { local, source });
}
Lang::C => {
// #include "foo.h" / <foo.h> → a file→file edge by path.
if node.kind() != "preproc_include" {
return;
}
let Some(path_node) = node.child_by_field_name("path") else {
return;
};
let source = text(path_node).trim_matches(['"', '<', '>']).to_string();
let local = source
.rsplit('/')
.next()
.unwrap_or(&source)
.trim_end_matches(".h")
.to_string();
out.push(ImportRef { local, source });
}
Lang::CSharp => {
// `using A.B;` / `using static A.B.C;` / `using X = A.B;` —
// bind the last namespace segment, or the alias when present.
if node.kind() != "using_directive" {
return;
}
let mut cursor = node.walk();
let names: Vec<String> = node
.children(&mut cursor)
.filter(|c| {
matches!(
c.kind(),
"identifier" | "qualified_name" | "alias_qualified_name"
)
})
.map(text)
.collect();
match names.as_slice() {
// `using Alias = Some.Namespace;`
[alias, source, ..] => out.push(ImportRef {
local: alias.clone(),
source: source.clone(),
}),
// `using Some.Namespace;` — local is the last segment.
[source] => out.push(ImportRef {
local: source.rsplit('.').next().unwrap_or(source).to_string(),
source: source.clone(),
}),
[] => {}
}
}
// Ruby's `require` is a method call, not an import node; calls
// still resolve same-file (tier 1) and globally by name (tier 3).
Lang::Ruby => {}
// SQL has no import construct.
Lang::Sql => {}
}
}
/// Doc comment attached to a definition node.
pub fn doc_comment(&self, node: Node, src: &str) -> Option<String> {
match self {
Lang::Python => {
// Docstring: first statement of the body is a string literal.
let body = node.child_by_field_name("body")?;
let first = body.named_child(0)?;
if first.kind() != "expression_statement" {
return None;
}
let s = first.named_child(0)?;
if s.kind() != "string" {
return None;
}
let raw = &src[s.byte_range()];
let cleaned = raw
.trim_start_matches(['r', 'b', 'f', 'u', 'R', 'B', 'F', 'U'])
.trim_matches(['"', '\''])
.trim();
Some(cleaned.lines().next().unwrap_or("").trim().to_string())
.filter(|s| !s.is_empty())
}
Lang::Rust
| Lang::Go
| Lang::TypeScript
| Lang::Tsx
| Lang::Java
| Lang::C
| Lang::CSharp
| Lang::Ruby
| Lang::Sql => {
// Contiguous comment siblings directly above the node
// (a blank line breaks the chain; `//!` belongs to the
// module, not this item).
// SQL wraps each `CREATE …` in a `statement`, so the comment
// is a sibling of that wrapper — climb to it first.
let mut anchor = node;
if matches!(self, Lang::Sql) {
while let Some(p) = anchor.parent() {
if p.kind() == "statement" {
anchor = p;
} else {
break;
}
}
}
let mut lines: Vec<String> = Vec::new();
let mut expect_row = anchor.start_position().row;
let mut prev = anchor.prev_sibling();
while let Some(p) = prev {
if !p.kind().contains("comment")
|| expect_row.saturating_sub(p.end_position().row) > 1
|| src[p.byte_range()].starts_with("//!")
{
break;
}
lines.push(src[p.byte_range()].to_string());
expect_row = p.start_position().row;
prev = p.prev_sibling();
}
if lines.is_empty() {
return None;
}
lines.reverse();
let cleaned: Vec<String> = lines
.iter()
.flat_map(|c| c.lines())
.map(|l| {
l.trim()
.trim_start_matches("///")
.trim_start_matches("//!")
.trim_start_matches("//")
.trim_start_matches("--") // SQL line comments
.trim_start_matches("/**")
.trim_start_matches("/*")
.trim_end_matches("*/")
.trim_start_matches('*')
.trim_start_matches('#') // Ruby line comments
.trim()
.to_string()
})
.filter(|l| !l.is_empty())
.collect();
if cleaned.is_empty() {
None
} else {
Some(cleaned.join(" ").chars().take(300).collect())
}
}
}
}
}
/// SQL `CREATE TABLE`/`VIEW`/`FUNCTION`/`PROCEDURE` name: the first
/// `object_reference` child; keep its bare `name` field (drops any schema
/// qualifier so `dbo.users` and `users` resolve to the same bucket).
fn sql_def_name(node: Node, src: &str) -> Option<String> {
let mut cursor = node.walk();
let obj = node
.children(&mut cursor)
.find(|c| c.kind() == "object_reference")?;
let name = obj.child_by_field_name("name").unwrap_or(obj);
Some(src[name.byte_range()].to_string())
}
/// `impl Foo`, `impl Foo<T>`, `impl Trait for Foo<T>` → "Foo".
fn base_type_name(ty: Node, src: &str) -> String {
match ty.kind() {
"generic_type" => ty
.child_by_field_name("type")
.map(|t| src[t.byte_range()].to_string())
.unwrap_or_else(|| src[ty.byte_range()].to_string()),
_ => src[ty.byte_range()].to_string(),
}
}
/// Ruby class/module name: a bare `constant` or `A::B` scope_resolution —
/// keep the last segment as the local name.
fn const_name(node: Node, src: &str) -> String {
let full = src[node.byte_range()].to_string();
full.rsplit("::").next().unwrap_or(&full).to_string()
}
/// C function name: unwrap pointer/function declarators down to the
/// identifier — `*foo(...)`, `foo(...)`, `(*foo)(...)` all yield "foo".
fn c_declarator_name(node: Node, src: &str) -> Option<String> {
match node.kind() {
"identifier" => Some(src[node.byte_range()].to_string()),
"function_declarator" | "pointer_declarator" | "parenthesized_declarator" => {
c_declarator_name(node.child_by_field_name("declarator")?, src)
}
_ => {
// Fall back to the first identifier descendant.
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(name) = c_declarator_name(child, src) {
return Some(name);
}
}
None
}
}
}
/// Go receiver `(s *Server)` → "Server".
fn receiver_type(receiver: Node, src: &str) -> Option<String> {
let mut cursor = receiver.walk();
for child in receiver.children(&mut cursor) {
if child.kind() == "parameter_declaration" {
let ty = child.child_by_field_name("type")?;
let base = match ty.kind() {
"pointer_type" => ty.named_child(0)?,
_ => ty,
};
return Some(src[base.byte_range()].to_string());
}
}
None
}
/// Rust use-tree walker: `use a::{b::C, d as E};` → C←a::b::C, E←a::d.
fn rust_use_tree(node: Node, src: &str, prefix: &str, out: &mut Vec<ImportRef>) {
let text = |n: Node| src[n.byte_range()].to_string();
let join = |prefix: &str, seg: &str| {
if prefix.is_empty() {
seg.to_string()
} else {
format!("{prefix}::{seg}")
}
};
match node.kind() {
"identifier" | "crate" | "self" | "super" => {
let seg = text(node);
out.push(ImportRef {
local: seg.clone(),
source: join(prefix, &seg),
});
}
"scoped_identifier" => {
let full = join(prefix, &text(node));
let local = node
.child_by_field_name("name")
.map(text)
.unwrap_or_default();
if !local.is_empty() {
out.push(ImportRef {
local,
source: full,
});
}
}
"use_as_clause" => {
let alias = node.child_by_field_name("alias").map(text);
let path = node.child_by_field_name("path").map(text);
if let (Some(alias), Some(path)) = (alias, path) {
out.push(ImportRef {
local: alias,
source: join(prefix, &path),
});
}
}
"scoped_use_list" => {
let new_prefix = node
.child_by_field_name("path")
.map(|p| join(prefix, &text(p)))
.unwrap_or_else(|| prefix.to_string());
if let Some(list) = node.child_by_field_name("list") {
let mut cursor = list.walk();
for child in list.named_children(&mut cursor) {
rust_use_tree(child, src, &new_prefix, out);
}
}
}
"use_list" => {
let mut cursor = node.walk();
for child in node.named_children(&mut cursor) {
rust_use_tree(child, src, prefix, out);
}
}
// use_wildcard and attributes: nothing useful to bind.
_ => {}
}
}