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.
This commit is contained in:
Max Brunsfeld 2022-02-11 16:16:29 -08:00
parent 06bb8d2779
commit d4b9d9e820
2 changed files with 17 additions and 27 deletions

View file

@ -11,7 +11,6 @@ use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use std::{cell::RefCell, fmt::Write}; use std::{cell::RefCell, fmt::Write};
use text::{Point, Selection}; use text::{Point, Selection};
use util::TryFutureExt;
use workspace::{ use workspace::{
ItemHandle, ItemNavHistory, ItemView, ItemViewHandle, NavHistory, PathOpener, Settings, ItemHandle, ItemNavHistory, ItemView, ItemViewHandle, NavHistory, PathOpener, Settings,
StatusItemView, WeakItemHandle, Workspace, StatusItemView, WeakItemHandle, Workspace,
@ -226,14 +225,21 @@ impl ItemView for Editor {
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<()>> { ) -> Task<Result<()>> {
let buffer = self.buffer().clone(); let buffer = self.buffer().clone();
cx.spawn(|editor, mut cx| async move { let buffers = buffer.read(cx).all_buffers();
buffer let transaction = project.update(cx, |project, cx| project.format(buffers, true, cx));
.update(&mut cx, |buffer, cx| buffer.format(project, cx).log_err()) cx.spawn(|this, mut cx| async move {
.await; let transaction = transaction.await?;
editor.update(&mut cx, |editor, cx| { this.update(&mut cx, |editor, cx| {
editor.request_autoscroll(Autoscroll::Fit, 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(()) Ok(())
}) })
} }

View file

@ -3,14 +3,13 @@ mod anchor;
pub use anchor::{Anchor, AnchorRangeExt}; pub use anchor::{Anchor, AnchorRangeExt};
use anyhow::Result; use anyhow::Result;
use clock::ReplicaId; use clock::ReplicaId;
use collections::{Bound, HashMap}; use collections::{Bound, HashMap, HashSet};
use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task}; use gpui::{AppContext, Entity, ModelContext, ModelHandle, Task};
pub use language::Completion; pub use language::Completion;
use language::{ use language::{
Buffer, BufferChunks, BufferSnapshot, Chunk, DiagnosticEntry, Event, File, Language, Outline, Buffer, BufferChunks, BufferSnapshot, Chunk, DiagnosticEntry, Event, File, Language, Outline,
OutlineItem, Selection, ToOffset as _, ToPoint as _, ToPointUtf16 as _, TransactionId, OutlineItem, Selection, ToOffset as _, ToPoint as _, ToPointUtf16 as _, TransactionId,
}; };
use project::Project;
use std::{ use std::{
cell::{Ref, RefCell}, cell::{Ref, RefCell},
cmp, fmt, io, cmp, fmt, io,
@ -931,27 +930,12 @@ impl MultiBuffer {
cx.emit(event.clone()); cx.emit(event.clone());
} }
pub fn format( pub fn all_buffers(&self) -> HashSet<ModelHandle<Buffer>> {
&mut self, self.buffers
project: ModelHandle<Project>,
cx: &mut ModelContext<Self>,
) -> Task<Result<()>> {
let buffers = self
.buffers
.borrow() .borrow()
.values() .values()
.map(|state| state.buffer.clone()) .map(|state| state.buffer.clone())
.collect(); .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(())
})
} }
pub fn save(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> { pub fn save(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {