From d4b9d9e82014d164ad38f3c860bbc90ddeadaa0f Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 11 Feb 2022 16:16:29 -0800 Subject: [PATCH] Inline MultiBuffer::format Put all the logic in Editor. Add an `all_buffers` method so the editor can format all of the buffers by itself. --- crates/editor/src/items.rs | 20 +++++++++++++------- crates/editor/src/multi_buffer.rs | 24 ++++-------------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 0e0a48b940..67a2d95dd4 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -11,7 +11,6 @@ use std::path::PathBuf; use std::rc::Rc; use std::{cell::RefCell, fmt::Write}; use text::{Point, Selection}; -use util::TryFutureExt; use workspace::{ ItemHandle, ItemNavHistory, ItemView, ItemViewHandle, NavHistory, PathOpener, Settings, StatusItemView, WeakItemHandle, Workspace, @@ -226,14 +225,21 @@ impl ItemView for Editor { cx: &mut ViewContext, ) -> Task> { let buffer = self.buffer().clone(); - cx.spawn(|editor, mut cx| async move { - buffer - .update(&mut cx, |buffer, cx| buffer.format(project, cx).log_err()) - .await; - editor.update(&mut cx, |editor, cx| { + let buffers = buffer.read(cx).all_buffers(); + let transaction = project.update(cx, |project, cx| project.format(buffers, true, cx)); + cx.spawn(|this, mut cx| async move { + let transaction = transaction.await?; + this.update(&mut cx, |editor, cx| { editor.request_autoscroll(Autoscroll::Fit, cx) }); - buffer.update(&mut cx, |buffer, cx| buffer.save(cx)).await?; + buffer + .update(&mut cx, |buffer, cx| { + if !buffer.is_singleton() { + buffer.push_transaction(&transaction.0); + } + buffer.save(cx) + }) + .await?; Ok(()) }) } diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index 19a11ddfa3..c639129e27 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -3,14 +3,13 @@ mod anchor; pub use anchor::{Anchor, AnchorRangeExt}; use anyhow::Result; use clock::ReplicaId; -use collections::{Bound, HashMap}; +use collections::{Bound, HashMap, HashSet}; use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task}; pub use language::Completion; use language::{ Buffer, BufferChunks, BufferSnapshot, Chunk, DiagnosticEntry, Event, File, Language, Outline, OutlineItem, Selection, ToOffset as _, ToPoint as _, ToPointUtf16 as _, TransactionId, }; -use project::Project; use std::{ cell::{Ref, RefCell}, cmp, fmt, io, @@ -931,27 +930,12 @@ impl MultiBuffer { cx.emit(event.clone()); } - pub fn format( - &mut self, - project: ModelHandle, - cx: &mut ModelContext, - ) -> Task> { - let buffers = self - .buffers + pub fn all_buffers(&self) -> HashSet> { + self.buffers .borrow() .values() .map(|state| state.buffer.clone()) - .collect(); - let transaction = project.update(cx, |project, cx| project.format(buffers, true, cx)); - cx.spawn(|this, mut cx| async move { - let transaction = transaction.await?; - this.update(&mut cx, |this, _| { - if !this.singleton { - this.push_transaction(&transaction.0); - } - }); - Ok(()) - }) + .collect() } pub fn save(&mut self, cx: &mut ModelContext) -> Task> {