From ed7238fe4bd0e914c9ef3ec6f592eb4e290fe83d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 10 Nov 2025 01:50:36 +0200 Subject: [PATCH] Properly store merged bracket highlights --- crates/editor/src/display_map.rs | 15 +++++++++++++-- crates/editor/src/editor.rs | 13 ++++++++++--- crates/language/src/buffer/row_chunk.rs | 5 +++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index 598ac9b143a0e1bf1b280e6a256c82a3acabc74c..a6a2b6e216212db3df32d703d33428ffb341d823 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -486,11 +486,20 @@ impl DisplayMap { ranges: Vec>, style: HighlightStyle, merge: bool, + cx: &App, ) { + let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx); let to_insert = match self.text_highlights.remove(&key).filter(|_| merge) { Some(previous) => { let mut merged_ranges = previous.1.clone(); - merged_ranges.extend(ranges); + for new_range in ranges { + let i = merged_ranges + .binary_search_by(|probe| { + probe.start.cmp(&new_range.start, &multi_buffer_snapshot) + }) + .unwrap_or_else(|i| i); + merged_ranges.insert(i, new_range); + } Arc::new((style, merged_ranges)) } None => Arc::new((style, ranges)), @@ -2412,6 +2421,7 @@ pub mod tests { ], red.into(), false, + cx, ); map.insert_blocks( [BlockProperties { @@ -2723,7 +2733,7 @@ pub mod tests { ..Default::default() }; - map.update(cx, |map, _cx| { + map.update(cx, |map, cx| { map.highlight_text( HighlightKey::Type(TypeId::of::()), highlighted_ranges @@ -2735,6 +2745,7 @@ pub mod tests { .collect(), style, false, + cx, ); }); diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 4a2e43ae4f687e49dedf38c1b08746b42121d1a0..c8da05c872d5dd482537ddcb0c4363b01709b8fd 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -20919,12 +20919,13 @@ impl Editor { merge: bool, cx: &mut Context, ) { - self.display_map.update(cx, |map, _| { + self.display_map.update(cx, |map, cx| { map.highlight_text( HighlightKey::TypePlus(TypeId::of::(), key), ranges, style, merge, + cx, ); }); cx.notify(); @@ -20936,8 +20937,14 @@ impl Editor { style: HighlightStyle, cx: &mut Context, ) { - self.display_map.update(cx, |map, _| { - map.highlight_text(HighlightKey::Type(TypeId::of::()), ranges, style, false) + self.display_map.update(cx, |map, cx| { + map.highlight_text( + HighlightKey::Type(TypeId::of::()), + ranges, + style, + false, + cx, + ) }); cx.notify(); } diff --git a/crates/language/src/buffer/row_chunk.rs b/crates/language/src/buffer/row_chunk.rs index 0542de50077c7bfff3f9907ac26f30ef827704b4..0bf37a3929e0a2f5c842543a15278f08e22f453b 100644 --- a/crates/language/src/buffer/row_chunk.rs +++ b/crates/language/src/buffer/row_chunk.rs @@ -67,7 +67,8 @@ impl RowChunks { .iter() .map(|range| range.to_point(&self.snapshot)) // Be lenient and yield multiple chunks if they "touch" the exclusive part of the range. - // This will result in LSP hints [re-]queried for more ranges, but also more hints already visible when scrolling around. .map(|point_range| point_range.start.row..point_range.end.row + 1) + // This will result in LSP hints [re-]queried for more ranges, but also more hints already visible when scrolling around. + .map(|point_range| point_range.start.row..point_range.end.row + 1) .collect::>(); self.chunks .iter() @@ -75,7 +76,7 @@ impl RowChunks { let chunk_range = chunk.row_range(); row_ranges .iter() - .any(|row_range| chunk_range.overlaps(row_range)) + .any(|row_range| chunk_range.overlaps(&row_range)) }) .copied() }