chroma-syntaxis/src/languages.rs

177 lines
4.7 KiB
Rust

use tree_sitter::QueryError;
use tree_sitter_highlight::HighlightConfiguration;
use crate::SyntaxHighlighter;
pub static COMMON_HIGHLIGHT_NAMES: &[&str] = &[
"attribute",
"comment",
"constant",
"constant.builtin",
"constant.character",
"constructor",
"delimiter",
"embedded",
"escape",
"function",
"function.builtin",
"function.macro",
"function.method",
"function.special",
"injection.content",
"injection.language",
"keyword",
"label",
"local.definition",
"local.reference",
"local.scope",
"number",
"operator",
"property",
"punctuation.bracket",
"punctuation.delimiter",
"punctuation.special",
"string",
"string.special",
"type",
"type.builtin",
"variable",
"variable.builtin",
"variable.parameter",
];
pub fn register_builtin_languages(highlighter: &mut SyntaxHighlighter) {
let mut add = |lang: &str, factory: fn() -> Result<HighlightConfiguration, QueryError>| {
if let Ok(mut config) = factory() {
config.configure(highlighter.highlight_names);
highlighter.register(lang.to_string(), config);
}
};
add("rust", rust_hl_factory);
add("javascript", javascript_hl_factory);
add("js", javascript_hl_factory);
add("jsx", jsx_hl_factory);
add("regex", regex_hl_factory);
add("python", python_hl_factory);
add("py", python_hl_factory);
add("c", c_hl_factory);
add("cpp", cpp_hl_factory);
add("c++", cpp_hl_factory);
add("typescript", typescript_hl_factory);
add("ts", typescript_hl_factory);
add("tsx", tsx_hl_factory);
}
pub fn rust_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_rust::language(),
tree_sitter_rust::HIGHLIGHT_QUERY,
"",
"",
)
}
pub fn javascript_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_javascript::language(),
tree_sitter_javascript::HIGHLIGHT_QUERY,
tree_sitter_javascript::INJECTION_QUERY,
tree_sitter_javascript::LOCALS_QUERY,
)
}
pub fn jsx_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_javascript::language(),
tree_sitter_javascript::JSX_HIGHLIGHT_QUERY,
tree_sitter_javascript::INJECTION_QUERY,
tree_sitter_javascript::LOCALS_QUERY,
)
}
pub fn regex_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_regex::language(),
tree_sitter_regex::HIGHLIGHTS_QUERY,
"",
"",
)
}
pub fn python_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_python::language(),
tree_sitter_python::HIGHLIGHT_QUERY,
"",
"",
)
}
pub fn c_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_c::language(),
tree_sitter_c::HIGHLIGHT_QUERY,
"",
"",
)
}
pub fn cpp_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_cpp::language(),
&format!(
"{}{}",
tree_sitter_cpp::HIGHLIGHT_QUERY,
tree_sitter_c::HIGHLIGHT_QUERY
.replace(
"(null) @constant",
"(true) @constant\n(false) @constant\n(null) @constant"
)
.replace("(primitive_type) @type", "(primitive_type) @type.builtin"),
),
"",
"",
)
}
pub fn typescript_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_typescript::language_typescript(),
&format!(
"{}{}",
tree_sitter_javascript::HIGHLIGHT_QUERY,
tree_sitter_typescript::HIGHLIGHT_QUERY,
),
tree_sitter_javascript::INJECTION_QUERY,
&format!(
"{}{}",
tree_sitter_typescript::LOCALS_QUERY,
tree_sitter_javascript::LOCALS_QUERY,
),
)
}
pub fn tsx_hl_factory() -> Result<HighlightConfiguration, QueryError> {
HighlightConfiguration::new(
tree_sitter_typescript::language_tsx(),
&format!(
"{}{}",
tree_sitter_javascript::HIGHLIGHT_QUERY,
tree_sitter_typescript::HIGHLIGHT_QUERY,
),
tree_sitter_javascript::INJECTION_QUERY,
&format!(
"{}{}",
tree_sitter_typescript::LOCALS_QUERY,
tree_sitter_javascript::LOCALS_QUERY,
),
)
}