From 5fd084ec09dedb5ea0cfc08c1fba5cae40f4d2cc Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 4 May 2021 19:04:11 -0700 Subject: [PATCH 01/10] Start work on creating and saving new files --- Cargo.lock | 12 ++-- Cargo.toml | 10 +-- gpui/src/app.rs | 32 ++++++++- gpui/src/platform/mac/platform.rs | 41 ++++++++++- gpui/src/platform/mod.rs | 13 +++- gpui/src/platform/test.rs | 4 +- zed/src/editor/buffer_view.rs | 16 +++-- zed/src/menus.rs | 21 ++++-- zed/src/workspace.rs | 112 +++++++++++++++++++++--------- zed/src/worktree.rs | 41 ++++++----- 10 files changed, 227 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7fa8a95f7f79083e0f7cbc126f6561305aea5f7..e17c480da60b752d7f353c7915719691df77c093 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -449,7 +449,7 @@ dependencies = [ [[package]] name = "cocoa" version = "0.24.0" -source = "git+https://github.com/servo/core-foundation-rs?rev=e9a65bb15d591ec22649e03659db8095d4f2dd60#e9a65bb15d591ec22649e03659db8095d4f2dd60" +source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" dependencies = [ "bitflags 1.2.1", "block", @@ -464,7 +464,7 @@ dependencies = [ [[package]] name = "cocoa-foundation" version = "0.1.0" -source = "git+https://github.com/servo/core-foundation-rs?rev=e9a65bb15d591ec22649e03659db8095d4f2dd60#e9a65bb15d591ec22649e03659db8095d4f2dd60" +source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" dependencies = [ "bitflags 1.2.1", "block", @@ -499,7 +499,7 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" [[package]] name = "core-foundation" version = "0.9.1" -source = "git+https://github.com/servo/core-foundation-rs?rev=e9a65bb15d591ec22649e03659db8095d4f2dd60#e9a65bb15d591ec22649e03659db8095d4f2dd60" +source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" dependencies = [ "core-foundation-sys", "libc", @@ -508,12 +508,12 @@ dependencies = [ [[package]] name = "core-foundation-sys" version = "0.8.2" -source = "git+https://github.com/servo/core-foundation-rs?rev=e9a65bb15d591ec22649e03659db8095d4f2dd60#e9a65bb15d591ec22649e03659db8095d4f2dd60" +source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" [[package]] name = "core-graphics" version = "0.22.2" -source = "git+https://github.com/servo/core-foundation-rs?rev=e9a65bb15d591ec22649e03659db8095d4f2dd60#e9a65bb15d591ec22649e03659db8095d4f2dd60" +source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" dependencies = [ "bitflags 1.2.1", "core-foundation", @@ -525,7 +525,7 @@ dependencies = [ [[package]] name = "core-graphics-types" version = "0.1.1" -source = "git+https://github.com/servo/core-foundation-rs?rev=e9a65bb15d591ec22649e03659db8095d4f2dd60#e9a65bb15d591ec22649e03659db8095d4f2dd60" +source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" dependencies = [ "bitflags 1.2.1", "core-foundation", diff --git a/Cargo.toml b/Cargo.toml index c58e56b67a4349d3dc7ada8b8019805ed603d37f..e1729a3a46d0180f3ab4baaa7a496bf980a02f78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,11 @@ members = ["zed", "gpui", "fsevent", "scoped_pool"] [patch.crates-io] async-task = {git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e"} -# TODO - Remove when a version is released with this PR: https://github.com/servo/core-foundation-rs/pull/454 -cocoa = {git = "https://github.com/servo/core-foundation-rs", rev = "e9a65bb15d591ec22649e03659db8095d4f2dd60"} -cocoa-foundation = {git = "https://github.com/servo/core-foundation-rs", rev = "e9a65bb15d591ec22649e03659db8095d4f2dd60"} -core-foundation = {git = "https://github.com/servo/core-foundation-rs", rev = "e9a65bb15d591ec22649e03659db8095d4f2dd60"} -core-graphics = {git = "https://github.com/servo/core-foundation-rs", rev = "e9a65bb15d591ec22649e03659db8095d4f2dd60"} +# TODO - Remove when a version is released with this PR: https://github.com/servo/core-foundation-rs/pull/457 +cocoa = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} +cocoa-foundation = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} +core-foundation = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} +core-graphics = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} [profile.dev] split-debuginfo = "unpacked" diff --git a/gpui/src/app.rs b/gpui/src/app.rs index ac4c4e69b1782a416f5058025db0eafb974b7e7a..75d0ae01462be12ede526938f4126111c20ab076 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -21,7 +21,7 @@ use std::{ fmt::{self, Debug}, hash::{Hash, Hasher}, marker::PhantomData, - path::PathBuf, + path::{Path, PathBuf}, rc::{self, Rc}, sync::{Arc, Weak}, time::Duration, @@ -586,6 +586,22 @@ impl MutableAppContext { ); } + pub fn prompt_for_new_path(&self, directory: &Path, done_fn: F) + where + F: 'static + FnOnce(Option, &mut MutableAppContext), + { + let app = self.weak_self.as_ref().unwrap().upgrade().unwrap(); + let foreground = self.foreground.clone(); + self.platform().prompt_for_new_path( + directory, + Box::new(move |path| { + foreground + .spawn(async move { (done_fn)(path, &mut *app.borrow_mut()) }) + .detach(); + }), + ); + } + pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) { self.pending_effects .push_back(Effect::ViewNotification { window_id, view_id }); @@ -1765,6 +1781,20 @@ impl<'a, T: View> ViewContext<'a, T> { &self.app.ctx.background } + pub fn prompt_for_paths(&self, options: PathPromptOptions, done_fn: F) + where + F: 'static + FnOnce(Option>, &mut MutableAppContext), + { + self.app.prompt_for_paths(options, done_fn) + } + + pub fn prompt_for_new_path(&self, directory: &Path, done_fn: F) + where + F: 'static + FnOnce(Option, &mut MutableAppContext), + { + self.app.prompt_for_new_path(directory, done_fn) + } + pub fn debug_elements(&self) -> crate::json::Value { self.app.debug_elements(self.window_id).unwrap() } diff --git a/gpui/src/platform/mac/platform.rs b/gpui/src/platform/mac/platform.rs index e9bf34d684373fb695c3340ae731ca43e73bbf26..29f75ed3ac81d5fc5a4d8342d5e7b08d0b54c435 100644 --- a/gpui/src/platform/mac/platform.rs +++ b/gpui/src/platform/mac/platform.rs @@ -5,7 +5,7 @@ use cocoa::{ appkit::{ NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular, NSEventModifierFlags, NSMenu, NSMenuItem, NSModalResponse, NSOpenPanel, NSPasteboard, - NSPasteboardTypeString, NSWindow, + NSPasteboardTypeString, NSSavePanel, NSWindow, }, base::{id, nil, selector}, foundation::{NSArray, NSAutoreleasePool, NSData, NSInteger, NSString, NSURL}, @@ -25,7 +25,7 @@ use std::{ convert::TryInto, ffi::{c_void, CStr}, os::raw::c_char, - path::PathBuf, + path::{Path, PathBuf}, ptr, rc::Rc, slice, str, @@ -305,6 +305,43 @@ impl platform::Platform for MacPlatform { } } + fn prompt_for_new_path( + &self, + directory: &Path, + done_fn: Box)>, + ) { + unsafe { + let panel = NSSavePanel::savePanel(nil); + let path = ns_string(directory.to_string_lossy().as_ref()); + let url = NSURL::fileURLWithPath_isDirectory_(nil, path, true.to_objc()); + panel.setDirectoryURL(url); + + let done_fn = Cell::new(Some(done_fn)); + let block = ConcreteBlock::new(move |response: NSModalResponse| { + let result = if response == NSModalResponse::NSModalResponseOk { + let url = panel.URL(); + let string = url.absoluteString(); + let string = std::ffi::CStr::from_ptr(string.UTF8String()) + .to_string_lossy() + .to_string(); + if let Some(path) = string.strip_prefix("file://") { + Some(PathBuf::from(path)) + } else { + None + } + } else { + None + }; + + if let Some(done_fn) = done_fn.take() { + (done_fn)(result); + } + }); + let block = block.copy(); + let _: () = msg_send![panel, beginWithCompletionHandler: block]; + } + } + fn fonts(&self) -> Arc { self.fonts.clone() } diff --git a/gpui/src/platform/mod.rs b/gpui/src/platform/mod.rs index b98d3a687bcd35a2dc22a4ca6cff600d5fea87ea..e5e81c424e729f3214f8af0b0ca07126d16e0e20 100644 --- a/gpui/src/platform/mod.rs +++ b/gpui/src/platform/mod.rs @@ -19,7 +19,13 @@ use crate::{ }; use async_task::Runnable; pub use event::Event; -use std::{any::Any, ops::Range, path::PathBuf, rc::Rc, sync::Arc}; +use std::{ + any::Any, + ops::Range, + path::{Path, PathBuf}, + rc::Rc, + sync::Arc, +}; pub trait Platform { fn on_menu_command(&self, callback: Box)>); @@ -45,6 +51,11 @@ pub trait Platform { options: PathPromptOptions, done_fn: Box>)>, ); + fn prompt_for_new_path( + &self, + directory: &Path, + done_fn: Box)>, + ); fn quit(&self); fn write_to_clipboard(&self, item: ClipboardItem); fn read_from_clipboard(&self) -> Option; diff --git a/gpui/src/platform/test.rs b/gpui/src/platform/test.rs index 878449a0216e9a0a95447d9abaef16e089f8672c..de385bbf844a0493579500a18aee8726bd34c608 100644 --- a/gpui/src/platform/test.rs +++ b/gpui/src/platform/test.rs @@ -1,6 +1,6 @@ use crate::ClipboardItem; use pathfinder_geometry::vector::Vector2F; -use std::{any::Any, cell::RefCell, rc::Rc, sync::Arc}; +use std::{any::Any, cell::RefCell, path::Path, rc::Rc, sync::Arc}; struct Platform { dispatcher: Arc, @@ -77,6 +77,8 @@ impl super::Platform for Platform { ) { } + fn prompt_for_new_path(&self, _: &Path, _: Box)>) {} + fn write_to_clipboard(&self, item: ClipboardItem) { *self.current_clipboard_item.borrow_mut() = Some(item); } diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 40055103c62e3c9529eb24f8b14250200c7d0633..58e4428d6004e3d1b31d4151be646b8510fea077 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -6,11 +6,10 @@ use crate::{settings::Settings, watch, workspace, worktree::FileHandle}; use anyhow::Result; use futures_core::future::LocalBoxFuture; use gpui::{ - fonts::Properties as FontProperties, keymap::Binding, text_layout, AppContext, ClipboardItem, - Element, ElementBox, Entity, FontCache, ModelHandle, MutableAppContext, View, ViewContext, - WeakViewHandle, + fonts::Properties as FontProperties, geometry::vector::Vector2F, keymap::Binding, text_layout, + AppContext, ClipboardItem, Element, ElementBox, Entity, FontCache, ModelHandle, + MutableAppContext, TextLayoutCache, View, ViewContext, WeakViewHandle, }; -use gpui::{geometry::vector::Vector2F, TextLayoutCache}; use parking_lot::Mutex; use serde::{Deserialize, Serialize}; use smallvec::SmallVec; @@ -2135,7 +2134,14 @@ impl workspace::ItemView for BufferView { Some(clone) } - fn save(&self, ctx: &mut ViewContext) -> LocalBoxFuture<'static, Result<()>> { + fn save( + &mut self, + file: Option, + ctx: &mut ViewContext, + ) -> LocalBoxFuture<'static, Result<()>> { + if file.is_some() { + self.file = file; + } if let Some(file) = self.file.as_ref() { self.buffer .update(ctx, |buffer, ctx| buffer.save(file, ctx)) diff --git a/zed/src/menus.rs b/zed/src/menus.rs index 08afb1e990c413f2fc89fae3d820d9c1d56dc462..8def5fafbac1ff246734a06819947571bff7db5e 100644 --- a/zed/src/menus.rs +++ b/zed/src/menus.rs @@ -24,12 +24,21 @@ pub fn menus(settings: Receiver) -> Vec> { }, Menu { name: "File", - items: vec![MenuItem::Action { - name: "Open…", - keystroke: Some("cmd-o"), - action: "workspace:open", - arg: Some(Box::new(settings)), - }], + items: vec![ + MenuItem::Action { + name: "New", + keystroke: Some("cmd-n"), + action: "workspace:new_file", + arg: None, + }, + MenuItem::Separator, + MenuItem::Action { + name: "Open…", + keystroke: Some("cmd-o"), + action: "workspace:open", + arg: Some(Box::new(settings)), + }, + ], }, Menu { name: "Edit", diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 629d23beecce0bdf1e1c59a76cd6fbe465f92f9b..50864a874bca59cbc6d8b937e9d4ba787ff95275 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -6,6 +6,7 @@ pub use pane_group::*; use crate::{ settings::Settings, watch::{self, Receiver}, + worktree::FileHandle, }; use gpui::{MutableAppContext, PathPromptOptions}; use std::path::PathBuf; @@ -15,6 +16,7 @@ pub fn init(app: &mut MutableAppContext) { app.add_global_action("app:quit", quit); app.add_action("workspace:save", Workspace::save_active_item); app.add_action("workspace:debug_elements", Workspace::debug_elements); + app.add_action("workspace:new_file", Workspace::open_new_file); app.add_bindings(vec![ Binding::new("cmd-s", "workspace:save", None), Binding::new("cmd-alt-i", "workspace:debug_elements", None), @@ -108,7 +110,11 @@ pub trait ItemView: View { fn is_dirty(&self, _: &AppContext) -> bool { false } - fn save(&self, _: &mut ViewContext) -> LocalBoxFuture<'static, anyhow::Result<()>> { + fn save( + &mut self, + _: Option, + _: &mut ViewContext, + ) -> LocalBoxFuture<'static, anyhow::Result<()>> { Box::pin(async { Ok(()) }) } fn should_activate_item_on_event(_: &Self::Event) -> bool { @@ -128,7 +134,11 @@ pub trait ItemViewHandle: Send + Sync { fn id(&self) -> usize; fn to_any(&self) -> AnyViewHandle; fn is_dirty(&self, ctx: &AppContext) -> bool; - fn save(&self, ctx: &mut MutableAppContext) -> LocalBoxFuture<'static, anyhow::Result<()>>; + fn save( + &self, + file: Option, + ctx: &mut MutableAppContext, + ) -> LocalBoxFuture<'static, anyhow::Result<()>>; } impl ItemViewHandle for ViewHandle { @@ -167,8 +177,12 @@ impl ItemViewHandle for ViewHandle { }) } - fn save(&self, ctx: &mut MutableAppContext) -> LocalBoxFuture<'static, anyhow::Result<()>> { - self.update(ctx, |item, ctx| item.save(ctx)) + fn save( + &self, + file: Option, + ctx: &mut MutableAppContext, + ) -> LocalBoxFuture<'static, anyhow::Result<()>> { + self.update(ctx, |item, ctx| item.save(file, ctx)) } fn is_dirty(&self, ctx: &AppContext) -> bool { @@ -209,6 +223,7 @@ pub struct Workspace { (usize, u64), postage::watch::Receiver, Arc>>>, >, + untitled_buffers: HashSet>, } impl Workspace { @@ -234,6 +249,7 @@ impl Workspace { replica_id, worktrees: Default::default(), buffers: Default::default(), + untitled_buffers: Default::default(), } } @@ -272,15 +288,7 @@ impl Workspace { let entries = paths .iter() .cloned() - .map(|path| { - for tree in self.worktrees.iter() { - if let Ok(relative_path) = path.strip_prefix(tree.read(ctx).abs_path()) { - return (tree.id(), relative_path.into()); - } - } - let worktree_id = self.add_worktree(&path, ctx); - (worktree_id, Path::new("").into()) - }) + .map(|path| self.file_for_path(&path, ctx)) .collect::>(); let bg = ctx.background_executor().clone(); @@ -288,12 +296,12 @@ impl Workspace { .iter() .cloned() .zip(entries.into_iter()) - .map(|(path, entry)| { + .map(|(abs_path, file)| { ctx.spawn( - bg.spawn(async move { path.is_file() }), - |me, is_file, ctx| { + bg.spawn(async move { abs_path.is_file() }), + move |me, is_file, ctx| { if is_file { - me.open_entry(entry, ctx) + me.open_entry(file.entry_id(), ctx) } else { None } @@ -310,13 +318,26 @@ impl Workspace { } } - pub fn add_worktree(&mut self, path: &Path, ctx: &mut ViewContext) -> usize { + fn file_for_path(&mut self, abs_path: &Path, ctx: &mut ViewContext) -> FileHandle { + for tree in self.worktrees.iter() { + if let Ok(relative_path) = abs_path.strip_prefix(tree.read(ctx).abs_path()) { + return tree.file(relative_path, ctx.as_ref()); + } + } + let worktree = self.add_worktree(&abs_path, ctx); + worktree.file(Path::new(""), ctx.as_ref()) + } + + pub fn add_worktree( + &mut self, + path: &Path, + ctx: &mut ViewContext, + ) -> ModelHandle { let worktree = ctx.add_model(|ctx| Worktree::new(path, ctx)); - let worktree_id = worktree.id(); ctx.observe_model(&worktree, |_, _, ctx| ctx.notify()); - self.worktrees.insert(worktree); + self.worktrees.insert(worktree.clone()); ctx.notify(); - worktree_id + worktree } pub fn toggle_modal(&mut self, ctx: &mut ViewContext, add_view: F) @@ -346,6 +367,15 @@ impl Workspace { } } + pub fn open_new_file(&mut self, _: &(), ctx: &mut ViewContext) { + let buffer = ctx.add_model(|_| Buffer::new(self.replica_id, "")); + let buffer_view = Box::new(ctx.add_view(|ctx| { + BufferView::for_buffer(buffer.clone(), None, self.settings.clone(), ctx) + })); + self.untitled_buffers.insert(buffer); + self.add_item(buffer_view, ctx); + } + #[must_use] pub fn open_entry( &mut self, @@ -381,13 +411,11 @@ impl Workspace { } }; - let file = match worktree.file(path.clone(), ctx.as_ref()) { - Some(file) => file, - None => { - log::error!("path {:?} does not exist", path); - return None; - } - }; + let file = worktree.file(path.clone(), ctx.as_ref()); + if file.is_deleted() { + log::error!("path {:?} does not exist", path); + return None; + } self.loading_entries.insert(entry.clone()); @@ -441,12 +469,34 @@ impl Workspace { } pub fn save_active_item(&mut self, _: &(), ctx: &mut ViewContext) { - self.active_pane.update(ctx, |pane, ctx| { + let handle = ctx.handle(); + let first_worktree = self.worktrees.iter().next(); + self.active_pane.update(ctx, move |pane, ctx| { if let Some(item) = pane.active_item() { - let task = item.save(ctx.as_mut()); + if item.entry_id(ctx.as_ref()).is_none() { + let start_path = first_worktree + .map_or(Path::new(""), |h| h.read(ctx).abs_path()) + .to_path_buf(); + ctx.prompt_for_new_path(&start_path, move |path, ctx| { + if let Some(path) = path { + handle.update(ctx, move |this, ctx| { + let file = this.file_for_path(&path, ctx); + let task = item.save(Some(file), ctx.as_mut()); + ctx.spawn(task, |_, result, _| { + if let Err(e) = result { + error!("failed to save item: {:?}, ", e); + } + }) + .detach() + }) + } + }); + return; + } + + let task = item.save(None, ctx.as_mut()); ctx.spawn(task, |_, result, _| { if let Err(e) = result { - // TODO - present this error to the user error!("failed to save item: {:?}, ", e); } }) diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index ea023fb813d49f2260abbea835aa97b07a33470e..b7fbdb3544ddbdf49d8a8c00f65d4dca3a4eb8df 100644 --- a/zed/src/worktree.rs +++ b/zed/src/worktree.rs @@ -1126,31 +1126,38 @@ struct UpdateIgnoreStatusJob { } pub trait WorktreeHandle { - fn file(&self, path: impl AsRef, app: &AppContext) -> Option; + fn file(&self, path: impl AsRef, app: &AppContext) -> FileHandle; } impl WorktreeHandle for ModelHandle { - fn file(&self, path: impl AsRef, app: &AppContext) -> Option { + fn file(&self, path: impl AsRef, app: &AppContext) -> FileHandle { + let path = path.as_ref(); let tree = self.read(app); - let entry = tree.entry_for_path(&path)?; - - let path = entry.path().clone(); let mut handles = tree.handles.lock(); - let state = if let Some(state) = handles.get(&path).and_then(Weak::upgrade) { + let state = if let Some(state) = handles.get(path).and_then(Weak::upgrade) { state } else { - let state = Arc::new(Mutex::new(FileHandleState { - path: path.clone(), - is_deleted: false, - })); - handles.insert(path, Arc::downgrade(&state)); + let handle_state = if let Some(entry) = tree.entry_for_path(path) { + FileHandleState { + path: entry.path().clone(), + is_deleted: false, + } + } else { + FileHandleState { + path: path.into(), + is_deleted: true, + } + }; + + let state = Arc::new(Mutex::new(handle_state.clone())); + handles.insert(handle_state.path, Arc::downgrade(&state)); state }; - Some(FileHandle { + FileHandle { worktree: self.clone(), state, - }) + } } } @@ -1389,10 +1396,10 @@ mod tests { let (file2, file3, file4, file5) = app.read(|ctx| { ( - tree.file("a/file2", ctx).unwrap(), - tree.file("a/file3", ctx).unwrap(), - tree.file("b/c/file4", ctx).unwrap(), - tree.file("b/c/file5", ctx).unwrap(), + tree.file("a/file2", ctx), + tree.file("a/file3", ctx), + tree.file("b/c/file4", ctx), + tree.file("b/c/file5", ctx), ) }); From 1fcbadaa990137b38b6e6bddcb352584ef7984ae Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 5 May 2021 11:04:39 -0700 Subject: [PATCH 02/10] Add TestAppContext::simulate_new_path_selection --- gpui/src/app.rs | 21 ++++++++++++++------- gpui/src/platform/test.rs | 33 +++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index 75d0ae01462be12ede526938f4126111c20ab076..798805f63ebb98923956d5ea73b2b558706e3545 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -87,7 +87,7 @@ pub enum MenuItem<'a> { pub struct App(Rc>); #[derive(Clone)] -pub struct TestAppContext(Rc>); +pub struct TestAppContext(Rc>, Rc); impl App { pub fn test T>( @@ -111,13 +111,16 @@ impl App { Fn: FnOnce(TestAppContext) -> F, F: Future, { - let platform = platform::test::platform(); + let platform = Rc::new(platform::test::platform()); let foreground = Rc::new(executor::Foreground::test()); - let ctx = TestAppContext(Rc::new(RefCell::new(MutableAppContext::new( - foreground.clone(), - Rc::new(platform), - asset_source, - )))); + let ctx = TestAppContext( + Rc::new(RefCell::new(MutableAppContext::new( + foreground.clone(), + platform.clone(), + asset_source, + ))), + platform, + ); ctx.0.borrow_mut().weak_self = Some(Rc::downgrade(&ctx.0)); let future = f(ctx); @@ -332,6 +335,10 @@ impl TestAppContext { pub fn platform(&self) -> Rc { self.0.borrow().platform.clone() } + + pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option) { + self.1.as_ref().simulate_new_path_selection(result); + } } impl UpdateModel for TestAppContext { diff --git a/gpui/src/platform/test.rs b/gpui/src/platform/test.rs index de385bbf844a0493579500a18aee8726bd34c608..6ee772c7e05467911dda82ef705ad512ada5b1da 100644 --- a/gpui/src/platform/test.rs +++ b/gpui/src/platform/test.rs @@ -1,11 +1,18 @@ use crate::ClipboardItem; use pathfinder_geometry::vector::Vector2F; -use std::{any::Any, cell::RefCell, path::Path, rc::Rc, sync::Arc}; - -struct Platform { +use std::{ + any::Any, + cell::RefCell, + path::{Path, PathBuf}, + rc::Rc, + sync::Arc, +}; + +pub(crate) struct Platform { dispatcher: Arc, fonts: Arc, current_clipboard_item: RefCell>, + last_prompt_for_new_path_args: RefCell)>)>>, } struct Dispatcher; @@ -23,9 +30,21 @@ impl Platform { Self { dispatcher: Arc::new(Dispatcher), fonts: Arc::new(super::current::FontSystem::new()), - current_clipboard_item: RefCell::new(None), + current_clipboard_item: Default::default(), + last_prompt_for_new_path_args: Default::default(), } } + + pub(crate) fn simulate_new_path_selection( + &self, + result: impl FnOnce(PathBuf) -> Option, + ) { + let (dir_path, callback) = self + .last_prompt_for_new_path_args + .take() + .expect("prompt_for_new_path was not called"); + callback(result(dir_path)); + } } impl super::Platform for Platform { @@ -77,7 +96,9 @@ impl super::Platform for Platform { ) { } - fn prompt_for_new_path(&self, _: &Path, _: Box)>) {} + fn prompt_for_new_path(&self, path: &Path, f: Box)>) { + *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), f)); + } fn write_to_clipboard(&self, item: ClipboardItem) { *self.current_clipboard_item.borrow_mut() = Some(item); @@ -134,6 +155,6 @@ impl super::Window for Window { } } -pub fn platform() -> impl super::Platform { +pub(crate) fn platform() -> Platform { Platform::new() } From 47d03498f482e9ef2fdb29f458730bfafed96cb7 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 5 May 2021 11:05:50 -0700 Subject: [PATCH 03/10] Wait to update bufferview's file handle until save has completed --- zed/src/editor/buffer/mod.rs | 3 +- zed/src/editor/buffer_view.rs | 21 +++--- zed/src/workspace.rs | 116 ++++++++++++++++++++++++---------- 3 files changed, 96 insertions(+), 44 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index 59b0bf9b36cee6bab3d03b00e3212d81b85fcd9e..fc0de35ca413609c509cbf94b3220b1af487805b 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -3123,8 +3123,7 @@ mod tests { let mut buffers = Vec::new(); let mut network = Network::new(); for i in 0..PEERS { - let buffer = - ctx.add_model(|_| Buffer::new(i as ReplicaId, base_text.as_str())); + let buffer = ctx.add_model(|_| Buffer::new(i as ReplicaId, base_text.as_str())); buffers.push(buffer); replica_ids.push(i as u16); network.add_peer(i as u16); diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 58e4428d6004e3d1b31d4151be646b8510fea077..27cb9b4be5fa505cc7d0bd94330b7149ecbc42dd 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -13,7 +13,7 @@ use gpui::{ use parking_lot::Mutex; use serde::{Deserialize, Serialize}; use smallvec::SmallVec; -use smol::Timer; +use smol::{future::FutureExt, Timer}; use std::{ cmp::{self, Ordering}, fmt::Write, @@ -2136,15 +2136,20 @@ impl workspace::ItemView for BufferView { fn save( &mut self, - file: Option, + new_file: Option, ctx: &mut ViewContext, ) -> LocalBoxFuture<'static, Result<()>> { - if file.is_some() { - self.file = file; - } - if let Some(file) = self.file.as_ref() { - self.buffer - .update(ctx, |buffer, ctx| buffer.save(file, ctx)) + if let Some(file) = new_file.as_ref().or(self.file.as_ref()) { + let save = self.buffer.update(ctx, |b, ctx| b.save(file, ctx)); + ctx.spawn(save, move |this, result, ctx| { + if new_file.is_some() && result.is_ok() { + this.file = new_file; + ctx.emit(Event::FileHandleChanged); + ctx.notify(); + } + result + }) + .boxed_local() } else { Box::pin(async { Ok(()) }) } diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 50864a874bca59cbc6d8b937e9d4ba787ff95275..5d570a845a9a6086395e722b607536ccc4960ab6 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -369,11 +369,11 @@ impl Workspace { pub fn open_new_file(&mut self, _: &(), ctx: &mut ViewContext) { let buffer = ctx.add_model(|_| Buffer::new(self.replica_id, "")); - let buffer_view = Box::new(ctx.add_view(|ctx| { + let buffer_view = ctx.add_view(|ctx| { BufferView::for_buffer(buffer.clone(), None, self.settings.clone(), ctx) - })); + }); self.untitled_buffers.insert(buffer); - self.add_item(buffer_view, ctx); + self.add_item(Box::new(buffer_view), ctx); } #[must_use] @@ -468,41 +468,45 @@ impl Workspace { )) } + pub fn active_item(&self, ctx: &ViewContext) -> Option> { + self.active_pane().read(ctx).active_item() + } + pub fn save_active_item(&mut self, _: &(), ctx: &mut ViewContext) { - let handle = ctx.handle(); - let first_worktree = self.worktrees.iter().next(); - self.active_pane.update(ctx, move |pane, ctx| { - if let Some(item) = pane.active_item() { - if item.entry_id(ctx.as_ref()).is_none() { - let start_path = first_worktree - .map_or(Path::new(""), |h| h.read(ctx).abs_path()) - .to_path_buf(); - ctx.prompt_for_new_path(&start_path, move |path, ctx| { - if let Some(path) = path { - handle.update(ctx, move |this, ctx| { - let file = this.file_for_path(&path, ctx); - let task = item.save(Some(file), ctx.as_mut()); - ctx.spawn(task, |_, result, _| { - if let Err(e) = result { - error!("failed to save item: {:?}, ", e); - } - }) - .detach() + if let Some(item) = self.active_item(ctx) { + if item.entry_id(ctx.as_ref()).is_none() { + let handle = ctx.handle(); + let start_path = self + .worktrees + .iter() + .next() + .map_or(Path::new(""), |h| h.read(ctx).abs_path()) + .to_path_buf(); + ctx.prompt_for_new_path(&start_path, move |path, ctx| { + if let Some(path) = path { + handle.update(ctx, move |this, ctx| { + let file = this.file_for_path(&path, ctx); + let task = item.save(Some(file), ctx.as_mut()); + ctx.spawn(task, |_, result, _| { + if let Err(e) = result { + error!("failed to save item: {:?}, ", e); + } }) - } - }); - return; - } - - let task = item.save(None, ctx.as_mut()); - ctx.spawn(task, |_, result, _| { - if let Err(e) = result { - error!("failed to save item: {:?}, ", e); + .detach() + }) } - }) - .detach() + }); + return; } - }); + + let task = item.save(None, ctx.as_mut()); + ctx.spawn(task, |_, result, _| { + if let Err(e) = result { + error!("failed to save item: {:?}, ", e); + } + }) + .detach() + } } pub fn debug_elements(&mut self, _: &(), ctx: &mut ViewContext) { @@ -660,6 +664,7 @@ mod tests { use gpui::App; use serde_json::json; use std::{collections::HashSet, os::unix}; + use tempdir::TempDir; #[test] fn test_open_paths_action() { @@ -942,6 +947,49 @@ mod tests { }); } + #[test] + fn test_open_and_save_new_file() { + App::test_async((), |mut app| async move { + let dir = TempDir::new("test-new-file").unwrap(); + let settings = settings::channel(&app.font_cache()).unwrap().1; + let (_, workspace) = app.add_window(|ctx| { + let mut workspace = Workspace::new(0, settings, ctx); + workspace.add_worktree(dir.path(), ctx); + workspace + }); + + // Create a new untitled buffer + let editor = workspace.update(&mut app, |workspace, ctx| { + workspace.open_new_file(&(), ctx); + workspace + .active_item(ctx) + .unwrap() + .to_any() + .downcast::() + .unwrap() + }); + editor.update(&mut app, |editor, ctx| { + assert_eq!(editor.title(ctx.as_ref()), "untitled"); + editor.insert(&"hi".to_string(), ctx) + }); + + // Save the buffer, selecting a filename + workspace.update(&mut app, |workspace, ctx| { + workspace.save_active_item(&(), ctx) + }); + app.simulate_new_path_selection(|parent_dir| { + assert_eq!(parent_dir, dir.path()); + Some(parent_dir.join("the-new-name")) + }); + app.read(|ctx| assert_eq!(editor.title(ctx), "untitled")); + + // When the save completes, the buffer's title is updated. + editor + .condition(&app, |editor, ctx| editor.title(ctx) == "the-new-name") + .await; + }); + } + #[test] fn test_pane_actions() { App::test_async((), |mut app| async move { From 78a5d0a37873ba2462a0c3c86d0a0d5d6a472ba5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 5 May 2021 13:12:51 -0700 Subject: [PATCH 04/10] Maintain workspace buffers state after saving untitled buffer --- zed/src/editor/buffer/mod.rs | 2 +- zed/src/editor/buffer_view.rs | 8 +++-- zed/src/workspace.rs | 67 +++++++++++++++++++++++++++++------ zed/src/worktree.rs | 20 +++++++---- 4 files changed, 77 insertions(+), 20 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index fc0de35ca413609c509cbf94b3220b1af487805b..b1cf5a09575bd64f20a04521e9c9d277f5effed2 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -436,7 +436,7 @@ impl Buffer { &mut self, file: &FileHandle, ctx: &mut ModelContext, - ) -> LocalBoxFuture<'static, Result<()>> { + ) -> LocalBoxFuture<'static, Result> { let snapshot = self.snapshot(); let version = self.version.clone(); let save_task = file.save(snapshot, ctx.as_ref()); diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 27cb9b4be5fa505cc7d0bd94330b7149ecbc42dd..15ce63120ef6832966d3bbbf15c77ff8286a1aee 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -2058,6 +2058,10 @@ impl BufferView { buffer::Event::FileHandleChanged => ctx.emit(Event::FileHandleChanged), } } + + pub fn file(&self) -> Option<&FileHandle> { + self.file.as_ref() + } } pub enum Event { @@ -2138,7 +2142,7 @@ impl workspace::ItemView for BufferView { &mut self, new_file: Option, ctx: &mut ViewContext, - ) -> LocalBoxFuture<'static, Result<()>> { + ) -> LocalBoxFuture<'static, Result> { if let Some(file) = new_file.as_ref().or(self.file.as_ref()) { let save = self.buffer.update(ctx, |b, ctx| b.save(file, ctx)); ctx.spawn(save, move |this, result, ctx| { @@ -2151,7 +2155,7 @@ impl workspace::ItemView for BufferView { }) .boxed_local() } else { - Box::pin(async { Ok(()) }) + Box::pin(async { Err(anyhow::anyhow!("can't save a buffer with no file")) }) } } diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 5d570a845a9a6086395e722b607536ccc4960ab6..24b6d8447b3be4593a9f2b688aeb60ffc51c4ec5 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -114,9 +114,7 @@ pub trait ItemView: View { &mut self, _: Option, _: &mut ViewContext, - ) -> LocalBoxFuture<'static, anyhow::Result<()>> { - Box::pin(async { Ok(()) }) - } + ) -> LocalBoxFuture<'static, anyhow::Result>; fn should_activate_item_on_event(_: &Self::Event) -> bool { false } @@ -138,7 +136,7 @@ pub trait ItemViewHandle: Send + Sync { &self, file: Option, ctx: &mut MutableAppContext, - ) -> LocalBoxFuture<'static, anyhow::Result<()>>; + ) -> LocalBoxFuture<'static, anyhow::Result>; } impl ItemViewHandle for ViewHandle { @@ -181,7 +179,7 @@ impl ItemViewHandle for ViewHandle { &self, file: Option, ctx: &mut MutableAppContext, - ) -> LocalBoxFuture<'static, anyhow::Result<()>> { + ) -> LocalBoxFuture<'static, anyhow::Result> { self.update(ctx, |item, ctx| item.save(file, ctx)) } @@ -223,7 +221,7 @@ pub struct Workspace { (usize, u64), postage::watch::Receiver, Arc>>>, >, - untitled_buffers: HashSet>, + untitled_buffers: HashMap>, } impl Workspace { @@ -372,7 +370,7 @@ impl Workspace { let buffer_view = ctx.add_view(|ctx| { BufferView::for_buffer(buffer.clone(), None, self.settings.clone(), ctx) }); - self.untitled_buffers.insert(buffer); + self.untitled_buffers.insert(buffer_view.id(), buffer); self.add_item(Box::new(buffer_view), ctx); } @@ -486,11 +484,20 @@ impl Workspace { if let Some(path) = path { handle.update(ctx, move |this, ctx| { let file = this.file_for_path(&path, ctx); + let worktree_id = file.worktree_id(); let task = item.save(Some(file), ctx.as_mut()); - ctx.spawn(task, |_, result, _| { - if let Err(e) = result { + let item_id = item.id(); + ctx.spawn(task, move |this, result, _| match result { + Err(e) => { error!("failed to save item: {:?}, ", e); } + Ok(inode) => { + if let Some(buffer) = this.untitled_buffers.remove(&item_id) { + let (_, rx) = + postage::watch::channel_with(Some(Ok(buffer))); + this.buffers.insert((worktree_id, inode), rx); + } + } }) .detach() }) @@ -984,9 +991,47 @@ mod tests { app.read(|ctx| assert_eq!(editor.title(ctx), "untitled")); // When the save completes, the buffer's title is updated. - editor - .condition(&app, |editor, ctx| editor.title(ctx) == "the-new-name") + let worktree = app.read(|ctx| { + workspace + .read(ctx) + .worktrees() + .iter() + .next() + .unwrap() + .clone() + }); + worktree + .condition(&app, |worktree, _| { + worktree.inode_for_path("the-new-name").is_some() + }) .await; + + // Open the same newly-created file in another pane item. + // The new editor should reuse the same buffer. + workspace + .update(&mut app, |workspace, ctx| { + workspace.open_new_file(&(), ctx); + workspace.split_pane( + workspace.active_pane().clone(), + SplitDirection::Right, + ctx, + ); + workspace + .open_entry((worktree.id(), Path::new("the-new-name").into()), ctx) + .unwrap() + }) + .await; + let editor2 = workspace.update(&mut app, |workspace, ctx| { + workspace + .active_item(ctx) + .unwrap() + .to_any() + .downcast::() + .unwrap() + }); + app.read(|ctx| { + assert_eq!(editor.read(ctx).buffer(), editor2.read(ctx).buffer()); + }) }); } diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index b7fbdb3544ddbdf49d8a8c00f65d4dca3a4eb8df..00900cdb9d35b0ba6c6f8ca1939868d8e2eb0b51 100644 --- a/zed/src/worktree.rs +++ b/zed/src/worktree.rs @@ -191,17 +191,18 @@ impl Worktree { path: &Path, content: BufferSnapshot, ctx: &AppContext, - ) -> Task> { + ) -> Task> { let abs_path = self.snapshot.abs_path.join(path); ctx.background_executor().spawn(async move { let buffer_size = content.text_summary().bytes.min(10 * 1024); let file = std::fs::File::create(&abs_path)?; + let metadata = file.metadata()?; let mut writer = std::io::BufWriter::with_capacity(buffer_size, file); for chunk in content.fragments() { writer.write(chunk.as_bytes())?; } writer.flush()?; - Ok(()) + Ok(metadata.ino()) }) } } @@ -406,15 +407,23 @@ impl FileHandle { self.state.lock().is_deleted } + pub fn exists(&self) -> bool { + !self.is_deleted() + } + pub fn load_history(&self, ctx: &AppContext) -> impl Future> { self.worktree.read(ctx).load_history(&self.path(), ctx) } - pub fn save<'a>(&self, content: BufferSnapshot, ctx: &AppContext) -> Task> { + pub fn save<'a>(&self, content: BufferSnapshot, ctx: &AppContext) -> Task> { let worktree = self.worktree.read(ctx); worktree.save(&self.path(), content, ctx) } + pub fn worktree_id(&self) -> usize { + self.worktree.id() + } + pub fn entry_id(&self) -> (usize, Arc) { (self.worktree.id(), self.path()) } @@ -971,9 +980,8 @@ impl BackgroundScanner { let snapshot = self.snapshot.lock(); handles.retain(|path, handle_state| { if let Some(handle_state) = Weak::upgrade(&handle_state) { - if snapshot.entry_for_path(&path).is_none() { - handle_state.lock().is_deleted = true; - } + let mut handle_state = handle_state.lock(); + handle_state.is_deleted = snapshot.entry_for_path(&path).is_none(); true } else { false From bf28aba1a79e48bbb8cf75173a65208deb20022c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 5 May 2021 17:23:44 -0700 Subject: [PATCH 05/10] Restore Buffer::file field and workspace::Item trait --- zed/src/editor/buffer/mod.rs | 121 +++++++----- zed/src/editor/buffer_view.rs | 146 +++++++-------- zed/src/editor/display_map/fold_map.rs | 14 +- zed/src/editor/display_map/mod.rs | 4 +- zed/src/workspace.rs | 243 +++++++++++++++---------- zed/src/worktree.rs | 22 +-- 6 files changed, 319 insertions(+), 231 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index b1cf5a09575bd64f20a04521e9c9d277f5effed2..6d87dd588019ca5db889a6cff883830216f9db8d 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -8,6 +8,7 @@ use futures_core::future::LocalBoxFuture; pub use point::*; use seahash::SeaHasher; pub use selection::*; +use smol::future::FutureExt; pub use text::*; use crate::{ @@ -64,6 +65,7 @@ pub struct Buffer { last_edit: time::Local, undo_map: UndoMap, history: History, + file: Option, selections: HashMap>, pub selections_last_update: SelectionsVersion, deferred_ops: OperationQueue, @@ -351,15 +353,33 @@ pub struct UndoOperation { } impl Buffer { - pub fn new>>(replica_id: ReplicaId, base_text: T) -> Self { - Self::build(replica_id, History::new(base_text.into())) + pub fn new>>( + replica_id: ReplicaId, + base_text: T, + ctx: &mut ModelContext, + ) -> Self { + Self::build(replica_id, History::new(base_text.into()), None, ctx) } - pub fn from_history(replica_id: ReplicaId, history: History) -> Self { - Self::build(replica_id, history) + pub fn from_history( + replica_id: ReplicaId, + history: History, + file: Option, + ctx: &mut ModelContext, + ) -> Self { + Self::build(replica_id, history, file, ctx) } - fn build(replica_id: ReplicaId, history: History) -> Self { + fn build( + replica_id: ReplicaId, + history: History, + file: Option, + ctx: &mut ModelContext, + ) -> Self { + if let Some(file) = file.as_ref() { + file.observe_from_model(ctx, |_, _, ctx| ctx.emit(Event::FileHandleChanged)); + } + let mut insertion_splits = HashMap::default(); let mut fragments = SumTree::new(); @@ -416,6 +436,7 @@ impl Buffer { last_edit: time::Local::default(), undo_map: Default::default(), history, + file, selections: HashMap::default(), selections_last_update: 0, deferred_ops: OperationQueue::new(), @@ -432,24 +453,38 @@ impl Buffer { } } + pub fn file(&self) -> Option<&FileHandle> { + self.file.as_ref() + } + pub fn save( &mut self, - file: &FileHandle, + new_file: Option, ctx: &mut ModelContext, - ) -> LocalBoxFuture<'static, Result> { + ) -> LocalBoxFuture<'static, Result<()>> { let snapshot = self.snapshot(); let version = self.version.clone(); - let save_task = file.save(snapshot, ctx.as_ref()); - let task = ctx.spawn(save_task, |me, save_result, ctx| { - if save_result.is_ok() { - me.did_save(version, ctx); - } - save_result - }); - Box::pin(task) + if let Some(file) = new_file.as_ref().or(self.file.as_ref()) { + let save_task = file.save(snapshot, ctx.as_ref()); + ctx.spawn(save_task, |me, save_result, ctx| { + if save_result.is_ok() { + me.did_save(version, new_file, ctx); + } + save_result + }) + .boxed_local() + } else { + async { Ok(()) }.boxed_local() + } } - fn did_save(&mut self, version: time::Global, ctx: &mut ModelContext) { + fn did_save( + &mut self, + version: time::Global, + file: Option, + ctx: &mut ModelContext, + ) { + self.file = file; self.saved_version = version; ctx.emit(Event::Saved); } @@ -1737,6 +1772,7 @@ impl Clone for Buffer { selections: self.selections.clone(), selections_last_update: self.selections_last_update.clone(), deferred_ops: self.deferred_ops.clone(), + file: self.file.clone(), deferred_replicas: self.deferred_replicas.clone(), replica_id: self.replica_id, local_clock: self.local_clock.clone(), @@ -2296,8 +2332,8 @@ mod tests { #[test] fn test_edit() { App::test((), |ctx| { - ctx.add_model(|_| { - let mut buffer = Buffer::new(0, "abc"); + ctx.add_model(|ctx| { + let mut buffer = Buffer::new(0, "abc", ctx); assert_eq!(buffer.text(), "abc"); buffer.edit(vec![3..3], "def", None).unwrap(); assert_eq!(buffer.text(), "abcdef"); @@ -2321,8 +2357,8 @@ mod tests { let buffer_1_events = Rc::new(RefCell::new(Vec::new())); let buffer_2_events = Rc::new(RefCell::new(Vec::new())); - let buffer1 = app.add_model(|_| Buffer::new(0, "abcdef")); - let buffer2 = app.add_model(|_| Buffer::new(1, "abcdef")); + let buffer1 = app.add_model(|ctx| Buffer::new(0, "abcdef", ctx)); + let buffer2 = app.add_model(|ctx| Buffer::new(1, "abcdef", ctx)); let mut buffer_ops = Vec::new(); buffer1.update(app, |buffer, ctx| { let buffer_1_events = buffer_1_events.clone(); @@ -2408,8 +2444,8 @@ mod tests { let mut reference_string = RandomCharIter::new(&mut rng) .take(reference_string_len) .collect::(); - ctx.add_model(|_| { - let mut buffer = Buffer::new(0, reference_string.as_str()); + ctx.add_model(|ctx| { + let mut buffer = Buffer::new(0, reference_string.as_str(), ctx); let mut buffer_versions = Vec::new(); for _i in 0..10 { let (old_ranges, new_text, _) = buffer.randomly_mutate(rng, None); @@ -2494,8 +2530,8 @@ mod tests { #[test] fn test_line_len() { App::test((), |ctx| { - ctx.add_model(|_| { - let mut buffer = Buffer::new(0, ""); + ctx.add_model(|ctx| { + let mut buffer = Buffer::new(0, "", ctx); buffer.edit(vec![0..0], "abcd\nefg\nhij", None).unwrap(); buffer.edit(vec![12..12], "kl\nmno", None).unwrap(); buffer.edit(vec![18..18], "\npqrs\n", None).unwrap(); @@ -2516,8 +2552,8 @@ mod tests { #[test] fn test_rightmost_point() { App::test((), |ctx| { - ctx.add_model(|_| { - let mut buffer = Buffer::new(0, ""); + ctx.add_model(|ctx| { + let mut buffer = Buffer::new(0, "", ctx); assert_eq!(buffer.rightmost_point().row, 0); buffer.edit(vec![0..0], "abcd\nefg\nhij", None).unwrap(); assert_eq!(buffer.rightmost_point().row, 0); @@ -2537,8 +2573,8 @@ mod tests { #[test] fn test_text_summary_for_range() { App::test((), |ctx| { - ctx.add_model(|_| { - let buffer = Buffer::new(0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz"); + ctx.add_model(|ctx| { + let buffer = Buffer::new(0, "ab\nefg\nhklm\nnopqrs\ntuvwxyz", ctx); let text = Text::from(buffer.text()); assert_eq!( buffer.text_summary_for_range(1..3), @@ -2568,8 +2604,8 @@ mod tests { #[test] fn test_chars_at() { App::test((), |ctx| { - ctx.add_model(|_| { - let mut buffer = Buffer::new(0, ""); + ctx.add_model(|ctx| { + let mut buffer = Buffer::new(0, "", ctx); buffer.edit(vec![0..0], "abcd\nefgh\nij", None).unwrap(); buffer.edit(vec![12..12], "kl\nmno", None).unwrap(); buffer.edit(vec![18..18], "\npqrs", None).unwrap(); @@ -2591,7 +2627,7 @@ mod tests { assert_eq!(chars.collect::(), "PQrs"); // Regression test: - let mut buffer = Buffer::new(0, ""); + let mut buffer = Buffer::new(0, "", ctx); buffer.edit(vec![0..0], "[workspace]\nmembers = [\n \"xray_core\",\n \"xray_server\",\n \"xray_cli\",\n \"xray_wasm\",\n]\n", None).unwrap(); buffer.edit(vec![60..60], "\n", None).unwrap(); @@ -2720,8 +2756,8 @@ mod tests { #[test] fn test_anchors() { App::test((), |ctx| { - ctx.add_model(|_| { - let mut buffer = Buffer::new(0, ""); + ctx.add_model(|ctx| { + let mut buffer = Buffer::new(0, "", ctx); buffer.edit(vec![0..0], "abc", None).unwrap(); let left_anchor = buffer.anchor_before(2).unwrap(); let right_anchor = buffer.anchor_after(2).unwrap(); @@ -2885,8 +2921,8 @@ mod tests { #[test] fn test_anchors_at_start_and_end() { App::test((), |ctx| { - ctx.add_model(|_| { - let mut buffer = Buffer::new(0, ""); + ctx.add_model(|ctx| { + let mut buffer = Buffer::new(0, "", ctx); let before_start_anchor = buffer.anchor_before(0).unwrap(); let after_end_anchor = buffer.anchor_after(0).unwrap(); @@ -2913,7 +2949,7 @@ mod tests { #[test] fn test_is_modified() { App::test((), |app| { - let model = app.add_model(|_| Buffer::new(0, "abc")); + let model = app.add_model(|ctx| Buffer::new(0, "abc", ctx)); let events = Rc::new(RefCell::new(Vec::new())); // initially, the buffer isn't dirty. @@ -2945,7 +2981,7 @@ mod tests { ); events.borrow_mut().clear(); - buffer.did_save(buffer.version(), ctx); + buffer.did_save(buffer.version(), None, ctx); }); // after saving, the buffer is not dirty, and emits a saved event. @@ -3000,8 +3036,8 @@ mod tests { #[test] fn test_undo_redo() { App::test((), |app| { - app.add_model(|_| { - let mut buffer = Buffer::new(0, "1234"); + app.add_model(|ctx| { + let mut buffer = Buffer::new(0, "1234", ctx); let edit1 = buffer.edit(vec![1..1], "abx", None).unwrap(); let edit2 = buffer.edit(vec![3..4], "yzef", None).unwrap(); @@ -3037,9 +3073,9 @@ mod tests { #[test] fn test_history() { App::test((), |app| { - app.add_model(|_| { + app.add_model(|ctx| { let mut now = Instant::now(); - let mut buffer = Buffer::new(0, "123456"); + let mut buffer = Buffer::new(0, "123456", ctx); let (set_id, _) = buffer .add_selection_set(buffer.selections_from_ranges(vec![4..4]).unwrap(), None); @@ -3123,7 +3159,8 @@ mod tests { let mut buffers = Vec::new(); let mut network = Network::new(); for i in 0..PEERS { - let buffer = ctx.add_model(|_| Buffer::new(i as ReplicaId, base_text.as_str())); + let buffer = + ctx.add_model(|ctx| Buffer::new(i as ReplicaId, base_text.as_str(), ctx)); buffers.push(buffer); replica_ids.push(i as u16); network.add_peer(i as u16); diff --git a/zed/src/editor/buffer_view.rs b/zed/src/editor/buffer_view.rs index 15ce63120ef6832966d3bbbf15c77ff8286a1aee..4fc58fb67324abf9d763efcc7709b6c0360f919a 100644 --- a/zed/src/editor/buffer_view.rs +++ b/zed/src/editor/buffer_view.rs @@ -13,7 +13,7 @@ use gpui::{ use parking_lot::Mutex; use serde::{Deserialize, Serialize}; use smallvec::SmallVec; -use smol::{future::FutureExt, Timer}; +use smol::Timer; use std::{ cmp::{self, Ordering}, fmt::Write, @@ -253,7 +253,6 @@ pub enum SelectAction { pub struct BufferView { handle: WeakViewHandle, buffer: ModelHandle, - file: Option, display_map: ModelHandle, selection_set_id: SelectionSetId, pending_selection: Option, @@ -275,24 +274,19 @@ struct ClipboardSelection { impl BufferView { pub fn single_line(settings: watch::Receiver, ctx: &mut ViewContext) -> Self { - let buffer = ctx.add_model(|_| Buffer::new(0, String::new())); - let mut view = Self::for_buffer(buffer, None, settings, ctx); + let buffer = ctx.add_model(|ctx| Buffer::new(0, String::new(), ctx)); + let mut view = Self::for_buffer(buffer, settings, ctx); view.single_line = true; view } pub fn for_buffer( buffer: ModelHandle, - file: Option, settings: watch::Receiver, ctx: &mut ViewContext, ) -> Self { settings.notify_view_on_change(ctx); - if let Some(file) = file.as_ref() { - file.observe_from_view(ctx, |_, _, ctx| ctx.emit(Event::FileHandleChanged)); - } - ctx.observe_model(&buffer, Self::on_buffer_changed); ctx.subscribe_to_model(&buffer, Self::on_buffer_event); let display_map = ctx.add_model(|ctx| { @@ -318,7 +312,6 @@ impl BufferView { Self { handle: ctx.handle().downgrade(), buffer, - file, display_map, selection_set_id, pending_selection: None, @@ -2058,10 +2051,6 @@ impl BufferView { buffer::Event::FileHandleChanged => ctx.emit(Event::FileHandleChanged), } } - - pub fn file(&self) -> Option<&FileHandle> { - self.file.as_ref() - } } pub enum Event { @@ -2099,6 +2088,22 @@ impl View for BufferView { } } +impl workspace::Item for Buffer { + type View = BufferView; + + fn file(&self) -> Option<&FileHandle> { + self.file() + } + + fn build_view( + handle: ModelHandle, + settings: watch::Receiver, + ctx: &mut ViewContext, + ) -> Self::View { + BufferView::for_buffer(handle, settings, ctx) + } +} + impl workspace::ItemView for BufferView { fn should_activate_item_on_event(event: &Self::Event) -> bool { matches!(event, Event::Activate) @@ -2112,7 +2117,11 @@ impl workspace::ItemView for BufferView { } fn title(&self, app: &AppContext) -> std::string::String { - let filename = self.file.as_ref().and_then(|file| file.file_name(app)); + let filename = self + .buffer + .read(app) + .file() + .and_then(|file| file.file_name(app)); if let Some(name) = filename { name.to_string_lossy().into() } else { @@ -2120,20 +2129,15 @@ impl workspace::ItemView for BufferView { } } - fn entry_id(&self, _: &AppContext) -> Option<(usize, Arc)> { - self.file.as_ref().map(|file| file.entry_id()) + fn entry_id(&self, ctx: &AppContext) -> Option<(usize, Arc)> { + self.buffer.read(ctx).file().map(|file| file.entry_id()) } fn clone_on_split(&self, ctx: &mut ViewContext) -> Option where Self: Sized, { - let clone = BufferView::for_buffer( - self.buffer.clone(), - self.file.clone(), - self.settings.clone(), - ctx, - ); + let clone = BufferView::for_buffer(self.buffer.clone(), self.settings.clone(), ctx); *clone.scroll_position.lock() = *self.scroll_position.lock(); Some(clone) } @@ -2142,21 +2146,8 @@ impl workspace::ItemView for BufferView { &mut self, new_file: Option, ctx: &mut ViewContext, - ) -> LocalBoxFuture<'static, Result> { - if let Some(file) = new_file.as_ref().or(self.file.as_ref()) { - let save = self.buffer.update(ctx, |b, ctx| b.save(file, ctx)); - ctx.spawn(save, move |this, result, ctx| { - if new_file.is_some() && result.is_ok() { - this.file = new_file; - ctx.emit(Event::FileHandleChanged); - ctx.notify(); - } - result - }) - .boxed_local() - } else { - Box::pin(async { Err(anyhow::anyhow!("can't save a buffer with no file")) }) - } + ) -> LocalBoxFuture<'static, Result<()>> { + self.buffer.update(ctx, |b, ctx| b.save(new_file, ctx)) } fn is_dirty(&self, ctx: &AppContext) -> bool { @@ -2174,10 +2165,11 @@ mod tests { #[test] fn test_selection_with_mouse() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, "aaaaaa\nbbbbbb\ncccccc\ndddddd\n")); + let buffer = + app.add_model(|ctx| Buffer::new(0, "aaaaaa\nbbbbbb\ncccccc\ndddddd\n", ctx)); let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, buffer_view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); buffer_view.update(app, |view, ctx| { view.begin_selection(DisplayPoint::new(2, 2), false, ctx); @@ -2288,11 +2280,11 @@ mod tests { let layout_cache = TextLayoutCache::new(app.platform().fonts()); let font_cache = app.font_cache().clone(); - let buffer = app.add_model(|_| Buffer::new(0, sample_text(6, 6))); + let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(6, 6), ctx)); let settings = settings::channel(&font_cache).unwrap().1; let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx)); + app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); let layouts = view .read(app) @@ -2305,7 +2297,7 @@ mod tests { #[test] fn test_fold() { App::test((), |app| { - let buffer = app.add_model(|_| { + let buffer = app.add_model(|ctx| { Buffer::new( 0, " @@ -2326,11 +2318,12 @@ mod tests { } " .unindent(), + ctx, ) }); let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx)); + app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( @@ -2399,10 +2392,10 @@ mod tests { #[test] fn test_move_cursor() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, sample_text(6, 6))); + let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(6, 6), ctx)); let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx)); + app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); buffer.update(app, |buffer, ctx| { buffer @@ -2477,10 +2470,9 @@ mod tests { #[test] fn test_beginning_end_of_line() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, "abc\n def")); + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\n def", ctx)); let settings = settings::channel(&app.font_cache()).unwrap().1; - let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( &[ @@ -2606,11 +2598,10 @@ mod tests { #[test] fn test_prev_next_word_boundary() { App::test((), |app| { - let buffer = - app.add_model(|_| Buffer::new(0, "use std::str::{foo, bar}\n\n {baz.qux()}")); + let buffer = app + .add_model(|ctx| Buffer::new(0, "use std::str::{foo, bar}\n\n {baz.qux()}", ctx)); let settings = settings::channel(&app.font_cache()).unwrap().1; - let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( &[ @@ -2789,12 +2780,16 @@ mod tests { #[test] fn test_backspace() { App::test((), |app| { - let buffer = app.add_model(|_| { - Buffer::new(0, "one two three\nfour five six\nseven eight nine\nten\n") + let buffer = app.add_model(|ctx| { + Buffer::new( + 0, + "one two three\nfour five six\nseven eight nine\nten\n", + ctx, + ) }); let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx)); + app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( @@ -2822,12 +2817,16 @@ mod tests { #[test] fn test_delete() { App::test((), |app| { - let buffer = app.add_model(|_| { - Buffer::new(0, "one two three\nfour five six\nseven eight nine\nten\n") + let buffer = app.add_model(|ctx| { + Buffer::new( + 0, + "one two three\nfour five six\nseven eight nine\nten\n", + ctx, + ) }); let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx)); + app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( @@ -2856,9 +2855,8 @@ mod tests { fn test_delete_line() { App::test((), |app| { let settings = settings::channel(&app.font_cache()).unwrap().1; - let buffer = app.add_model(|_| Buffer::new(0, "abc\ndef\nghi\n")); - let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( &[ @@ -2881,9 +2879,8 @@ mod tests { ); let settings = settings::channel(&app.font_cache()).unwrap().1; - let buffer = app.add_model(|_| Buffer::new(0, "abc\ndef\nghi\n")); - let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( &[DisplayPoint::new(2, 0)..DisplayPoint::new(0, 1)], @@ -2904,9 +2901,8 @@ mod tests { fn test_duplicate_line() { App::test((), |app| { let settings = settings::channel(&app.font_cache()).unwrap().1; - let buffer = app.add_model(|_| Buffer::new(0, "abc\ndef\nghi\n")); - let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( &[ @@ -2935,9 +2931,8 @@ mod tests { ); let settings = settings::channel(&app.font_cache()).unwrap().1; - let buffer = app.add_model(|_| Buffer::new(0, "abc\ndef\nghi\n")); - let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\ndef\nghi\n", ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); view.update(app, |view, ctx| { view.select_display_ranges( &[ @@ -2966,10 +2961,10 @@ mod tests { #[test] fn test_clipboard() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, "one two three four five six ")); + let buffer = app.add_model(|ctx| Buffer::new(0, "one two three four five six ", ctx)); let settings = settings::channel(&app.font_cache()).unwrap().1; let view = app - .add_window(|ctx| BufferView::for_buffer(buffer.clone(), None, settings, ctx)) + .add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx)) .1; // Cut with three selections. Clipboard text is divided into three slices. @@ -3107,10 +3102,9 @@ mod tests { #[test] fn test_select_all() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, "abc\nde\nfgh")); + let buffer = app.add_model(|ctx| Buffer::new(0, "abc\nde\nfgh", ctx)); let settings = settings::channel(&app.font_cache()).unwrap().1; - let (_, view) = - app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx)); + let (_, view) = app.add_window(|ctx| BufferView::for_buffer(buffer, settings, ctx)); view.update(app, |b, ctx| b.select_all(&(), ctx)); assert_eq!( view.read(app).selection_ranges(app.as_ref()), diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 8a105f8af39d76ccebce43558ab9273ba50ae906..98c47ff08a84ea1a79ded4a0edf03da7ad0a681a 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -505,7 +505,7 @@ mod tests { #[test] fn test_basic_folds() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6))); + let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6), ctx)); let mut map = FoldMap::new(buffer.clone(), app.as_ref()); map.fold( @@ -556,7 +556,7 @@ mod tests { #[test] fn test_adjacent_folds() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, "abcdefghijkl")); + let buffer = app.add_model(|ctx| Buffer::new(0, "abcdefghijkl", ctx)); { let mut map = FoldMap::new(buffer.clone(), app.as_ref()); @@ -600,7 +600,7 @@ mod tests { #[test] fn test_overlapping_folds() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6))); + let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6), ctx)); let mut map = FoldMap::new(buffer.clone(), app.as_ref()); map.fold( vec![ @@ -619,7 +619,7 @@ mod tests { #[test] fn test_merging_folds_via_edit() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6))); + let buffer = app.add_model(|ctx| Buffer::new(0, sample_text(5, 6), ctx)); let mut map = FoldMap::new(buffer.clone(), app.as_ref()); map.fold( @@ -670,10 +670,10 @@ mod tests { let mut rng = StdRng::seed_from_u64(seed); App::test((), |app| { - let buffer = app.add_model(|_| { + let buffer = app.add_model(|ctx| { let len = rng.gen_range(0..10); let text = RandomCharIter::new(&mut rng).take(len).collect::(); - Buffer::new(0, text) + Buffer::new(0, text, ctx) }); let mut map = FoldMap::new(buffer.clone(), app.as_ref()); @@ -749,7 +749,7 @@ mod tests { fn test_buffer_rows() { App::test((), |app| { let text = sample_text(6, 6) + "\n"; - let buffer = app.add_model(|_| Buffer::new(0, text)); + let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx)); let mut map = FoldMap::new(buffer.clone(), app.as_ref()); diff --git a/zed/src/editor/display_map/mod.rs b/zed/src/editor/display_map/mod.rs index e2c422ad36169ebedc44ef7367003ca1f1af3cdb..b69a70f3979870eb061f72c0cfdff25931165ccf 100644 --- a/zed/src/editor/display_map/mod.rs +++ b/zed/src/editor/display_map/mod.rs @@ -324,7 +324,7 @@ mod tests { fn test_chars_at() { App::test((), |app| { let text = sample_text(6, 6); - let buffer = app.add_model(|_| Buffer::new(0, text)); + let buffer = app.add_model(|ctx| Buffer::new(0, text, ctx)); let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx)); buffer .update(app, |buffer, ctx| { @@ -391,7 +391,7 @@ mod tests { #[test] fn test_max_point() { App::test((), |app| { - let buffer = app.add_model(|_| Buffer::new(0, "aaa\n\t\tbbb")); + let buffer = app.add_model(|ctx| Buffer::new(0, "aaa\n\t\tbbb", ctx)); let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx)); assert_eq!( map.read(app).max_point(app.as_ref()), diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 24b6d8447b3be4593a9f2b688aeb60ffc51c4ec5..1d1cfaf0476908b05fd66de0bf325f674f141b40 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -8,8 +8,8 @@ use crate::{ watch::{self, Receiver}, worktree::FileHandle, }; -use gpui::{MutableAppContext, PathPromptOptions}; -use std::path::PathBuf; +use std::{collections::HashMap, fmt, path::PathBuf}; + pub fn init(app: &mut MutableAppContext) { app.add_global_action("workspace:open", open); app.add_global_action("workspace:open_paths", open_paths); @@ -31,12 +31,13 @@ use crate::{ use futures_core::{future::LocalBoxFuture, Future}; use gpui::{ color::rgbu, elements::*, json::to_string_pretty, keymap::Binding, AnyViewHandle, AppContext, - ClipboardItem, Entity, EntityTask, ModelHandle, View, ViewContext, ViewHandle, + ClipboardItem, Entity, EntityTask, ModelHandle, MutableAppContext, PathPromptOptions, View, + ViewContext, ViewHandle, }; use log::error; use smol::prelude::*; use std::{ - collections::{hash_map::Entry, HashMap, HashSet}, + collections::{hash_map::Entry, HashSet}, path::Path, sync::Arc, }; @@ -98,6 +99,18 @@ fn quit(_: &(), app: &mut MutableAppContext) { app.platform().quit(); } +pub trait Item: Entity + Sized { + type View: ItemView; + + fn build_view( + handle: ModelHandle, + settings: watch::Receiver, + ctx: &mut ViewContext, + ) -> Self::View; + + fn file(&self) -> Option<&FileHandle>; +} + pub trait ItemView: View { fn title(&self, app: &AppContext) -> String; fn entry_id(&self, app: &AppContext) -> Option<(usize, Arc)>; @@ -114,7 +127,7 @@ pub trait ItemView: View { &mut self, _: Option, _: &mut ViewContext, - ) -> LocalBoxFuture<'static, anyhow::Result>; + ) -> LocalBoxFuture<'static, anyhow::Result<()>>; fn should_activate_item_on_event(_: &Self::Event) -> bool { false } @@ -123,6 +136,18 @@ pub trait ItemView: View { } } +pub trait ItemHandle: Send + Sync { + fn id(&self) -> usize; + fn file<'a>(&'a self, ctx: &'a AppContext) -> Option<&'a FileHandle>; + fn add_view( + &self, + window_id: usize, + settings: watch::Receiver, + app: &mut MutableAppContext, + ) -> Box; + fn boxed_clone(&self) -> Box; +} + pub trait ItemViewHandle: Send + Sync { fn title(&self, app: &AppContext) -> String; fn entry_id(&self, app: &AppContext) -> Option<(usize, Arc)>; @@ -136,7 +161,30 @@ pub trait ItemViewHandle: Send + Sync { &self, file: Option, ctx: &mut MutableAppContext, - ) -> LocalBoxFuture<'static, anyhow::Result>; + ) -> LocalBoxFuture<'static, anyhow::Result<()>>; +} + +impl ItemHandle for ModelHandle { + fn id(&self) -> usize { + self.id() + } + + fn file<'a>(&'a self, ctx: &'a AppContext) -> Option<&'a FileHandle> { + self.read(ctx).file() + } + + fn add_view( + &self, + window_id: usize, + settings: watch::Receiver, + app: &mut MutableAppContext, + ) -> Box { + Box::new(app.add_view(window_id, |ctx| T::build_view(self.clone(), settings, ctx))) + } + + fn boxed_clone(&self) -> Box { + Box::new(self.clone()) + } } impl ItemViewHandle for ViewHandle { @@ -179,7 +227,7 @@ impl ItemViewHandle for ViewHandle { &self, file: Option, ctx: &mut MutableAppContext, - ) -> LocalBoxFuture<'static, anyhow::Result> { + ) -> LocalBoxFuture<'static, anyhow::Result<()>> { self.update(ctx, |item, ctx| item.save(file, ctx)) } @@ -202,6 +250,18 @@ impl Clone for Box { } } +impl Clone for Box { + fn clone(&self) -> Box { + self.boxed_clone() + } +} + +impl fmt::Debug for Box { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ItemHandle {{id: {}}}", self.id()) + } +} + #[derive(Debug)] pub struct State { pub modal: Option, @@ -214,14 +274,13 @@ pub struct Workspace { center: PaneGroup, panes: Vec>, active_pane: ViewHandle, - loading_entries: HashSet<(usize, Arc)>, replica_id: ReplicaId, worktrees: HashSet>, - buffers: HashMap< - (usize, u64), - postage::watch::Receiver, Arc>>>, + items: Vec>, + loading_items: HashMap< + (usize, Arc), + postage::watch::Receiver, Arc>>>, >, - untitled_buffers: HashMap>, } impl Workspace { @@ -242,12 +301,11 @@ impl Workspace { center: PaneGroup::new(pane.id()), panes: vec![pane.clone()], active_pane: pane.clone(), - loading_entries: HashSet::new(), settings, replica_id, worktrees: Default::default(), - buffers: Default::default(), - untitled_buffers: Default::default(), + items: Default::default(), + loading_items: Default::default(), } } @@ -366,11 +424,10 @@ impl Workspace { } pub fn open_new_file(&mut self, _: &(), ctx: &mut ViewContext) { - let buffer = ctx.add_model(|_| Buffer::new(self.replica_id, "")); - let buffer_view = ctx.add_view(|ctx| { - BufferView::for_buffer(buffer.clone(), None, self.settings.clone(), ctx) - }); - self.untitled_buffers.insert(buffer_view.id(), buffer); + let buffer = ctx.add_model(|ctx| Buffer::new(self.replica_id, "", ctx)); + let buffer_view = + ctx.add_view(|ctx| BufferView::for_buffer(buffer.clone(), self.settings.clone(), ctx)); + self.items.push(Box::new(buffer)); self.add_item(Box::new(buffer_view), ctx); } @@ -380,10 +437,8 @@ impl Workspace { entry: (usize, Arc), ctx: &mut ViewContext, ) -> Option> { - if self.loading_entries.contains(&entry) { - return None; - } - + // If the active pane contains a view for this file, then activate + // that item view. if self .active_pane() .update(ctx, |pane, ctx| pane.activate_entry(entry.clone(), ctx)) @@ -391,6 +446,19 @@ impl Workspace { return None; } + let window_id = ctx.window_id(); + let settings = self.settings.clone(); + + // Otherwise, if this file is already open somewhere in the workspace, + // then add another view for it. + if let Some(item) = self.items.iter().find(|item| { + item.file(ctx.as_ref()) + .map_or(false, |f| f.entry_id() == entry) + }) { + self.add_item(item.add_view(window_id, settings, ctx.as_mut()), ctx); + return None; + } + let (worktree_id, path) = entry.clone(); let worktree = match self.worktrees.get(&worktree_id).cloned() { @@ -401,40 +469,31 @@ impl Workspace { } }; - let inode = match worktree.read(ctx).inode_for_path(&path) { - Some(inode) => inode, - None => { - log::error!("path {:?} does not exist", path); - return None; - } - }; - let file = worktree.file(path.clone(), ctx.as_ref()); if file.is_deleted() { log::error!("path {:?} does not exist", path); return None; } - self.loading_entries.insert(entry.clone()); - - if let Entry::Vacant(entry) = self.buffers.entry((worktree_id, inode)) { + if let Entry::Vacant(entry) = self.loading_items.entry(entry.clone()) { let (mut tx, rx) = postage::watch::channel(); entry.insert(rx); - let history = file.load_history(ctx.as_ref()); let replica_id = self.replica_id; - let buffer = ctx + let history = ctx .background_executor() - .spawn(async move { Ok(Buffer::from_history(replica_id, history.await?)) }); - ctx.spawn(buffer, move |_, from_history_result, ctx| { - *tx.borrow_mut() = Some(match from_history_result { - Ok(buffer) => Ok(ctx.add_model(|_| buffer)), + .spawn(file.load_history(ctx.as_ref())); + ctx.spawn(history, move |_, history, ctx| { + *tx.borrow_mut() = Some(match history { + Ok(history) => Ok(Box::new(ctx.add_model(|ctx| { + Buffer::from_history(replica_id, history, Some(file), ctx) + }))), Err(error) => Err(Arc::new(error)), }) }) .detach() } - let mut watch = self.buffers.get(&(worktree_id, inode)).unwrap().clone(); + let mut watch = self.loading_items.get(&entry).unwrap().clone(); Some(ctx.spawn( async move { loop { @@ -445,18 +504,12 @@ impl Workspace { } }, move |me, load_result, ctx| { - me.loading_entries.remove(&entry); + me.loading_items.remove(&entry); match load_result { - Ok(buffer_handle) => { - let buffer_view = Box::new(ctx.add_view(|ctx| { - BufferView::for_buffer( - buffer_handle, - Some(file), - me.settings.clone(), - ctx, - ) - })); - me.add_item(buffer_view, ctx); + Ok(item) => { + me.items.push(item.clone()); + let view = item.add_view(window_id, settings, ctx.as_mut()); + me.add_item(view, ctx); } Err(error) => { log::error!("error opening item: {}", error); @@ -484,20 +537,11 @@ impl Workspace { if let Some(path) = path { handle.update(ctx, move |this, ctx| { let file = this.file_for_path(&path, ctx); - let worktree_id = file.worktree_id(); let task = item.save(Some(file), ctx.as_mut()); - let item_id = item.id(); - ctx.spawn(task, move |this, result, _| match result { - Err(e) => { + ctx.spawn(task, move |_, result, _| { + if let Err(e) = result { error!("failed to save item: {:?}, ", e); } - Ok(inode) => { - if let Some(buffer) = this.untitled_buffers.remove(&item_id) { - let (_, rx) = - postage::watch::channel_with(Some(Ok(buffer))); - this.buffers.insert((worktree_id, inode), rx); - } - } }) .detach() }) @@ -760,15 +804,13 @@ mod tests { let file2 = entries[1].clone(); let file3 = entries[2].clone(); - let pane = app.read(|ctx| workspace.read(ctx).active_pane().clone()); - // Open the first entry workspace .update(&mut app, |w, ctx| w.open_entry(file1.clone(), ctx)) .unwrap() .await; app.read(|ctx| { - let pane = pane.read(ctx); + let pane = workspace.read(ctx).active_pane().read(ctx); assert_eq!( pane.active_item().unwrap().entry_id(ctx), Some(file1.clone()) @@ -782,7 +824,7 @@ mod tests { .unwrap() .await; app.read(|ctx| { - let pane = pane.read(ctx); + let pane = workspace.read(ctx).active_pane().read(ctx); assert_eq!( pane.active_item().unwrap().entry_id(ctx), Some(file2.clone()) @@ -795,7 +837,7 @@ mod tests { assert!(w.open_entry(file1.clone(), ctx).is_none()) }); app.read(|ctx| { - let pane = pane.read(ctx); + let pane = workspace.read(ctx).active_pane().read(ctx); assert_eq!( pane.active_item().unwrap().entry_id(ctx), Some(file1.clone()) @@ -803,21 +845,42 @@ mod tests { assert_eq!(pane.items().len(), 2); }); - // Open the third entry twice concurrently. Only one pane item is added. - workspace - .update(&mut app, |w, ctx| { - let task = w.open_entry(file3.clone(), ctx).unwrap(); - assert!(w.open_entry(file3.clone(), ctx).is_none()); - task - }) - .await; + // Split the pane with the first entry, then open the second entry again. + workspace.update(&mut app, |w, ctx| { + w.split_pane(w.active_pane().clone(), SplitDirection::Right, ctx); + assert!(w.open_entry(file2.clone(), ctx).is_none()); + assert_eq!( + w.active_pane() + .read(ctx) + .active_item() + .unwrap() + .entry_id(ctx.as_ref()), + Some(file2.clone()) + ); + }); + + // Open the third entry twice concurrently. Two pane items + // are added. + let (t1, t2) = workspace.update(&mut app, |w, ctx| { + ( + w.open_entry(file3.clone(), ctx).unwrap(), + w.open_entry(file3.clone(), ctx).unwrap(), + ) + }); + t1.await; + t2.await; app.read(|ctx| { - let pane = pane.read(ctx); + let pane = workspace.read(ctx).active_pane().read(ctx); assert_eq!( pane.active_item().unwrap().entry_id(ctx), Some(file3.clone()) ); - assert_eq!(pane.items().len(), 3); + let pane_entries = pane + .items() + .iter() + .map(|i| i.entry_id(ctx).unwrap()) + .collect::>(); + assert_eq!(pane_entries, &[file1, file2, file3.clone(), file3]); }); }); } @@ -1008,19 +1071,13 @@ mod tests { // Open the same newly-created file in another pane item. // The new editor should reuse the same buffer. - workspace - .update(&mut app, |workspace, ctx| { - workspace.open_new_file(&(), ctx); - workspace.split_pane( - workspace.active_pane().clone(), - SplitDirection::Right, - ctx, - ); - workspace - .open_entry((worktree.id(), Path::new("the-new-name").into()), ctx) - .unwrap() - }) - .await; + workspace.update(&mut app, |workspace, ctx| { + workspace.open_new_file(&(), ctx); + workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, ctx); + assert!(workspace + .open_entry((worktree.id(), Path::new("the-new-name").into()), ctx) + .is_none()); + }); let editor2 = workspace.update(&mut app, |workspace, ctx| { workspace .active_item(ctx) @@ -1030,7 +1087,7 @@ mod tests { .unwrap() }); app.read(|ctx| { - assert_eq!(editor.read(ctx).buffer(), editor2.read(ctx).buffer()); + assert_eq!(editor2.read(ctx).buffer(), editor.read(ctx).buffer()); }) }); } diff --git a/zed/src/worktree.rs b/zed/src/worktree.rs index 00900cdb9d35b0ba6c6f8ca1939868d8e2eb0b51..ba6c37bac1bab31485bec9f98b874f41e8ec6ab8 100644 --- a/zed/src/worktree.rs +++ b/zed/src/worktree.rs @@ -9,7 +9,7 @@ use crate::{ use ::ignore::gitignore::Gitignore; use anyhow::{Context, Result}; pub use fuzzy::{match_paths, PathMatch}; -use gpui::{scoped_pool, AppContext, Entity, ModelContext, ModelHandle, Task, View, ViewContext}; +use gpui::{scoped_pool, AppContext, Entity, ModelContext, ModelHandle, Task}; use lazy_static::lazy_static; use parking_lot::Mutex; use postage::{ @@ -53,7 +53,7 @@ pub struct Worktree { poll_scheduled: bool, } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct FileHandle { worktree: ModelHandle, state: Arc>, @@ -191,18 +191,17 @@ impl Worktree { path: &Path, content: BufferSnapshot, ctx: &AppContext, - ) -> Task> { + ) -> Task> { let abs_path = self.snapshot.abs_path.join(path); ctx.background_executor().spawn(async move { let buffer_size = content.text_summary().bytes.min(10 * 1024); let file = std::fs::File::create(&abs_path)?; - let metadata = file.metadata()?; let mut writer = std::io::BufWriter::with_capacity(buffer_size, file); for chunk in content.fragments() { writer.write(chunk.as_bytes())?; } writer.flush()?; - Ok(metadata.ino()) + Ok(()) }) } } @@ -415,7 +414,7 @@ impl FileHandle { self.worktree.read(ctx).load_history(&self.path(), ctx) } - pub fn save<'a>(&self, content: BufferSnapshot, ctx: &AppContext) -> Task> { + pub fn save<'a>(&self, content: BufferSnapshot, ctx: &AppContext) -> Task> { let worktree = self.worktree.read(ctx); worktree.save(&self.path(), content, ctx) } @@ -428,14 +427,14 @@ impl FileHandle { (self.worktree.id(), self.path()) } - pub fn observe_from_view( + pub fn observe_from_model( &self, - ctx: &mut ViewContext, - mut callback: impl FnMut(&mut T, FileHandle, &mut ViewContext) + 'static, + ctx: &mut ModelContext, + mut callback: impl FnMut(&mut T, FileHandle, &mut ModelContext) + 'static, ) { let mut prev_state = self.state.lock().clone(); let cur_state = Arc::downgrade(&self.state); - ctx.observe_model(&self.worktree, move |observer, worktree, ctx| { + ctx.observe(&self.worktree, move |observer, worktree, ctx| { if let Some(cur_state) = cur_state.upgrade() { let cur_state_unlocked = cur_state.lock(); if *cur_state_unlocked != prev_state { @@ -1361,7 +1360,8 @@ mod tests { app.read(|ctx| tree.read(ctx).scan_complete()).await; app.read(|ctx| assert_eq!(tree.read(ctx).file_count(), 1)); - let buffer = app.add_model(|_| Buffer::new(1, "a line of text.\n".repeat(10 * 1024))); + let buffer = + app.add_model(|ctx| Buffer::new(1, "a line of text.\n".repeat(10 * 1024), ctx)); let path = tree.update(&mut app, |tree, ctx| { let path = tree.files(0).next().unwrap().path().clone(); From 290fcb4c06ac19c8014f3df69567cde7775bdbc0 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 6 May 2021 13:18:41 -0700 Subject: [PATCH 06/10] In handle ::condition, re-poll on events as well as notifications --- gpui/src/app.rs | 147 +++++++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 63 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index 798805f63ebb98923956d5ea73b2b558706e3545..c6eaa5df9a95d5260e6b7931492ba44c644b4aeb 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -12,12 +12,12 @@ use keymap::MatchResult; use parking_lot::{Mutex, RwLock}; use pathfinder_geometry::{rect::RectF, vector::vec2f}; use platform::Event; -use postage::{sink::Sink as _, stream::Stream as _}; +use postage::{mpsc, sink::Sink as _, stream::Stream as _}; use smol::prelude::*; use std::{ any::{type_name, Any, TypeId}, cell::RefCell, - collections::{hash_map::Entry, HashMap, HashSet, VecDeque}, + collections::{HashMap, HashSet, VecDeque}, fmt::{self, Debug}, hash::{Hash, Hasher}, marker::PhantomData, @@ -388,7 +388,6 @@ pub struct MutableAppContext { subscriptions: HashMap>, model_observations: HashMap>, view_observations: HashMap>, - async_observations: HashMap>, window_invalidations: HashMap, presenters_and_platform_windows: HashMap>, Box)>, @@ -430,7 +429,6 @@ impl MutableAppContext { subscriptions: HashMap::new(), model_observations: HashMap::new(), view_observations: HashMap::new(), - async_observations: HashMap::new(), window_invalidations: HashMap::new(), presenters_and_platform_windows: HashMap::new(), debug_elements_callbacks: HashMap::new(), @@ -897,13 +895,11 @@ impl MutableAppContext { self.ctx.models.remove(&model_id); self.subscriptions.remove(&model_id); self.model_observations.remove(&model_id); - self.async_observations.remove(&model_id); } for (window_id, view_id) in dropped_views { self.subscriptions.remove(&view_id); self.model_observations.remove(&view_id); - self.async_observations.remove(&view_id); if let Some(window) = self.ctx.windows.get_mut(&window_id) { self.window_invalidations .entry(window_id) @@ -1082,12 +1078,6 @@ impl MutableAppContext { } } } - - if let Entry::Occupied(mut entry) = self.async_observations.entry(observed_id) { - if entry.get_mut().blocking_send(()).is_err() { - entry.remove_entry(); - } - } } fn notify_view_observers(&mut self, window_id: usize, view_id: usize) { @@ -1098,7 +1088,12 @@ impl MutableAppContext { .insert(view_id); if let Some(observations) = self.view_observations.remove(&view_id) { - if self.ctx.models.contains_key(&view_id) { + if self + .ctx + .windows + .get(&window_id) + .map_or(false, |w| w.views.contains_key(&view_id)) + { for mut observation in observations { let alive = if let Some(mut view) = self .ctx @@ -1134,12 +1129,6 @@ impl MutableAppContext { } } } - - if let Entry::Occupied(mut entry) = self.async_observations.entry(view_id) { - if entry.get_mut().blocking_send(()).is_err() { - entry.remove_entry(); - } - } } fn focus(&mut self, window_id: usize, focused_id: usize) { @@ -1780,6 +1769,10 @@ impl<'a, T: View> ViewContext<'a, T> { self.window_id } + pub fn view_id(&self) -> usize { + self.view_id + } + pub fn foreground(&self) -> &Rc { self.app.foreground_executor() } @@ -1855,22 +1848,11 @@ impl<'a, T: View> ViewContext<'a, T> { F: 'static + FnMut(&mut T, ModelHandle, &E::Event, &mut ViewContext), { let emitter_handle = handle.downgrade(); - self.app - .subscriptions - .entry(handle.id()) - .or_default() - .push(Subscription::FromView { - window_id: self.window_id, - view_id: self.view_id, - callback: Box::new(move |view, payload, app, window_id, view_id| { - if let Some(emitter_handle) = emitter_handle.upgrade(app.as_ref()) { - let model = view.downcast_mut().expect("downcast is type safe"); - let payload = payload.downcast_ref().expect("downcast is type safe"); - let mut ctx = ViewContext::new(app, window_id, view_id); - callback(model, emitter_handle, payload, &mut ctx); - } - }), - }); + self.subscribe(handle, move |model, payload, ctx| { + if let Some(emitter_handle) = emitter_handle.upgrade(ctx.as_ref()) { + callback(model, emitter_handle, payload, ctx); + } + }); } pub fn subscribe_to_view(&mut self, handle: &ViewHandle, mut callback: F) @@ -1880,7 +1862,19 @@ impl<'a, T: View> ViewContext<'a, T> { F: 'static + FnMut(&mut T, ViewHandle, &V::Event, &mut ViewContext), { let emitter_handle = handle.downgrade(); + self.subscribe(handle, move |view, payload, ctx| { + if let Some(emitter_handle) = emitter_handle.upgrade(ctx.as_ref()) { + callback(view, emitter_handle, payload, ctx); + } + }); + } + pub fn subscribe(&mut self, handle: &impl Handle, mut callback: F) + where + E: Entity, + E::Event: 'static, + F: 'static + FnMut(&mut T, &E::Event, &mut ViewContext), + { self.app .subscriptions .entry(handle.id()) @@ -1888,13 +1882,11 @@ impl<'a, T: View> ViewContext<'a, T> { .push(Subscription::FromView { window_id: self.window_id, view_id: self.view_id, - callback: Box::new(move |view, payload, app, window_id, view_id| { - if let Some(emitter_handle) = emitter_handle.upgrade(&app) { - let model = view.downcast_mut().expect("downcast is type safe"); - let payload = payload.downcast_ref().expect("downcast is type safe"); - let mut ctx = ViewContext::new(app, window_id, view_id); - callback(model, emitter_handle, payload, &mut ctx); - } + callback: Box::new(move |entity, payload, app, window_id, view_id| { + let entity = entity.downcast_mut().expect("downcast is type safe"); + let payload = payload.downcast_ref().expect("downcast is type safe"); + let mut ctx = ViewContext::new(app, window_id, view_id); + callback(entity, payload, &mut ctx); }), }); } @@ -2138,12 +2130,24 @@ impl ModelHandle { ctx: &TestAppContext, mut predicate: impl FnMut(&T, &AppContext) -> bool, ) -> impl Future { + let (tx, mut rx) = mpsc::channel(1024); + let mut ctx = ctx.0.borrow_mut(); - let tx = ctx - .async_observations - .entry(self.id()) - .or_insert_with(|| postage::broadcast::channel(128).0); - let mut rx = tx.subscribe(); + self.update(&mut *ctx, |_, ctx| { + ctx.observe(self, { + let mut tx = tx.clone(); + move |_, _, _| { + tx.blocking_send(()).ok(); + } + }); + ctx.subscribe(self, { + let mut tx = tx.clone(); + move |_, _, _| { + tx.blocking_send(()).ok(); + } + }) + }); + let ctx = ctx.weak_self.as_ref().unwrap().upgrade().unwrap(); let handle = self.downgrade(); @@ -2310,19 +2314,41 @@ impl ViewHandle { pub fn condition( &self, ctx: &TestAppContext, - mut predicate: impl 'static + FnMut(&T, &AppContext) -> bool, - ) -> impl 'static + Future { + predicate: impl FnMut(&T, &AppContext) -> bool, + ) -> impl Future { + self.condition_with_duration(Duration::from_millis(500), ctx, predicate) + } + + pub fn condition_with_duration( + &self, + duration: Duration, + ctx: &TestAppContext, + mut predicate: impl FnMut(&T, &AppContext) -> bool, + ) -> impl Future { + let (tx, mut rx) = mpsc::channel(1024); + let mut ctx = ctx.0.borrow_mut(); - let tx = ctx - .async_observations - .entry(self.id()) - .or_insert_with(|| postage::broadcast::channel(128).0); - let mut rx = tx.subscribe(); + self.update(&mut *ctx, |_, ctx| { + ctx.observe_view(self, { + let mut tx = tx.clone(); + move |_, _, _| { + tx.blocking_send(()).ok(); + } + }); + + ctx.subscribe(self, { + let mut tx = tx.clone(); + move |_, _, _| { + tx.blocking_send(()).ok(); + } + }) + }); + let ctx = ctx.weak_self.as_ref().unwrap().upgrade().unwrap(); let handle = self.downgrade(); async move { - timeout(Duration::from_millis(200), async move { + timeout(duration, async move { loop { { let ctx = ctx.borrow(); @@ -2330,7 +2356,7 @@ impl ViewHandle { if predicate( handle .upgrade(ctx) - .expect("model dropped with pending condition") + .expect("view dropped with pending condition") .read(ctx), ctx, ) { @@ -2340,7 +2366,7 @@ impl ViewHandle { rx.recv() .await - .expect("model dropped with pending condition"); + .expect("view dropped with pending condition"); } }) .await @@ -3537,9 +3563,7 @@ mod tests { model.update(&mut app, |model, ctx| model.inc(ctx)); assert_eq!(poll_once(&mut condition2).await, Some(())); - // Broadcast channel should be removed if no conditions remain on next notification. model.update(&mut app, |_, ctx| ctx.notify()); - app.update(|ctx| assert!(ctx.async_observations.get(&model.id()).is_none())); }); } @@ -3617,10 +3641,7 @@ mod tests { view.update(&mut app, |view, ctx| view.inc(ctx)); assert_eq!(poll_once(&mut condition2).await, Some(())); - - // Broadcast channel should be removed if no conditions remain on next notification. view.update(&mut app, |_, ctx| ctx.notify()); - app.update(|ctx| assert!(ctx.async_observations.get(&view.id()).is_none())); }); } @@ -3650,7 +3671,7 @@ mod tests { } #[test] - #[should_panic(expected = "model dropped with pending condition")] + #[should_panic(expected = "view dropped with pending condition")] fn test_view_condition_panic_on_drop() { struct View; From e6323f0d02ef89e5d3bfff36d4c492ca07694f44 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 6 May 2021 21:06:20 -0700 Subject: [PATCH 07/10] Fix handling of new files with new buffer/file structure --- gpui/src/app.rs | 4 ++ gpui/src/platform/test.rs | 4 ++ zed/src/editor/buffer/mod.rs | 4 +- zed/src/workspace.rs | 115 ++++++++++++----------------------- 4 files changed, 50 insertions(+), 77 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index c6eaa5df9a95d5260e6b7931492ba44c644b4aeb..cc844581ae71c04aea3b5a756e487b6c312d59ac 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -339,6 +339,10 @@ impl TestAppContext { pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option) { self.1.as_ref().simulate_new_path_selection(result); } + + pub fn did_prompt_for_new_path(&self) -> bool { + self.1.as_ref().did_prompt_for_new_path() + } } impl UpdateModel for TestAppContext { diff --git a/gpui/src/platform/test.rs b/gpui/src/platform/test.rs index 6ee772c7e05467911dda82ef705ad512ada5b1da..c0e1057409cb8474d8c59be018f402a9bd9e7b50 100644 --- a/gpui/src/platform/test.rs +++ b/gpui/src/platform/test.rs @@ -45,6 +45,10 @@ impl Platform { .expect("prompt_for_new_path was not called"); callback(result(dir_path)); } + + pub(crate) fn did_prompt_for_new_path(&self) -> bool { + self.last_prompt_for_new_path_args.borrow().is_some() + } } impl super::Platform for Platform { diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index 0c96ac0bff514401d8f73d4e7c3a674f5df1972f..72ead904d26a3dc703417f319ddbde3f901d461d 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -484,7 +484,9 @@ impl Buffer { file: Option, ctx: &mut ModelContext, ) { - self.file = file; + if file.is_some() { + self.file = file; + } self.saved_version = version; ctx.emit(Event::Saved); } diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 1d1cfaf0476908b05fd66de0bf325f674f141b40..12888237e3919e7f800ef093692cf30d17f85731 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -714,7 +714,8 @@ mod tests { use crate::{editor::BufferView, settings, test::temp_tree}; use gpui::App; use serde_json::json; - use std::{collections::HashSet, os::unix}; + use std::collections::HashSet; + use std::time; use tempdir::TempDir; #[test] @@ -957,75 +958,23 @@ mod tests { } #[test] - fn test_open_two_paths_to_the_same_file() { - use crate::workspace::ItemViewHandle; - + fn test_open_and_save_new_file() { App::test_async((), |mut app| async move { - // Create a worktree with a symlink: - // dir - // ├── hello.txt - // └── hola.txt -> hello.txt - let temp_dir = temp_tree(json!({ "hello.txt": "hi" })); - let dir = temp_dir.path(); - unix::fs::symlink(dir.join("hello.txt"), dir.join("hola.txt")).unwrap(); - + let dir = TempDir::new("test-new-file").unwrap(); let settings = settings::channel(&app.font_cache()).unwrap().1; let (_, workspace) = app.add_window(|ctx| { let mut workspace = Workspace::new(0, settings, ctx); - workspace.add_worktree(dir, ctx); + workspace.add_worktree(dir.path(), ctx); workspace }); - app.read(|ctx| workspace.read(ctx).worktree_scans_complete(ctx)) - .await; - - // Simultaneously open both the original file and the symlink to the same file. - app.update(|ctx| { - workspace.update(ctx, |view, ctx| { - view.open_paths(&[dir.join("hello.txt"), dir.join("hola.txt")], ctx) - }) - }) - .await; - - // The same content shows up with two different editors. - let buffer_views = app.read(|ctx| { + let worktree = app.read(|ctx| { workspace .read(ctx) - .active_pane() - .read(ctx) - .items() + .worktrees() .iter() - .map(|i| i.to_any().downcast::().unwrap()) - .collect::>() - }); - app.read(|ctx| { - assert_eq!(buffer_views[0].title(ctx), "hello.txt"); - assert_eq!(buffer_views[1].title(ctx), "hola.txt"); - assert_eq!(buffer_views[0].read(ctx).text(ctx), "hi"); - assert_eq!(buffer_views[1].read(ctx).text(ctx), "hi"); - }); - - // When modifying one buffer, the changes appear in both editors. - app.update(|ctx| { - buffer_views[0].update(ctx, |buf, ctx| { - buf.insert(&"oh, ".to_string(), ctx); - }); - }); - app.read(|ctx| { - assert_eq!(buffer_views[0].read(ctx).text(ctx), "oh, hi"); - assert_eq!(buffer_views[1].read(ctx).text(ctx), "oh, hi"); - }); - }); - } - - #[test] - fn test_open_and_save_new_file() { - App::test_async((), |mut app| async move { - let dir = TempDir::new("test-new-file").unwrap(); - let settings = settings::channel(&app.font_cache()).unwrap().1; - let (_, workspace) = app.add_window(|ctx| { - let mut workspace = Workspace::new(0, settings, ctx); - workspace.add_worktree(dir.path(), ctx); - workspace + .next() + .unwrap() + .clone() }); // Create a new untitled buffer @@ -1039,11 +988,13 @@ mod tests { .unwrap() }); editor.update(&mut app, |editor, ctx| { + assert!(!editor.is_dirty(ctx.as_ref())); assert_eq!(editor.title(ctx.as_ref()), "untitled"); - editor.insert(&"hi".to_string(), ctx) + editor.insert(&"hi".to_string(), ctx); + assert!(editor.is_dirty(ctx.as_ref())); }); - // Save the buffer, selecting a filename + // Save the buffer. This prompts for a filename. workspace.update(&mut app, |workspace, ctx| { workspace.save_active_item(&(), ctx) }); @@ -1051,26 +1002,38 @@ mod tests { assert_eq!(parent_dir, dir.path()); Some(parent_dir.join("the-new-name")) }); - app.read(|ctx| assert_eq!(editor.title(ctx), "untitled")); + app.read(|ctx| { + assert!(editor.is_dirty(ctx)); + assert_eq!(editor.title(ctx), "untitled"); + }); // When the save completes, the buffer's title is updated. - let worktree = app.read(|ctx| { - workspace - .read(ctx) - .worktrees() - .iter() - .next() - .unwrap() - .clone() - }); + editor + .condition(&app, |editor, ctx| !editor.is_dirty(ctx)) + .await; worktree - .condition(&app, |worktree, _| { + .condition_with_duration(time::Duration::from_millis(500), &app, |worktree, _| { worktree.inode_for_path("the-new-name").is_some() }) .await; + app.read(|ctx| assert_eq!(editor.title(ctx), "the-new-name")); + + // Edit the file and save it again. This time, there is no filename prompt. + editor.update(&mut app, |editor, ctx| { + editor.insert(&" there".to_string(), ctx); + assert_eq!(editor.is_dirty(ctx.as_ref()), true); + }); + workspace.update(&mut app, |workspace, ctx| { + workspace.save_active_item(&(), ctx) + }); + assert!(!app.did_prompt_for_new_path()); + editor + .condition(&app, |editor, ctx| !editor.is_dirty(ctx)) + .await; + app.read(|ctx| assert_eq!(editor.title(ctx), "the-new-name")); - // Open the same newly-created file in another pane item. - // The new editor should reuse the same buffer. + // Open the same newly-created file in another pane item. The new editor should reuse + // the same buffer. workspace.update(&mut app, |workspace, ctx| { workspace.open_new_file(&(), ctx); workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, ctx); From 3c3cf3b7c532d639c61e80d36e21b1ca20aadc2b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 7 May 2021 09:52:15 -0700 Subject: [PATCH 08/10] Keep weak handles to workspace items --- gpui/src/app.rs | 11 +++- zed/src/workspace.rs | 124 +++++++++++++++++++++++++------------------ 2 files changed, 83 insertions(+), 52 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index cc844581ae71c04aea3b5a756e487b6c312d59ac..2fe6af421230e27b67d83449c1e18a7562960745 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -2100,7 +2100,7 @@ impl ModelHandle { } } - fn downgrade(&self) -> WeakModelHandle { + pub fn downgrade(&self) -> WeakModelHandle { WeakModelHandle::new(self.model_id) } @@ -2268,6 +2268,15 @@ impl WeakModelHandle { } } +impl Clone for WeakModelHandle { + fn clone(&self) -> Self { + Self { + model_id: self.model_id, + model_type: PhantomData, + } + } +} + pub struct ViewHandle { window_id: usize, view_id: usize, diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 12888237e3919e7f800ef093692cf30d17f85731..441e0d291d46541247d0460b489b69b57e287df5 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -1,14 +1,28 @@ pub mod pane; pub mod pane_group; -pub use pane::*; -pub use pane_group::*; - use crate::{ + editor::{Buffer, BufferView}, settings::Settings, + time::ReplicaId, watch::{self, Receiver}, - worktree::FileHandle, + worktree::{FileHandle, Worktree, WorktreeHandle}, +}; +use futures_core::{future::LocalBoxFuture, Future}; +use gpui::{ + color::rgbu, elements::*, json::to_string_pretty, keymap::Binding, AnyViewHandle, AppContext, + ClipboardItem, Entity, EntityTask, ModelHandle, MutableAppContext, PathPromptOptions, View, + ViewContext, ViewHandle, WeakModelHandle, +}; +use log::error; +pub use pane::*; +pub use pane_group::*; +use smol::prelude::*; +use std::{collections::HashMap, path::PathBuf}; +use std::{ + collections::{hash_map::Entry, HashSet}, + path::Path, + sync::Arc, }; -use std::{collections::HashMap, fmt, path::PathBuf}; pub fn init(app: &mut MutableAppContext) { app.add_global_action("workspace:open", open); @@ -23,24 +37,6 @@ pub fn init(app: &mut MutableAppContext) { ]); pane::init(app); } -use crate::{ - editor::{Buffer, BufferView}, - time::ReplicaId, - worktree::{Worktree, WorktreeHandle}, -}; -use futures_core::{future::LocalBoxFuture, Future}; -use gpui::{ - color::rgbu, elements::*, json::to_string_pretty, keymap::Binding, AnyViewHandle, AppContext, - ClipboardItem, Entity, EntityTask, ModelHandle, MutableAppContext, PathPromptOptions, View, - ViewContext, ViewHandle, -}; -use log::error; -use smol::prelude::*; -use std::{ - collections::{hash_map::Entry, HashSet}, - path::Path, - sync::Arc, -}; pub struct OpenParams { pub paths: Vec, @@ -137,15 +133,19 @@ pub trait ItemView: View { } pub trait ItemHandle: Send + Sync { - fn id(&self) -> usize; + fn boxed_clone(&self) -> Box; + fn downgrade(&self) -> Box; +} + +pub trait WeakItemHandle: Send + Sync { fn file<'a>(&'a self, ctx: &'a AppContext) -> Option<&'a FileHandle>; fn add_view( &self, window_id: usize, settings: watch::Receiver, app: &mut MutableAppContext, - ) -> Box; - fn boxed_clone(&self) -> Box; + ) -> Option>; + fn alive(&self, ctx: &AppContext) -> bool; } pub trait ItemViewHandle: Send + Sync { @@ -165,25 +165,37 @@ pub trait ItemViewHandle: Send + Sync { } impl ItemHandle for ModelHandle { - fn id(&self) -> usize { - self.id() + fn boxed_clone(&self) -> Box { + Box::new(self.clone()) + } + + fn downgrade(&self) -> Box { + Box::new(self.downgrade()) } +} +impl WeakItemHandle for WeakModelHandle { fn file<'a>(&'a self, ctx: &'a AppContext) -> Option<&'a FileHandle> { - self.read(ctx).file() + self.upgrade(ctx).and_then(|h| h.read(ctx).file()) } fn add_view( &self, window_id: usize, - settings: watch::Receiver, - app: &mut MutableAppContext, - ) -> Box { - Box::new(app.add_view(window_id, |ctx| T::build_view(self.clone(), settings, ctx))) + settings: Receiver, + ctx: &mut MutableAppContext, + ) -> Option> { + if let Some(handle) = self.upgrade(ctx.as_ref()) { + Some(Box::new(ctx.add_view(window_id, |ctx| { + T::build_view(handle, settings, ctx) + }))) + } else { + None + } } - fn boxed_clone(&self) -> Box { - Box::new(self.clone()) + fn alive(&self, ctx: &AppContext) -> bool { + self.upgrade(ctx).is_some() } } @@ -256,12 +268,6 @@ impl Clone for Box { } } -impl fmt::Debug for Box { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "ItemHandle {{id: {}}}", self.id()) - } -} - #[derive(Debug)] pub struct State { pub modal: Option, @@ -276,7 +282,7 @@ pub struct Workspace { active_pane: ViewHandle, replica_id: ReplicaId, worktrees: HashSet>, - items: Vec>, + items: Vec>, loading_items: HashMap< (usize, Arc), postage::watch::Receiver, Arc>>>, @@ -427,7 +433,7 @@ impl Workspace { let buffer = ctx.add_model(|ctx| Buffer::new(self.replica_id, "", ctx)); let buffer_view = ctx.add_view(|ctx| BufferView::for_buffer(buffer.clone(), self.settings.clone(), ctx)); - self.items.push(Box::new(buffer)); + self.items.push(ItemHandle::downgrade(&buffer)); self.add_item(Box::new(buffer_view), ctx); } @@ -451,12 +457,25 @@ impl Workspace { // Otherwise, if this file is already open somewhere in the workspace, // then add another view for it. - if let Some(item) = self.items.iter().find(|item| { - item.file(ctx.as_ref()) - .map_or(false, |f| f.entry_id() == entry) - }) { - self.add_item(item.add_view(window_id, settings, ctx.as_mut()), ctx); - return None; + let mut i = 0; + while i < self.items.len() { + let item = &self.items[i]; + if item.alive(ctx.as_ref()) { + if item + .file(ctx.as_ref()) + .map_or(false, |f| f.entry_id() == entry) + { + self.add_item( + item.add_view(window_id, settings.clone(), ctx.as_mut()) + .unwrap(), + ctx, + ); + return None; + } + i += 1; + } else { + self.items.remove(i); + } } let (worktree_id, path) = entry.clone(); @@ -507,8 +526,11 @@ impl Workspace { me.loading_items.remove(&entry); match load_result { Ok(item) => { - me.items.push(item.clone()); - let view = item.add_view(window_id, settings, ctx.as_mut()); + let weak_item = item.downgrade(); + let view = weak_item + .add_view(window_id, settings, ctx.as_mut()) + .unwrap(); + me.items.push(weak_item); me.add_item(view, ctx); } Err(error) => { From 29d44c8a100e9f6c51d61dcb18bbf01fe890f896 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 7 May 2021 09:53:07 -0700 Subject: [PATCH 09/10] Go back to using upstream core-foundation-rs --- Cargo.lock | 12 ++++++------ Cargo.toml | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e17c480da60b752d7f353c7915719691df77c093..1fba36fd1210a8ddb0427046923b468f11463463 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -449,7 +449,7 @@ dependencies = [ [[package]] name = "cocoa" version = "0.24.0" -source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" +source = "git+https://github.com/servo/core-foundation-rs?rev=025dcb3c0d1ef01530f57ef65f3b1deb948f5737#025dcb3c0d1ef01530f57ef65f3b1deb948f5737" dependencies = [ "bitflags 1.2.1", "block", @@ -464,7 +464,7 @@ dependencies = [ [[package]] name = "cocoa-foundation" version = "0.1.0" -source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" +source = "git+https://github.com/servo/core-foundation-rs?rev=025dcb3c0d1ef01530f57ef65f3b1deb948f5737#025dcb3c0d1ef01530f57ef65f3b1deb948f5737" dependencies = [ "bitflags 1.2.1", "block", @@ -499,7 +499,7 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" [[package]] name = "core-foundation" version = "0.9.1" -source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" +source = "git+https://github.com/servo/core-foundation-rs?rev=025dcb3c0d1ef01530f57ef65f3b1deb948f5737#025dcb3c0d1ef01530f57ef65f3b1deb948f5737" dependencies = [ "core-foundation-sys", "libc", @@ -508,12 +508,12 @@ dependencies = [ [[package]] name = "core-foundation-sys" version = "0.8.2" -source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" +source = "git+https://github.com/servo/core-foundation-rs?rev=025dcb3c0d1ef01530f57ef65f3b1deb948f5737#025dcb3c0d1ef01530f57ef65f3b1deb948f5737" [[package]] name = "core-graphics" version = "0.22.2" -source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" +source = "git+https://github.com/servo/core-foundation-rs?rev=025dcb3c0d1ef01530f57ef65f3b1deb948f5737#025dcb3c0d1ef01530f57ef65f3b1deb948f5737" dependencies = [ "bitflags 1.2.1", "core-foundation", @@ -525,7 +525,7 @@ dependencies = [ [[package]] name = "core-graphics-types" version = "0.1.1" -source = "git+https://github.com/zed-industries/core-foundation-rs?rev=39e1e0eeef11a17cf49aa6a500c37e665d967d2a#39e1e0eeef11a17cf49aa6a500c37e665d967d2a" +source = "git+https://github.com/servo/core-foundation-rs?rev=025dcb3c0d1ef01530f57ef65f3b1deb948f5737#025dcb3c0d1ef01530f57ef65f3b1deb948f5737" dependencies = [ "bitflags 1.2.1", "core-foundation", diff --git a/Cargo.toml b/Cargo.toml index e1729a3a46d0180f3ab4baaa7a496bf980a02f78..80fbb251e5707b8b727a59aaf58ccafb0b79d75f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,10 @@ members = ["zed", "gpui", "fsevent", "scoped_pool"] async-task = {git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e"} # TODO - Remove when a version is released with this PR: https://github.com/servo/core-foundation-rs/pull/457 -cocoa = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} -cocoa-foundation = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} -core-foundation = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} -core-graphics = {git = "https://github.com/zed-industries/core-foundation-rs", rev = "39e1e0eeef11a17cf49aa6a500c37e665d967d2a"} +cocoa = {git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737"} +cocoa-foundation = {git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737"} +core-foundation = {git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737"} +core-graphics = {git = "https://github.com/servo/core-foundation-rs", rev = "025dcb3c0d1ef01530f57ef65f3b1deb948f5737"} [profile.dev] split-debuginfo = "unpacked" From 4b0bd6b8e323e6699637f3115454d8f3e9efd51b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 7 May 2021 11:21:49 -0700 Subject: [PATCH 10/10] Clean up Workspace::open_entry --- zed/src/workspace.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/zed/src/workspace.rs b/zed/src/workspace.rs index 441e0d291d46541247d0460b489b69b57e287df5..00166073e2a1c99fcac7cf2c27c5511447693427 100644 --- a/zed/src/workspace.rs +++ b/zed/src/workspace.rs @@ -434,7 +434,7 @@ impl Workspace { let buffer_view = ctx.add_view(|ctx| BufferView::for_buffer(buffer.clone(), self.settings.clone(), ctx)); self.items.push(ItemHandle::downgrade(&buffer)); - self.add_item(Box::new(buffer_view), ctx); + self.add_item_view(Box::new(buffer_view), ctx); } #[must_use] @@ -452,30 +452,30 @@ impl Workspace { return None; } - let window_id = ctx.window_id(); - let settings = self.settings.clone(); - // Otherwise, if this file is already open somewhere in the workspace, // then add another view for it. - let mut i = 0; - while i < self.items.len() { - let item = &self.items[i]; + let settings = self.settings.clone(); + let mut view_for_existing_item = None; + self.items.retain(|item| { if item.alive(ctx.as_ref()) { - if item - .file(ctx.as_ref()) - .map_or(false, |f| f.entry_id() == entry) + if view_for_existing_item.is_none() + && item + .file(ctx.as_ref()) + .map_or(false, |f| f.entry_id() == entry) { - self.add_item( - item.add_view(window_id, settings.clone(), ctx.as_mut()) + view_for_existing_item = Some( + item.add_view(ctx.window_id(), settings.clone(), ctx.as_mut()) .unwrap(), - ctx, ); - return None; } - i += 1; + true } else { - self.items.remove(i); + false } + }); + if let Some(view) = view_for_existing_item { + self.add_item_view(view, ctx); + return None; } let (worktree_id, path) = entry.clone(); @@ -528,10 +528,10 @@ impl Workspace { Ok(item) => { let weak_item = item.downgrade(); let view = weak_item - .add_view(window_id, settings, ctx.as_mut()) + .add_view(ctx.window_id(), settings, ctx.as_mut()) .unwrap(); me.items.push(weak_item); - me.add_item(view, ctx); + me.add_item_view(view, ctx); } Err(error) => { log::error!("error opening item: {}", error); @@ -648,7 +648,7 @@ impl Workspace { self.activate_pane(new_pane.clone(), ctx); if let Some(item) = pane.read(ctx).active_item() { if let Some(clone) = item.clone_on_split(ctx.as_mut()) { - self.add_item(clone, ctx); + self.add_item_view(clone, ctx); } } self.center @@ -673,7 +673,7 @@ impl Workspace { &self.active_pane } - fn add_item(&self, item: Box, ctx: &mut ViewContext) { + fn add_item_view(&self, item: Box, ctx: &mut ViewContext) { let active_pane = self.active_pane(); item.set_parent_pane(&active_pane, ctx.as_mut()); active_pane.update(ctx, |pane, ctx| {