strop_syntax/
languages.rs1use tree_sitter::Language;
8
9pub struct LanguageSpec {
10 pub name: &'static str,
11 pub language: Language,
12 pub highlights: &'static str,
13}
14
15macro_rules! lang_fn {
18 ($name:literal, $f:expr, $q:expr) => {
19 LanguageSpec {
20 name: $name,
21 language: $f.into(),
22 highlights: $q,
23 }
24 };
25}
26
27pub fn for_extension(ext: &str) -> Option<LanguageSpec> {
29 Some(match ext {
30 ".rs" => lang_fn!(
31 "rust",
32 tree_sitter_rust::LANGUAGE,
33 include_str!("../queries/rust/highlights.scm")
34 ),
35 ".py" | ".pyi" => {
36 lang_fn!(
37 "python",
38 tree_sitter_python::LANGUAGE,
39 include_str!("../queries/python/highlights.scm")
40 )
41 }
42 ".js" | ".jsx" | ".mjs" | ".cjs" => {
43 lang_fn!(
44 "javascript",
45 tree_sitter_javascript::LANGUAGE,
46 include_str!("../queries/javascript/highlights.scm")
47 )
48 }
49 ".ts" => lang_fn!(
50 "typescript",
51 tree_sitter_typescript::LANGUAGE_TYPESCRIPT,
52 include_str!("../queries/typescript/highlights.scm")
53 ),
54 ".tsx" => lang_fn!(
55 "tsx",
56 tree_sitter_typescript::LANGUAGE_TSX,
57 include_str!("../queries/tsx/highlights.scm")
58 ),
59 ".go" => lang_fn!(
60 "go",
61 tree_sitter_go::LANGUAGE,
62 include_str!("../queries/go/highlights.scm")
63 ),
64 ".c" | ".h" => lang_fn!("c", tree_sitter_c::LANGUAGE, tree_sitter_c::HIGHLIGHT_QUERY),
65 ".cpp" | ".cc" | ".cxx" | ".hpp" | ".hh" => {
66 lang_fn!(
67 "cpp",
68 tree_sitter_cpp::LANGUAGE,
69 tree_sitter_cpp::HIGHLIGHT_QUERY
70 )
71 }
72 ".json" => lang_fn!(
73 "json",
74 tree_sitter_json::LANGUAGE,
75 include_str!("../queries/json/highlights.scm")
76 ),
77 ".sh" | ".bash" => lang_fn!(
78 "bash",
79 tree_sitter_bash::LANGUAGE,
80 include_str!("../queries/bash/highlights.scm")
81 ),
82 _ => return None,
83 })
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn covers_the_curated_set() {
92 for ext in [
93 ".rs", ".py", ".js", ".ts", ".tsx", ".go", ".c", ".cpp", ".json", ".sh",
94 ] {
95 assert!(for_extension(ext).is_some(), "missing {ext}");
96 }
97 assert!(for_extension(".xyz").is_none());
98 }
99}