feat: broader syntax highlighting support
This commit is contained in:
@@ -12,6 +12,10 @@ pub(crate) enum ContentType {
|
||||
pub(crate) enum FileType {
|
||||
Rust,
|
||||
JavaScript,
|
||||
Jsx,
|
||||
TypeScript,
|
||||
Tsx,
|
||||
Go,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@@ -52,7 +56,11 @@ pub(crate) fn classify_content(content: &[u8]) -> ContentType {
|
||||
pub(crate) fn file_type_from_path(path: &str) -> FileType {
|
||||
match Path::new(path).extension().map(|it| it.to_str()).flatten() {
|
||||
| Some("rs") => FileType::Rust,
|
||||
| Some("js") | Some("jsx") => FileType::JavaScript,
|
||||
| Some("js") => FileType::JavaScript,
|
||||
| Some("jsx") => FileType::Jsx,
|
||||
| Some("ts") => FileType::TypeScript,
|
||||
| Some("tsx") => FileType::Tsx,
|
||||
| Some("go") => FileType::Go,
|
||||
| _ => FileType::Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use crate::{component::code_view::symbol_highlight_style, theme, util};
|
||||
|
||||
pub(crate) type HighlightedRange = (std::ops::Range<usize>, gpui::HighlightStyle);
|
||||
@@ -11,6 +13,31 @@ pub(crate) struct HighlightedLine {
|
||||
highlights: Vec<(std::ops::Range<usize>, gpui::HighlightStyle)>,
|
||||
}
|
||||
|
||||
static TS_HIGHLIGHTS: LazyLock<String> = LazyLock::new(|| {
|
||||
[
|
||||
tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
tree_sitter_typescript::HIGHLIGHTS_QUERY,
|
||||
]
|
||||
.join("\n")
|
||||
});
|
||||
|
||||
static JSX_HIGHLIGHTS: LazyLock<String> = LazyLock::new(|| {
|
||||
[
|
||||
tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
tree_sitter_javascript::JSX_HIGHLIGHT_QUERY,
|
||||
]
|
||||
.join("\n")
|
||||
});
|
||||
|
||||
static TSX_HIGHLIGHTS: LazyLock<String> = LazyLock::new(|| {
|
||||
[
|
||||
tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
tree_sitter_javascript::JSX_HIGHLIGHT_QUERY,
|
||||
tree_sitter_typescript::HIGHLIGHTS_QUERY,
|
||||
]
|
||||
.join("\n")
|
||||
});
|
||||
|
||||
fn ts_highlight_configuration_for_file_type(
|
||||
file_type: util::file::FileType,
|
||||
) -> Option<tree_sitter_highlight::HighlightConfiguration> {
|
||||
@@ -24,6 +51,51 @@ fn ts_highlight_configuration_for_file_type(
|
||||
)
|
||||
.ok(),
|
||||
|
||||
| util::file::FileType::JavaScript => tree_sitter_highlight::HighlightConfiguration::new(
|
||||
tree_sitter_javascript::LANGUAGE.into(),
|
||||
"javascript",
|
||||
tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
tree_sitter_javascript::INJECTIONS_QUERY,
|
||||
tree_sitter_javascript::LOCALS_QUERY,
|
||||
)
|
||||
.ok(),
|
||||
|
||||
| util::file::FileType::Jsx => tree_sitter_highlight::HighlightConfiguration::new(
|
||||
tree_sitter_javascript::LANGUAGE.into(),
|
||||
"jsx",
|
||||
&JSX_HIGHLIGHTS,
|
||||
tree_sitter_javascript::INJECTIONS_QUERY,
|
||||
tree_sitter_javascript::LOCALS_QUERY,
|
||||
)
|
||||
.ok(),
|
||||
|
||||
| util::file::FileType::TypeScript => tree_sitter_highlight::HighlightConfiguration::new(
|
||||
tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
|
||||
"typescript",
|
||||
&TS_HIGHLIGHTS,
|
||||
tree_sitter_javascript::INJECTIONS_QUERY,
|
||||
tree_sitter_typescript::LOCALS_QUERY,
|
||||
)
|
||||
.ok(),
|
||||
|
||||
| util::file::FileType::Tsx => tree_sitter_highlight::HighlightConfiguration::new(
|
||||
tree_sitter_typescript::LANGUAGE_TSX.into(),
|
||||
"tsx",
|
||||
&TSX_HIGHLIGHTS,
|
||||
tree_sitter_javascript::INJECTIONS_QUERY,
|
||||
tree_sitter_typescript::LOCALS_QUERY,
|
||||
)
|
||||
.ok(),
|
||||
|
||||
| util::file::FileType::Go => tree_sitter_highlight::HighlightConfiguration::new(
|
||||
tree_sitter_go::LANGUAGE.into(),
|
||||
"go",
|
||||
tree_sitter_go::HIGHLIGHTS_QUERY,
|
||||
"",
|
||||
"",
|
||||
)
|
||||
.ok(),
|
||||
|
||||
| _ => None,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user