normalize-languages 0.3.2

Tree-sitter language support and dynamic grammar loading
Documentation
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
//! D language support.

use std::path::{Path, PathBuf};

use crate::{
    ContainerBody, Import, ImportSpec, Language, LanguageSymbols, ModuleId, ModuleResolver,
    Resolution, ResolverConfig, Visibility,
};
use tree_sitter::Node;

/// D language support.
pub struct D;

impl D {
    /// Recursively collect type names from a D inheritance clause.
    /// D nests: base_class_list > super_class_or_interface/interfaces/interface >
    /// qualified_identifier > identifier(s)
    fn collect_identifiers(node: &Node, content: &str, out: &mut Vec<String>) {
        if node.kind() == "qualified_identifier" {
            out.push(content[node.byte_range()].to_string());
            return;
        }
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            Self::collect_identifiers(&child, content, out);
        }
    }
}

impl Language for D {
    fn name(&self) -> &'static str {
        "D"
    }
    fn extensions(&self) -> &'static [&'static str] {
        &["d", "di"]
    }
    fn grammar_name(&self) -> &'static str {
        "d"
    }

    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> {
        let mut prev = node.prev_sibling();
        let mut doc_lines = Vec::new();
        while let Some(sibling) = prev {
            let text = &content[sibling.byte_range()];
            match sibling.kind() {
                "comment" => {
                    if text.starts_with("///") {
                        let line = text.strip_prefix("///").unwrap_or(text).trim();
                        if !line.is_empty() {
                            doc_lines.push(line.to_string());
                        }
                        prev = sibling.prev_sibling();
                    } else {
                        break;
                    }
                }
                "block_comment" => {
                    if text.starts_with("/**") {
                        let inner = text
                            .strip_prefix("/**")
                            .unwrap_or(text)
                            .strip_suffix("*/")
                            .unwrap_or(text);
                        for line in inner.lines() {
                            let clean = line.trim().strip_prefix('*').unwrap_or(line).trim();
                            if !clean.is_empty() {
                                doc_lines.push(clean.to_string());
                            }
                        }
                    }
                    break;
                }
                "nesting_block_comment" => {
                    if text.starts_with("/++") {
                        let inner = text
                            .strip_prefix("/++")
                            .unwrap_or(text)
                            .strip_suffix("+/")
                            .unwrap_or(text);
                        for line in inner.lines() {
                            let clean = line.trim().strip_prefix('+').unwrap_or(line).trim();
                            if !clean.is_empty() {
                                doc_lines.push(clean.to_string());
                            }
                        }
                    }
                    break;
                }
                _ => break,
            }
        }
        if doc_lines.is_empty() {
            return None;
        }
        doc_lines.reverse();
        Some(doc_lines.join(" "))
    }

    fn extract_attributes(&self, node: &Node, content: &str) -> Vec<String> {
        let mut attrs = Vec::new();
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "attribute_specifier" {
                let text = content[child.byte_range()].trim().to_string();
                if !text.is_empty() {
                    attrs.push(text);
                }
            }
        }
        let mut prev = node.prev_sibling();
        while let Some(sibling) = prev {
            if sibling.kind() == "attribute_specifier" {
                let text = content[sibling.byte_range()].trim().to_string();
                if !text.is_empty() {
                    attrs.insert(0, text);
                }
                prev = sibling.prev_sibling();
            } else {
                break;
            }
        }
        attrs
    }

    fn build_signature(&self, node: &Node, content: &str) -> String {
        let name = self.node_name(node, content).unwrap_or("");
        match node.kind() {
            "module_declaration" => format!("module {}", name),
            _ => {
                let text = &content[node.byte_range()];
                text.lines().next().unwrap_or(text).trim().to_string()
            }
        }
    }

    fn extract_implements(&self, node: &Node, content: &str) -> crate::ImplementsInfo {
        let mut implements = Vec::new();
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "base_class_list" {
                D::collect_identifiers(&child, content, &mut implements);
            }
        }
        crate::ImplementsInfo {
            is_interface: false,
            implements,
        }
    }

    fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
        if node.kind() != "import_declaration" {
            return Vec::new();
        }

        let text = &content[node.byte_range()];
        // Strip "import " prefix and trailing ";"
        let module = text
            .trim()
            .strip_prefix("import ")
            .unwrap_or(text.trim())
            .trim_end_matches(';')
            .trim()
            .to_string();
        let is_wildcard = module.contains(':');
        vec![Import {
            module,
            names: Vec::new(),
            alias: None,
            is_wildcard,
            is_relative: false,
            line: node.start_position().row + 1,
        }]
    }

    fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
        // D: import module; or import module : a, b, c;
        let names_to_use: Vec<&str> = names
            .map(|n| n.to_vec())
            .unwrap_or_else(|| import.names.iter().map(|s| s.as_str()).collect());
        if names_to_use.is_empty() {
            format!("import {};", import.module)
        } else {
            format!("import {} : {};", import.module, names_to_use.join(", "))
        }
    }

    fn get_visibility(&self, node: &Node, content: &str) -> Visibility {
        let text = &content[node.byte_range()];
        if text.starts_with("private ") {
            Visibility::Private
        } else if text.starts_with("protected ") {
            Visibility::Protected
        } else {
            Visibility::Public
        }
    }

    fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
        let name = symbol.name.as_str();
        match symbol.kind {
            crate::SymbolKind::Function | crate::SymbolKind::Method => name.starts_with("test_"),
            crate::SymbolKind::Module => name == "tests" || name == "test",
            _ => false,
        }
    }

    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 node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
        if let Some(name_node) = node.child_by_field_name("name") {
            return Some(&content[name_node.byte_range()]);
        }
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "identifier" {
                return Some(&content[child.byte_range()]);
            }
            // func_declaration: name is inside func_declarator
            if child.kind() == "func_declarator" {
                let mut inner = child.walk();
                for grandchild in child.children(&mut inner) {
                    if grandchild.kind() == "identifier" {
                        return Some(&content[grandchild.byte_range()]);
                    }
                }
            }
        }
        None
    }

    fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
        static RESOLVER: DModuleResolver = DModuleResolver;
        Some(&RESOLVER)
    }
}

impl LanguageSymbols for D {}

// =============================================================================
// D Module Resolver
// =============================================================================

/// Module resolver for D.
///
/// Uses `dub.json` at the workspace root to find source paths (`sourcePaths`,
/// default `["source"]`). Module names map to file paths: `mypackage.utils` →
/// `mypackage/utils.d` under a source root.
pub struct DModuleResolver;

impl ModuleResolver for DModuleResolver {
    fn workspace_config(&self, root: &Path) -> ResolverConfig {
        let mut search_roots: Vec<PathBuf> = Vec::new();

        // Try dub.json first
        let dub_json = root.join("dub.json");
        if let Ok(content) = std::fs::read_to_string(&dub_json)
            && let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&content)
            && let Some(paths) = parsed.get("sourcePaths").and_then(|v| v.as_array())
        {
            for path in paths {
                if let Some(s) = path.as_str() {
                    search_roots.push(root.join(s));
                }
            }
        }

        // Default to source/ (dub convention) if not found in config
        if search_roots.is_empty() {
            search_roots.push(root.join("source"));
        }

        ResolverConfig {
            workspace_root: root.to_path_buf(),
            path_mappings: Vec::new(),
            search_roots,
        }
    }

    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 != "d" && ext != "di" {
            return Vec::new();
        }

        for root in &cfg.search_roots {
            if let Ok(rel) = file.strip_prefix(root) {
                let rel_str = rel.to_string_lossy();
                // Strip .d/.di extension and replace / with .
                let base = rel_str
                    .strip_suffix(".di")
                    .or_else(|| rel_str.strip_suffix(".d"))
                    .unwrap_or(&rel_str);
                let canonical = if cfg!(windows) {
                    base.replace('\\', ".")
                } else {
                    base.replace('/', ".")
                };
                if !canonical.is_empty() {
                    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 != "d" && ext != "di" {
            return Resolution::NotApplicable;
        }

        // `mypackage.utils` → `mypackage/utils.d`
        let file_path = spec.raw.replace('.', "/") + ".d";

        for root in &cfg.search_roots {
            let candidate = root.join(&file_path);
            if candidate.exists() {
                return Resolution::Resolved(candidate, String::new());
            }
        }

        Resolution::NotFound
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::validate_unused_kinds_audit;

    #[test]
    fn unused_node_kinds_audit() {
        #[rustfmt::skip]
        let documented_unused: &[&str] = &[
            // Expressions
            "add_expression", "and_and_expression", "and_expression", "assign_expression",
            "assert_expression", "cat_expression", "cast_expression", "comma_expression",
            "complement_expression", "conditional_expression", "delete_expression", "equal_expression",
            "expression", "identity_expression", "import_expression", "in_expression",
            "index_expression", "is_expression", "key_expression", "lwr_expression",
            "mixin_expression", "mul_expression", "new_anon_class_expression", "new_expression",
            "or_expression", "or_or_expression", "postfix_expression", "pow_expression",
            "primary_expression", "qualified_identifier", "rel_expression", "shift_expression",
            "slice_expression", "traits_expression", "typeid_expression", "unary_expression",
            "upr_expression", "value_expression", "xor_expression",
            // Statements
            "asm_statement", "break_statement", "case_range_statement", "case_statement",
            "conditional_statement", "continue_statement", "declaration_statement", "default_statement",
            "do_statement", "empty_statement", "expression_statement", "final_switch_statement",
            "foreach_range_statement", "goto_statement", "labeled_statement", "mixin_statement",
            "out_statement", "pragma_statement", "return_statement", "scope_block_statement",
            "scope_guard_statement", "scope_statement_list", "statement_list",
            "statement_list_no_case_no_default", "static_foreach_statement", "synchronized_statement",
            "then_statement", "throw_statement", "try_statement", "with_statement",
            // Declarations
            "anonymous_enum_declaration", "anonymous_enum_member",
            "anonymous_enum_members", "anon_struct_declaration", "anon_union_declaration",
            "auto_func_declaration", "class_template_declaration",
            "conditional_declaration", "debug_specification", "destructor", "empty_declaration",
            "enum_body", "enum_member", "enum_member_attribute", "enum_member_attributes",
            "enum_members", "interface_template_declaration", "mixin_declaration",
            "module", "shared_static_constructor", "shared_static_destructor", "static_constructor",
            "static_destructor", "static_foreach_declaration", "struct_template_declaration",
            "template_declaration", "template_mixin_declaration", "union_declaration",
            "union_template_declaration", "var_declarations", "version_specification",
            // Foreach-related
            "aggregate_foreach", "foreach", "foreach_aggregate", "foreach_type",
            "foreach_type_attribute", "foreach_type_attributes", "foreach_type_list",
            "range_foreach", "static_foreach",
            // Function-related
            "constructor_args", "constructor_template", "function_attribute_kwd",
            "function_attributes", "function_contracts", "function_literal_body",
            "function_literal_body2", "member_function_attribute", "member_function_attributes",
            "missing_function_body", "out_contract_expression", "in_contract_expression",
            "in_statement", "parameter_with_attributes", "parameter_with_member_attributes",
            "shortened_function_body", "specified_function_body",
            // Template-related
            "template_type_parameter", "template_type_parameter_default",
            "template_type_parameter_specialization", "type_specialization",
            // Type-related
            "aggregate_body", "basic_type", "catch_parameter", "catches", "constructor",
            "else_statement", "enum_base_type", "finally_statement", "fundamental_type",
            "if_condition", "interfaces", "linkage_type", "module_alias_identifier",
            "module_attributes", "module_fully_qualified_name", "module_name", "mixin_type",
            "mixin_qualified_identifier", "storage_class", "storage_classes", "type",
            "type_ctor", "type_ctors", "type_suffix", "type_suffixes", "typeof", "interface",
            // Import-related
            "import", "import_bind", "import_bind_list", "import_bindings", "import_list",
            // ASM-related
            "asm_instruction", "asm_instruction_list", "asm_shift_exp", "asm_type_prefix",
            "gcc_asm_instruction_list", "gcc_asm_statement", "gcc_basic_asm_instruction",
            "gcc_ext_asm_instruction", "gcc_goto_asm_instruction",
            // Misc
            "alt_declarator_identifier", "base_class_list", "base_interface_list",
            "block_comment", "declaration_block", "declarator_identifier_list", "dot_identifier", "nesting_block_comment", "static_if_condition", "struct_initializer",
            "struct_member_initializer", "struct_member_initializers", "super_class_or_interface",
            "traits_arguments", "traits_keyword", "var_declarator_identifier", "vector_base_type",
            "attribute_specifier",
            // structural node, not extracted as symbols
            "alias_declaration",
            "auto_declaration",
            "module_declaration",
            "block_statement",
            "import_declaration",
            "while_statement",
            "switch_statement",
            "if_statement",
            "function_literal",
            "for_statement",
            "foreach_statement",
            "catch",
        ];
        validate_unused_kinds_audit(&D, documented_unused)
            .expect("D unused node kinds audit failed");
    }
}