use std::ffi::OsStr;
use std::path::Path;
use std::sync::OnceLock;
use tree_sitter::Language;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SlocMode {
Default,
Python,
Ada,
Lua,
Fortran,
Hash,
Markdown,
Rst,
}
#[derive(Debug, Clone, Copy)]
pub struct LanguageInfo {
pub name: &'static str,
pub key: &'static str,
pub extensions: &'static [&'static str],
pub explicit_only: &'static [&'static str],
pub filenames: &'static [&'static str],
pub sloc_mode: SlocMode,
}
#[allow(clippy::vec_init_then_push)]
pub fn languages() -> &'static [LanguageInfo] {
static LANGS: OnceLock<Vec<LanguageInfo>> = OnceLock::new();
LANGS.get_or_init(|| {
#[allow(unused_mut)]
let mut v: Vec<LanguageInfo> = Vec::new();
#[cfg(feature = "lang-c")]
v.push(LanguageInfo {
name: "C",
key: "c",
extensions: &["c"],
explicit_only: &["h"],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-cpp")]
v.push(LanguageInfo {
name: "C++",
key: "cpp",
extensions: &["cpp", "cc", "cxx", "hpp", "hxx"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-rust")]
v.push(LanguageInfo {
name: "Rust",
key: "rust",
extensions: &["rs"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-python")]
v.push(LanguageInfo {
name: "Python",
key: "python",
extensions: &["py"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Python,
});
#[cfg(feature = "lang-javascript")]
v.push(LanguageInfo {
name: "JavaScript",
key: "javascript",
extensions: &["js", "mjs", "cjs", "jsx"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-typescript")]
v.push(LanguageInfo {
name: "TypeScript",
key: "typescript",
extensions: &["ts", "tsx"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-ada")]
v.push(LanguageInfo {
name: "Ada",
key: "ada",
extensions: &["adb", "ada"],
explicit_only: &["ads"],
filenames: &[],
sloc_mode: SlocMode::Ada,
});
#[cfg(feature = "lang-go")]
v.push(LanguageInfo {
name: "Go",
key: "go",
extensions: &["go"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-java")]
v.push(LanguageInfo {
name: "Java",
key: "java",
extensions: &["java"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-csharp")]
v.push(LanguageInfo {
name: "C#",
key: "csharp",
extensions: &["cs"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-kotlin")]
v.push(LanguageInfo {
name: "Kotlin",
key: "kotlin",
extensions: &["kt", "kts"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-swift")]
v.push(LanguageInfo {
name: "Swift",
key: "swift",
extensions: &["swift"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-php")]
v.push(LanguageInfo {
name: "PHP",
key: "php",
extensions: &["php"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-fortran")]
v.push(LanguageInfo {
name: "Fortran",
key: "fortran",
extensions: &["f90", "f95", "f03", "f08", "F90", "F95", "F03", "F08"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Fortran,
});
#[cfg(feature = "lang-scala")]
v.push(LanguageInfo {
name: "Scala",
key: "scala",
extensions: &["scala", "sc"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-lua")]
v.push(LanguageInfo {
name: "Lua",
key: "lua",
extensions: &["lua"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Lua,
});
#[cfg(feature = "lang-yaml")]
v.push(LanguageInfo {
name: "YAML",
key: "yaml",
extensions: &["yaml", "yml"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Hash,
});
#[cfg(feature = "lang-json")]
v.push(LanguageInfo {
name: "JSON",
key: "json",
extensions: &["json", "jsonc"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Default,
});
#[cfg(feature = "lang-markdown")]
v.push(LanguageInfo {
name: "Markdown",
key: "markdown",
extensions: &["md", "markdown"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Markdown,
});
#[cfg(feature = "lang-cmake")]
v.push(LanguageInfo {
name: "CMake",
key: "cmake",
extensions: &["cmake"],
explicit_only: &[],
filenames: &["CMakeLists.txt"],
sloc_mode: SlocMode::Hash,
});
#[cfg(feature = "lang-bash")]
v.push(LanguageInfo {
name: "Bash",
key: "bash",
extensions: &["sh", "bash"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Hash,
});
#[cfg(feature = "lang-rst")]
v.push(LanguageInfo {
name: "reStructuredText",
key: "rst",
extensions: &["rst"],
explicit_only: &[],
filenames: &[],
sloc_mode: SlocMode::Rst,
});
v
})
}
pub fn language_for_file(path: &Path) -> Option<Language> {
#[allow(clippy::match_single_binding)]
match path.file_name().and_then(|n| n.to_str()) {
#[cfg(feature = "lang-cmake")]
Some("CMakeLists.txt") => return Some(tree_sitter_cmake::LANGUAGE.into()),
_ => {}
}
match path_extension(path).as_deref() {
#[cfg(feature = "lang-ada")]
Some("adb" | "ada" | "ads") => Some(tree_sitter_ada::LANGUAGE.into()),
#[cfg(feature = "lang-cpp")]
Some("cpp" | "cc" | "cxx" | "hpp" | "hxx") => Some(tree_sitter_cpp::LANGUAGE.into()),
#[cfg(feature = "lang-rust")]
Some("rs") => Some(tree_sitter_rust::LANGUAGE.into()),
#[cfg(feature = "lang-python")]
Some("py") => Some(tree_sitter_python::LANGUAGE.into()),
#[cfg(feature = "lang-javascript")]
Some("js" | "mjs" | "cjs" | "jsx") => Some(tree_sitter_javascript::LANGUAGE.into()),
#[cfg(feature = "lang-typescript")]
Some("ts") => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
#[cfg(feature = "lang-typescript")]
Some("tsx") => Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
#[cfg(feature = "lang-go")]
Some("go") => Some(tree_sitter_go::LANGUAGE.into()),
#[cfg(feature = "lang-java")]
Some("java") => Some(tree_sitter_java::LANGUAGE.into()),
#[cfg(feature = "lang-csharp")]
Some("cs") => Some(tree_sitter_c_sharp::LANGUAGE.into()),
#[cfg(feature = "lang-kotlin")]
Some("kt" | "kts") => Some(tree_sitter_kotlin_ng::LANGUAGE.into()),
#[cfg(feature = "lang-swift")]
Some("swift") => Some(tree_sitter_swift::LANGUAGE.into()),
#[cfg(feature = "lang-php")]
Some("php") => Some(tree_sitter_php::LANGUAGE_PHP.into()),
#[cfg(feature = "lang-fortran")]
Some("f90" | "f95" | "f03" | "f08") => Some(tree_sitter_fortran::LANGUAGE.into()),
#[cfg(feature = "lang-scala")]
Some("scala" | "sc") => Some(tree_sitter_scala::LANGUAGE.into()),
#[cfg(feature = "lang-lua")]
Some("lua") => Some(tree_sitter_lua::LANGUAGE.into()),
#[cfg(feature = "lang-yaml")]
Some("yaml" | "yml") => Some(tree_sitter_yaml::LANGUAGE.into()),
#[cfg(feature = "lang-json")]
Some("json" | "jsonc") => Some(tree_sitter_json::LANGUAGE.into()),
#[cfg(feature = "lang-markdown")]
Some("md" | "markdown") => Some(tree_sitter_md::LANGUAGE.into()),
#[cfg(feature = "lang-cmake")]
Some("cmake") => Some(tree_sitter_cmake::LANGUAGE.into()),
#[cfg(feature = "lang-bash")]
Some("sh" | "bash") => Some(tree_sitter_bash::LANGUAGE.into()),
#[cfg(feature = "lang-rst")]
Some("rst") => Some(tree_sitter_rst::LANGUAGE.into()),
#[cfg(feature = "lang-c")]
Some("c" | "h") => Some(tree_sitter_c::LANGUAGE.into()),
_ => None,
}
}
#[cfg(all(feature = "lang-c", feature = "lang-cpp"))]
pub fn language_for_header_content(path: &Path, source: &[u8]) -> Option<Language> {
match path_extension(path).as_deref() {
Some("h") if crate::cpp_header::sniff_header(source) == crate::HeaderSniff::Cpp => {
Some(tree_sitter_cpp::LANGUAGE.into())
}
_ => language_for_file(path),
}
}
pub fn language_for_key(key: &str) -> Option<Language> {
match key {
#[cfg(feature = "lang-ada")]
"ada" => Some(tree_sitter_ada::LANGUAGE.into()),
#[cfg(feature = "lang-bash")]
"bash" => Some(tree_sitter_bash::LANGUAGE.into()),
#[cfg(feature = "lang-c")]
"c" => Some(tree_sitter_c::LANGUAGE.into()),
#[cfg(feature = "lang-cmake")]
"cmake" => Some(tree_sitter_cmake::LANGUAGE.into()),
#[cfg(feature = "lang-cpp")]
"cpp" => Some(tree_sitter_cpp::LANGUAGE.into()),
#[cfg(feature = "lang-csharp")]
"csharp" => Some(tree_sitter_c_sharp::LANGUAGE.into()),
#[cfg(feature = "lang-fortran")]
"fortran" => Some(tree_sitter_fortran::LANGUAGE.into()),
#[cfg(feature = "lang-go")]
"go" => Some(tree_sitter_go::LANGUAGE.into()),
#[cfg(feature = "lang-java")]
"java" => Some(tree_sitter_java::LANGUAGE.into()),
#[cfg(feature = "lang-javascript")]
"javascript" => Some(tree_sitter_javascript::LANGUAGE.into()),
#[cfg(feature = "lang-json")]
"json" => Some(tree_sitter_json::LANGUAGE.into()),
#[cfg(feature = "lang-kotlin")]
"kotlin" => Some(tree_sitter_kotlin_ng::LANGUAGE.into()),
#[cfg(feature = "lang-lua")]
"lua" => Some(tree_sitter_lua::LANGUAGE.into()),
#[cfg(feature = "lang-markdown")]
"markdown" => Some(tree_sitter_md::LANGUAGE.into()),
#[cfg(feature = "lang-markdown")]
"markdown_inline" => Some(tree_sitter_md::INLINE_LANGUAGE.into()),
#[cfg(feature = "lang-php")]
"php" => Some(tree_sitter_php::LANGUAGE_PHP.into()),
#[cfg(feature = "lang-python")]
"python" => Some(tree_sitter_python::LANGUAGE.into()),
#[cfg(feature = "lang-rst")]
"rst" => Some(tree_sitter_rst::LANGUAGE.into()),
#[cfg(feature = "lang-rust")]
"rust" => Some(tree_sitter_rust::LANGUAGE.into()),
#[cfg(feature = "lang-scala")]
"scala" => Some(tree_sitter_scala::LANGUAGE.into()),
#[cfg(feature = "lang-swift")]
"swift" => Some(tree_sitter_swift::LANGUAGE.into()),
#[cfg(feature = "lang-typescript")]
"typescript" => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
#[cfg(feature = "lang-typescript")]
"tsx" => Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
#[cfg(feature = "lang-yaml")]
"yaml" => Some(tree_sitter_yaml::LANGUAGE.into()),
_ => None,
}
}
pub fn language_info_for_file(path: &Path) -> Option<&'static LanguageInfo> {
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if let Some(info) = languages().iter().find(|l| l.filenames.contains(&name)) {
return Some(info);
}
}
let ext = path_extension(path)?;
languages().iter().find(|l| has_extension(l, &ext))
}
fn folded_extension(ext: &OsStr) -> Option<String> {
ext.to_str().map(str::to_ascii_lowercase)
}
fn path_extension(path: &Path) -> Option<String> {
path.extension().and_then(folded_extension)
}
fn has_extension(lang: &LanguageInfo, ext: &str) -> bool {
lang.extensions.contains(&ext) || lang.explicit_only.contains(&ext)
}
pub fn sloc_mode_for_file(path: &Path) -> Option<SlocMode> {
language_info_for_file(path).map(|l| l.sloc_mode)
}
pub fn is_discoverable_file(path: &Path) -> bool {
let named = path
.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| languages().iter().any(|l| l.filenames.contains(&n)));
named || path.extension().is_some_and(is_source_extension)
}
pub fn is_source_extension(ext: &OsStr) -> bool {
folded_extension(ext).is_some_and(|e| {
languages()
.iter()
.any(|l| l.extensions.contains(&e.as_str()))
})
}
pub fn is_parseable_extension(ext: &OsStr) -> bool {
folded_extension(ext).is_some_and(|e| languages().iter().any(|l| has_extension(l, &e)))
}
pub fn is_extension_for_language(ext: &OsStr, key: &str) -> bool {
folded_extension(ext).is_some_and(|e| {
languages()
.iter()
.find(|l| l.key == key)
.is_some_and(|l| has_extension(l, &e))
})
}
pub fn supported_languages_report() -> String {
let langs = languages();
let width = langs.iter().map(|l| l.name.len()).max().unwrap_or(0);
let mut out = String::from("Supported languages:\n");
for lang in langs {
let exts = lang
.filenames
.iter()
.map(|n| n.to_string())
.chain(lang.extensions.iter().map(|e| format!(".{e}")))
.collect::<Vec<_>>()
.join(" ");
out.push_str(&format!(" {:<width$} {exts}", lang.name, width = width));
if !lang.explicit_only.is_empty() {
let extra = lang
.explicit_only
.iter()
.map(|e| format!(".{e}"))
.collect::<Vec<_>>()
.join(" ");
out.push_str(&format!(" (also {extra} when passed explicitly)"));
}
out.push('\n');
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[cfg(feature = "lang-c")]
#[test]
fn every_registered_extension_maps_to_its_grammar() {
let c_lang: Language = tree_sitter_c::LANGUAGE.into();
for lang in languages() {
for ext in lang.extensions.iter().chain(lang.explicit_only) {
let mapped = language_for_file(Path::new(&format!("f.{ext}")))
.unwrap_or_else(|| panic!(".{ext} ({}) did not map to a grammar", lang.name));
if lang.key != "c" {
assert_ne!(
mapped, c_lang,
".{ext} ({}) fell through to the C grammar",
lang.name
);
}
}
}
}
#[test]
fn unknown_extension_has_no_language() {
assert!(language_for_file(Path::new("notes.txt")).is_none());
assert!(sloc_mode_for_file(Path::new("notes.txt")).is_none());
assert!(language_for_file(Path::new("noext")).is_none());
}
#[cfg(feature = "lang-fortran")]
#[test]
fn fortran_sloc_mode() {
for ext in ["f90", "f95", "F90"] {
assert_eq!(
sloc_mode_for_file(Path::new(&format!("modern.{ext}"))),
Some(SlocMode::Fortran),
".{ext} should be free-form Fortran",
);
}
}
#[cfg(feature = "lang-cmake")]
#[test]
fn cmakelists_resolves_by_file_name() {
let cmake: Language = tree_sitter_cmake::LANGUAGE.into();
for path in ["CMakeLists.txt", "src/sub/CMakeLists.txt", "util.cmake"] {
assert_eq!(
language_for_file(Path::new(path)),
Some(cmake.clone()),
"{path}"
);
assert_eq!(
language_info_for_file(Path::new(path)).map(|l| l.key),
Some("cmake"),
"{path}"
);
}
for path in ["cmakelists.txt", "CMakeLists.txt.bak", "notes.txt"] {
assert!(language_for_file(Path::new(path)).is_none(), "{path}");
assert!(language_info_for_file(Path::new(path)).is_none(), "{path}");
}
assert!(supported_languages_report().contains("CMakeLists.txt .cmake"));
}
#[test]
fn is_discoverable_file_covers_names_and_discovery_extensions() {
for lang in languages() {
for name in lang.filenames {
assert!(is_discoverable_file(Path::new(name)), "{name}");
}
for ext in lang.extensions {
assert!(
is_discoverable_file(Path::new(&format!("d/f.{ext}"))),
".{ext}"
);
}
for ext in lang.explicit_only {
assert!(
!is_discoverable_file(Path::new(&format!("f.{ext}"))),
".{ext}"
);
}
}
assert!(!is_discoverable_file(Path::new("notes.txt")));
assert!(!is_discoverable_file(Path::new("noext")));
}
#[test]
fn every_registered_filename_maps_to_its_language() {
for lang in languages() {
for name in lang.filenames {
let path = Path::new(name);
assert_eq!(
language_info_for_file(path).map(|l| l.key),
Some(lang.key),
"{name}"
);
assert_eq!(
language_for_file(path),
language_for_key(lang.key),
"{name} disagrees with language_for_key({:?})",
lang.key
);
}
}
}
#[cfg(feature = "lang-markdown")]
#[test]
fn markdown_inline_is_a_grammar_key_only() {
let inline: Language = tree_sitter_md::INLINE_LANGUAGE.into();
assert_eq!(language_for_key("markdown_inline"), Some(inline.clone()));
assert_ne!(language_for_key("markdown"), Some(inline));
assert!(languages().iter().all(|l| l.key != "markdown_inline"));
}
#[cfg(feature = "lang-python")]
#[test]
fn python_sloc_mode() {
assert_eq!(
sloc_mode_for_file(Path::new("a.py")),
Some(SlocMode::Python)
);
}
#[test]
fn keys_are_lowercase_and_unique() {
let mut seen = std::collections::HashSet::new();
for lang in languages() {
assert!(
lang.key.chars().all(|c| c.is_ascii_lowercase()),
"{} key {:?} is not lowercase ASCII",
lang.name,
lang.key
);
assert!(
seen.insert(lang.key),
"duplicate LanguageInfo.key {:?}",
lang.key
);
}
}
#[test]
fn language_info_for_file_matches_extensions() {
for lang in languages() {
for ext in lang.extensions.iter().chain(lang.explicit_only) {
let info =
language_info_for_file(Path::new(&format!("f.{ext}"))).unwrap_or_else(|| {
panic!(".{ext} ({}) did not resolve to a LanguageInfo", lang.name)
});
assert_eq!(
info.key, lang.key,
".{ext} resolved to the wrong LanguageInfo"
);
}
}
assert!(language_info_for_file(Path::new("notes.txt")).is_none());
assert!(language_info_for_file(Path::new("noext")).is_none());
}
#[test]
fn source_vs_parseable_extension_distinction() {
for lang in languages() {
for ext in lang.extensions {
let os_ext = std::ffi::OsStr::new(ext);
assert!(
is_source_extension(os_ext),
".{ext} ({}) should be a source extension",
lang.name
);
assert!(
is_parseable_extension(os_ext),
".{ext} ({}) should be parseable",
lang.name
);
}
for ext in lang.explicit_only {
let os_ext = std::ffi::OsStr::new(ext);
assert!(
!is_source_extension(os_ext),
".{ext} ({}) is explicit-only and must not be a source extension",
lang.name
);
assert!(
is_parseable_extension(os_ext),
".{ext} ({}) should still be parseable",
lang.name
);
}
}
assert!(!is_source_extension(std::ffi::OsStr::new("txt")));
assert!(!is_parseable_extension(std::ffi::OsStr::new("txt")));
}
#[test]
fn is_extension_for_language_is_scoped_to_one_language() {
for lang in languages() {
for ext in lang.extensions.iter().chain(lang.explicit_only) {
let os_ext = std::ffi::OsStr::new(ext);
assert!(
is_extension_for_language(os_ext, lang.key),
".{ext} should be an extension of {}",
lang.key
);
for other in languages().iter().filter(|l| l.key != lang.key) {
assert!(
!is_extension_for_language(os_ext, other.key),
".{ext} (belongs to {}) must not be reported as an extension of {}",
lang.key,
other.key
);
}
}
}
assert!(!is_extension_for_language(std::ffi::OsStr::new("txt"), "c"));
assert!(!is_extension_for_language(
std::ffi::OsStr::new("c"),
"not-a-real-key"
));
}
#[test]
fn language_for_key_round_trips_with_language_info() {
for lang in languages() {
let by_key = language_for_key(lang.key).unwrap_or_else(|| {
panic!(
"language_for_key({:?}) returned None for compiled-in language {}",
lang.key, lang.name
)
});
let ext = lang
.extensions
.first()
.or_else(|| lang.explicit_only.first())
.unwrap();
let by_file = language_for_file(Path::new(&format!("f.{ext}"))).unwrap_or_else(|| {
panic!(
".{ext} ({}) did not resolve via language_for_file",
lang.name
)
});
assert_eq!(
by_key, by_file,
"language_for_key({:?}) disagrees with language_for_file for {}",
lang.key, lang.name
);
}
}
#[test]
fn language_for_key_unknown_returns_none() {
assert!(language_for_key("").is_none());
assert!(language_for_key("txt").is_none());
assert!(language_for_key("C").is_none()); }
#[test]
fn supported_languages_report_lists_every_language() {
let report = supported_languages_report();
for lang in languages() {
assert!(
report.contains(lang.name),
"report missing language {}",
lang.name
);
for ext in lang.extensions {
assert!(
report.contains(&format!(".{ext}")),
"report missing extension .{ext} for {}",
lang.name
);
}
}
}
#[test]
fn extension_lookups_fold_case() {
for lang in languages() {
for ext in lang.extensions.iter().chain(lang.explicit_only) {
let lower = ext.to_ascii_lowercase();
let mut mixed: Vec<char> = lower.chars().collect();
mixed[0] = mixed[0].to_ascii_uppercase();
let mixed: String = mixed.into_iter().collect();
for variant in [ext.to_ascii_uppercase(), mixed] {
let path = format!("f.{variant}");
let os_ext = std::ffi::OsStr::new(&variant);
assert_eq!(
language_for_file(Path::new(&path)),
language_for_file(Path::new(&format!("f.{lower}"))),
"{path} ({})",
lang.name
);
assert_eq!(
language_info_for_file(Path::new(&path)).map(|l| l.key),
Some(lang.key),
"{path}"
);
assert!(is_extension_for_language(os_ext, lang.key), "{path}");
assert!(is_parseable_extension(os_ext), "{path}");
let discoverable = lang.extensions.contains(ext);
assert_eq!(is_source_extension(os_ext), discoverable, "{path}");
assert_eq!(
is_discoverable_file(Path::new(&path)),
discoverable,
"{path}"
);
}
}
}
}
#[cfg(all(feature = "lang-c", feature = "lang-cpp"))]
#[test]
fn uppercase_c_is_c_and_mixed_case_cpp_is_cpp() {
let c: Language = tree_sitter_c::LANGUAGE.into();
let cpp: Language = tree_sitter_cpp::LANGUAGE.into();
let os = std::ffi::OsStr::new;
for name in ["A.C", "B.H", "src/x.C", "x.h"] {
assert_eq!(
language_for_file(Path::new(name)),
Some(c.clone()),
"{name}"
);
assert_eq!(
language_info_for_file(Path::new(name)).map(|l| l.key),
Some("c"),
"{name}"
);
}
for name in ["a.Cpp", "a.CPP", "b.HPP", "c.Cc", "d.CXX"] {
assert_eq!(
language_for_file(Path::new(name)),
Some(cpp.clone()),
"{name}"
);
}
assert!(is_extension_for_language(os("C"), "c"));
assert!(is_extension_for_language(os("H"), "c"));
assert!(!is_extension_for_language(os("C"), "cpp"));
assert!(is_extension_for_language(os("Cpp"), "cpp"));
assert!(!is_extension_for_language(os("Cpp"), "c"));
assert!(is_source_extension(os("C")));
assert!(!is_source_extension(os("H")), ".H stays explicit-only");
assert!(is_parseable_extension(os("H")));
assert!(is_discoverable_file(Path::new("up/A.C")));
assert!(!is_discoverable_file(Path::new("up/B.H")));
assert_eq!(
language_for_header_content(Path::new("B.H"), b"#define X 1\n"),
Some(c)
);
assert_eq!(
language_for_header_content(Path::new("B.H"), b"namespace n { }\n"),
Some(cpp)
);
}
}