chore: audit components
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
//! code without re-parsing it.
|
||||
|
||||
use gpui::SharedString;
|
||||
use gpui_native_utils::ResultLogErr;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use tree_sitter::StreamingIterator as _;
|
||||
@@ -29,7 +30,7 @@ pub enum Language {
|
||||
}
|
||||
|
||||
/// The semantic role a span plays.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Role {
|
||||
Text,
|
||||
Keyword,
|
||||
@@ -47,20 +48,31 @@ pub enum Role {
|
||||
}
|
||||
|
||||
/// A run of text sharing one role.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Span {
|
||||
pub text: SharedString,
|
||||
pub role: Role,
|
||||
}
|
||||
|
||||
/// One rendered line of code.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub struct Line {
|
||||
pub spans: Vec<Span>,
|
||||
}
|
||||
|
||||
impl Line {
|
||||
/// The line's flat text, in character order.
|
||||
pub fn text(&self) -> String {
|
||||
let mut text = String::new();
|
||||
for span in &self.spans {
|
||||
text.push_str(&span.text);
|
||||
}
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
/// Source text split into role-styled lines.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub struct HighlightedCode {
|
||||
pub lines: Vec<Line>,
|
||||
}
|
||||
@@ -77,10 +89,8 @@ pub fn highlight(source: &str, language: Language) -> HighlightedCode {
|
||||
.entry(language)
|
||||
.or_insert_with(|| {
|
||||
let mut parser = tree_sitter::Parser::new();
|
||||
if parser.set_language(&grammar).is_err() {
|
||||
return None;
|
||||
}
|
||||
let query = tree_sitter::Query::new(&grammar, query_source).ok()?;
|
||||
parser.set_language(&grammar).log_err()?;
|
||||
let query = tree_sitter::Query::new(&grammar, query_source).log_err()?;
|
||||
let roles = query
|
||||
.capture_names()
|
||||
.iter()
|
||||
@@ -99,6 +109,7 @@ pub fn highlight(source: &str, language: Language) -> HighlightedCode {
|
||||
};
|
||||
|
||||
let Some(tree) = highlighter.parser.parse(source, None) else {
|
||||
log::warn!("tree-sitter failed to parse {language:?}; rendering plain text");
|
||||
return plain(source);
|
||||
};
|
||||
|
||||
@@ -137,33 +148,73 @@ pub(crate) fn plain(source: &str) -> HighlightedCode {
|
||||
/// The grammar and its highlight query for a language, when compiled in.
|
||||
fn grammar_for(language: Language) -> Option<(tree_sitter::Language, &'static str)> {
|
||||
match language {
|
||||
Language::Rust => cfg!(feature = "rust").then(|| {
|
||||
(
|
||||
tree_sitter_rust::LANGUAGE.into(),
|
||||
tree_sitter_rust::HIGHLIGHTS_QUERY,
|
||||
)
|
||||
}),
|
||||
Language::TypeScript => cfg!(feature = "typescript").then(|| {
|
||||
(
|
||||
tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
|
||||
tree_sitter_typescript::HIGHLIGHTS_QUERY,
|
||||
)
|
||||
}),
|
||||
Language::JavaScript => cfg!(feature = "javascript").then(|| {
|
||||
(
|
||||
tree_sitter_javascript::LANGUAGE.into(),
|
||||
tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
)
|
||||
}),
|
||||
Language::Python => cfg!(feature = "python").then(|| {
|
||||
(
|
||||
tree_sitter_python::LANGUAGE.into(),
|
||||
tree_sitter_python::HIGHLIGHTS_QUERY,
|
||||
)
|
||||
}),
|
||||
Language::Rust => rust_grammar(),
|
||||
Language::TypeScript => typescript_grammar(),
|
||||
Language::JavaScript => javascript_grammar(),
|
||||
Language::Python => python_grammar(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The Rust grammar, compiled in only when the `rust` feature is on.
|
||||
#[cfg(feature = "rust")]
|
||||
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
Some((
|
||||
tree_sitter_rust::LANGUAGE.into(),
|
||||
tree_sitter_rust::HIGHLIGHTS_QUERY,
|
||||
))
|
||||
}
|
||||
|
||||
/// Without the feature, Rust sources render as plain text.
|
||||
#[cfg(not(feature = "rust"))]
|
||||
fn rust_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The TypeScript grammar, compiled in only when the `typescript` feature is on.
|
||||
#[cfg(feature = "typescript")]
|
||||
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
Some((
|
||||
tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
|
||||
tree_sitter_typescript::HIGHLIGHTS_QUERY,
|
||||
))
|
||||
}
|
||||
|
||||
/// Without the feature, TypeScript sources render as plain text.
|
||||
#[cfg(not(feature = "typescript"))]
|
||||
fn typescript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The JavaScript grammar, compiled in only when the `javascript` feature is on.
|
||||
#[cfg(feature = "javascript")]
|
||||
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
Some((
|
||||
tree_sitter_javascript::LANGUAGE.into(),
|
||||
tree_sitter_javascript::HIGHLIGHT_QUERY,
|
||||
))
|
||||
}
|
||||
|
||||
/// Without the feature, JavaScript sources render as plain text.
|
||||
#[cfg(not(feature = "javascript"))]
|
||||
fn javascript_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The Python grammar, compiled in only when the `python` feature is on.
|
||||
#[cfg(feature = "python")]
|
||||
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
Some((
|
||||
tree_sitter_python::LANGUAGE.into(),
|
||||
tree_sitter_python::HIGHLIGHTS_QUERY,
|
||||
))
|
||||
}
|
||||
|
||||
/// Without the feature, Python sources render as plain text.
|
||||
#[cfg(not(feature = "python"))]
|
||||
fn python_grammar() -> Option<(tree_sitter::Language, &'static str)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Maps a query capture name to a role; unmapped captures render as text.
|
||||
fn role_for_capture(name: &str) -> Option<Role> {
|
||||
match name.split('.').next()? {
|
||||
|
||||
Reference in New Issue
Block a user