use tree_sitter::Language;
pub(crate) const ALL_TREE_SITTER_EXTENSIONS: &[&str] = &[
"rs", "js", "jsx", "mjs", "cjs", "ts", "tsx", "py", "pyi", "pyx", "json", "toml", "sh", "bash",
"zsh", "css", "html", "htm", "go", "rb", "c", "h", "sql", "md", "markdown",
];
#[must_use]
pub fn tree_sitter_language_for_extension(ext: &str) -> Option<Language> {
match ext {
"rs" => Some(tree_sitter_rust::LANGUAGE.into()),
"js" | "jsx" | "mjs" | "cjs" => Some(tree_sitter_javascript::LANGUAGE.into()),
"ts" => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
"tsx" => Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
"py" | "pyi" | "pyx" => Some(tree_sitter_python::LANGUAGE.into()),
"json" => Some(tree_sitter_json::LANGUAGE.into()),
"toml" => Some(tree_sitter_toml_ng::LANGUAGE.into()),
"sh" | "bash" | "zsh" => Some(tree_sitter_bash::LANGUAGE.into()),
"css" => Some(tree_sitter_css::LANGUAGE.into()),
"html" | "htm" => Some(tree_sitter_html::LANGUAGE.into()),
"go" => Some(tree_sitter_go::LANGUAGE.into()),
"rb" => Some(tree_sitter_ruby::LANGUAGE.into()),
"c" | "h" => Some(tree_sitter_c::LANGUAGE.into()),
"sql" => Some(tree_sitter_sequel::LANGUAGE.into()),
"md" | "markdown" => Some(tree_sitter_md::LANGUAGE.into()),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_constant_extensions_are_supported() {
for ext in ALL_TREE_SITTER_EXTENSIONS {
assert!(
tree_sitter_language_for_extension(ext).is_some(),
"expected tree_sitter_language_for_extension(\"{ext}\") to return Some, \
but the function returned None. \
If you removed this extension from the function, remove it from \
ALL_TREE_SITTER_EXTENSIONS too."
);
}
}
}