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