Keep old bracket highlights for the same version

Kirill Bulatov created

Change summary

crates/debugger_ui/src/session/running/console.rs |  1 +
crates/editor/src/bracket_colorization.rs         |  1 +
crates/editor/src/display_map.rs                  | 15 +++++++++++++--
crates/editor/src/editor.rs                       |  2 ++
crates/language/src/buffer.rs                     |  3 ++-
5 files changed, 19 insertions(+), 3 deletions(-)

Detailed changes

crates/editor/src/display_map.rs 🔗

@@ -485,8 +485,17 @@ impl DisplayMap {
         key: HighlightKey,
         ranges: Vec<Range<Anchor>>,
         style: HighlightStyle,
+        merge: bool,
     ) {
-        self.text_highlights.insert(key, Arc::new((style, ranges)));
+        let to_insert = match self.text_highlights.remove(&key).filter(|_| merge) {
+            Some(previous) => {
+                let mut merged_ranges = previous.1.clone();
+                merged_ranges.extend(ranges);
+                Arc::new((style, merged_ranges))
+            }
+            None => Arc::new((style, ranges)),
+        };
+        self.text_highlights.insert(key, to_insert);
     }
 
     pub(crate) fn highlight_inlays(
@@ -523,7 +532,7 @@ impl DisplayMap {
     pub fn clear_highlights(&mut self, type_id: TypeId) -> bool {
         let mut cleared = self
             .text_highlights
-            .remove(HighlightKey::Type(type_id))
+            .remove(&HighlightKey::Type(type_id))
             .is_some();
         self.text_highlights.retain(|key, _| {
             let retain = if let HighlightKey::TypePlus(key_type_id, _) = key {
@@ -2402,6 +2411,7 @@ pub mod tests {
                         ..buffer_snapshot.anchor_after(Point::new(3, 18)),
                 ],
                 red.into(),
+                false,
             );
             map.insert_blocks(
                 [BlockProperties {
@@ -2724,6 +2734,7 @@ pub mod tests {
                     })
                     .collect(),
                 style,
+                false,
             );
         });
 

crates/editor/src/editor.rs 🔗

@@ -20916,6 +20916,7 @@ impl Editor {
         key: usize,
         ranges: Vec<Range<Anchor>>,
         style: HighlightStyle,
+        merge: bool,
         cx: &mut Context<Self>,
     ) {
         self.display_map.update(cx, |map, _| {
@@ -20923,6 +20924,7 @@ impl Editor {
                 HighlightKey::TypePlus(TypeId::of::<T>(), key),
                 ranges,
                 style,
+                merge,
             );
         });
         cx.notify();

crates/language/src/buffer.rs 🔗

@@ -4202,7 +4202,8 @@ impl BufferSnapshot {
                         .map(|grammar| grammar.brackets_config.as_ref().unwrap())
                         .collect::<Vec<_>>();
 
-                    // todo! this seems like a wrong parameter: instead, use chunk range, `Range<BufferRow>`, as a key part + add bracket_id that will be used for each bracket
+                    // todo! this seems like a wrong parameter: add bracket_id that will be used for each bracket
+                    // this will require changing `depth` treatment during style application, we'll need to group brackets by their hsla
                     let mut depth = 0;
                     let chunk_range = chunk_range.clone();
                     let new_matches = iter::from_fn(move || {