@@ -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 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();
}
}
});