Merge pull request #619 from zed-industries/project-entry-ids

Ensure that worktree entry ids are unique across the project
This commit is contained in:
Max Brunsfeld 2022-03-14 12:43:58 -07:00 committed by GitHub
commit 7bdb91f4ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View file

@ -38,7 +38,10 @@ use std::{
ops::Range, ops::Range,
path::{Component, Path, PathBuf}, path::{Component, Path, PathBuf},
rc::Rc, rc::Rc,
sync::{atomic::AtomicBool, Arc}, sync::{
atomic::{AtomicBool, AtomicUsize},
Arc,
},
time::Instant, time::Instant,
}; };
use util::{post_inc, ResultExt, TryFutureExt as _}; use util::{post_inc, ResultExt, TryFutureExt as _};
@ -56,6 +59,7 @@ pub struct Project {
language_server_settings: Arc<Mutex<serde_json::Value>>, language_server_settings: Arc<Mutex<serde_json::Value>>,
next_language_server_id: usize, next_language_server_id: usize,
client: Arc<client::Client>, client: Arc<client::Client>,
next_entry_id: Arc<AtomicUsize>,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
client_state: ProjectClientState, client_state: ProjectClientState,
@ -331,6 +335,7 @@ impl Project {
client, client,
user_store, user_store,
fs, fs,
next_entry_id: Default::default(),
language_servers_with_diagnostics_running: 0, language_servers_with_diagnostics_running: 0,
language_servers: Default::default(), language_servers: Default::default(),
started_language_servers: Default::default(), started_language_servers: Default::default(),
@ -381,6 +386,7 @@ impl Project {
languages, languages,
user_store: user_store.clone(), user_store: user_store.clone(),
fs, fs,
next_entry_id: Default::default(),
subscriptions: vec![client.add_model_for_remote_entity(remote_id, cx)], subscriptions: vec![client.add_model_for_remote_entity(remote_id, cx)],
client: client.clone(), client: client.clone(),
client_state: ProjectClientState::Remote { client_state: ProjectClientState::Remote {
@ -2979,6 +2985,7 @@ impl Project {
) -> Task<Result<ModelHandle<Worktree>>> { ) -> Task<Result<ModelHandle<Worktree>>> {
let fs = self.fs.clone(); let fs = self.fs.clone();
let client = self.client.clone(); let client = self.client.clone();
let next_entry_id = self.next_entry_id.clone();
let path: Arc<Path> = abs_path.as_ref().into(); let path: Arc<Path> = abs_path.as_ref().into();
let task = self let task = self
.loading_local_worktrees .loading_local_worktrees
@ -2986,9 +2993,15 @@ impl Project {
.or_insert_with(|| { .or_insert_with(|| {
cx.spawn(|project, mut cx| { cx.spawn(|project, mut cx| {
async move { async move {
let worktree = let worktree = Worktree::local(
Worktree::local(client.clone(), path.clone(), visible, fs, &mut cx) client.clone(),
.await; path.clone(),
visible,
fs,
next_entry_id,
&mut cx,
)
.await;
project.update(&mut cx, |project, _| { project.update(&mut cx, |project, _| {
project.loading_local_worktrees.remove(&path); project.loading_local_worktrees.remove(&path);
}); });

View file

@ -172,10 +172,11 @@ impl Worktree {
path: impl Into<Arc<Path>>, path: impl Into<Arc<Path>>,
visible: bool, visible: bool,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
next_entry_id: Arc<AtomicUsize>,
cx: &mut AsyncAppContext, cx: &mut AsyncAppContext,
) -> Result<ModelHandle<Self>> { ) -> Result<ModelHandle<Self>> {
let (tree, scan_states_tx) = let (tree, scan_states_tx) =
LocalWorktree::new(client, path, visible, fs.clone(), cx).await?; LocalWorktree::new(client, path, visible, fs.clone(), next_entry_id, cx).await?;
tree.update(cx, |tree, cx| { tree.update(cx, |tree, cx| {
let tree = tree.as_local_mut().unwrap(); let tree = tree.as_local_mut().unwrap();
let abs_path = tree.abs_path().clone(); let abs_path = tree.abs_path().clone();
@ -425,11 +426,11 @@ impl LocalWorktree {
path: impl Into<Arc<Path>>, path: impl Into<Arc<Path>>,
visible: bool, visible: bool,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
next_entry_id: Arc<AtomicUsize>,
cx: &mut AsyncAppContext, cx: &mut AsyncAppContext,
) -> Result<(ModelHandle<Worktree>, UnboundedSender<ScanState>)> { ) -> Result<(ModelHandle<Worktree>, UnboundedSender<ScanState>)> {
let abs_path = path.into(); let abs_path = path.into();
let path: Arc<Path> = Arc::from(Path::new("")); let path: Arc<Path> = Arc::from(Path::new(""));
let next_entry_id = AtomicUsize::new(0);
// After determining whether the root entry is a file or a directory, populate the // After determining whether the root entry is a file or a directory, populate the
// snapshot's "root name", which will be used for the purpose of fuzzy matching. // snapshot's "root name", which will be used for the purpose of fuzzy matching.
@ -457,7 +458,7 @@ impl LocalWorktree {
scan_id: 0, scan_id: 0,
ignores: Default::default(), ignores: Default::default(),
removed_entry_ids: Default::default(), removed_entry_ids: Default::default(),
next_entry_id: Arc::new(next_entry_id), next_entry_id,
snapshot: Snapshot { snapshot: Snapshot {
id: WorktreeId::from_usize(cx.model_id()), id: WorktreeId::from_usize(cx.model_id()),
root_name: root_name.clone(), root_name: root_name.clone(),
@ -2425,6 +2426,7 @@ mod tests {
Arc::from(Path::new("/root")), Arc::from(Path::new("/root")),
true, true,
fs, fs,
Default::default(),
&mut cx.to_async(), &mut cx.to_async(),
) )
.await .await
@ -2468,6 +2470,7 @@ mod tests {
dir.path(), dir.path(),
true, true,
Arc::new(RealFs), Arc::new(RealFs),
Default::default(),
&mut cx.to_async(), &mut cx.to_async(),
) )
.await .await