Add worktree to workspace when joining a remote worktree

This commit is contained in:
Antonio Scandurra
2021-06-18 14:52:38 +02:00
parent 79cac1340e
commit 89d25458ac
5 changed files with 103 additions and 29 deletions
+10 -2
View File
@@ -705,7 +705,7 @@ impl Workspace {
let rpc = self.rpc.clone();
let executor = cx.background_executor().clone();
let task = cx.spawn(|_, cx| async move {
let task = cx.spawn(|this, mut cx| async move {
let connection_id = rpc.connect_to_server(&cx, &executor).await?;
let worktree_url = cx
@@ -725,7 +725,15 @@ impl Workspace {
},
)
.await?;
log::info!("joined worktree: {:?}", open_worktree_response);
let worktree = open_worktree_response
.worktree
.ok_or_else(|| anyhow!("empty worktree"))?;
this.update(&mut cx, |workspace, cx| {
let worktree = cx.add_model(|cx| Worktree::remote(worktree, cx));
cx.observe_model(&worktree, |_, _, cx| cx.notify());
workspace.worktrees.insert(worktree);
cx.notify();
});
surf::Result::Ok(())
});
+67 -5
View File
@@ -48,6 +48,7 @@ enum ScanState {
pub enum Worktree {
Local(LocalWorktree),
Remote(RemoteWorktree),
}
impl Entity for Worktree {
@@ -59,6 +60,10 @@ impl Worktree {
Worktree::Local(LocalWorktree::new(path, cx))
}
pub fn remote(worktree: proto::Worktree, cx: &mut ModelContext<Worktree>) -> Self {
Worktree::Remote(RemoteWorktree::new(worktree, cx))
}
pub fn as_local(&self) -> Option<&LocalWorktree> {
if let Worktree::Local(worktree) = self {
Some(worktree)
@@ -78,6 +83,7 @@ impl Worktree {
pub fn snapshot(&self) -> Snapshot {
match self {
Worktree::Local(worktree) => worktree.snapshot(),
Worktree::Remote(worktree) => worktree.snapshot.clone(),
}
}
@@ -88,6 +94,7 @@ impl Worktree {
) -> impl Future<Output = Result<History>> {
match self {
Worktree::Local(worktree) => worktree.load_history(path, cx),
Worktree::Remote(worktree) => todo!(),
}
}
@@ -99,6 +106,7 @@ impl Worktree {
) -> impl Future<Output = Result<()>> {
match self {
Worktree::Local(worktree) => worktree.save(path, content, cx),
Worktree::Remote(worktree) => todo!(),
}
}
}
@@ -109,6 +117,7 @@ impl Deref for Worktree {
fn deref(&self) -> &Self::Target {
match self {
Worktree::Local(worktree) => &worktree.snapshot,
Worktree::Remote(worktree) => &worktree.snapshot,
}
}
}
@@ -137,7 +146,7 @@ struct FileHandleState {
}
impl LocalWorktree {
pub fn new(path: impl Into<Arc<Path>>, cx: &mut ModelContext<Worktree>) -> Self {
fn new(path: impl Into<Arc<Path>>, cx: &mut ModelContext<Worktree>) -> Self {
let abs_path = path.into();
let (scan_state_tx, scan_state_rx) = smol::channel::unbounded();
let id = cx.model_id();
@@ -309,14 +318,22 @@ impl LocalWorktree {
cx: &mut ModelContext<Worktree>,
) -> Task<anyhow::Result<(u64, String)>> {
self.rpc = Some(client.clone());
let root_name = self.root_name.clone();
let snapshot = self.snapshot();
cx.spawn(|_this, cx| async move {
let paths = cx
let entries = cx
.background_executor()
.spawn(async move {
snapshot
.paths()
.map(|path| path.to_string_lossy().to_string())
.entries
.cursor::<(), ()>()
.map(|entry| proto::Entry {
is_dir: entry.is_dir(),
path: entry.path.to_string_lossy().to_string(),
inode: entry.inode,
is_symlink: entry.is_symlink,
is_ignored: entry.is_ignored,
})
.collect()
})
.await;
@@ -325,7 +342,7 @@ impl LocalWorktree {
.request(
connection_id,
proto::ShareWorktree {
worktree: Some(proto::Worktree { paths }),
worktree: Some(proto::Worktree { root_name, entries }),
},
)
.await?;
@@ -350,6 +367,50 @@ impl fmt::Debug for LocalWorktree {
}
}
pub struct RemoteWorktree {
snapshot: Snapshot,
}
impl RemoteWorktree {
fn new(worktree: proto::Worktree, cx: &mut ModelContext<Worktree>) -> Self {
let id = cx.model_id();
let root_char_bag: CharBag = worktree
.root_name
.chars()
.map(|c| c.to_ascii_lowercase())
.collect();
let mut entries = SumTree::new();
entries.extend(
worktree.entries.into_iter().map(|entry| {
let kind = if entry.is_dir {
EntryKind::Dir
} else {
let mut char_bag = root_char_bag.clone();
char_bag.extend(entry.path.chars().map(|c| c.to_ascii_lowercase()));
EntryKind::File(char_bag)
};
Entry {
kind,
path: Path::new(&entry.path).into(),
inode: entry.inode,
is_symlink: entry.is_symlink,
is_ignored: entry.is_ignored,
}
}),
&(),
);
let snapshot = Snapshot {
id,
scan_id: 0,
abs_path: Path::new("").into(),
root_name: worktree.root_name,
ignores: Default::default(),
entries,
};
Self { snapshot }
}
}
#[derive(Clone)]
pub struct Snapshot {
id: usize,
@@ -1367,6 +1428,7 @@ impl WorktreeHandle for ModelHandle<Worktree> {
}
})
}
Worktree::Remote(tree) => todo!(),
}
}