Properly store merged bracket highlights

Kirill Bulatov created

Change summary

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(-)

Detailed changes

crates/editor/src/display_map.rs 🔗

@@ -486,11 +486,20 @@ impl DisplayMap {
         ranges: Vec<Range<Anchor>>,
         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::<MyType>()),
                 highlighted_ranges
@@ -2735,6 +2745,7 @@ pub mod tests {
                     .collect(),
                 style,
                 false,
+                cx,
             );
         });
 

crates/editor/src/editor.rs 🔗

@@ -20919,12 +20919,13 @@ impl Editor {
         merge: bool,
         cx: &mut Context<Self>,
     ) {
-        self.display_map.update(cx, |map, _| {
+        self.display_map.update(cx, |map, cx| {
             map.highlight_text(
                 HighlightKey::TypePlus(TypeId::of::<T>(), key),
                 ranges,
                 style,
                 merge,
+                cx,
             );
         });
         cx.notify();
@@ -20936,8 +20937,14 @@ impl Editor {
         style: HighlightStyle,
         cx: &mut Context<Self>,
     ) {
-        self.display_map.update(cx, |map, _| {
-            map.highlight_text(HighlightKey::Type(TypeId::of::<T>()), ranges, style, false)
+        self.display_map.update(cx, |map, cx| {
+            map.highlight_text(
+                HighlightKey::Type(TypeId::of::<T>()),
+                ranges,
+                style,
+                false,
+                cx,
+            )
         });
         cx.notify();
     }

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::<Vec<_>>();
         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()
     }