Offload text::Buffer
construction to background worker
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
parent
7e06062bdb
commit
1883e260ce
3 changed files with 16 additions and 17 deletions
|
@ -357,20 +357,6 @@ impl Buffer {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_file<T: Into<String>>(
|
|
||||||
replica_id: ReplicaId,
|
|
||||||
base_text: T,
|
|
||||||
diff_base: Option<T>,
|
|
||||||
file: Arc<dyn File>,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> Self {
|
|
||||||
Self::build(
|
|
||||||
TextBuffer::new(replica_id, cx.model_id() as u64, base_text.into()),
|
|
||||||
diff_base.map(|h| h.into().into_boxed_str().into()),
|
|
||||||
Some(file),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_proto(
|
pub fn from_proto(
|
||||||
replica_id: ReplicaId,
|
replica_id: ReplicaId,
|
||||||
message: proto::BufferState,
|
message: proto::BufferState,
|
||||||
|
@ -460,7 +446,11 @@ impl Buffer {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(buffer: TextBuffer, diff_base: Option<String>, file: Option<Arc<dyn File>>) -> Self {
|
pub fn build(
|
||||||
|
buffer: TextBuffer,
|
||||||
|
diff_base: Option<String>,
|
||||||
|
file: Option<Arc<dyn File>>,
|
||||||
|
) -> Self {
|
||||||
let saved_mtime = if let Some(file) = file.as_ref() {
|
let saved_mtime = if let Some(file) = file.as_ref() {
|
||||||
file.mtime()
|
file.mtime()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -109,6 +109,7 @@ pub struct Project {
|
||||||
collaborators: HashMap<proto::PeerId, Collaborator>,
|
collaborators: HashMap<proto::PeerId, Collaborator>,
|
||||||
client_subscriptions: Vec<client::Subscription>,
|
client_subscriptions: Vec<client::Subscription>,
|
||||||
_subscriptions: Vec<gpui::Subscription>,
|
_subscriptions: Vec<gpui::Subscription>,
|
||||||
|
next_buffer_id: u64,
|
||||||
opened_buffer: (watch::Sender<()>, watch::Receiver<()>),
|
opened_buffer: (watch::Sender<()>, watch::Receiver<()>),
|
||||||
shared_buffers: HashMap<proto::PeerId, HashSet<u64>>,
|
shared_buffers: HashMap<proto::PeerId, HashSet<u64>>,
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
|
@ -441,6 +442,7 @@ impl Project {
|
||||||
worktrees: Default::default(),
|
worktrees: Default::default(),
|
||||||
buffer_ordered_messages_tx: tx,
|
buffer_ordered_messages_tx: tx,
|
||||||
collaborators: Default::default(),
|
collaborators: Default::default(),
|
||||||
|
next_buffer_id: 0,
|
||||||
opened_buffers: Default::default(),
|
opened_buffers: Default::default(),
|
||||||
shared_buffers: Default::default(),
|
shared_buffers: Default::default(),
|
||||||
incomplete_remote_buffers: Default::default(),
|
incomplete_remote_buffers: Default::default(),
|
||||||
|
@ -509,6 +511,7 @@ impl Project {
|
||||||
worktrees: Vec::new(),
|
worktrees: Vec::new(),
|
||||||
buffer_ordered_messages_tx: tx,
|
buffer_ordered_messages_tx: tx,
|
||||||
loading_buffers_by_path: Default::default(),
|
loading_buffers_by_path: Default::default(),
|
||||||
|
next_buffer_id: 0,
|
||||||
opened_buffer: watch::channel(),
|
opened_buffer: watch::channel(),
|
||||||
shared_buffers: Default::default(),
|
shared_buffers: Default::default(),
|
||||||
incomplete_remote_buffers: Default::default(),
|
incomplete_remote_buffers: Default::default(),
|
||||||
|
@ -1401,9 +1404,10 @@ impl Project {
|
||||||
worktree: &ModelHandle<Worktree>,
|
worktree: &ModelHandle<Worktree>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Task<Result<ModelHandle<Buffer>>> {
|
) -> Task<Result<ModelHandle<Buffer>>> {
|
||||||
|
let buffer_id = post_inc(&mut self.next_buffer_id);
|
||||||
let load_buffer = worktree.update(cx, |worktree, cx| {
|
let load_buffer = worktree.update(cx, |worktree, cx| {
|
||||||
let worktree = worktree.as_local_mut().unwrap();
|
let worktree = worktree.as_local_mut().unwrap();
|
||||||
worktree.load_buffer(path, cx)
|
worktree.load_buffer(buffer_id, path, cx)
|
||||||
});
|
});
|
||||||
cx.spawn(|this, mut cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
let buffer = load_buffer.await?;
|
let buffer = load_buffer.await?;
|
||||||
|
|
|
@ -506,6 +506,7 @@ impl LocalWorktree {
|
||||||
|
|
||||||
pub(crate) fn load_buffer(
|
pub(crate) fn load_buffer(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
id: u64,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
cx: &mut ModelContext<Worktree>,
|
cx: &mut ModelContext<Worktree>,
|
||||||
) -> Task<Result<ModelHandle<Buffer>>> {
|
) -> Task<Result<ModelHandle<Buffer>>> {
|
||||||
|
@ -514,8 +515,12 @@ impl LocalWorktree {
|
||||||
let (file, contents, diff_base) = this
|
let (file, contents, diff_base) = this
|
||||||
.update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
|
.update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
|
||||||
.await?;
|
.await?;
|
||||||
|
let text_buffer = cx
|
||||||
|
.background()
|
||||||
|
.spawn(async move { text::Buffer::new(0, id, contents) })
|
||||||
|
.await;
|
||||||
Ok(cx.add_model(|cx| {
|
Ok(cx.add_model(|cx| {
|
||||||
let mut buffer = Buffer::from_file(0, contents, diff_base, Arc::new(file), cx);
|
let mut buffer = Buffer::build(text_buffer, diff_base, Some(Arc::new(file)));
|
||||||
buffer.git_diff_recalc(cx);
|
buffer.git_diff_recalc(cx);
|
||||||
buffer
|
buffer
|
||||||
}))
|
}))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue