Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Fix bracket highlight flickering when editing (#49797)
Xin Zhaoc22eaa7a2628
diff --git a/crates/editor/src/highlight_matching_bracket.rs b/crates/editor/src/highlight_matching_bracket.rsindex 4401a87195e..15db878791c 100644--- a/crates/editor/src/highlight_matching_bracket.rs+++ b/crates/editor/src/highlight_matching_bracket.rs@@ -11,11 +11,10 @@ impl Editor {snapshot: &DisplaySnapshot,cx: &mut Context<Editor>,) {- self.clear_highlights(HighlightKey::MatchingBracket, cx);-let newest_selection = self.selections.newest::<MultiBufferOffset>(&snapshot);// Don't highlight brackets if the selection isn't emptyif !newest_selection.is_empty() {+ self.clear_highlights(HighlightKey::MatchingBracket, cx);return;}@@ -40,16 +39,32 @@ impl Editor {});self.refresh_matching_bracket_highlights_task = cx.spawn({let buffer_snapshot = buffer_snapshot.clone();- async move |editor, cx| {- if let Some((opening_range, closing_range)) = task.await {- editor- .update(cx, |editor, cx| {+ async move |this, cx| {+ let bracket_ranges = task.await;+ let current_ranges = this+ .read_with(cx, |editor, cx| {+ editor+ .display_map+ .read(cx)+ .text_highlights(HighlightKey::MatchingBracket)+ .map(|(_, ranges)| ranges.to_vec())+ })+ .ok()+ .flatten();+ let new_ranges = bracket_ranges.map(|(opening_range, closing_range)| {+ vec![+ opening_range.to_anchors(&buffer_snapshot),+ closing_range.to_anchors(&buffer_snapshot),+ ]+ });++ if current_ranges != new_ranges {+ this.update(cx, |editor, cx| {+ editor.clear_highlights(HighlightKey::MatchingBracket, cx);+ if let Some(new_ranges) = new_ranges {editor.highlight_text(HighlightKey::MatchingBracket,- vec![- opening_range.to_anchors(&buffer_snapshot),- closing_range.to_anchors(&buffer_snapshot),- ],+ new_ranges,HighlightStyle {background_color: Some(cx.theme()@@ -60,8 +75,9 @@ impl Editor {},cx,)- })- .ok();+ }+ })+ .ok();}}});