Replace MultiBuffer::files with ::for_each_buffer

Max Brunsfeld created

Change summary

crates/editor/src/items.rs        | 14 ++++++++------
crates/editor/src/multi_buffer.rs | 12 +++++-------
crates/workspace/src/pane.rs      |  2 +-
3 files changed, 14 insertions(+), 14 deletions(-)

Detailed changes

crates/editor/src/items.rs 🔗

@@ -565,12 +565,14 @@ impl Item for Editor {
     }
 
     fn project_entry_ids(&self, cx: &AppContext) -> SmallVec<[ProjectEntryId; 3]> {
-        self.buffer
-            .read(cx)
-            .files(cx)
-            .into_iter()
-            .filter_map(|file| File::from_dyn(Some(file))?.project_entry_id(cx))
-            .collect()
+        let mut result = SmallVec::new();
+        self.buffer.read(cx).for_each_buffer(|buffer| {
+            let buffer = buffer.read(cx);
+            if let Some(file) = File::from_dyn(buffer.file()) {
+                result.extend(file.project_entry_id(cx));
+            }
+        });
+        result
     }
 
     fn is_singleton(&self, cx: &AppContext) -> bool {

crates/editor/src/multi_buffer.rs 🔗

@@ -9,11 +9,10 @@ use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
 pub use language::Completion;
 use language::{
     char_kind, AutoindentMode, Buffer, BufferChunks, BufferSnapshot, CharKind, Chunk, CursorShape,
-    DiagnosticEntry, File, IndentSize, Language, OffsetRangeExt, OffsetUtf16, Outline, OutlineItem,
+    DiagnosticEntry, IndentSize, Language, OffsetRangeExt, OffsetUtf16, Outline, OutlineItem,
     Point, PointUtf16, Selection, TextDimension, ToOffset as _, ToOffsetUtf16 as _, ToPoint as _,
     ToPointUtf16 as _, TransactionId, Unclipped,
 };
-use smallvec::SmallVec;
 use std::{
     borrow::Cow,
     cell::{Ref, RefCell},
@@ -1311,12 +1310,11 @@ impl MultiBuffer {
             .and_then(|(buffer, offset)| buffer.read(cx).language_at(offset))
     }
 
-    pub fn files<'a>(&'a self, cx: &'a AppContext) -> SmallVec<[&'a Arc<dyn File>; 2]> {
-        let buffers = self.buffers.borrow();
-        buffers
+    pub fn for_each_buffer(&self, mut f: impl FnMut(&ModelHandle<Buffer>)) {
+        self.buffers
+            .borrow()
             .values()
-            .filter_map(|buffer| buffer.buffer.read(cx).file())
-            .collect()
+            .for_each(|state| f(&state.buffer))
     }
 
     pub fn title<'a>(&'a self, cx: &'a AppContext) -> Cow<'a, str> {

crates/workspace/src/pane.rs 🔗

@@ -482,7 +482,7 @@ impl Pane {
     ) -> Box<dyn ItemHandle> {
         let existing_item = pane.update(cx, |pane, cx| {
             for (index, item) in pane.items.iter().enumerate() {
-                if item.project_path(cx).is_some()
+                if item.is_singleton(cx)
                     && item.project_entry_ids(cx).as_slice() == [project_entry_id]
                 {
                     let item = item.boxed_clone();