| 1 | use gpui::SharedString; |
| 2 | |
| 3 | use crate::highlighter::GrammarConfig; |
| 4 | |
| 5 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, enum_iterator::Sequence)] |
| 6 | pub enum Language { |
| 7 | Json, |
| 8 | Plain, |
| 9 | #[cfg(feature = "tree-sitter-astro")] |
| 10 | Astro, |
| 11 | #[cfg(feature = "tree-sitter-bash")] |
| 12 | Bash, |
| 13 | #[cfg(feature = "tree-sitter-c")] |
| 14 | C, |
| 15 | #[cfg(feature = "tree-sitter-cmake")] |
| 16 | CMake, |
| 17 | #[cfg(feature = "tree-sitter-csharp")] |
| 18 | CSharp, |
| 19 | #[cfg(feature = "tree-sitter-cpp")] |
| 20 | Cpp, |
| 21 | #[cfg(feature = "tree-sitter-css")] |
| 22 | Css, |
| 23 | #[cfg(feature = "tree-sitter-diff")] |
| 24 | Diff, |
| 25 | #[cfg(feature = "tree-sitter-ejs")] |
| 26 | Ejs, |
| 27 | #[cfg(feature = "tree-sitter-elixir")] |
| 28 | Elixir, |
| 29 | #[cfg(feature = "tree-sitter-erb")] |
| 30 | Erb, |
| 31 | #[cfg(feature = "tree-sitter-go")] |
| 32 | Go, |
| 33 | #[cfg(feature = "tree-sitter-graphql")] |
| 34 | GraphQL, |
| 35 | #[cfg(feature = "tree-sitter-html")] |
| 36 | Html, |
| 37 | #[cfg(feature = "tree-sitter-java")] |
| 38 | Java, |
| 39 | #[cfg(feature = "tree-sitter-javascript")] |
| 40 | JavaScript, |
| 41 | #[cfg(feature = "tree-sitter-jsdoc")] |
| 42 | JsDoc, |
| 43 | #[cfg(feature = "tree-sitter-kotlin")] |
| 44 | Kotlin, |
| 45 | #[cfg(feature = "tree-sitter-lua")] |
| 46 | Lua, |
| 47 | #[cfg(feature = "tree-sitter-make")] |
| 48 | Make, |
| 49 | #[cfg(feature = "tree-sitter-markdown")] |
| 50 | Markdown, |
| 51 | #[cfg(feature = "tree-sitter-markdown")] |
| 52 | MarkdownInline, |
| 53 | #[cfg(feature = "tree-sitter-php")] |
| 54 | Php, |
| 55 | #[cfg(feature = "tree-sitter-proto")] |
| 56 | Proto, |
| 57 | #[cfg(feature = "tree-sitter-python")] |
| 58 | Python, |
| 59 | #[cfg(feature = "tree-sitter-ruby")] |
| 60 | Ruby, |
| 61 | #[cfg(feature = "tree-sitter-rust")] |
| 62 | Rust, |
| 63 | #[cfg(feature = "tree-sitter-scala")] |
| 64 | Scala, |
| 65 | #[cfg(feature = "tree-sitter-sql")] |
| 66 | Sql, |
| 67 | #[cfg(feature = "tree-sitter-svelte")] |
| 68 | Svelte, |
| 69 | #[cfg(feature = "tree-sitter-swift")] |
| 70 | Swift, |
| 71 | #[cfg(feature = "tree-sitter-toml")] |
| 72 | Toml, |
| 73 | #[cfg(feature = "tree-sitter-tsx")] |
| 74 | Tsx, |
| 75 | #[cfg(feature = "tree-sitter-typescript")] |
| 76 | TypeScript, |
| 77 | #[cfg(feature = "tree-sitter-yaml")] |
| 78 | Yaml, |
| 79 | #[cfg(feature = "tree-sitter-zig")] |
| 80 | Zig, |
| 81 | } |
| 82 | |
| 83 | impl From<Language> for SharedString { |
| 84 | fn from(language: Language) -> Self { |
| 85 | language.name().into() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Language { |
| 90 | pub fn all() -> impl Iterator<Item = Self> { |
| 91 | enum_iterator::all::<Language>() |
| 92 | } |
| 93 | |
| 94 | pub fn name(&self) -> &'static str { |
| 95 | match self { |
| 96 | Self::Json => "json", |
| 97 | Self::Plain => "text", |
| 98 | #[cfg(feature = "tree-sitter-astro")] |
| 99 | Self::Astro => "astro", |
| 100 | #[cfg(feature = "tree-sitter-bash")] |
| 101 | Self::Bash => "bash", |
| 102 | #[cfg(feature = "tree-sitter-c")] |
| 103 | Self::C => "c", |
| 104 | #[cfg(feature = "tree-sitter-cmake")] |
| 105 | Self::CMake => "cmake", |
| 106 | #[cfg(feature = "tree-sitter-csharp")] |
| 107 | Self::CSharp => "csharp", |
| 108 | #[cfg(feature = "tree-sitter-cpp")] |
| 109 | Self::Cpp => "cpp", |
| 110 | #[cfg(feature = "tree-sitter-css")] |
| 111 | Self::Css => "css", |
| 112 | #[cfg(feature = "tree-sitter-diff")] |
| 113 | Self::Diff => "diff", |
| 114 | #[cfg(feature = "tree-sitter-ejs")] |
| 115 | Self::Ejs => "ejs", |
| 116 | #[cfg(feature = "tree-sitter-elixir")] |
| 117 | Self::Elixir => "elixir", |
| 118 | #[cfg(feature = "tree-sitter-erb")] |
| 119 | Self::Erb => "erb", |
| 120 | #[cfg(feature = "tree-sitter-go")] |
| 121 | Self::Go => "go", |
| 122 | #[cfg(feature = "tree-sitter-graphql")] |
| 123 | Self::GraphQL => "graphql", |
| 124 | #[cfg(feature = "tree-sitter-html")] |
| 125 | Self::Html => "html", |
| 126 | #[cfg(feature = "tree-sitter-java")] |
| 127 | Self::Java => "java", |
| 128 | #[cfg(feature = "tree-sitter-javascript")] |
| 129 | Self::JavaScript => "javascript", |
| 130 | #[cfg(feature = "tree-sitter-jsdoc")] |
| 131 | Self::JsDoc => "jsdoc", |
| 132 | #[cfg(feature = "tree-sitter-kotlin")] |
| 133 | Self::Kotlin => "kotlin", |
| 134 | #[cfg(feature = "tree-sitter-lua")] |
| 135 | Self::Lua => "lua", |
| 136 | #[cfg(feature = "tree-sitter-make")] |
| 137 | Self::Make => "make", |
| 138 | #[cfg(feature = "tree-sitter-markdown")] |
| 139 | Self::Markdown => "markdown", |
| 140 | #[cfg(feature = "tree-sitter-markdown")] |
| 141 | Self::MarkdownInline => "markdown_inline", |
| 142 | #[cfg(feature = "tree-sitter-php")] |
| 143 | Self::Php => "php", |
| 144 | #[cfg(feature = "tree-sitter-proto")] |
| 145 | Self::Proto => "proto", |
| 146 | #[cfg(feature = "tree-sitter-python")] |
| 147 | Self::Python => "python", |
| 148 | #[cfg(feature = "tree-sitter-ruby")] |
| 149 | Self::Ruby => "ruby", |
| 150 | #[cfg(feature = "tree-sitter-rust")] |
| 151 | Self::Rust => "rust", |
| 152 | #[cfg(feature = "tree-sitter-scala")] |
| 153 | Self::Scala => "scala", |
| 154 | #[cfg(feature = "tree-sitter-sql")] |
| 155 | Self::Sql => "sql", |
| 156 | #[cfg(feature = "tree-sitter-svelte")] |
| 157 | Self::Svelte => "svelte", |
| 158 | #[cfg(feature = "tree-sitter-swift")] |
| 159 | Self::Swift => "swift", |
| 160 | #[cfg(feature = "tree-sitter-toml")] |
| 161 | Self::Toml => "toml", |
| 162 | #[cfg(feature = "tree-sitter-tsx")] |
| 163 | Self::Tsx => "tsx", |
| 164 | #[cfg(feature = "tree-sitter-typescript")] |
| 165 | Self::TypeScript => "typescript", |
| 166 | #[cfg(feature = "tree-sitter-yaml")] |
| 167 | Self::Yaml => "yaml", |
| 168 | #[cfg(feature = "tree-sitter-zig")] |
| 169 | Self::Zig => "zig", |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #[allow(unused)] |
| 174 | pub fn from_str(s: &str) -> Self { |
| 175 | Self::from_name(s).unwrap_or(Self::Plain) |
| 176 | } |
| 177 | |
| 178 | pub(crate) fn from_name(s: &str) -> Option<Self> { |
| 179 | match s { |
| 180 | "text" | "plain" | "plaintext" => Some(Self::Plain), |
| 181 | "json" | "jsonc" => Some(Self::Json), |
| 182 | #[cfg(feature = "tree-sitter-astro")] |
| 183 | "astro" => Some(Self::Astro), |
| 184 | #[cfg(feature = "tree-sitter-bash")] |
| 185 | "bash" | "sh" => Some(Self::Bash), |
| 186 | #[cfg(feature = "tree-sitter-c")] |
| 187 | "c" => Some(Self::C), |
| 188 | #[cfg(feature = "tree-sitter-cmake")] |
| 189 | "cmake" => Some(Self::CMake), |
| 190 | #[cfg(feature = "tree-sitter-cpp")] |
| 191 | "cpp" | "c++" => Some(Self::Cpp), |
| 192 | #[cfg(feature = "tree-sitter-csharp")] |
| 193 | "csharp" | "cs" => Some(Self::CSharp), |
| 194 | #[cfg(feature = "tree-sitter-css")] |
| 195 | "css" | "scss" => Some(Self::Css), |
| 196 | #[cfg(feature = "tree-sitter-diff")] |
| 197 | "diff" => Some(Self::Diff), |
| 198 | #[cfg(feature = "tree-sitter-ejs")] |
| 199 | "ejs" => Some(Self::Ejs), |
| 200 | #[cfg(feature = "tree-sitter-elixir")] |
| 201 | "elixir" | "ex" => Some(Self::Elixir), |
| 202 | #[cfg(feature = "tree-sitter-erb")] |
| 203 | "erb" => Some(Self::Erb), |
| 204 | #[cfg(feature = "tree-sitter-go")] |
| 205 | "go" => Some(Self::Go), |
| 206 | #[cfg(feature = "tree-sitter-graphql")] |
| 207 | "graphql" => Some(Self::GraphQL), |
| 208 | #[cfg(feature = "tree-sitter-html")] |
| 209 | "html" => Some(Self::Html), |
| 210 | #[cfg(feature = "tree-sitter-java")] |
| 211 | "java" => Some(Self::Java), |
| 212 | #[cfg(feature = "tree-sitter-javascript")] |
| 213 | "javascript" | "js" => Some(Self::JavaScript), |
| 214 | #[cfg(feature = "tree-sitter-jsdoc")] |
| 215 | "jsdoc" => Some(Self::JsDoc), |
| 216 | #[cfg(feature = "tree-sitter-kotlin")] |
| 217 | "kt" | "kts" | "ktm" | "kotlin" => Some(Self::Kotlin), |
| 218 | #[cfg(feature = "tree-sitter-lua")] |
| 219 | "lua" => Some(Self::Lua), |
| 220 | #[cfg(feature = "tree-sitter-make")] |
| 221 | "make" | "makefile" => Some(Self::Make), |
| 222 | #[cfg(feature = "tree-sitter-markdown")] |
| 223 | "markdown" | "md" | "mdx" => Some(Self::Markdown), |
| 224 | #[cfg(feature = "tree-sitter-markdown")] |
| 225 | "markdown_inline" | "markdown-inline" => Some(Self::MarkdownInline), |
| 226 | #[cfg(feature = "tree-sitter-php")] |
| 227 | "php" | "php3" | "php4" | "php5" | "phtml" => Some(Self::Php), |
| 228 | #[cfg(feature = "tree-sitter-proto")] |
| 229 | "proto" | "protobuf" => Some(Self::Proto), |
| 230 | #[cfg(feature = "tree-sitter-python")] |
| 231 | "python" | "py" => Some(Self::Python), |
| 232 | #[cfg(feature = "tree-sitter-ruby")] |
| 233 | "ruby" | "rb" => Some(Self::Ruby), |
| 234 | #[cfg(feature = "tree-sitter-rust")] |
| 235 | "rust" | "rs" => Some(Self::Rust), |
| 236 | #[cfg(feature = "tree-sitter-scala")] |
| 237 | "scala" => Some(Self::Scala), |
| 238 | #[cfg(feature = "tree-sitter-sql")] |
| 239 | "sql" => Some(Self::Sql), |
| 240 | #[cfg(feature = "tree-sitter-svelte")] |
| 241 | "svelte" => Some(Self::Svelte), |
| 242 | #[cfg(feature = "tree-sitter-swift")] |
| 243 | "swift" => Some(Self::Swift), |
| 244 | #[cfg(feature = "tree-sitter-toml")] |
| 245 | "toml" => Some(Self::Toml), |
| 246 | #[cfg(feature = "tree-sitter-tsx")] |
| 247 | "tsx" => Some(Self::Tsx), |
| 248 | #[cfg(feature = "tree-sitter-typescript")] |
| 249 | "typescript" | "ts" => Some(Self::TypeScript), |
| 250 | #[cfg(feature = "tree-sitter-yaml")] |
| 251 | "yaml" | "yml" => Some(Self::Yaml), |
| 252 | #[cfg(feature = "tree-sitter-zig")] |
| 253 | "zig" => Some(Self::Zig), |
| 254 | _ => None, |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | #[allow(unused)] |
| 259 | pub(super) fn injection_languages(&self) -> Vec<SharedString> { |
| 260 | let mut languages: Vec<&'static str> = Vec::new(); |
| 261 | |
| 262 | match self { |
| 263 | #[cfg(feature = "tree-sitter-markdown")] |
| 264 | Self::Markdown => { |
| 265 | languages.push("markdown_inline"); |
| 266 | #[cfg(feature = "tree-sitter-html")] |
| 267 | languages.push("html"); |
| 268 | #[cfg(feature = "tree-sitter-toml")] |
| 269 | languages.push("toml"); |
| 270 | #[cfg(feature = "tree-sitter-yaml")] |
| 271 | languages.push("yaml"); |
| 272 | } |
| 273 | #[cfg(feature = "tree-sitter-html")] |
| 274 | Self::Html => { |
| 275 | #[cfg(feature = "tree-sitter-javascript")] |
| 276 | languages.push("javascript"); |
| 277 | #[cfg(feature = "tree-sitter-css")] |
| 278 | languages.push("css"); |
| 279 | } |
| 280 | #[cfg(feature = "tree-sitter-rust")] |
| 281 | Self::Rust => { |
| 282 | languages.push("rust"); |
| 283 | } |
| 284 | #[cfg(feature = "tree-sitter-javascript")] |
| 285 | Self::JavaScript => { |
| 286 | #[cfg(feature = "tree-sitter-jsdoc")] |
| 287 | languages.push("jsdoc"); |
| 288 | languages.push("json"); |
| 289 | #[cfg(feature = "tree-sitter-css")] |
| 290 | languages.push("css"); |
| 291 | #[cfg(feature = "tree-sitter-html")] |
| 292 | languages.push("html"); |
| 293 | #[cfg(feature = "tree-sitter-sql")] |
| 294 | languages.push("sql"); |
| 295 | #[cfg(feature = "tree-sitter-typescript")] |
| 296 | languages.push("typescript"); |
| 297 | languages.push("javascript"); |
| 298 | #[cfg(feature = "tree-sitter-tsx")] |
| 299 | languages.push("tsx"); |
| 300 | #[cfg(feature = "tree-sitter-yaml")] |
| 301 | languages.push("yaml"); |
| 302 | #[cfg(feature = "tree-sitter-graphql")] |
| 303 | languages.push("graphql"); |
| 304 | } |
| 305 | #[cfg(feature = "tree-sitter-typescript")] |
| 306 | Self::TypeScript => { |
| 307 | #[cfg(feature = "tree-sitter-jsdoc")] |
| 308 | languages.push("jsdoc"); |
| 309 | languages.push("json"); |
| 310 | #[cfg(feature = "tree-sitter-css")] |
| 311 | languages.push("css"); |
| 312 | #[cfg(feature = "tree-sitter-html")] |
| 313 | languages.push("html"); |
| 314 | #[cfg(feature = "tree-sitter-sql")] |
| 315 | languages.push("sql"); |
| 316 | languages.push("typescript"); |
| 317 | #[cfg(feature = "tree-sitter-javascript")] |
| 318 | languages.push("javascript"); |
| 319 | #[cfg(feature = "tree-sitter-tsx")] |
| 320 | languages.push("tsx"); |
| 321 | #[cfg(feature = "tree-sitter-yaml")] |
| 322 | languages.push("yaml"); |
| 323 | #[cfg(feature = "tree-sitter-graphql")] |
| 324 | languages.push("graphql"); |
| 325 | } |
| 326 | #[cfg(feature = "tree-sitter-astro")] |
| 327 | Self::Astro => { |
| 328 | #[cfg(feature = "tree-sitter-html")] |
| 329 | languages.push("html"); |
| 330 | #[cfg(feature = "tree-sitter-css")] |
| 331 | languages.push("css"); |
| 332 | #[cfg(feature = "tree-sitter-javascript")] |
| 333 | languages.push("javascript"); |
| 334 | #[cfg(feature = "tree-sitter-typescript")] |
| 335 | languages.push("typescript"); |
| 336 | } |
| 337 | #[cfg(feature = "tree-sitter-php")] |
| 338 | Self::Php => { |
| 339 | languages.push("php"); |
| 340 | #[cfg(feature = "tree-sitter-html")] |
| 341 | languages.push("html"); |
| 342 | #[cfg(feature = "tree-sitter-css")] |
| 343 | languages.push("css"); |
| 344 | #[cfg(feature = "tree-sitter-javascript")] |
| 345 | languages.push("javascript"); |
| 346 | languages.push("json"); |
| 347 | #[cfg(feature = "tree-sitter-jsdoc")] |
| 348 | languages.push("jsdoc"); |
| 349 | #[cfg(feature = "tree-sitter-graphql")] |
| 350 | languages.push("graphql"); |
| 351 | } |
| 352 | #[cfg(feature = "tree-sitter-svelte")] |
| 353 | Self::Svelte => { |
| 354 | languages.push("svelte"); |
| 355 | #[cfg(feature = "tree-sitter-html")] |
| 356 | languages.push("html"); |
| 357 | #[cfg(feature = "tree-sitter-css")] |
| 358 | languages.push("css"); |
| 359 | #[cfg(feature = "tree-sitter-typescript")] |
| 360 | languages.push("typescript"); |
| 361 | } |
| 362 | _ => {} |
| 363 | } |
| 364 | |
| 365 | languages.into_iter().map(SharedString::from).collect() |
| 366 | } |
| 367 | |
| 368 | /// Return the language info for the language. |
| 369 | /// |
| 370 | /// (language, query, injection, locals) |
| 371 | pub(super) fn config(&self) -> GrammarConfig { |
| 372 | let (language, query, injection, locals) = match self { |
| 373 | Self::Plain => return GrammarConfig::plain(self.name()), |
| 374 | Self::Json => ( |
| 375 | tree_sitter_json::LANGUAGE, |
| 376 | include_str!("languages/json/highlights.scm"), |
| 377 | "", |
| 378 | "", |
| 379 | ), |
| 380 | #[cfg(feature = "tree-sitter-markdown")] |
| 381 | Self::Markdown => ( |
| 382 | tree_sitter_md::LANGUAGE, |
| 383 | include_str!("languages/markdown/highlights.scm"), |
| 384 | include_str!("languages/markdown/injections.scm"), |
| 385 | "", |
| 386 | ), |
| 387 | #[cfg(feature = "tree-sitter-markdown")] |
| 388 | Self::MarkdownInline => ( |
| 389 | tree_sitter_md::INLINE_LANGUAGE, |
| 390 | include_str!("languages/markdown_inline/highlights.scm"), |
| 391 | "", |
| 392 | "", |
| 393 | ), |
| 394 | #[cfg(feature = "tree-sitter-toml")] |
| 395 | Self::Toml => ( |
| 396 | tree_sitter_toml_ng::LANGUAGE, |
| 397 | tree_sitter_toml_ng::HIGHLIGHTS_QUERY, |
| 398 | "", |
| 399 | "", |
| 400 | ), |
| 401 | #[cfg(feature = "tree-sitter-yaml")] |
| 402 | Self::Yaml => ( |
| 403 | tree_sitter_yaml::LANGUAGE, |
| 404 | tree_sitter_yaml::HIGHLIGHTS_QUERY, |
| 405 | "", |
| 406 | "", |
| 407 | ), |
| 408 | #[cfg(feature = "tree-sitter-rust")] |
| 409 | Self::Rust => ( |
| 410 | tree_sitter_rust::LANGUAGE, |
| 411 | include_str!("languages/rust/highlights.scm"), |
| 412 | include_str!("languages/rust/injections.scm"), |
| 413 | "", |
| 414 | ), |
| 415 | #[cfg(feature = "tree-sitter-go")] |
| 416 | Self::Go => ( |
| 417 | tree_sitter_go::LANGUAGE, |
| 418 | include_str!("languages/go/highlights.scm"), |
| 419 | "", |
| 420 | "", |
| 421 | ), |
| 422 | #[cfg(feature = "tree-sitter-c")] |
| 423 | Self::C => ( |
| 424 | tree_sitter_c::LANGUAGE, |
| 425 | tree_sitter_c::HIGHLIGHT_QUERY, |
| 426 | "", |
| 427 | "", |
| 428 | ), |
| 429 | #[cfg(feature = "tree-sitter-cpp")] |
| 430 | Self::Cpp => ( |
| 431 | tree_sitter_cpp::LANGUAGE, |
| 432 | tree_sitter_cpp::HIGHLIGHT_QUERY, |
| 433 | "", |
| 434 | "", |
| 435 | ), |
| 436 | #[cfg(feature = "tree-sitter-javascript")] |
| 437 | Self::JavaScript => ( |
| 438 | tree_sitter_javascript::LANGUAGE, |
| 439 | include_str!("languages/javascript/highlights.scm"), |
| 440 | include_str!("languages/javascript/injections.scm"), |
| 441 | tree_sitter_javascript::LOCALS_QUERY, |
| 442 | ), |
| 443 | #[cfg(feature = "tree-sitter-jsdoc")] |
| 444 | Self::JsDoc => ( |
| 445 | tree_sitter_jsdoc::LANGUAGE, |
| 446 | tree_sitter_jsdoc::HIGHLIGHTS_QUERY, |
| 447 | "", |
| 448 | "", |
| 449 | ), |
| 450 | #[cfg(feature = "tree-sitter-zig")] |
| 451 | Self::Zig => ( |
| 452 | tree_sitter_zig::LANGUAGE, |
| 453 | include_str!("languages/zig/highlights.scm"), |
| 454 | include_str!("languages/zig/injections.scm"), |
| 455 | "", |
| 456 | ), |
| 457 | #[cfg(feature = "tree-sitter-java")] |
| 458 | Self::Java => ( |
| 459 | tree_sitter_java::LANGUAGE, |
| 460 | tree_sitter_java::HIGHLIGHTS_QUERY, |
| 461 | "", |
| 462 | "", |
| 463 | ), |
| 464 | #[cfg(feature = "tree-sitter-python")] |
| 465 | Self::Python => ( |
| 466 | tree_sitter_python::LANGUAGE, |
| 467 | tree_sitter_python::HIGHLIGHTS_QUERY, |
| 468 | "", |
| 469 | "", |
| 470 | ), |
| 471 | #[cfg(feature = "tree-sitter-ruby")] |
| 472 | Self::Ruby => ( |
| 473 | tree_sitter_ruby::LANGUAGE, |
| 474 | tree_sitter_ruby::HIGHLIGHTS_QUERY, |
| 475 | "", |
| 476 | tree_sitter_ruby::LOCALS_QUERY, |
| 477 | ), |
| 478 | #[cfg(feature = "tree-sitter-bash")] |
| 479 | Self::Bash => ( |
| 480 | tree_sitter_bash::LANGUAGE, |
| 481 | tree_sitter_bash::HIGHLIGHT_QUERY, |
| 482 | "", |
| 483 | "", |
| 484 | ), |
| 485 | #[cfg(feature = "tree-sitter-html")] |
| 486 | Self::Html => ( |
| 487 | tree_sitter_html::LANGUAGE, |
| 488 | include_str!("languages/html/highlights.scm"), |
| 489 | include_str!("languages/html/injections.scm"), |
| 490 | "", |
| 491 | ), |
| 492 | #[cfg(feature = "tree-sitter-css")] |
| 493 | Self::Css => ( |
| 494 | tree_sitter_css::LANGUAGE, |
| 495 | tree_sitter_css::HIGHLIGHTS_QUERY, |
| 496 | "", |
| 497 | "", |
| 498 | ), |
| 499 | #[cfg(feature = "tree-sitter-swift")] |
| 500 | Self::Swift => (tree_sitter_swift::LANGUAGE, "", "", ""), |
| 501 | #[cfg(feature = "tree-sitter-scala")] |
| 502 | Self::Scala => ( |
| 503 | tree_sitter_scala::LANGUAGE, |
| 504 | tree_sitter_scala::HIGHLIGHTS_QUERY, |
| 505 | "", |
| 506 | tree_sitter_scala::LOCALS_QUERY, |
| 507 | ), |
| 508 | #[cfg(feature = "tree-sitter-sql")] |
| 509 | Self::Sql => ( |
| 510 | tree_sitter_sequel::LANGUAGE, |
| 511 | tree_sitter_sequel::HIGHLIGHTS_QUERY, |
| 512 | "", |
| 513 | "", |
| 514 | ), |
| 515 | #[cfg(feature = "tree-sitter-csharp")] |
| 516 | Self::CSharp => (tree_sitter_c_sharp::LANGUAGE, "", "", ""), |
| 517 | #[cfg(feature = "tree-sitter-graphql")] |
| 518 | Self::GraphQL => (tree_sitter_graphql::LANGUAGE, "", "", ""), |
| 519 | #[cfg(feature = "tree-sitter-proto")] |
| 520 | Self::Proto => (tree_sitter_proto::LANGUAGE, "", "", ""), |
| 521 | #[cfg(feature = "tree-sitter-make")] |
| 522 | Self::Make => ( |
| 523 | tree_sitter_make::LANGUAGE, |
| 524 | tree_sitter_make::HIGHLIGHTS_QUERY, |
| 525 | "", |
| 526 | "", |
| 527 | ), |
| 528 | #[cfg(feature = "tree-sitter-cmake")] |
| 529 | Self::CMake => (tree_sitter_cmake::LANGUAGE, "", "", ""), |
| 530 | #[cfg(feature = "tree-sitter-typescript")] |
| 531 | Self::TypeScript => ( |
| 532 | tree_sitter_typescript::LANGUAGE_TYPESCRIPT, |
| 533 | include_str!("languages/typescript/highlights.scm"), |
| 534 | include_str!("languages/javascript/injections.scm"), |
| 535 | tree_sitter_typescript::LOCALS_QUERY, |
| 536 | ), |
| 537 | #[cfg(feature = "tree-sitter-tsx")] |
| 538 | Self::Tsx => ( |
| 539 | tree_sitter_typescript::LANGUAGE_TSX, |
| 540 | tree_sitter_typescript::HIGHLIGHTS_QUERY, |
| 541 | "", |
| 542 | tree_sitter_typescript::LOCALS_QUERY, |
| 543 | ), |
| 544 | #[cfg(feature = "tree-sitter-diff")] |
| 545 | Self::Diff => ( |
| 546 | tree_sitter_diff::LANGUAGE, |
| 547 | tree_sitter_diff::HIGHLIGHTS_QUERY, |
| 548 | "", |
| 549 | "", |
| 550 | ), |
| 551 | #[cfg(feature = "tree-sitter-elixir")] |
| 552 | Self::Elixir => ( |
| 553 | tree_sitter_elixir::LANGUAGE, |
| 554 | tree_sitter_elixir::HIGHLIGHTS_QUERY, |
| 555 | tree_sitter_elixir::INJECTIONS_QUERY, |
| 556 | "", |
| 557 | ), |
| 558 | #[cfg(feature = "tree-sitter-erb")] |
| 559 | Self::Erb => ( |
| 560 | tree_sitter_embedded_template::LANGUAGE, |
| 561 | tree_sitter_embedded_template::HIGHLIGHTS_QUERY, |
| 562 | tree_sitter_embedded_template::INJECTIONS_EJS_QUERY, |
| 563 | "", |
| 564 | ), |
| 565 | #[cfg(feature = "tree-sitter-ejs")] |
| 566 | Self::Ejs => ( |
| 567 | tree_sitter_embedded_template::LANGUAGE, |
| 568 | tree_sitter_embedded_template::HIGHLIGHTS_QUERY, |
| 569 | tree_sitter_embedded_template::INJECTIONS_EJS_QUERY, |
| 570 | "", |
| 571 | ), |
| 572 | #[cfg(feature = "tree-sitter-php")] |
| 573 | Self::Php => ( |
| 574 | tree_sitter_php::LANGUAGE_PHP, |
| 575 | tree_sitter_php::HIGHLIGHTS_QUERY, |
| 576 | include_str!("languages/php/injections.scm"), |
| 577 | "", |
| 578 | ), |
| 579 | #[cfg(feature = "tree-sitter-astro")] |
| 580 | Self::Astro => ( |
| 581 | tree_sitter_astro_next::LANGUAGE, |
| 582 | tree_sitter_astro_next::HIGHLIGHTS_QUERY, |
| 583 | tree_sitter_astro_next::INJECTIONS_QUERY, |
| 584 | "", |
| 585 | ), |
| 586 | #[cfg(feature = "tree-sitter-kotlin")] |
| 587 | Self::Kotlin => ( |
| 588 | tree_sitter_kotlin_sg::LANGUAGE, |
| 589 | include_str!("languages/kotlin/highlights.scm"), |
| 590 | "", |
| 591 | "", |
| 592 | ), |
| 593 | #[cfg(feature = "tree-sitter-lua")] |
| 594 | Self::Lua => ( |
| 595 | tree_sitter_lua::LANGUAGE, |
| 596 | include_str!("languages/lua/highlights.scm"), |
| 597 | tree_sitter_lua::INJECTIONS_QUERY, |
| 598 | tree_sitter_lua::LOCALS_QUERY, |
| 599 | ), |
| 600 | #[cfg(feature = "tree-sitter-svelte")] |
| 601 | Self::Svelte => ( |
| 602 | tree_sitter_svelte_next::LANGUAGE, |
| 603 | tree_sitter_svelte_next::HIGHLIGHTS_QUERY, |
| 604 | tree_sitter_svelte_next::INJECTIONS_QUERY, |
| 605 | tree_sitter_svelte_next::LOCALS_QUERY, |
| 606 | ), |
| 607 | }; |
| 608 | |
| 609 | let language = tree_sitter::Language::new(language); |
| 610 | |
| 611 | GrammarConfig::new( |
| 612 | self.name(), |
| 613 | language, |
| 614 | self.injection_languages(), |
| 615 | query, |
| 616 | injection, |
| 617 | locals, |
| 618 | ) |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | #[cfg(test)] |
| 623 | mod tests { |
| 624 | use super::*; |
| 625 | |
| 626 | #[test] |
| 627 | fn test_language_name() { |
| 628 | assert_eq!(Language::Plain.name(), "text"); |
| 629 | assert_eq!(Language::Json.name(), "json"); |
| 630 | |
| 631 | #[cfg(feature = "tree-sitter-markdown")] |
| 632 | { |
| 633 | assert_eq!(Language::MarkdownInline.name(), "markdown_inline"); |
| 634 | assert_eq!(Language::Markdown.name(), "markdown"); |
| 635 | } |
| 636 | |
| 637 | #[cfg(feature = "tree-sitter-yaml")] |
| 638 | assert_eq!(Language::Yaml.name(), "yaml"); |
| 639 | #[cfg(feature = "tree-sitter-rust")] |
| 640 | assert_eq!(Language::Rust.name(), "rust"); |
| 641 | #[cfg(feature = "tree-sitter-go")] |
| 642 | assert_eq!(Language::Go.name(), "go"); |
| 643 | #[cfg(feature = "tree-sitter-c")] |
| 644 | assert_eq!(Language::C.name(), "c"); |
| 645 | #[cfg(feature = "tree-sitter-cpp")] |
| 646 | assert_eq!(Language::Cpp.name(), "cpp"); |
| 647 | #[cfg(feature = "tree-sitter-sql")] |
| 648 | assert_eq!(Language::Sql.name(), "sql"); |
| 649 | #[cfg(feature = "tree-sitter-javascript")] |
| 650 | assert_eq!(Language::JavaScript.name(), "javascript"); |
| 651 | #[cfg(feature = "tree-sitter-zig")] |
| 652 | assert_eq!(Language::Zig.name(), "zig"); |
| 653 | #[cfg(feature = "tree-sitter-csharp")] |
| 654 | assert_eq!(Language::CSharp.name(), "csharp"); |
| 655 | #[cfg(feature = "tree-sitter-typescript")] |
| 656 | assert_eq!(Language::TypeScript.name(), "typescript"); |
| 657 | #[cfg(feature = "tree-sitter-tsx")] |
| 658 | assert_eq!(Language::Tsx.name(), "tsx"); |
| 659 | #[cfg(feature = "tree-sitter-diff")] |
| 660 | assert_eq!(Language::Diff.name(), "diff"); |
| 661 | #[cfg(feature = "tree-sitter-elixir")] |
| 662 | assert_eq!(Language::Elixir.name(), "elixir"); |
| 663 | #[cfg(feature = "tree-sitter-erb")] |
| 664 | assert_eq!(Language::Erb.name(), "erb"); |
| 665 | #[cfg(feature = "tree-sitter-ejs")] |
| 666 | assert_eq!(Language::Ejs.name(), "ejs"); |
| 667 | } |
| 668 | |
| 669 | #[test] |
| 670 | fn test_language_aliases_only_resolve_enabled_features() { |
| 671 | assert_eq!(Language::from_name("text"), Some(Language::Plain)); |
| 672 | assert_eq!(Language::from_name("jsonc"), Some(Language::Json)); |
| 673 | assert_eq!(Language::from_name("unknown"), None); |
| 674 | |
| 675 | #[cfg(feature = "tree-sitter-rust")] |
| 676 | assert_eq!(Language::from_name("rs"), Some(Language::Rust)); |
| 677 | #[cfg(not(feature = "tree-sitter-rust"))] |
| 678 | assert_eq!(Language::from_name("rs"), None); |
| 679 | |
| 680 | #[cfg(feature = "tree-sitter-markdown")] |
| 681 | assert_eq!(Language::from_name("md"), Some(Language::Markdown)); |
| 682 | #[cfg(not(feature = "tree-sitter-markdown"))] |
| 683 | assert_eq!(Language::from_name("md"), None); |
| 684 | |
| 685 | #[cfg(feature = "tree-sitter-typescript")] |
| 686 | assert_eq!(Language::from_name("ts"), Some(Language::TypeScript)); |
| 687 | #[cfg(not(feature = "tree-sitter-typescript"))] |
| 688 | assert_eq!(Language::from_name("ts"), None); |
| 689 | |
| 690 | assert_eq!(Language::from_str("unknown"), Language::Plain); |
| 691 | } |
| 692 | |
| 693 | #[test] |
| 694 | fn test_every_language_query_compiles() { |
| 695 | let failures: Vec<String> = Language::all() |
| 696 | .filter_map(|language| { |
| 697 | let config = language.config(); |
| 698 | let grammar = config.language?; |
| 699 | let source = format!( |
| 700 | "{}{}{}", |
| 701 | config.injections, config.locals, config.highlights |
| 702 | ); |
| 703 | let err = tree_sitter::Query::new(&grammar, &source).err()?; |
| 704 | Some(format!("{}: {err}", language.name())) |
| 705 | }) |
| 706 | .collect(); |
| 707 | assert!(failures.is_empty(), "{failures:#?}"); |
| 708 | } |
| 709 | } |