Move file handle from buffer to buffer view

This commit is contained in:
Max Brunsfeld
2021-04-29 17:47:06 -07:00
parent d2f309d10d
commit 8cffa8bdb2
5 changed files with 136 additions and 81 deletions
+16 -9
View File
@@ -4,7 +4,7 @@ use crate::{
settings::Settings,
time::ReplicaId,
watch,
worktree::{Worktree, WorktreeHandle as _},
worktree::{FileHandle, Worktree, WorktreeHandle as _},
};
use anyhow::anyhow;
use gpui::{AppContext, Entity, Handle, ModelContext, ModelHandle, MutableAppContext, ViewContext};
@@ -25,6 +25,7 @@ where
fn build_view(
handle: ModelHandle<Self>,
settings: watch::Receiver<Settings>,
file: Option<FileHandle>,
ctx: &mut ViewContext<Self::View>,
) -> Self::View;
}
@@ -34,6 +35,7 @@ pub trait ItemHandle: Debug + Send + Sync {
&self,
window_id: usize,
settings: watch::Receiver<Settings>,
file: Option<FileHandle>,
app: &mut MutableAppContext,
) -> Box<dyn ItemViewHandle>;
fn id(&self) -> usize;
@@ -45,9 +47,12 @@ impl<T: 'static + Item> ItemHandle for ModelHandle<T> {
&self,
window_id: usize,
settings: watch::Receiver<Settings>,
file: Option<FileHandle>,
app: &mut MutableAppContext,
) -> Box<dyn ItemViewHandle> {
Box::new(app.add_view(window_id, |ctx| T::build_view(self.clone(), settings, ctx)))
Box::new(app.add_view(window_id, |ctx| {
T::build_view(self.clone(), settings, file, ctx)
}))
}
fn id(&self) -> usize {
@@ -148,7 +153,7 @@ impl Workspace {
&mut self,
(worktree_id, path): (usize, Arc<Path>),
ctx: &mut ModelContext<'_, Self>,
) -> anyhow::Result<Pin<Box<dyn Future<Output = OpenResult> + Send>>> {
) -> anyhow::Result<Pin<Box<dyn Future<Output = (OpenResult, FileHandle)> + Send>>> {
let worktree = self
.worktrees
.get(&worktree_id)
@@ -160,18 +165,20 @@ impl Workspace {
.inode_for_path(&path)
.ok_or_else(|| anyhow!("path {:?} does not exist", path))?;
let file = worktree.file(path.clone(), ctx.as_ref())?;
let item_key = (worktree_id, inode);
if let Some(item) = self.items.get(&item_key).cloned() {
return Ok(async move {
match item {
OpenedItem::Loaded(handle) => {
return Ok(handle);
return (Ok(handle), file);
}
OpenedItem::Loading(rx) => loop {
rx.updated().await;
if let Some(result) = smol::block_on(rx.read()).clone() {
return result;
return (result, file);
}
},
}
@@ -180,7 +187,6 @@ impl Workspace {
}
let replica_id = self.replica_id;
let file = worktree.file(path.clone(), ctx.as_ref())?;
let history = file.load_history(ctx.as_ref());
let (mut tx, rx) = watch::channel(None);
@@ -190,7 +196,7 @@ impl Workspace {
move |me, history: anyhow::Result<History>, ctx| match history {
Ok(history) => {
let handle = Box::new(
ctx.add_model(|ctx| Buffer::from_history(replica_id, file, history, ctx)),
ctx.add_model(|ctx| Buffer::from_history(replica_id, history, ctx)),
) as Box<dyn ItemHandle>;
me.items
.insert(item_key, OpenedItem::Loaded(handle.clone()));
@@ -282,14 +288,15 @@ mod tests {
)
});
let handle_1 = future_1.await.unwrap();
let handle_2 = future_2.await.unwrap();
let handle_1 = future_1.await.0.unwrap();
let handle_2 = future_2.await.0.unwrap();
assert_eq!(handle_1.id(), handle_2.id());
// Open the same entry again now that it has loaded
let handle_3 = workspace
.update(&mut app, |w, app| w.open_entry(entry, app).unwrap())
.await
.0
.unwrap();
assert_eq!(handle_3.id(), handle_1.id());
+63 -5
View File
@@ -250,13 +250,14 @@ impl WorkspaceView {
error!("{}", error);
None
}
Ok(item) => {
Ok(future) => {
let settings = self.settings.clone();
Some(ctx.spawn(item, move |me, item, ctx| {
Some(ctx.spawn(future, move |me, (item, file), ctx| {
me.loading_entries.remove(&entry);
match item {
Ok(item) => {
let item_view = item.add_view(ctx.window_id(), settings, ctx.as_mut());
let item_view =
item.add_view(ctx.window_id(), settings, Some(file), ctx.as_mut());
me.add_item(item_view, ctx);
}
Err(error) => {
@@ -417,10 +418,10 @@ impl View for WorkspaceView {
#[cfg(test)]
mod tests {
use super::{pane, Workspace, WorkspaceView};
use crate::{settings, test::temp_tree, workspace::WorkspaceHandle as _};
use crate::{editor::BufferView, settings, test::temp_tree, workspace::WorkspaceHandle as _};
use gpui::App;
use serde_json::json;
use std::collections::HashSet;
use std::{collections::HashSet, os::unix};
#[test]
fn test_open_entry() {
@@ -575,6 +576,63 @@ mod tests {
});
}
#[test]
fn test_open_two_paths_to_the_same_file() {
use crate::workspace::ItemViewHandle;
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 workspace = app.add_model(|ctx| Workspace::new(vec![dir.into()], ctx));
let settings = settings::channel(&app.font_cache()).unwrap().1;
let (_, workspace_view) =
app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
// Simultaneously open both the original file and the symlink to the same file.
app.update(|ctx| {
workspace_view.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| {
workspace_view
.read(ctx)
.active_pane()
.read(ctx)
.items()
.iter()
.map(|i| i.to_any().downcast::<BufferView>().unwrap())
.collect::<Vec<_>>()
});
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_pane_actions() {
App::test_async((), |mut app| async move {