From 528a4dd9b48af6852ea2cc7c4853fa21f3a8b4e0 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 24 Jan 2022 11:28:46 +0100 Subject: [PATCH] Fix regression causing guests to miss operations while opening a buffer --- crates/project/src/project.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 04f8aa511a..f39e5a33b0 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -524,11 +524,7 @@ impl Project { // Record the fact that the buffer is no longer loading. this.loading_buffers.remove(&project_path); let buffer = load_result.map_err(Arc::new)?; - this.open_buffers.insert( - buffer.read(cx).remote_id() as usize, - OpenBuffer::Loaded(buffer.downgrade()), - ); - this.assign_language_to_buffer(&worktree, &buffer, cx); + this.register_buffer(&buffer, &worktree, cx)?; Ok(buffer) })); }) @@ -568,9 +564,7 @@ impl Project { }) .await?; this.update(&mut cx, |this, cx| { - this.open_buffers - .insert(buffer.id(), OpenBuffer::Loaded(buffer.downgrade())); - this.assign_language_to_buffer(&worktree, &buffer, cx); + this.assign_language_to_buffer(&buffer, &worktree, cx); }); Ok(()) }) @@ -618,10 +612,32 @@ impl Project { result } + fn register_buffer( + &mut self, + buffer: &ModelHandle, + worktree: &ModelHandle, + cx: &mut ModelContext, + ) -> Result<()> { + match self.open_buffers.insert( + buffer.read(cx).remote_id() as usize, + OpenBuffer::Loaded(buffer.downgrade()), + ) { + Some(OpenBuffer::Operations(pending_ops)) => { + buffer.update(cx, |buf, cx| buf.apply_ops(pending_ops, cx))?; + } + Some(OpenBuffer::Loaded(_)) => { + return Err(anyhow!("registered the same buffer twice")); + } + None => {} + } + self.assign_language_to_buffer(&buffer, &worktree, cx); + Ok(()) + } + fn assign_language_to_buffer( &mut self, - worktree: &ModelHandle, buffer: &ModelHandle, + worktree: &ModelHandle, cx: &mut ModelContext, ) -> Option<()> { let (path, full_path) = {