mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-02 13:30:26 +01:00
Do not panic on wrong grammar results (#66474)
Closes https://github.com/zed-industries/zed/issues/65051 Adjust the code to not panic if an underlying grammar returns an error. This does not fix the slim extension per se, instead just not showing any highlights at all. To fix Slim extension, it has to update to a never tree-sitter grammar that contains the fix. Release Notes: - Fixed a panic when underlying tree-sitter highlights fail
This commit is contained in:
@@ -1158,7 +1158,7 @@ impl Language {
|
||||
};
|
||||
let text_len = text.source_len();
|
||||
let highlights = if text_len > MAX_TEXT_HIGHLIGHT_ENTRY_BYTES {
|
||||
Self::compute_resolved_highlights(grammar, &highlights_config.query, text)
|
||||
self.compute_resolved_highlights(grammar, &highlights_config.query, text)
|
||||
} else {
|
||||
let key = TextHighlightKey::new(text.source_chunks(), text_len);
|
||||
match highlights_config
|
||||
@@ -1172,7 +1172,7 @@ impl Language {
|
||||
highlights_config.text_highlight_cache.insert(
|
||||
key,
|
||||
cached_text,
|
||||
Self::compute_resolved_highlights(grammar, &highlights_config.query, text),
|
||||
self.compute_resolved_highlights(grammar, &highlights_config.query, text),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1187,13 +1187,23 @@ impl Language {
|
||||
}
|
||||
|
||||
fn compute_resolved_highlights(
|
||||
&self,
|
||||
grammar: &Arc<Grammar>,
|
||||
query: &tree_sitter::Query,
|
||||
text: &(impl HighlightSource + ?Sized),
|
||||
) -> ResolvedHighlights {
|
||||
let highlight_map = grammar.highlight_map();
|
||||
let text_len = text.source_len();
|
||||
let tree = text.parse(grammar);
|
||||
let tree = match text.parse(grammar) {
|
||||
Ok(tree) => tree,
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
"highlighting {} text of {text_len} bytes: {error:#}",
|
||||
self.name()
|
||||
);
|
||||
return ResolvedHighlights::default();
|
||||
}
|
||||
};
|
||||
let regions = with_query_cursor(|cursor| {
|
||||
cursor.set_byte_range(0..text_len);
|
||||
let mut captures = cursor.captures(query, tree.root_node(), text.text_provider());
|
||||
@@ -1474,7 +1484,7 @@ impl Debug for Language {
|
||||
pub trait HighlightSource {
|
||||
fn source_len(&self) -> usize;
|
||||
fn source_chunks(&self) -> impl Iterator<Item = &str>;
|
||||
fn parse(&self, grammar: &Grammar) -> Tree;
|
||||
fn parse(&self, grammar: &Grammar) -> Result<Tree>;
|
||||
fn text_provider<'a>(&'a self) -> impl tree_sitter::TextProvider<&'a [u8]> + 'a;
|
||||
}
|
||||
|
||||
@@ -1487,12 +1497,12 @@ impl HighlightSource for str {
|
||||
iter::once(self)
|
||||
}
|
||||
|
||||
fn parse(&self, grammar: &Grammar) -> Tree {
|
||||
fn parse(&self, grammar: &Grammar) -> Result<Tree> {
|
||||
with_parser(|parser| {
|
||||
parser.set_language(&grammar.ts_language)?;
|
||||
parser
|
||||
.set_language(&grammar.ts_language)
|
||||
.expect("incompatible grammar");
|
||||
parser.parse(self, None).unwrap()
|
||||
.parse(self, None)
|
||||
.context("tree-sitter returned no tree")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1510,7 +1520,7 @@ impl HighlightSource for Rope {
|
||||
self.chunks()
|
||||
}
|
||||
|
||||
fn parse(&self, grammar: &Grammar) -> Tree {
|
||||
fn parse(&self, grammar: &Grammar) -> Result<Tree> {
|
||||
parse_text(grammar, self, None)
|
||||
}
|
||||
|
||||
@@ -1519,11 +1529,9 @@ impl HighlightSource for Rope {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn parse_text(grammar: &Grammar, text: &Rope, old_tree: Option<Tree>) -> Tree {
|
||||
pub(crate) fn parse_text(grammar: &Grammar, text: &Rope, old_tree: Option<Tree>) -> Result<Tree> {
|
||||
with_parser(|parser| {
|
||||
parser
|
||||
.set_language(&grammar.ts_language)
|
||||
.expect("incompatible grammar");
|
||||
parser.set_language(&grammar.ts_language)?;
|
||||
let mut chunks = text.chunks_in_range(0..text.len());
|
||||
parser
|
||||
.parse_with_options(
|
||||
@@ -1534,7 +1542,7 @@ pub(crate) fn parse_text(grammar: &Grammar, text: &Rope, old_tree: Option<Tree>)
|
||||
old_tree.as_ref(),
|
||||
None,
|
||||
)
|
||||
.unwrap()
|
||||
.context("tree-sitter returned no tree")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user