Skip to main content

aft_base/
lang_id.rs

1//! Language names and extension-only classification, without loading grammars.
2
3use std::path::Path;
4
5aft_store::heap_bytes_none!(LangId);
6
7/// Supported language identifier.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum LangId {
10    TypeScript,
11    Tsx,
12    JavaScript,
13    Python,
14    Rust,
15    Go,
16    C,
17    Cpp,
18    Cuda,
19    Metal,
20    Zig,
21    CSharp,
22    Bash,
23    Html,
24    Markdown,
25    Solidity,
26    Scss,
27    Vue,
28    Json,
29    Scala,
30    Java,
31    Ruby,
32    Kotlin,
33    Swift,
34    Php,
35    Lua,
36    Perl,
37    Yaml,
38    Pascal,
39    R,
40    Groovy,
41    ObjC,
42    Toml,
43    Luau,
44}
45
46#[cfg(any(test, feature = "test-support"))]
47pub const TEST_EXTENSIONS: &[&str] = &[
48    "ts", "mts", "cts", "tsx", "js", "jsx", "mjs", "cjs", "py", "pyi", "rs", "go", "c", "h", "cc",
49    "cpp", "cxx", "hpp", "hh", "cu", "cuh", "metal", "zig", "cs", "sh", "bash", "zsh", "html",
50    "htm", "md", "markdown", "mdx", "qmd", "Qmd", "rmd", "Rmd", "sol", "scss", "vue", "json",
51    "jsonc", "scala", "sc", "java", "rb", "kt", "kts", "swift", "inc", "php", "lua", "pl", "pm",
52    "t", "yaml", "yml", "pas", "pp", "dpr", "dpk", "lpr", "R", "r", "groovy", "gvy", "gy", "gsh",
53    "gradle", "m", "mm", "toml", "luau", "H", "unknown", "",
54];
55
56/// Classify by name without reading the file. A `.h` result is provisional C;
57/// the parser resolves C, C++ or Objective-C from the header's contents.
58pub fn language_by_extension(path: &Path) -> Option<LangId> {
59    if path.file_name().and_then(|name| name.to_str()) == Some("Jenkinsfile") {
60        return Some(LangId::Groovy);
61    }
62
63    native_language_by_extension(path.extension()?.to_str()?)
64}
65
66pub fn native_language_by_extension(ext: &str) -> Option<LangId> {
67    match ext {
68        "ts" | "mts" | "cts" => Some(LangId::TypeScript),
69        "tsx" => Some(LangId::Tsx),
70        "js" | "jsx" | "mjs" | "cjs" => Some(LangId::JavaScript),
71        "py" | "pyi" => Some(LangId::Python),
72        "rs" => Some(LangId::Rust),
73        "go" => Some(LangId::Go),
74        "c" | "h" => Some(LangId::C),
75        "cc" | "cpp" | "cxx" | "hpp" | "hh" => Some(LangId::Cpp),
76        "cu" | "cuh" => Some(LangId::Cuda),
77        "metal" => Some(LangId::Metal),
78        "zig" => Some(LangId::Zig),
79        "cs" => Some(LangId::CSharp),
80        "sh" | "bash" | "zsh" => Some(LangId::Bash),
81        "html" | "htm" => Some(LangId::Html),
82        "md" | "markdown" | "mdx" | "qmd" | "Qmd" | "rmd" | "Rmd" => Some(LangId::Markdown),
83        "sol" => Some(LangId::Solidity),
84        "scss" => Some(LangId::Scss),
85        "vue" => Some(LangId::Vue),
86        "json" | "jsonc" => Some(LangId::Json),
87        "scala" | "sc" => Some(LangId::Scala),
88        "java" => Some(LangId::Java),
89        "rb" => Some(LangId::Ruby),
90        "kt" | "kts" => Some(LangId::Kotlin),
91        "swift" => Some(LangId::Swift),
92        "inc" | "php" => Some(LangId::Php),
93        "lua" => Some(LangId::Lua),
94        "pl" | "pm" | "t" => Some(LangId::Perl),
95        "yaml" | "yml" => Some(LangId::Yaml),
96        "pas" | "pp" | "dpr" | "dpk" | "lpr" => Some(LangId::Pascal),
97        "R" | "r" => Some(LangId::R),
98        "groovy" | "gvy" | "gy" | "gsh" | "gradle" => Some(LangId::Groovy),
99        "m" | "mm" => Some(LangId::ObjC),
100        "toml" => Some(LangId::Toml),
101        _ => None,
102    }
103}