Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Do not panic on wrong grammar results (#66474)
Kirill Bulatov8a47b3510959
diff --git a/crates/language/src/language.rs b/crates/language/src/language.rsindex cf2dfcbd523..720a087c9ac 100644--- a/crates/language/src/language.rs+++ b/crates/language/src/language.rs@@ -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")})}