Remove ExcerptProperties struct

Max Brunsfeld and Nathan Sobo created

Pass buffer and range as separate parameters

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

crates/diagnostics/src/diagnostics.rs |   8 
crates/editor/src/editor.rs           |  50 +-------
crates/editor/src/movement.rs         |  18 --
crates/editor/src/multi_buffer.rs     | 169 ++++++----------------------
4 files changed, 54 insertions(+), 191 deletions(-)

Detailed changes

crates/diagnostics/src/diagnostics.rs 🔗

@@ -7,7 +7,7 @@ use editor::{
     display_map::{BlockDisposition, BlockId, BlockProperties, RenderBlock},
     highlight_diagnostic_message,
     items::BufferItemHandle,
-    Autoscroll, BuildSettings, Editor, ExcerptId, ExcerptProperties, MultiBuffer, ToOffset,
+    Autoscroll, BuildSettings, Editor, ExcerptId, MultiBuffer, ToOffset,
 };
 use gpui::{
     action, elements::*, fonts::TextStyle, keymap::Binding, AnyViewHandle, AppContext, Entity,
@@ -335,10 +335,8 @@ impl ProjectDiagnosticsEditor {
                             );
                             let excerpt_id = excerpts.insert_excerpt_after(
                                 &prev_excerpt_id,
-                                ExcerptProperties {
-                                    buffer: &buffer,
-                                    range: excerpt_start..excerpt_end,
-                                },
+                                buffer.clone(),
+                                excerpt_start..excerpt_end,
                                 excerpts_cx,
                             );
 

crates/editor/src/editor.rs 🔗

@@ -35,8 +35,8 @@ use language::{
 };
 use multi_buffer::MultiBufferChunks;
 pub use multi_buffer::{
-    char_kind, Anchor, AnchorRangeExt, CharKind, ExcerptId, ExcerptProperties, MultiBuffer,
-    MultiBufferSnapshot, ToOffset, ToPoint,
+    char_kind, Anchor, AnchorRangeExt, CharKind, ExcerptId, MultiBuffer, MultiBufferSnapshot,
+    ToOffset, ToPoint,
 };
 use ordered_float::OrderedFloat;
 use postage::watch;
@@ -7870,20 +7870,8 @@ mod tests {
         let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(3, 4, 'a'), cx));
         let multibuffer = cx.add_model(|cx| {
             let mut multibuffer = MultiBuffer::new(0);
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(0, 0)..Point::new(0, 4),
-                },
-                cx,
-            );
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(1, 0)..Point::new(1, 4),
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer.clone(), Point::new(0, 0)..Point::new(0, 4), cx);
+            multibuffer.push_excerpt(buffer.clone(), Point::new(1, 0)..Point::new(1, 4), cx);
             multibuffer
         });
 
@@ -7921,20 +7909,8 @@ mod tests {
         let buffer = cx.add_model(|cx| Buffer::new(0, sample_text(3, 4, 'a'), cx));
         let multibuffer = cx.add_model(|cx| {
             let mut multibuffer = MultiBuffer::new(0);
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(0, 0)..Point::new(1, 4),
-                },
-                cx,
-            );
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(1, 0)..Point::new(2, 4),
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer, Point::new(0, 0)..Point::new(1, 4), cx);
+            multibuffer.push_excerpt(buffer, Point::new(1, 0)..Point::new(2, 4), cx);
             multibuffer
         });
 
@@ -7986,19 +7962,11 @@ mod tests {
         let multibuffer = cx.add_model(|cx| {
             let mut multibuffer = MultiBuffer::new(0);
             excerpt1_id = Some(multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(0, 0)..Point::new(1, 4),
-                },
+                buffer.clone(),
+                Point::new(0, 0)..Point::new(1, 4),
                 cx,
             ));
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(1, 0)..Point::new(2, 4),
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer.clone(), Point::new(1, 0)..Point::new(2, 4), cx);
             multibuffer
         });
         assert_eq!(

crates/editor/src/movement.rs 🔗

@@ -225,7 +225,7 @@ pub fn surrounding_word(map: &DisplaySnapshot, position: DisplayPoint) -> Range<
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::{Buffer, DisplayMap, ExcerptProperties, MultiBuffer};
+    use crate::{Buffer, DisplayMap, MultiBuffer};
     use language::Point;
 
     #[gpui::test]
@@ -239,20 +239,8 @@ mod tests {
         let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
         let multibuffer = cx.add_model(|cx| {
             let mut multibuffer = MultiBuffer::new(0);
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(0, 0)..Point::new(1, 4),
-                },
-                cx,
-            );
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer,
-                    range: Point::new(2, 0)..Point::new(3, 2),
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer.clone(), Point::new(0, 0)..Point::new(1, 4), cx);
+            multibuffer.push_excerpt(buffer.clone(), Point::new(2, 0)..Point::new(3, 2), cx);
             multibuffer
         });
 

crates/editor/src/multi_buffer.rs 🔗

@@ -98,11 +98,6 @@ pub struct MultiBufferSnapshot {
     has_conflict: bool,
 }
 
-pub struct ExcerptProperties<'a, T> {
-    pub buffer: &'a ModelHandle<Buffer>,
-    pub range: Range<T>,
-}
-
 pub struct ExcerptBoundary {
     pub row: u32,
     pub buffer: BufferSnapshot,
@@ -178,13 +173,7 @@ impl MultiBuffer {
     pub fn singleton(buffer: ModelHandle<Buffer>, cx: &mut ModelContext<Self>) -> Self {
         let mut this = Self::new(buffer.read(cx).replica_id());
         this.singleton = true;
-        this.push_excerpt(
-            ExcerptProperties {
-                buffer: &buffer,
-                range: text::Anchor::min()..text::Anchor::max(),
-            },
-            cx,
-        );
+        this.push_excerpt(buffer, text::Anchor::min()..text::Anchor::max(), cx);
         this.snapshot.borrow_mut().singleton = true;
         this
     }
@@ -587,19 +576,21 @@ impl MultiBuffer {
 
     pub fn push_excerpt<O>(
         &mut self,
-        props: ExcerptProperties<O>,
+        buffer: ModelHandle<Buffer>,
+        range: Range<O>,
         cx: &mut ModelContext<Self>,
     ) -> ExcerptId
     where
         O: text::ToOffset,
     {
-        self.insert_excerpt_after(&ExcerptId::max(), props, cx)
+        self.insert_excerpt_after(&ExcerptId::max(), buffer, range, cx)
     }
 
     pub fn insert_excerpt_after<O>(
         &mut self,
         prev_excerpt_id: &ExcerptId,
-        props: ExcerptProperties<O>,
+        buffer: ModelHandle<Buffer>,
+        range: Range<O>,
         cx: &mut ModelContext<Self>,
     ) -> ExcerptId
     where
@@ -608,9 +599,10 @@ impl MultiBuffer {
         assert_eq!(self.history.transaction_depth, 0);
         self.sync(cx);
 
-        let buffer_snapshot = props.buffer.read(cx).snapshot();
-        let range = buffer_snapshot.anchor_before(&props.range.start)
-            ..buffer_snapshot.anchor_after(&props.range.end);
+        let buffer_id = buffer.id();
+        let buffer_snapshot = buffer.read(cx).snapshot();
+        let range =
+            buffer_snapshot.anchor_before(&range.start)..buffer_snapshot.anchor_after(&range.end);
         let mut snapshot = self.snapshot.borrow_mut();
         let mut cursor = snapshot.excerpts.cursor::<Option<&ExcerptId>>();
         let mut new_excerpts = cursor.slice(&Some(prev_excerpt_id), Bias::Right, &());
@@ -633,28 +625,26 @@ impl MultiBuffer {
         let id = ExcerptId::between(&prev_id, &next_id);
 
         let mut buffers = self.buffers.borrow_mut();
-        let buffer_state = buffers
-            .entry(props.buffer.id())
-            .or_insert_with(|| BufferState {
-                last_version: buffer_snapshot.version().clone(),
-                last_parse_count: buffer_snapshot.parse_count(),
-                last_selections_update_count: buffer_snapshot.selections_update_count(),
-                last_diagnostics_update_count: buffer_snapshot.diagnostics_update_count(),
-                last_file_update_count: buffer_snapshot.file_update_count(),
-                excerpts: Default::default(),
-                _subscriptions: [
-                    cx.observe(&props.buffer, |_, _, cx| cx.notify()),
-                    cx.subscribe(&props.buffer, Self::on_buffer_event),
-                ],
-                buffer: props.buffer.clone(),
-            });
+        let buffer_state = buffers.entry(buffer_id).or_insert_with(|| BufferState {
+            last_version: buffer_snapshot.version().clone(),
+            last_parse_count: buffer_snapshot.parse_count(),
+            last_selections_update_count: buffer_snapshot.selections_update_count(),
+            last_diagnostics_update_count: buffer_snapshot.diagnostics_update_count(),
+            last_file_update_count: buffer_snapshot.file_update_count(),
+            excerpts: Default::default(),
+            _subscriptions: [
+                cx.observe(&buffer, |_, _, cx| cx.notify()),
+                cx.subscribe(&buffer, Self::on_buffer_event),
+            ],
+            buffer,
+        });
         if let Err(ix) = buffer_state.excerpts.binary_search(&id) {
             buffer_state.excerpts.insert(ix, id.clone());
         }
 
         let excerpt = Excerpt::new(
             id.clone(),
-            props.buffer.id(),
+            buffer_id,
             buffer_snapshot,
             range,
             cursor.item().is_some(),
@@ -1082,13 +1072,7 @@ impl MultiBuffer {
                     &buffer.text()[start_ix..end_ix]
                 );
 
-                let excerpt_id = self.push_excerpt(
-                    ExcerptProperties {
-                        buffer: &buffer_handle,
-                        range: start_ix..end_ix,
-                    },
-                    cx,
-                );
+                let excerpt_id = self.push_excerpt(buffer_handle.clone(), start_ix..end_ix, cx);
                 log::info!("Inserted with id: {:?}", excerpt_id);
             } else {
                 let remove_count = rng.gen_range(1..=excerpt_ids.len());
@@ -2683,13 +2667,7 @@ mod tests {
 
         let subscription = multibuffer.update(cx, |multibuffer, cx| {
             let subscription = multibuffer.subscribe();
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_1,
-                    range: Point::new(1, 2)..Point::new(2, 5),
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer_1.clone(), Point::new(1, 2)..Point::new(2, 5), cx);
             assert_eq!(
                 subscription.consume().into_inner(),
                 [Edit {
@@ -2698,20 +2676,8 @@ mod tests {
                 }]
             );
 
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_1,
-                    range: Point::new(3, 3)..Point::new(4, 4),
-                },
-                cx,
-            );
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_2,
-                    range: Point::new(3, 1)..Point::new(3, 3),
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer_1.clone(), Point::new(3, 3)..Point::new(4, 4), cx);
+            multibuffer.push_excerpt(buffer_2.clone(), Point::new(3, 1)..Point::new(3, 3), cx);
             assert_eq!(
                 subscription.consume().into_inner(),
                 [Edit {
@@ -2913,20 +2879,8 @@ 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_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_1,
-                    range: 0..4,
-                },
-                cx,
-            );
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_2,
-                    range: 0..5,
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer_1.clone(), 0..4, cx);
+            multibuffer.push_excerpt(buffer_2.clone(), 0..5, cx);
             multibuffer
         });
         let old_snapshot = multibuffer.read(cx).snapshot(cx);
@@ -2975,13 +2929,7 @@ mod tests {
         // Add an excerpt from buffer 1 that spans this new insertion.
         buffer_1.update(cx, |buffer, cx| buffer.edit([4..4], "123", cx));
         let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| {
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_1,
-                    range: 0..7,
-                },
-                cx,
-            )
+            multibuffer.push_excerpt(buffer_1.clone(), 0..7, cx)
         });
 
         let snapshot_1 = multibuffer.read(cx).snapshot(cx);
@@ -2991,27 +2939,9 @@ mod tests {
         let (excerpt_id_2, excerpt_id_3, _) = multibuffer.update(cx, |multibuffer, cx| {
             multibuffer.remove_excerpts([&excerpt_id_1], cx);
             (
-                multibuffer.push_excerpt(
-                    ExcerptProperties {
-                        buffer: &buffer_2,
-                        range: 0..4,
-                    },
-                    cx,
-                ),
-                multibuffer.push_excerpt(
-                    ExcerptProperties {
-                        buffer: &buffer_2,
-                        range: 6..10,
-                    },
-                    cx,
-                ),
-                multibuffer.push_excerpt(
-                    ExcerptProperties {
-                        buffer: &buffer_2,
-                        range: 12..16,
-                    },
-                    cx,
-                ),
+                multibuffer.push_excerpt(buffer_2.clone(), 0..4, cx),
+                multibuffer.push_excerpt(buffer_2.clone(), 6..10, cx),
+                multibuffer.push_excerpt(buffer_2.clone(), 12..16, cx),
             )
         });
         let snapshot_2 = multibuffer.read(cx).snapshot(cx);
@@ -3049,14 +2979,7 @@ mod tests {
         // that intersects the old excerpt.
         let excerpt_id_5 = multibuffer.update(cx, |multibuffer, cx| {
             multibuffer.remove_excerpts([&excerpt_id_3], cx);
-            multibuffer.insert_excerpt_after(
-                &excerpt_id_3,
-                ExcerptProperties {
-                    buffer: &buffer_2,
-                    range: 5..8,
-                },
-                cx,
-            )
+            multibuffer.insert_excerpt_after(&excerpt_id_3, buffer_2.clone(), 5..8, cx)
         });
 
         let snapshot_3 = multibuffer.read(cx).snapshot(cx);
@@ -3198,10 +3121,8 @@ mod tests {
                     let excerpt_id = multibuffer.update(cx, |multibuffer, cx| {
                         multibuffer.insert_excerpt_after(
                             &prev_excerpt_id,
-                            ExcerptProperties {
-                                buffer: &buffer_handle,
-                                range: start_ix..end_ix,
-                            },
+                            buffer_handle.clone(),
+                            start_ix..end_ix,
                             cx,
                         )
                     });
@@ -3507,20 +3428,8 @@ mod tests {
         let multibuffer = cx.add_model(|_| MultiBuffer::new(0));
         let group_interval = multibuffer.read(cx).history.group_interval;
         multibuffer.update(cx, |multibuffer, cx| {
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_1,
-                    range: 0..buffer_1.read(cx).len(),
-                },
-                cx,
-            );
-            multibuffer.push_excerpt(
-                ExcerptProperties {
-                    buffer: &buffer_2,
-                    range: 0..buffer_2.read(cx).len(),
-                },
-                cx,
-            );
+            multibuffer.push_excerpt(buffer_1.clone(), 0..buffer_1.read(cx).len(), cx);
+            multibuffer.push_excerpt(buffer_2.clone(), 0..buffer_2.read(cx).len(), cx);
         });
 
         let mut now = Instant::now();