Merge pull request #925 from zed-industries/sort-refactor-multibuffers

Max Brunsfeld created

Sort buffers by their path in refactor multi-buffers

Change summary

crates/collab/src/rpc.rs    | 12 ++++++------
crates/editor/src/editor.rs | 15 ++++++++++-----
2 files changed, 16 insertions(+), 11 deletions(-)

Detailed changes

crates/collab/src/rpc.rs 🔗

@@ -3832,14 +3832,14 @@ mod tests {
                 .unwrap()
         });
         code_action_editor.update(cx_b, |editor, cx| {
-            assert_eq!(editor.text(cx), "\nmod other;\nfn main() { let foo = 4; }");
+            assert_eq!(editor.text(cx), "mod other;\nfn main() { let foo = 4; }\n");
             editor.undo(&Undo, cx);
             assert_eq!(
                 editor.text(cx),
-                "pub fn foo() -> usize { 4 }\nmod other;\nfn main() { let foo = other::foo(); }"
+                "mod other;\nfn main() { let foo = other::foo(); }\npub fn foo() -> usize { 4 }"
             );
             editor.redo(&Redo, cx);
-            assert_eq!(editor.text(cx), "\nmod other;\nfn main() { let foo = 4; }");
+            assert_eq!(editor.text(cx), "mod other;\nfn main() { let foo = 4; }\n");
         });
     }
 
@@ -4037,17 +4037,17 @@ mod tests {
         rename_editor.update(cx_b, |editor, cx| {
             assert_eq!(
                 editor.text(cx),
-                "const TWO: usize = one::THREE + one::THREE;\nconst THREE: usize = 1;"
+                "const THREE: usize = 1;\nconst TWO: usize = one::THREE + one::THREE;"
             );
             editor.undo(&Undo, cx);
             assert_eq!(
                 editor.text(cx),
-                "const TWO: usize = one::ONE + one::ONE;\nconst ONE: usize = 1;"
+                "const ONE: usize = 1;\nconst TWO: usize = one::ONE + one::ONE;"
             );
             editor.redo(&Redo, cx);
             assert_eq!(
                 editor.text(cx),
-                "const TWO: usize = one::THREE + one::THREE;\nconst THREE: usize = 1;"
+                "const THREE: usize = 1;\nconst TWO: usize = one::THREE + one::THREE;"
             );
         });
 

crates/editor/src/editor.rs 🔗

@@ -2499,11 +2499,16 @@ impl Editor {
     ) -> Result<()> {
         let replica_id = this.read_with(&cx, |this, cx| this.replica_id(cx));
 
+        let mut entries = transaction.0.into_iter().collect::<Vec<_>>();
+        entries.sort_unstable_by_key(|(buffer, _)| {
+            buffer.read_with(&cx, |buffer, _| buffer.file().map(|f| f.path().clone()))
+        });
+
         // If the project transaction's edits are all contained within this editor, then
         // avoid opening a new editor to display them.
-        let mut entries = transaction.0.iter();
-        if let Some((buffer, transaction)) = entries.next() {
-            if entries.next().is_none() {
+
+        if let Some((buffer, transaction)) = entries.first() {
+            if entries.len() == 1 {
                 let excerpt = this.read_with(&cx, |editor, cx| {
                     editor
                         .buffer()
@@ -2532,7 +2537,7 @@ impl Editor {
         let mut ranges_to_highlight = Vec::new();
         let excerpt_buffer = cx.add_model(|cx| {
             let mut multibuffer = MultiBuffer::new(replica_id).with_title(title);
-            for (buffer, transaction) in &transaction.0 {
+            for (buffer, transaction) in &entries {
                 let snapshot = buffer.read(cx).snapshot();
                 ranges_to_highlight.extend(
                     multibuffer.push_excerpts_with_context_lines(
@@ -2545,7 +2550,7 @@ impl Editor {
                     ),
                 );
             }
-            multibuffer.push_transaction(&transaction.0);
+            multibuffer.push_transaction(entries.iter().map(|(b, t)| (b, t)));
             multibuffer
         });