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
//! Go language support.
use std::path::{Path, PathBuf};
use crate::docstring::extract_preceding_prefix_comments;
use crate::{
ContainerBody, Import, ImportSpec, Language, LanguageSymbols, ModuleId, ModuleResolver,
Resolution, ResolverConfig, Visibility,
};
use tree_sitter::Node;
/// Go language support.
pub struct Go;
impl Language for Go {
fn name(&self) -> &'static str {
"Go"
}
fn extensions(&self) -> &'static [&'static str] {
&["go"]
}
fn grammar_name(&self) -> &'static str {
"go"
}
fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
Some(self)
}
fn signature_suffix(&self) -> &'static str {
" {}"
}
fn extract_docstring(&self, node: &Node, content: &str) -> Option<String> {
extract_preceding_prefix_comments(node, content, "//")
}
fn refine_kind(
&self,
node: &Node,
_content: &str,
tag_kind: crate::SymbolKind,
) -> crate::SymbolKind {
// Go type_spec wraps the actual type (struct_type, interface_type, etc.)
if node.kind() == "type_spec"
&& let Some(type_node) = node.child_by_field_name("type")
{
return match type_node.kind() {
"struct_type" => crate::SymbolKind::Struct,
"interface_type" => crate::SymbolKind::Interface,
_ => tag_kind,
};
}
tag_kind
}
fn build_signature(&self, node: &Node, content: &str) -> String {
let name = match self.node_name(node, content) {
Some(n) => n,
None => {
return content[node.byte_range()]
.lines()
.next()
.unwrap_or("")
.trim()
.to_string();
}
};
match node.kind() {
"function_declaration" | "method_declaration" => {
let params = node
.child_by_field_name("parameters")
.map(|p| content[p.byte_range()].to_string())
.unwrap_or_else(|| "()".to_string());
format!("func {}{}", name, params)
}
"type_spec" => format!("type {}", name),
_ => {
let text = &content[node.byte_range()];
text.lines().next().unwrap_or(text).trim().to_string()
}
}
}
fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
if node.kind() != "import_declaration" {
return Vec::new();
}
let mut imports = Vec::new();
let line = node.start_position().row + 1;
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"import_spec" => {
// import "path" or import alias "path"
if let Some(imp) = Self::parse_import_spec(&child, content, line) {
imports.push(imp);
}
}
"import_spec_list" => {
// Grouped imports
let mut list_cursor = child.walk();
for spec in child.children(&mut list_cursor) {
if spec.kind() == "import_spec"
&& let Some(imp) = Self::parse_import_spec(&spec, content, line)
{
imports.push(imp);
}
}
}
_ => {}
}
}
imports
}
fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
// Go: import "pkg" or import alias "pkg"
if let Some(ref alias) = import.alias {
format!("import {} \"{}\"", alias, import.module)
} else {
format!("import \"{}\"", import.module)
}
}
fn get_visibility(&self, node: &Node, content: &str) -> Visibility {
let is_exported = self
.node_name(node, content)
.and_then(|n| n.chars().next())
.map(|c| c.is_uppercase())
.unwrap_or(false);
if is_exported {
Visibility::Public
} else {
Visibility::Private
}
}
fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
match symbol.kind {
crate::SymbolKind::Function => {
let name = symbol.name.as_str();
name.starts_with("Test")
|| name.starts_with("Benchmark")
|| name.starts_with("Example")
}
_ => false,
}
}
fn test_file_globs(&self) -> &'static [&'static str] {
&["**/*_test.go"]
}
fn extract_module_doc(&self, src: &str) -> Option<String> {
extract_go_package_doc(src)
}
fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
node.child_by_field_name("body")
}
fn analyze_container_body(
&self,
body_node: &Node,
content: &str,
inner_indent: &str,
) -> Option<ContainerBody> {
crate::body::analyze_brace_body(body_node, content, inner_indent)
}
fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
static RESOLVER: GoModuleResolver = GoModuleResolver;
Some(&RESOLVER)
}
}
impl LanguageSymbols for Go {}
// =============================================================================
// Go Module Resolver
// =============================================================================
/// Module resolver for Go.
///
/// Uses `go.mod` at the workspace root to extract the module path.
/// In Go, a package = a directory, so all `.go` files in the same directory
/// belong to the same package (same import path).
pub struct GoModuleResolver;
impl ModuleResolver for GoModuleResolver {
fn workspace_config(&self, root: &Path) -> ResolverConfig {
let mut path_mappings: Vec<(String, PathBuf)> = Vec::new();
let go_mod = root.join("go.mod");
if let Ok(content) = std::fs::read_to_string(&go_mod) {
// Parse `module <path>` line
for line in content.lines() {
let trimmed = line.trim();
if let Some(module_path) = trimmed.strip_prefix("module ") {
let module_path = module_path.trim().to_string();
path_mappings.push((module_path, root.to_path_buf()));
break;
}
}
}
ResolverConfig {
workspace_root: root.to_path_buf(),
path_mappings,
search_roots: Vec::new(),
}
}
fn module_of_file(&self, _root: &Path, file: &Path, cfg: &ResolverConfig) -> Vec<ModuleId> {
let ext = file.extension().and_then(|e| e.to_str()).unwrap_or("");
if ext != "go" {
return Vec::new();
}
// Package import path = module path + path from root to file's directory
for (module_path, module_root) in &cfg.path_mappings {
let file_dir = match file.parent() {
Some(d) => d,
None => continue,
};
if let Ok(rel) = file_dir.strip_prefix(module_root) {
let rel_str = rel
.components()
.filter_map(|c| {
if let std::path::Component::Normal(s) = c {
s.to_str()
} else {
None
}
})
.collect::<Vec<_>>()
.join("/");
let canonical = if rel_str.is_empty() {
module_path.clone()
} else {
format!("{}/{}", module_path, rel_str)
};
return vec![ModuleId {
canonical_path: canonical,
}];
}
}
Vec::new()
}
fn resolve(&self, from_file: &Path, spec: &ImportSpec, cfg: &ResolverConfig) -> Resolution {
let ext = from_file.extension().and_then(|e| e.to_str()).unwrap_or("");
if ext != "go" {
return Resolution::NotApplicable;
}
let raw = &spec.raw;
// Check if the import path starts with our module path
for (module_path, module_root) in &cfg.path_mappings {
if raw == module_path {
// Importing the root package itself
return Resolution::Resolved(module_root.clone(), String::new());
}
if let Some(rest) = raw.strip_prefix(&format!("{}/", module_path)) {
// rest is the subdirectory path within the module
let target_dir = module_root.join(rest);
if target_dir.is_dir() {
return Resolution::Resolved(target_dir, String::new());
}
return Resolution::NotFound;
}
}
// Not in this module (stdlib or third-party)
Resolution::NotFound
}
}
/// Extract the Go package comment from source.
///
/// The Go convention is a block of `//` comments immediately before
/// the `package` keyword. Scans backwards from the `package` line.
/// A blank line between the comment and `package` means it is NOT a doc comment.
fn extract_go_package_doc(src: &str) -> Option<String> {
let lines: Vec<&str> = src.lines().collect();
// Find the package declaration line
let pkg_idx = lines.iter().position(|l| {
let t = l.trim();
t.starts_with("package ") || t == "package"
})?;
// A blank line immediately before package means no doc comment
if pkg_idx > 0 && lines[pkg_idx - 1].trim().is_empty() {
return None;
}
// Collect comment lines immediately preceding the package line
let mut doc_lines: Vec<&str> = Vec::new();
let mut idx = pkg_idx;
while idx > 0 {
idx -= 1;
let t = lines[idx].trim();
if t.starts_with("//") {
doc_lines.push(t);
} else {
break;
}
}
if doc_lines.is_empty() {
return None;
}
// Reverse to get lines in original order and strip `//` prefix
doc_lines.reverse();
let text = doc_lines
.iter()
.map(|l| l.trim_start_matches("//").trim_start())
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string();
if text.is_empty() { None } else { Some(text) }
}
impl Go {
fn parse_import_spec(node: &Node, content: &str, line: usize) -> Option<Import> {
let mut path = String::new();
let mut alias = None;
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
match child.kind() {
"interpreted_string_literal" => {
let text = &content[child.byte_range()];
path = text.trim_matches('"').to_string();
}
"package_identifier" | "blank_identifier" | "dot" => {
alias = Some(content[child.byte_range()].to_string());
}
_ => {}
}
}
if path.is_empty() {
return None;
}
let is_wildcard = alias.as_deref() == Some(".");
Some(Import {
module: path,
names: Vec::new(),
alias,
is_wildcard,
is_relative: false, // Go doesn't have relative imports in the traditional sense
line,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Documents node kinds that exist in the Go grammar but aren't used in trait methods.
/// Run `cross_check_node_kinds` in registry.rs to see all potentially useful kinds.
#[test]
fn unused_node_kinds_audit() {
use crate::validate_unused_kinds_audit;
#[rustfmt::skip]
let documented_unused: &[&str] = &[
// STRUCTURAL
"blank_identifier", // _
"field_declaration", // struct field
"field_declaration_list", // struct body
"field_identifier", // field name // too common // package foo
"package_identifier", // package name
"parameter_declaration", // func param
"statement_list", // block contents
"variadic_parameter_declaration", // ...T
// CLAUSE
"default_case", // default:
"for_clause", // for init; cond; post
"import_spec", // import spec
"import_spec_list", // import block
"method_elem", // interface method
"range_clause", // for range
// EXPRESSION // foo()
"index_expression", // arr[i]// (expr) // foo.bar
"slice_expression", // arr[1:3]
"type_assertion_expression", // x.(T)
"type_conversion_expression", // T(x)
"type_instantiation_expression", // generic instantiation
"unary_expression", // -x, !x
// TYPE
"array_type", // [N]T
"channel_type", // chan T
"implicit_length_array_type", // [...]T
"function_type", // func(T) U
"generic_type", // T[U]
"interface_type", // interface{}
"map_type", // map[K]V
"negated_type", // ~T
"parenthesized_type", // (T)
"pointer_type", // *T
"qualified_type", // pkg.Type
"slice_type", // []T
"struct_type", // struct{}
"type_arguments", // [T, U]
"type_constraint", // T constraint
"type_elem", // type element // type name
"type_parameter_declaration", // [T any]
"type_parameter_list", // type params
// DECLARATION
"assignment_statement", // x = y // const x = 1
"dec_statement", // x--
"expression_list", // a, b, c
"expression_statement", // expr
"inc_statement", // x++
"short_var_declaration", // x := y
"type_alias", // type X = Y // type X struct{} // var x int
// CONTROL FLOW DETAILS
"empty_statement", // ;
"fallthrough_statement", // fallthrough
"go_statement", // go foo()
"labeled_statement", // label:
"receive_statement", // <-ch
"send_statement", // ch <- x
// control flow — not extracted as symbols
"return_statement",
"continue_statement",
"break_statement",
"if_statement",
"for_statement",
"goto_statement",
"expression_switch_statement",
"expression_case",
"type_case",
"type_switch_statement",
"select_statement",
"block",
"defer_statement",
"binary_expression",
"communication_case",
];
validate_unused_kinds_audit(&Go, documented_unused)
.expect("Go unused node kinds audit failed");
}
}