Introduce an optional `primary` field to `ExcerptRange`

Antonio Scandurra created

Change summary

crates/diagnostics/src/diagnostics.rs |  1 
crates/editor/src/editor.rs           | 10 ++++
crates/editor/src/movement.rs         |  2 
crates/editor/src/multi_buffer.rs     | 61 +++++++++++++++++++++++++---
4 files changed, 66 insertions(+), 8 deletions(-)

Detailed changes

crates/diagnostics/src/diagnostics.rs 🔗

@@ -351,6 +351,7 @@ impl ProjectDiagnosticsEditor {
                                     buffer.clone(),
                                     [ExcerptRange {
                                         context: excerpt_start..excerpt_end,
+                                        primary: Some(range.clone()),
                                     }],
                                     excerpts_cx,
                                 )

crates/editor/src/editor.rs 🔗

@@ -7721,6 +7721,7 @@ mod tests {
                 toml_buffer.clone(),
                 [ExcerptRange {
                     context: Point::new(0, 0)..Point::new(2, 0),
+                    primary: None,
                 }],
                 cx,
             );
@@ -7728,6 +7729,7 @@ mod tests {
                 rust_buffer.clone(),
                 [ExcerptRange {
                     context: Point::new(0, 0)..Point::new(1, 0),
+                    primary: None,
                 }],
                 cx,
             );
@@ -9602,9 +9604,11 @@ mod tests {
                 [
                     ExcerptRange {
                         context: Point::new(0, 0)..Point::new(0, 4),
+                        primary: None,
                     },
                     ExcerptRange {
                         context: Point::new(1, 0)..Point::new(1, 4),
+                        primary: None,
                     },
                 ],
                 cx,
@@ -9645,7 +9649,7 @@ mod tests {
                 cccc)"});
         let excerpt_ranges = excerpt_ranges
             .into_iter()
-            .map(|context| ExcerptRange { context });
+            .map(|context| ExcerptRange { context, primary: None });
         let buffer = cx.add_model(|cx| Buffer::new(0, initial_text, cx));
         let multibuffer = cx.add_model(|cx| {
             let mut multibuffer = MultiBuffer::new(0);
@@ -9701,9 +9705,11 @@ mod tests {
                     [
                         ExcerptRange {
                             context: Point::new(0, 0)..Point::new(1, 4),
+                            primary: None,
                         },
                         ExcerptRange {
                             context: Point::new(1, 0)..Point::new(2, 4),
+                            primary: None,
                         },
                     ],
                     cx,
@@ -9789,9 +9795,11 @@ mod tests {
                     [
                         ExcerptRange {
                             context: Point::new(0, 0)..Point::new(1, 4),
+                            primary: None,
                         },
                         ExcerptRange {
                             context: Point::new(1, 0)..Point::new(2, 4),
+                            primary: None,
                         },
                     ],
                     cx,

crates/editor/src/movement.rs 🔗

@@ -496,9 +496,11 @@ mod tests {
                 [
                     ExcerptRange {
                         context: Point::new(0, 0)..Point::new(1, 4),
+                        primary: None,
                     },
                     ExcerptRange {
                         context: Point::new(2, 0)..Point::new(3, 2),
+                        primary: None,
                     },
                 ],
                 cx,

crates/editor/src/multi_buffer.rs 🔗

@@ -122,6 +122,7 @@ struct Excerpt {
 #[derive(Clone, Debug, Eq, PartialEq)]
 pub struct ExcerptRange<T> {
     pub context: Range<T>,
+    pub primary: Option<Range<T>>,
 }
 
 #[derive(Clone, Debug, Default)]
@@ -224,6 +225,7 @@ impl MultiBuffer {
             buffer,
             [ExcerptRange {
                 context: text::Anchor::MIN..text::Anchor::MAX,
+                primary: None,
             }],
             cx,
         );
@@ -722,6 +724,7 @@ impl MultiBuffer {
 
             excerpt_ranges.push(ExcerptRange {
                 context: excerpt_start..excerpt_end,
+                primary: Some(range),
             });
             range_counts.push(ranges_in_excerpt);
         }
@@ -819,6 +822,10 @@ impl MultiBuffer {
             let range = ExcerptRange {
                 context: buffer_snapshot.anchor_before(&range.context.start)
                     ..buffer_snapshot.anchor_after(&range.context.end),
+                primary: range.primary.map(|primary| {
+                    buffer_snapshot.anchor_before(&primary.start)
+                        ..buffer_snapshot.anchor_after(&primary.end)
+                }),
             };
             let excerpt = Excerpt::new(
                 id.clone(),
@@ -1418,6 +1425,7 @@ impl MultiBuffer {
                         let start_ix = buffer.clip_offset(rng.gen_range(0..=end_ix), Bias::Left);
                         ExcerptRange {
                             context: start_ix..end_ix,
+                            primary: None,
                         }
                     })
                     .collect::<Vec<_>>();
@@ -3142,6 +3150,7 @@ mod tests {
                 buffer_1.clone(),
                 [ExcerptRange {
                     context: Point::new(1, 2)..Point::new(2, 5),
+                    primary: None,
                 }],
                 cx,
             );
@@ -3157,6 +3166,7 @@ mod tests {
                 buffer_1.clone(),
                 [ExcerptRange {
                     context: Point::new(3, 3)..Point::new(4, 4),
+                    primary: None,
                 }],
                 cx,
             );
@@ -3164,6 +3174,7 @@ mod tests {
                 buffer_2.clone(),
                 [ExcerptRange {
                     context: Point::new(3, 1)..Point::new(3, 3),
+                    primary: None,
                 }],
                 cx,
             );
@@ -3411,8 +3422,22 @@ mod tests {
         let buffer_2 = cx.add_model(|cx| Buffer::new(0, "efghi", cx));
         let multibuffer = cx.add_model(|cx| {
             let mut multibuffer = MultiBuffer::new(0);
-            multibuffer.push_excerpts(buffer_1.clone(), [ExcerptRange { context: 0..4 }], cx);
-            multibuffer.push_excerpts(buffer_2.clone(), [ExcerptRange { context: 0..5 }], cx);
+            multibuffer.push_excerpts(
+                buffer_1.clone(),
+                [ExcerptRange {
+                    context: 0..4,
+                    primary: None,
+                }],
+                cx,
+            );
+            multibuffer.push_excerpts(
+                buffer_2.clone(),
+                [ExcerptRange {
+                    context: 0..5,
+                    primary: None,
+                }],
+                cx,
+            );
             multibuffer
         });
         let old_snapshot = multibuffer.read(cx).snapshot(cx);
@@ -3462,7 +3487,14 @@ mod tests {
         buffer_1.update(cx, |buffer, cx| buffer.edit([(4..4, "123")], cx));
         let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| {
             multibuffer
-                .push_excerpts(buffer_1.clone(), [ExcerptRange { context: 0..7 }], cx)
+                .push_excerpts(
+                    buffer_1.clone(),
+                    [ExcerptRange {
+                        context: 0..7,
+                        primary: None,
+                    }],
+                    cx,
+                )
                 .pop()
                 .unwrap()
         });
@@ -3477,9 +3509,18 @@ mod tests {
                 .push_excerpts(
                     buffer_2.clone(),
                     [
-                        ExcerptRange { context: 0..4 },
-                        ExcerptRange { context: 6..10 },
-                        ExcerptRange { context: 12..16 },
+                        ExcerptRange {
+                            context: 0..4,
+                            primary: None,
+                        },
+                        ExcerptRange {
+                            context: 6..10,
+                            primary: None,
+                        },
+                        ExcerptRange {
+                            context: 12..16,
+                            primary: None,
+                        },
                     ],
                     cx,
                 )
@@ -3525,7 +3566,10 @@ mod tests {
                 .insert_excerpts_after(
                     &excerpt_id_3,
                     buffer_2.clone(),
-                    [ExcerptRange { context: 5..8 }],
+                    [ExcerptRange {
+                        context: 5..8,
+                        primary: None,
+                    }],
                     cx,
                 )
                 .pop()
@@ -3676,6 +3720,7 @@ mod tests {
                                 buffer_handle.clone(),
                                 [ExcerptRange {
                                     context: start_ix..end_ix,
+                                    primary: None,
                                 }],
                                 cx,
                             )
@@ -3989,6 +4034,7 @@ mod tests {
                 buffer_1.clone(),
                 [ExcerptRange {
                     context: 0..buffer_1.read(cx).len(),
+                    primary: None,
                 }],
                 cx,
             );
@@ -3996,6 +4042,7 @@ mod tests {
                 buffer_2.clone(),
                 [ExcerptRange {
                     context: 0..buffer_2.read(cx).len(),
+                    primary: None,
                 }],
                 cx,
             );