use crate::{ContainerBody, Import, Language, LanguageSymbols};
use tree_sitter::Node;
pub struct CMake;
impl Language for CMake {
fn name(&self) -> &'static str {
"CMake"
}
fn extensions(&self) -> &'static [&'static str] {
&["cmake"]
}
fn grammar_name(&self) -> &'static str {
"cmake"
}
fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
Some(self)
}
fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
if node.kind() != "normal_command" {
return Vec::new();
}
let text = &content[node.byte_range()];
let line = node.start_position().row + 1;
if text.starts_with("include(") || text.starts_with("find_package(") {
let inner = text
.split('(')
.nth(1)
.and_then(|s| s.split(')').next())
.map(|s| s.trim().to_string());
if let Some(module) = inner {
return vec![Import {
module,
names: Vec::new(),
alias: None,
is_wildcard: false,
is_relative: text.starts_with("include("),
line,
}];
}
}
Vec::new()
}
fn format_import(&self, import: &Import, _names: Option<&[&str]>) -> String {
format!("include({})", import.module)
}
fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
let mut c = node.walk();
node.children(&mut c).find(|&child| child.kind() == "body")
}
fn analyze_container_body(
&self,
body_node: &Node,
content: &str,
inner_indent: &str,
) -> Option<ContainerBody> {
crate::body::analyze_end_body(body_node, content, inner_indent)
}
fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "argument" {
return Some(&content[child.byte_range()]);
}
}
None
}
}
impl LanguageSymbols for CMake {}
#[cfg(test)]
mod tests {
use super::*;
use crate::validate_unused_kinds_audit;
#[test]
fn unused_node_kinds_audit() {
#[rustfmt::skip]
let documented_unused: &[&str] = &[
"block", "block_command", "block_def", "body", "else", "else_command",
"elseif", "endblock", "endblock_command", "endforeach", "endforeach_command",
"endfunction", "endfunction_command", "endif", "endif_command", "endwhile",
"endwhile_command", "foreach", "foreach_command", "function",
"identifier", "if", "if_command", "while",
"while_command",
"if_condition",
"foreach_loop",
"while_loop",
"elseif_command",
];
validate_unused_kinds_audit(&CMake, documented_unused)
.expect("CMake unused node kinds audit failed");
}
}