Add `Editor::for_multibuffer` and repurpose `Editor::for_buffer`

Antonio Scandurra and Nathan Sobo created

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

Change summary

crates/diagnostics/src/diagnostics.rs |  3 ++-
crates/editor/src/editor.rs           | 23 ++++++++++++++---------
crates/search/src/buffer_search.rs    | 10 ++++++----
crates/search/src/project_search.rs   |  2 +-
crates/server/src/rpc.rs              | 15 ++++-----------
5 files changed, 27 insertions(+), 26 deletions(-)

Detailed changes

crates/diagnostics/src/diagnostics.rs 🔗

@@ -113,7 +113,8 @@ impl ProjectDiagnosticsEditor {
 
         let excerpts = cx.add_model(|cx| MultiBuffer::new(project_handle.read(cx).replica_id()));
         let editor = cx.add_view(|cx| {
-            let mut editor = Editor::for_buffer(excerpts.clone(), Some(project_handle.clone()), cx);
+            let mut editor =
+                Editor::for_multibuffer(excerpts.clone(), Some(project_handle.clone()), cx);
             editor.set_vertical_scroll_margin(5, cx);
             editor
         });

crates/editor/src/editor.rs 🔗

@@ -341,7 +341,6 @@ pub fn init(cx: &mut MutableAppContext) {
     cx.add_async_action(Editor::find_all_references);
 
     workspace::register_editor_builder(cx, |project, buffer, cx| {
-        let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
         Editor::for_buffer(buffer, Some(project), cx)
     });
 }
@@ -832,6 +831,15 @@ impl Editor {
     }
 
     pub fn for_buffer(
+        buffer: ModelHandle<Buffer>,
+        project: Option<ModelHandle<Project>>,
+        cx: &mut ViewContext<Self>,
+    ) -> Self {
+        let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
+        Self::new(EditorMode::Full, buffer, project, None, cx)
+    }
+
+    pub fn for_multibuffer(
         buffer: ModelHandle<MultiBuffer>,
         project: Option<ModelHandle<Project>>,
         cx: &mut ViewContext<Self>,
@@ -855,8 +863,7 @@ impl Editor {
             return item;
         }
 
-        let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
-        let editor = cx.add_view(|cx| Editor::for_buffer(multibuffer, Some(project.clone()), cx));
+        let editor = cx.add_view(|cx| Editor::for_buffer(buffer, Some(project.clone()), cx));
         workspace.add_item(Box::new(editor.clone()), cx);
         editor
     }
@@ -969,11 +976,8 @@ impl Editor {
             .update(cx, |project, cx| project.create_buffer(cx))
             .log_err()
         {
-            let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
             workspace.add_item(
-                Box::new(
-                    cx.add_view(|cx| Editor::for_buffer(multibuffer, Some(project.clone()), cx)),
-                ),
+                Box::new(cx.add_view(|cx| Editor::for_buffer(buffer, Some(project.clone()), cx))),
                 cx,
             );
         }
@@ -2380,7 +2384,8 @@ impl Editor {
 
         workspace.update(&mut cx, |workspace, cx| {
             let project = workspace.project().clone();
-            let editor = cx.add_view(|cx| Editor::for_buffer(excerpt_buffer, Some(project), cx));
+            let editor =
+                cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx));
             workspace.add_item(Box::new(editor.clone()), cx);
             editor.update(cx, |editor, cx| {
                 let color = editor.style(cx).highlighted_line_background;
@@ -4397,7 +4402,7 @@ impl Editor {
 
             workspace.update(&mut cx, |workspace, cx| {
                 let editor =
-                    cx.add_view(|cx| Editor::for_buffer(excerpt_buffer, Some(project), cx));
+                    cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx));
                 editor.update(cx, |editor, cx| {
                     let color = editor.style(cx).highlighted_line_background;
                     editor.highlight_background::<Self>(ranges_to_highlight, color, cx);

crates/search/src/buffer_search.rs 🔗

@@ -510,8 +510,9 @@ impl SearchBar {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use editor::{DisplayPoint, Editor, MultiBuffer};
+    use editor::{DisplayPoint, Editor};
     use gpui::{color::Color, TestAppContext};
+    use language::Buffer;
     use std::sync::Arc;
     use unindent::Unindent as _;
 
@@ -523,9 +524,10 @@ mod tests {
         let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
         cx.update(|cx| cx.set_global(settings));
 
-        let buffer = cx.update(|cx| {
-            MultiBuffer::build_simple(
-                &r#"
+        let buffer = cx.add_model(|cx| {
+            Buffer::new(
+                0,
+                r#"
                 A regular expression (shortened as regex or regexp;[1] also referred to as
                 rational expression[2][3]) is a sequence of characters that specifies a search
                 pattern in text. Usually such patterns are used by string-searching algorithms

crates/search/src/project_search.rs 🔗

@@ -339,7 +339,7 @@ impl ProjectSearchView {
         });
 
         let results_editor = cx.add_view(|cx| {
-            let mut editor = Editor::for_buffer(excerpts, Some(project), cx);
+            let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
             editor.set_searchable(false);
             editor
         });

crates/server/src/rpc.rs 🔗

@@ -1013,8 +1013,8 @@ mod tests {
     };
     use collections::BTreeMap;
     use editor::{
-        self, ConfirmCodeAction, ConfirmCompletion, ConfirmRename, Editor, Input, MultiBuffer,
-        Redo, Rename, ToOffset, ToggleCodeActions, Undo,
+        self, ConfirmCodeAction, ConfirmCompletion, ConfirmRename, Editor, Input, Redo, Rename,
+        ToOffset, ToggleCodeActions, Undo,
     };
     use gpui::{executor, ModelHandle, TestAppContext};
     use language::{
@@ -1140,10 +1140,7 @@ mod tests {
             .update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.txt"), cx))
             .await
             .unwrap();
-        let buffer_b = cx_b.add_model(|cx| MultiBuffer::singleton(buffer_b, cx));
-        buffer_b.read_with(cx_b, |buf, cx| {
-            assert_eq!(buf.read(cx).text(), "b-contents")
-        });
+        buffer_b.read_with(cx_b, |buf, _| assert_eq!(buf.text(), "b-contents"));
         project_a.read_with(cx_a, |project, cx| {
             assert!(project.has_open_buffer((worktree_id, "b.txt"), cx))
         });
@@ -2176,11 +2173,7 @@ mod tests {
             .unwrap();
         let (window_b, _) = cx_b.add_window(|_| EmptyView);
         let editor_b = cx_b.add_view(window_b, |cx| {
-            Editor::for_buffer(
-                cx.add_model(|cx| MultiBuffer::singleton(buffer_b.clone(), cx)),
-                Some(project_b.clone()),
-                cx,
-            )
+            Editor::for_buffer(buffer_b.clone(), Some(project_b.clone()), cx)
         });
 
         let mut fake_language_server = fake_language_servers.next().await.unwrap();