Skip to main content

dk_engine/parser/langs/
kotlin.rs

1//! Kotlin language configuration for the query-driven parser.
2
3use crate::parser::lang_config::{CommentStyle, LanguageConfig};
4use dk_core::{Symbol, SymbolKind, Visibility};
5use tree_sitter::Language;
6
7/// Kotlin language configuration for [`QueryDrivenParser`](crate::parser::engine::QueryDrivenParser).
8pub struct KotlinConfig;
9
10impl LanguageConfig for KotlinConfig {
11    fn language(&self) -> Language {
12        tree_sitter_kotlin_codanna::language()
13    }
14
15    fn extensions(&self) -> &'static [&'static str] {
16        &["kt", "kts"]
17    }
18
19    fn symbols_query(&self) -> &'static str {
20        include_str!("../queries/kotlin_symbols.scm")
21    }
22
23    fn calls_query(&self) -> &'static str {
24        include_str!("../queries/kotlin_calls.scm")
25    }
26
27    fn imports_query(&self) -> &'static str {
28        include_str!("../queries/kotlin_imports.scm")
29    }
30
31    fn comment_style(&self) -> CommentStyle {
32        CommentStyle::SlashSlash
33    }
34
35    fn resolve_visibility(&self, _modifiers: Option<&str>, _name: &str) -> Visibility {
36        // Kotlin defaults to public. Actual visibility modifiers are
37        // handled in `adjust_symbol` by walking the AST.
38        Visibility::Public
39    }
40
41    fn adjust_symbol(&self, sym: &mut Symbol, node: &tree_sitter::Node, source: &[u8]) {
42        // Walk children to find `modifiers` and process ALL modifier types
43        // (visibility, class_modifier, inheritance_modifier) before returning.
44        let mut cursor = node.walk();
45        for child in node.children(&mut cursor) {
46            if child.kind() == "modifiers" {
47                let mut mod_cursor = child.walk();
48                for modifier in child.children(&mut mod_cursor) {
49                    if modifier.kind() == "visibility_modifier" {
50                        let text = &source[modifier.start_byte()..modifier.end_byte()];
51                        let text_str = std::str::from_utf8(text).unwrap_or("");
52                        match text_str.trim() {
53                            "private" | "internal" => {
54                                sym.visibility = Visibility::Private;
55                            }
56                            "protected" => {
57                                sym.visibility = Visibility::Private;
58                            }
59                            "public" => {
60                                sym.visibility = Visibility::Public;
61                            }
62                            _ => {}
63                        }
64                        // Do NOT return here — fall through so class_modifier
65                        // (enum) and the interface-keyword check still run.
66                    }
67                    if modifier.kind() == "class_modifier" {
68                        let text = &source[modifier.start_byte()..modifier.end_byte()];
69                        let text_str = std::str::from_utf8(text).unwrap_or("");
70                        if text_str.trim() == "enum" {
71                            sym.kind = SymbolKind::Enum;
72                        }
73                    }
74                    if modifier.kind() == "inheritance_modifier" {
75                        let text = &source[modifier.start_byte()..modifier.end_byte()];
76                        let text_str = std::str::from_utf8(text).unwrap_or("");
77                        if text_str.trim() == "abstract" {
78                            // Keep as class
79                        }
80                    }
81                }
82            }
83        }
84
85        // Check if this is an interface (the "interface" keyword appears
86        // as a direct child of class_declaration, not inside modifiers).
87        if node.kind() == "class_declaration" && sym.kind == SymbolKind::Class {
88            let mut cursor2 = node.walk();
89            for child in node.children(&mut cursor2) {
90                let text = &source[child.start_byte()..child.end_byte()];
91                let text_str = std::str::from_utf8(text).unwrap_or("");
92                if text_str == "interface" {
93                    sym.kind = SymbolKind::Interface;
94                    break;
95                }
96            }
97        }
98    }
99
100    fn is_external_import(&self, _module_path: &str) -> bool {
101        // Without Gradle/Maven context we can't distinguish internal
102        // vs external packages. Treat all as external.
103        true
104    }
105}