feat: broader syntax highlighting support
This commit is contained in:
@@ -26,6 +26,9 @@ thiserror = "2.0.18"
|
|||||||
tree-sitter-highlight = "0.26.9"
|
tree-sitter-highlight = "0.26.9"
|
||||||
tree-sitter-rust = "0.24.2"
|
tree-sitter-rust = "0.24.2"
|
||||||
unicode-segmentation = "1.13.2"
|
unicode-segmentation = "1.13.2"
|
||||||
|
tree-sitter-typescript = "0.23.2"
|
||||||
|
tree-sitter-go = "0.25.0"
|
||||||
|
tree-sitter-javascript = "0.25.0"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
serde_json = "1.0.149"
|
serde_json = "1.0.149"
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ pub(crate) enum ContentType {
|
|||||||
pub(crate) enum FileType {
|
pub(crate) enum FileType {
|
||||||
Rust,
|
Rust,
|
||||||
JavaScript,
|
JavaScript,
|
||||||
|
Jsx,
|
||||||
|
TypeScript,
|
||||||
|
Tsx,
|
||||||
|
Go,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +56,11 @@ pub(crate) fn classify_content(content: &[u8]) -> ContentType {
|
|||||||
pub(crate) fn file_type_from_path(path: &str) -> FileType {
|
pub(crate) fn file_type_from_path(path: &str) -> FileType {
|
||||||
match Path::new(path).extension().map(|it| it.to_str()).flatten() {
|
match Path::new(path).extension().map(|it| it.to_str()).flatten() {
|
||||||
| Some("rs") => FileType::Rust,
|
| 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,
|
| _ => FileType::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use crate::{component::code_view::symbol_highlight_style, theme, util};
|
use crate::{component::code_view::symbol_highlight_style, theme, util};
|
||||||
|
|
||||||
pub(crate) type HighlightedRange = (std::ops::Range<usize>, gpui::HighlightStyle);
|
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)>,
|
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(
|
fn ts_highlight_configuration_for_file_type(
|
||||||
file_type: util::file::FileType,
|
file_type: util::file::FileType,
|
||||||
) -> Option<tree_sitter_highlight::HighlightConfiguration> {
|
) -> Option<tree_sitter_highlight::HighlightConfiguration> {
|
||||||
@@ -24,6 +51,51 @@ fn ts_highlight_configuration_for_file_type(
|
|||||||
)
|
)
|
||||||
.ok(),
|
.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,
|
| _ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user