use crate::{Import, Language, LanguageSymbols};
use tree_sitter::Node;
pub struct Typst;
impl Language for Typst {
fn name(&self) -> &'static str {
"Typst"
}
fn extensions(&self) -> &'static [&'static str] {
&["typ"]
}
fn grammar_name(&self) -> &'static str {
"typst"
}
fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
Some(self)
}
fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
if node.kind() != "import" {
return Vec::new();
}
let text = &content[node.byte_range()];
vec![Import {
module: text.trim().to_string(),
names: Vec::new(),
alias: None,
is_wildcard: text.contains('*'),
is_relative: false,
line: node.start_position().row + 1,
}]
}
fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
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 if import.is_wildcard {
format!("#import \"{}\": *", import.module)
} else {
format!("#import \"{}\": {}", import.module, names_to_use.join(", "))
}
}
}
impl LanguageSymbols for Typst {}
#[cfg(test)]
mod tests {
use super::*;
use crate::validate_unused_kinds_audit;
#[test]
fn unused_node_kinds_audit() {
#[rustfmt::skip]
let documented_unused: &[&str] = &[
"formula",
"return",
"lambda",
"for", "while",
"import",
"block",
];
validate_unused_kinds_audit(&Typst, documented_unused)
.expect("Typst unused node kinds audit failed");
}
}