From fcf6a9d58aa3819c1ba2b65feda0dd9053ccf8eb Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 1 Oct 2021 17:32:22 -0600 Subject: [PATCH] Move Buffer::save_as to Editor This removes buffer's dependency on Worktree, preparing the path for us to pull it into its own crate. Co-Authored-By: Max Brunsfeld --- zed/src/editor.rs | 22 ++++++++++++++-- zed/src/editor/buffer.rs | 54 +++++++++++----------------------------- zed/src/worktree.rs | 2 +- 3 files changed, 36 insertions(+), 42 deletions(-) diff --git a/zed/src/editor.rs b/zed/src/editor.rs index 4d0ead1f1c..4fea4be1de 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -2656,8 +2656,26 @@ impl workspace::ItemView for Editor { path: &Path, cx: &mut ViewContext, ) -> Task> { - self.buffer - .update(cx, |b, cx| b.save_as(worktree, path, cx)) + self.buffer.update(cx, |buffer, cx| { + let handle = cx.handle(); + let text = buffer.as_rope().clone(); + let version = buffer.version(); + + let save_as = worktree.update(cx, |worktree, cx| { + worktree + .as_local_mut() + .unwrap() + .save_buffer_as(handle, path, text, cx) + }); + + cx.spawn(|buffer, mut cx| async move { + save_as.await.map(|new_file| { + buffer.update(&mut cx, |buffer, cx| { + buffer.did_save(version, new_file.mtime, Some(new_file), cx); + }); + }) + }) + }) } fn is_dirty(&self, cx: &AppContext) -> bool { diff --git a/zed/src/editor/buffer.rs b/zed/src/editor/buffer.rs index d7ad880093..fd6cd0c4d0 100644 --- a/zed/src/editor/buffer.rs +++ b/zed/src/editor/buffer.rs @@ -9,14 +9,11 @@ use crate::{ settings::{HighlightId, HighlightMap}, time::{self, ReplicaId}, util::Bias, - worktree::{File, Worktree}, + worktree::File, }; pub use anchor::*; use anyhow::{anyhow, Result}; -use sum_tree::{self, FilterCursor, SumTree}; -use gpui::{ - AppContext, Entity, ModelContext, ModelHandle, Task, -}; +use gpui::{AppContext, Entity, ModelContext, Task}; use lazy_static::lazy_static; use operation_queue::OperationQueue; use parking_lot::Mutex; @@ -37,6 +34,7 @@ use std::{ sync::Arc, time::{Duration, Instant, SystemTime, UNIX_EPOCH}, }; +use sum_tree::{self, FilterCursor, SumTree}; use tree_sitter::{InputEdit, Parser, QueryCursor}; use zrpc::proto; @@ -702,54 +700,32 @@ impl Buffer { Ok(cx.spawn(|this, mut cx| async move { let (version, mtime) = save.await?; this.update(&mut cx, |this, cx| { - this.did_save(version.clone(), mtime, cx); + this.did_save(version.clone(), mtime, None, cx); }); Ok((version, mtime)) })) } - pub fn save_as( - &mut self, - worktree: &ModelHandle, - path: impl Into>, - cx: &mut ModelContext, - ) -> Task> { - let path = path.into(); - let handle = cx.handle(); - let text = self.visible_text.clone(); - let version = self.version.clone(); - - let save_as = worktree.update(cx, |worktree, cx| { - worktree - .as_local_mut() - .unwrap() - .save_buffer_as(handle, path, text, cx) - }); - - cx.spawn(|this, mut cx| async move { - save_as.await.map(|new_file| { - this.update(&mut cx, |this, cx| { - if let Some(language) = new_file.select_language(cx) { - this.language = Some(language); - this.reparse(cx); - } - - let mtime = new_file.mtime; - this.file = Some(new_file); - this.did_save(version, mtime, cx); - }); - }) - }) + pub fn as_rope(&self) -> &Rope { + &self.visible_text } pub fn did_save( &mut self, version: time::Global, mtime: SystemTime, + new_file: Option, cx: &mut ModelContext, ) { self.saved_mtime = mtime; self.saved_version = version; + if let Some(new_file) = new_file { + if let Some(language) = new_file.select_language(cx) { + self.language = Some(language); + self.reparse(cx); + } + self.file = Some(new_file); + } cx.emit(Event::Saved); } @@ -3438,7 +3414,7 @@ mod tests { assert!(buffer.is_dirty()); assert_eq!(*events.borrow(), &[Event::Edited, Event::Dirtied]); events.borrow_mut().clear(); - buffer.did_save(buffer.version(), buffer.file().unwrap().mtime, cx); + buffer.did_save(buffer.version(), buffer.file().unwrap().mtime, None, cx); }); // after saving, the buffer is not dirty, and emits a saved event. diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index b20b7530fd..425fb5e7d8 100644 --- a/zed/src/worktree.rs +++ b/zed/src/worktree.rs @@ -518,7 +518,7 @@ impl Worktree { .mtime .ok_or_else(|| anyhow!("missing mtime"))? .into(); - buffer.did_save(version, mtime, cx); + buffer.did_save(version, mtime, None, cx); Result::<_, anyhow::Error>::Ok(()) })?; }