diff --git a/crates/editor/src/highlight_matching_bracket.rs b/crates/editor/src/highlight_matching_bracket.rs index 4401a87195eb13413cfc2fa9c8e7784f737ac3fc..15db878791cc04d430786c2329055fb3e805c4c5 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, ) { - self.clear_highlights(HighlightKey::MatchingBracket, cx); - let newest_selection = self.selections.newest::(&snapshot); // Don't highlight brackets if the selection isn't empty if !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(); } } });