normalize_languages/
kdl.rs1use crate::external_packages::ResolvedPackage;
4use crate::{Export, Import, Language, Symbol, SymbolKind, Visibility, VisibilityMechanism};
5use std::path::{Path, PathBuf};
6use tree_sitter::Node;
7
8pub struct Kdl;
10
11impl Language for Kdl {
12 fn name(&self) -> &'static str {
13 "KDL"
14 }
15 fn extensions(&self) -> &'static [&'static str] {
16 &["kdl"]
17 }
18 fn grammar_name(&self) -> &'static str {
19 "kdl"
20 }
21
22 fn has_symbols(&self) -> bool {
23 true
24 }
25
26 fn container_kinds(&self) -> &'static [&'static str] {
27 &["node"]
28 }
29
30 fn function_kinds(&self) -> &'static [&'static str] {
31 &[]
32 }
33 fn type_kinds(&self) -> &'static [&'static str] {
34 &[]
35 }
36 fn import_kinds(&self) -> &'static [&'static str] {
37 &[]
38 }
39
40 fn public_symbol_kinds(&self) -> &'static [&'static str] {
41 &["node"]
42 }
43
44 fn visibility_mechanism(&self) -> VisibilityMechanism {
45 VisibilityMechanism::AllPublic
46 }
47
48 fn extract_public_symbols(&self, node: &Node, content: &str) -> Vec<Export> {
49 if node.kind() != "node" {
50 return Vec::new();
51 }
52
53 let name = match self.node_name(node, content) {
54 Some(n) => n.to_string(),
55 None => return Vec::new(),
56 };
57
58 vec![Export {
59 name,
60 kind: SymbolKind::Module,
61 line: node.start_position().row + 1,
62 }]
63 }
64
65 fn scope_creating_kinds(&self) -> &'static [&'static str] {
66 &["node"]
67 }
68
69 fn control_flow_kinds(&self) -> &'static [&'static str] {
70 &[]
71 }
72 fn complexity_nodes(&self) -> &'static [&'static str] {
73 &[]
74 }
75 fn nesting_nodes(&self) -> &'static [&'static str] {
76 &["node"]
77 }
78
79 fn signature_suffix(&self) -> &'static str {
80 ""
81 }
82
83 fn extract_function(
84 &self,
85 _node: &Node,
86 _content: &str,
87 _in_container: bool,
88 ) -> Option<Symbol> {
89 None
90 }
91
92 fn extract_container(&self, node: &Node, content: &str) -> Option<Symbol> {
93 if node.kind() != "node" {
94 return None;
95 }
96
97 let name = self.node_name(node, content)?;
98 let text = &content[node.byte_range()];
99 let first_line = text.lines().next().unwrap_or(text);
100
101 Some(Symbol {
102 name: name.to_string(),
103 kind: SymbolKind::Module,
104 signature: first_line.trim().to_string(),
105 docstring: None,
106 attributes: Vec::new(),
107 start_line: node.start_position().row + 1,
108 end_line: node.end_position().row + 1,
109 visibility: Visibility::Public,
110 children: Vec::new(),
111 is_interface_impl: false,
112 implements: Vec::new(),
113 })
114 }
115
116 fn extract_type(&self, _node: &Node, _content: &str) -> Option<Symbol> {
117 None
118 }
119 fn extract_docstring(&self, _node: &Node, _content: &str) -> Option<String> {
120 None
121 }
122
123 fn extract_attributes(&self, _node: &Node, _content: &str) -> Vec<String> {
124 Vec::new()
125 }
126 fn extract_imports(&self, _node: &Node, _content: &str) -> Vec<Import> {
127 Vec::new()
128 }
129
130 fn format_import(&self, _import: &Import, _names: Option<&[&str]>) -> String {
131 String::new()
133 }
134
135 fn is_public(&self, _node: &Node, _content: &str) -> bool {
136 true
137 }
138 fn get_visibility(&self, _node: &Node, _content: &str) -> Visibility {
139 Visibility::Public
140 }
141
142 fn is_test_symbol(&self, _symbol: &crate::Symbol) -> bool {
143 false
144 }
145
146 fn embedded_content(&self, _node: &Node, _content: &str) -> Option<crate::EmbeddedBlock> {
147 None
148 }
149
150 fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
151 node.child_by_field_name("children")
152 }
153
154 fn body_has_docstring(&self, _body: &Node, _content: &str) -> bool {
155 false
156 }
157
158 fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
159 let mut cursor = node.walk();
161 for child in node.children(&mut cursor) {
162 if child.kind() == "identifier" || child.kind() == "string" {
163 return Some(&content[child.byte_range()]);
164 }
165 }
166 None
167 }
168
169 fn file_path_to_module_name(&self, path: &Path) -> Option<String> {
170 let ext = path.extension()?.to_str()?;
171 if ext != "kdl" {
172 return None;
173 }
174 let stem = path.file_stem()?.to_str()?;
175 Some(stem.to_string())
176 }
177
178 fn module_name_to_paths(&self, module: &str) -> Vec<String> {
179 vec![format!("{}.kdl", module)]
180 }
181
182 fn lang_key(&self) -> &'static str {
183 "kdl"
184 }
185
186 fn is_stdlib_import(&self, _: &str, _: &Path) -> bool {
187 false
188 }
189 fn find_stdlib(&self, _: &Path) -> Option<PathBuf> {
190 None
191 }
192 fn resolve_local_import(&self, _: &str, _: &Path, _: &Path) -> Option<PathBuf> {
193 None
194 }
195 fn resolve_external_import(&self, _: &str, _: &Path) -> Option<ResolvedPackage> {
196 None
197 }
198 fn get_version(&self, _: &Path) -> Option<String> {
199 None
200 }
201 fn find_package_cache(&self, _: &Path) -> Option<PathBuf> {
202 None
203 }
204 fn indexable_extensions(&self) -> &'static [&'static str] {
205 &["kdl"]
206 }
207 fn package_sources(&self, _: &Path) -> Vec<crate::PackageSource> {
208 Vec::new()
209 }
210
211 fn should_skip_package_entry(&self, name: &str, is_dir: bool) -> bool {
212 use crate::traits::{has_extension, skip_dotfiles};
213 if skip_dotfiles(name) {
214 return true;
215 }
216 !is_dir && !has_extension(name, self.indexable_extensions())
217 }
218
219 fn discover_packages(&self, _: &crate::PackageSource) -> Vec<(String, PathBuf)> {
220 Vec::new()
221 }
222
223 fn package_module_name(&self, entry_name: &str) -> String {
224 entry_name
225 .strip_suffix(".kdl")
226 .unwrap_or(entry_name)
227 .to_string()
228 }
229
230 fn find_package_entry(&self, path: &Path) -> Option<PathBuf> {
231 if path.is_file() {
232 Some(path.to_path_buf())
233 } else {
234 None
235 }
236 }
237}
238
239#[cfg(test)]
240mod tests {
241 use super::*;
242 use crate::validate_unused_kinds_audit;
243
244 #[test]
245 fn unused_node_kinds_audit() {
246 #[rustfmt::skip]
247 let documented_unused: &[&str] = &[
248 "annotation_type", "identifier_string", "multi_line_string_body", "type", ];
253 validate_unused_kinds_audit(&Kdl, documented_unused)
254 .expect("KDL unused node kinds audit failed");
255 }
256}