worktree.rs

   1use super::{
   2    fs::{self, Fs},
   3    ignore::IgnoreStack,
   4    DiagnosticSummary,
   5};
   6use ::ignore::gitignore::{Gitignore, GitignoreBuilder};
   7use anyhow::{anyhow, Result};
   8use client::{proto, Client, PeerId, TypedEnvelope, UserStore};
   9use clock::ReplicaId;
  10use collections::{hash_map, HashMap, HashSet};
  11use futures::{Stream, StreamExt};
  12use fuzzy::CharBag;
  13use gpui::{
  14    executor, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext,
  15    Task, UpgradeModelHandle, WeakModelHandle,
  16};
  17use language::{
  18    range_from_lsp, Buffer, Diagnostic, DiagnosticEntry, DiagnosticSeverity, File as _, Operation,
  19    PointUtf16, Rope,
  20};
  21use lazy_static::lazy_static;
  22use parking_lot::Mutex;
  23use postage::{
  24    prelude::{Sink as _, Stream as _},
  25    watch,
  26};
  27use serde::Deserialize;
  28use smol::channel::{self, Sender};
  29use std::{
  30    any::Any,
  31    cmp::{self, Ordering},
  32    convert::{TryFrom, TryInto},
  33    ffi::{OsStr, OsString},
  34    fmt,
  35    future::Future,
  36    ops::Deref,
  37    path::{Path, PathBuf},
  38    sync::{
  39        atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
  40        Arc,
  41    },
  42    time::{Duration, SystemTime},
  43};
  44use sum_tree::{Bias, TreeMap};
  45use sum_tree::{Edit, SeekTarget, SumTree};
  46use util::{post_inc, ResultExt, TryFutureExt};
  47
  48lazy_static! {
  49    static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
  50}
  51
  52#[derive(Clone, Debug)]
  53enum ScanState {
  54    Idle,
  55    Scanning,
  56    Err(Arc<anyhow::Error>),
  57}
  58
  59#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
  60pub struct WorktreeId(usize);
  61
  62pub enum Worktree {
  63    Local(LocalWorktree),
  64    Remote(RemoteWorktree),
  65}
  66
  67impl Entity for Worktree {
  68    type Event = ();
  69
  70    fn release(&mut self, cx: &mut MutableAppContext) {
  71        if let Some(worktree) = self.as_local_mut() {
  72            if let Registration::Done { project_id } = worktree.registration {
  73                let client = worktree.client.clone();
  74                let unregister_message = proto::UnregisterWorktree {
  75                    project_id,
  76                    worktree_id: worktree.id().to_proto(),
  77                };
  78                cx.foreground()
  79                    .spawn(async move {
  80                        client.send(unregister_message).await?;
  81                        Ok::<_, anyhow::Error>(())
  82                    })
  83                    .detach_and_log_err(cx);
  84            }
  85        }
  86    }
  87}
  88
  89impl Worktree {
  90    pub async fn open_local(
  91        client: Arc<Client>,
  92        user_store: ModelHandle<UserStore>,
  93        path: impl Into<Arc<Path>>,
  94        weak: bool,
  95        fs: Arc<dyn Fs>,
  96        cx: &mut AsyncAppContext,
  97    ) -> Result<ModelHandle<Self>> {
  98        let (tree, scan_states_tx) =
  99            LocalWorktree::new(client, user_store, path, weak, fs.clone(), cx).await?;
 100        tree.update(cx, |tree, cx| {
 101            let tree = tree.as_local_mut().unwrap();
 102            let abs_path = tree.snapshot.abs_path.clone();
 103            let background_snapshot = tree.background_snapshot.clone();
 104            let background = cx.background().clone();
 105            tree._background_scanner_task = Some(cx.background().spawn(async move {
 106                let events = fs.watch(&abs_path, Duration::from_millis(100)).await;
 107                let scanner =
 108                    BackgroundScanner::new(background_snapshot, scan_states_tx, fs, background);
 109                scanner.run(events).await;
 110            }));
 111        });
 112        Ok(tree)
 113    }
 114
 115    pub async fn remote(
 116        project_remote_id: u64,
 117        replica_id: ReplicaId,
 118        worktree: proto::Worktree,
 119        client: Arc<Client>,
 120        user_store: ModelHandle<UserStore>,
 121        cx: &mut AsyncAppContext,
 122    ) -> Result<ModelHandle<Self>> {
 123        let remote_id = worktree.id;
 124        let root_char_bag: CharBag = worktree
 125            .root_name
 126            .chars()
 127            .map(|c| c.to_ascii_lowercase())
 128            .collect();
 129        let root_name = worktree.root_name.clone();
 130        let weak = worktree.weak;
 131        let (entries_by_path, entries_by_id, diagnostic_summaries) = cx
 132            .background()
 133            .spawn(async move {
 134                let mut entries_by_path_edits = Vec::new();
 135                let mut entries_by_id_edits = Vec::new();
 136                for entry in worktree.entries {
 137                    match Entry::try_from((&root_char_bag, entry)) {
 138                        Ok(entry) => {
 139                            entries_by_id_edits.push(Edit::Insert(PathEntry {
 140                                id: entry.id,
 141                                path: entry.path.clone(),
 142                                is_ignored: entry.is_ignored,
 143                                scan_id: 0,
 144                            }));
 145                            entries_by_path_edits.push(Edit::Insert(entry));
 146                        }
 147                        Err(err) => log::warn!("error for remote worktree entry {:?}", err),
 148                    }
 149                }
 150
 151                let mut entries_by_path = SumTree::new();
 152                let mut entries_by_id = SumTree::new();
 153                entries_by_path.edit(entries_by_path_edits, &());
 154                entries_by_id.edit(entries_by_id_edits, &());
 155
 156                let diagnostic_summaries = TreeMap::from_ordered_entries(
 157                    worktree.diagnostic_summaries.into_iter().map(|summary| {
 158                        (
 159                            PathKey(PathBuf::from(summary.path).into()),
 160                            DiagnosticSummary {
 161                                error_count: summary.error_count as usize,
 162                                warning_count: summary.warning_count as usize,
 163                                info_count: summary.info_count as usize,
 164                                hint_count: summary.hint_count as usize,
 165                            },
 166                        )
 167                    }),
 168                );
 169
 170                (entries_by_path, entries_by_id, diagnostic_summaries)
 171            })
 172            .await;
 173
 174        let worktree = cx.update(|cx| {
 175            cx.add_model(|cx: &mut ModelContext<Worktree>| {
 176                let snapshot = Snapshot {
 177                    id: WorktreeId(remote_id as usize),
 178                    scan_id: 0,
 179                    abs_path: Path::new("").into(),
 180                    root_name,
 181                    root_char_bag,
 182                    ignores: Default::default(),
 183                    entries_by_path,
 184                    entries_by_id,
 185                    removed_entry_ids: Default::default(),
 186                    next_entry_id: Default::default(),
 187                };
 188
 189                let (updates_tx, mut updates_rx) = postage::mpsc::channel(64);
 190                let (mut snapshot_tx, snapshot_rx) = watch::channel_with(snapshot.clone());
 191
 192                cx.background()
 193                    .spawn(async move {
 194                        while let Some(update) = updates_rx.recv().await {
 195                            let mut snapshot = snapshot_tx.borrow().clone();
 196                            if let Err(error) = snapshot.apply_update(update) {
 197                                log::error!("error applying worktree update: {}", error);
 198                            }
 199                            *snapshot_tx.borrow_mut() = snapshot;
 200                        }
 201                    })
 202                    .detach();
 203
 204                {
 205                    let mut snapshot_rx = snapshot_rx.clone();
 206                    cx.spawn_weak(|this, mut cx| async move {
 207                        while let Some(_) = snapshot_rx.recv().await {
 208                            if let Some(this) = cx.read(|cx| this.upgrade(cx)) {
 209                                this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
 210                            } else {
 211                                break;
 212                            }
 213                        }
 214                    })
 215                    .detach();
 216                }
 217
 218                Worktree::Remote(RemoteWorktree {
 219                    project_id: project_remote_id,
 220                    replica_id,
 221                    snapshot,
 222                    snapshot_rx,
 223                    updates_tx,
 224                    client: client.clone(),
 225                    loading_buffers: Default::default(),
 226                    open_buffers: Default::default(),
 227                    queued_operations: Default::default(),
 228                    user_store,
 229                    diagnostic_summaries,
 230                    weak,
 231                })
 232            })
 233        });
 234
 235        Ok(worktree)
 236    }
 237
 238    pub fn as_local(&self) -> Option<&LocalWorktree> {
 239        if let Worktree::Local(worktree) = self {
 240            Some(worktree)
 241        } else {
 242            None
 243        }
 244    }
 245
 246    pub fn as_remote(&self) -> Option<&RemoteWorktree> {
 247        if let Worktree::Remote(worktree) = self {
 248            Some(worktree)
 249        } else {
 250            None
 251        }
 252    }
 253
 254    pub fn as_local_mut(&mut self) -> Option<&mut LocalWorktree> {
 255        if let Worktree::Local(worktree) = self {
 256            Some(worktree)
 257        } else {
 258            None
 259        }
 260    }
 261
 262    pub fn as_remote_mut(&mut self) -> Option<&mut RemoteWorktree> {
 263        if let Worktree::Remote(worktree) = self {
 264            Some(worktree)
 265        } else {
 266            None
 267        }
 268    }
 269
 270    pub fn is_local(&self) -> bool {
 271        matches!(self, Worktree::Local(_))
 272    }
 273
 274    pub fn snapshot(&self) -> Snapshot {
 275        match self {
 276            Worktree::Local(worktree) => worktree.snapshot(),
 277            Worktree::Remote(worktree) => worktree.snapshot(),
 278        }
 279    }
 280
 281    pub fn is_weak(&self) -> bool {
 282        match self {
 283            Worktree::Local(worktree) => worktree.weak,
 284            Worktree::Remote(worktree) => worktree.weak,
 285        }
 286    }
 287
 288    pub fn replica_id(&self) -> ReplicaId {
 289        match self {
 290            Worktree::Local(_) => 0,
 291            Worktree::Remote(worktree) => worktree.replica_id,
 292        }
 293    }
 294
 295    pub fn remove_collaborator(
 296        &mut self,
 297        peer_id: PeerId,
 298        replica_id: ReplicaId,
 299        cx: &mut ModelContext<Self>,
 300    ) {
 301        match self {
 302            Worktree::Local(worktree) => worktree.remove_collaborator(peer_id, replica_id, cx),
 303            Worktree::Remote(worktree) => worktree.remove_collaborator(replica_id, cx),
 304        }
 305    }
 306
 307    pub fn user_store(&self) -> &ModelHandle<UserStore> {
 308        match self {
 309            Worktree::Local(worktree) => &worktree.user_store,
 310            Worktree::Remote(worktree) => &worktree.user_store,
 311        }
 312    }
 313
 314    pub fn diagnostic_summaries<'a>(
 315        &'a self,
 316    ) -> impl Iterator<Item = (Arc<Path>, DiagnosticSummary)> + 'a {
 317        match self {
 318            Worktree::Local(worktree) => &worktree.diagnostic_summaries,
 319            Worktree::Remote(worktree) => &worktree.diagnostic_summaries,
 320        }
 321        .iter()
 322        .map(|(path, summary)| (path.0.clone(), summary.clone()))
 323    }
 324
 325    pub(crate) fn loading_buffers<'a>(&'a mut self) -> &'a mut LoadingBuffers {
 326        match self {
 327            Worktree::Local(worktree) => &mut worktree.loading_buffers,
 328            Worktree::Remote(worktree) => &mut worktree.loading_buffers,
 329        }
 330    }
 331
 332    pub(crate) fn open_buffer(
 333        &mut self,
 334        path: impl AsRef<Path>,
 335        cx: &mut ModelContext<Self>,
 336    ) -> Task<Result<(ModelHandle<Buffer>, bool)>> {
 337        let path = path.as_ref();
 338
 339        // If there is already a buffer for the given path, then return it.
 340        let existing_buffer = match self {
 341            Worktree::Local(worktree) => worktree.get_open_buffer(path, cx),
 342            Worktree::Remote(worktree) => worktree.get_open_buffer(path, cx),
 343        };
 344        if let Some(existing_buffer) = existing_buffer {
 345            return cx.spawn(move |_, _| async move { Ok((existing_buffer, false)) });
 346        }
 347
 348        let is_new = Arc::new(AtomicBool::new(true));
 349        let path: Arc<Path> = Arc::from(path);
 350        let mut loading_watch = match self.loading_buffers().entry(path.clone()) {
 351            // If the given path is already being loaded, then wait for that existing
 352            // task to complete and return the same buffer.
 353            hash_map::Entry::Occupied(e) => e.get().clone(),
 354
 355            // Otherwise, record the fact that this path is now being loaded.
 356            hash_map::Entry::Vacant(entry) => {
 357                let (mut tx, rx) = postage::watch::channel();
 358                entry.insert(rx.clone());
 359
 360                let load_buffer = match self {
 361                    Worktree::Local(worktree) => worktree.open_buffer(&path, cx),
 362                    Worktree::Remote(worktree) => worktree.open_buffer(&path, cx),
 363                };
 364                cx.spawn(move |this, mut cx| async move {
 365                    let result = load_buffer.await;
 366
 367                    // After the buffer loads, record the fact that it is no longer
 368                    // loading.
 369                    this.update(&mut cx, |this, _| this.loading_buffers().remove(&path));
 370                    *tx.borrow_mut() = Some(match result {
 371                        Ok(buffer) => Ok((buffer, is_new)),
 372                        Err(error) => Err(Arc::new(error)),
 373                    });
 374                })
 375                .detach();
 376                rx
 377            }
 378        };
 379
 380        cx.spawn(|_, _| async move {
 381            loop {
 382                if let Some(result) = loading_watch.borrow().as_ref() {
 383                    return match result {
 384                        Ok((buf, is_new)) => Ok((buf.clone(), is_new.fetch_and(false, SeqCst))),
 385                        Err(error) => Err(anyhow!("{}", error)),
 386                    };
 387                }
 388                loading_watch.recv().await;
 389            }
 390        })
 391    }
 392
 393    #[cfg(feature = "test-support")]
 394    pub(crate) fn has_open_buffer(&self, path: impl AsRef<Path>, cx: &AppContext) -> bool {
 395        let mut open_buffers: Box<dyn Iterator<Item = _>> = match self {
 396            Worktree::Local(worktree) => Box::new(worktree.open_buffers.values()),
 397            Worktree::Remote(worktree) => {
 398                Box::new(worktree.open_buffers.values().filter_map(|buf| {
 399                    if let RemoteBuffer::Loaded(buf) = buf {
 400                        Some(buf)
 401                    } else {
 402                        None
 403                    }
 404                }))
 405            }
 406        };
 407
 408        let path = path.as_ref();
 409        open_buffers
 410            .find(|buffer| {
 411                if let Some(file) = buffer.upgrade(cx).and_then(|buffer| buffer.read(cx).file()) {
 412                    file.path().as_ref() == path
 413                } else {
 414                    false
 415                }
 416            })
 417            .is_some()
 418    }
 419
 420    pub fn handle_update_buffer(
 421        &mut self,
 422        envelope: TypedEnvelope<proto::UpdateBuffer>,
 423        cx: &mut ModelContext<Self>,
 424    ) -> Result<()> {
 425        let payload = envelope.payload.clone();
 426        let buffer_id = payload.buffer_id as usize;
 427        let ops = payload
 428            .operations
 429            .into_iter()
 430            .map(|op| language::proto::deserialize_operation(op))
 431            .collect::<Result<Vec<_>, _>>()?;
 432
 433        match self {
 434            Worktree::Local(worktree) => {
 435                let buffer = worktree
 436                    .open_buffers
 437                    .get(&buffer_id)
 438                    .and_then(|buf| buf.upgrade(cx))
 439                    .ok_or_else(|| {
 440                        anyhow!("invalid buffer {} in update buffer message", buffer_id)
 441                    })?;
 442                buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx))?;
 443            }
 444            Worktree::Remote(worktree) => match worktree.open_buffers.get_mut(&buffer_id) {
 445                Some(RemoteBuffer::Operations(pending_ops)) => pending_ops.extend(ops),
 446                Some(RemoteBuffer::Loaded(buffer)) => {
 447                    if let Some(buffer) = buffer.upgrade(cx) {
 448                        buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx))?;
 449                    } else {
 450                        worktree
 451                            .open_buffers
 452                            .insert(buffer_id, RemoteBuffer::Operations(ops));
 453                    }
 454                }
 455                None => {
 456                    worktree
 457                        .open_buffers
 458                        .insert(buffer_id, RemoteBuffer::Operations(ops));
 459                }
 460            },
 461        }
 462
 463        Ok(())
 464    }
 465
 466    pub fn handle_save_buffer(
 467        &mut self,
 468        envelope: TypedEnvelope<proto::SaveBuffer>,
 469        rpc: Arc<Client>,
 470        cx: &mut ModelContext<Self>,
 471    ) -> Result<()> {
 472        let sender_id = envelope.original_sender_id()?;
 473        let this = self.as_local().unwrap();
 474        let project_id = this
 475            .share
 476            .as_ref()
 477            .ok_or_else(|| anyhow!("can't save buffer while disconnected"))?
 478            .project_id;
 479
 480        let buffer = this
 481            .shared_buffers
 482            .get(&sender_id)
 483            .and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
 484            .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
 485
 486        let receipt = envelope.receipt();
 487        let worktree_id = envelope.payload.worktree_id;
 488        let buffer_id = envelope.payload.buffer_id;
 489        let save = cx.spawn(|_, mut cx| async move {
 490            buffer.update(&mut cx, |buffer, cx| buffer.save(cx)).await
 491        });
 492
 493        cx.background()
 494            .spawn(
 495                async move {
 496                    let (version, mtime) = save.await?;
 497
 498                    rpc.respond(
 499                        receipt,
 500                        proto::BufferSaved {
 501                            project_id,
 502                            worktree_id,
 503                            buffer_id,
 504                            version: (&version).into(),
 505                            mtime: Some(mtime.into()),
 506                        },
 507                    )
 508                    .await?;
 509
 510                    Ok(())
 511                }
 512                .log_err(),
 513            )
 514            .detach();
 515
 516        Ok(())
 517    }
 518
 519    pub fn handle_buffer_saved(
 520        &mut self,
 521        envelope: TypedEnvelope<proto::BufferSaved>,
 522        cx: &mut ModelContext<Self>,
 523    ) -> Result<()> {
 524        let payload = envelope.payload.clone();
 525        let worktree = self.as_remote_mut().unwrap();
 526        if let Some(buffer) = worktree
 527            .open_buffers
 528            .get(&(payload.buffer_id as usize))
 529            .and_then(|buf| buf.upgrade(cx))
 530        {
 531            buffer.update(cx, |buffer, cx| {
 532                let version = payload.version.try_into()?;
 533                let mtime = payload
 534                    .mtime
 535                    .ok_or_else(|| anyhow!("missing mtime"))?
 536                    .into();
 537                buffer.did_save(version, mtime, None, cx);
 538                Result::<_, anyhow::Error>::Ok(())
 539            })?;
 540        }
 541        Ok(())
 542    }
 543
 544    pub fn handle_format_buffer(
 545        &mut self,
 546        envelope: TypedEnvelope<proto::FormatBuffer>,
 547        rpc: Arc<Client>,
 548        cx: &mut ModelContext<Self>,
 549    ) -> Result<()> {
 550        let sender_id = envelope.original_sender_id()?;
 551        let this = self.as_local().unwrap();
 552        let buffer = this
 553            .shared_buffers
 554            .get(&sender_id)
 555            .and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
 556            .ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
 557
 558        let receipt = envelope.receipt();
 559        cx.spawn(|_, mut cx| async move {
 560            let format = buffer.update(&mut cx, |buffer, cx| buffer.format(cx)).await;
 561            // We spawn here in order to enqueue the sending of `Ack` *after* transmission of edits
 562            // associated with formatting.
 563            cx.spawn(|_| async move {
 564                match format {
 565                    Ok(()) => rpc.respond(receipt, proto::Ack {}).await?,
 566                    Err(error) => {
 567                        rpc.respond_with_error(
 568                            receipt,
 569                            proto::Error {
 570                                message: error.to_string(),
 571                            },
 572                        )
 573                        .await?
 574                    }
 575                }
 576                Ok::<_, anyhow::Error>(())
 577            })
 578            .await
 579            .log_err();
 580        })
 581        .detach();
 582
 583        Ok(())
 584    }
 585
 586    fn poll_snapshot(&mut self, cx: &mut ModelContext<Self>) {
 587        match self {
 588            Self::Local(worktree) => {
 589                let is_fake_fs = worktree.fs.is_fake();
 590                worktree.snapshot = worktree.background_snapshot.lock().clone();
 591                if worktree.is_scanning() {
 592                    if worktree.poll_task.is_none() {
 593                        worktree.poll_task = Some(cx.spawn(|this, mut cx| async move {
 594                            if is_fake_fs {
 595                                smol::future::yield_now().await;
 596                            } else {
 597                                smol::Timer::after(Duration::from_millis(100)).await;
 598                            }
 599                            this.update(&mut cx, |this, cx| {
 600                                this.as_local_mut().unwrap().poll_task = None;
 601                                this.poll_snapshot(cx);
 602                            })
 603                        }));
 604                    }
 605                } else {
 606                    worktree.poll_task.take();
 607                    self.update_open_buffers(cx);
 608                }
 609            }
 610            Self::Remote(worktree) => {
 611                worktree.snapshot = worktree.snapshot_rx.borrow().clone();
 612                self.update_open_buffers(cx);
 613            }
 614        };
 615
 616        cx.notify();
 617    }
 618
 619    fn update_open_buffers(&mut self, cx: &mut ModelContext<Self>) {
 620        let open_buffers: Box<dyn Iterator<Item = _>> = match &self {
 621            Self::Local(worktree) => Box::new(worktree.open_buffers.iter()),
 622            Self::Remote(worktree) => {
 623                Box::new(worktree.open_buffers.iter().filter_map(|(id, buf)| {
 624                    if let RemoteBuffer::Loaded(buf) = buf {
 625                        Some((id, buf))
 626                    } else {
 627                        None
 628                    }
 629                }))
 630            }
 631        };
 632
 633        let local = self.as_local().is_some();
 634        let worktree_path = self.abs_path.clone();
 635        let worktree_handle = cx.handle();
 636        let mut buffers_to_delete = Vec::new();
 637        for (buffer_id, buffer) in open_buffers {
 638            if let Some(buffer) = buffer.upgrade(cx) {
 639                buffer.update(cx, |buffer, cx| {
 640                    if let Some(old_file) = File::from_dyn(buffer.file()) {
 641                        let new_file = if let Some(entry) = old_file
 642                            .entry_id
 643                            .and_then(|entry_id| self.entry_for_id(entry_id))
 644                        {
 645                            File {
 646                                is_local: local,
 647                                worktree_path: worktree_path.clone(),
 648                                entry_id: Some(entry.id),
 649                                mtime: entry.mtime,
 650                                path: entry.path.clone(),
 651                                worktree: worktree_handle.clone(),
 652                            }
 653                        } else if let Some(entry) = self.entry_for_path(old_file.path().as_ref()) {
 654                            File {
 655                                is_local: local,
 656                                worktree_path: worktree_path.clone(),
 657                                entry_id: Some(entry.id),
 658                                mtime: entry.mtime,
 659                                path: entry.path.clone(),
 660                                worktree: worktree_handle.clone(),
 661                            }
 662                        } else {
 663                            File {
 664                                is_local: local,
 665                                worktree_path: worktree_path.clone(),
 666                                entry_id: None,
 667                                path: old_file.path().clone(),
 668                                mtime: old_file.mtime(),
 669                                worktree: worktree_handle.clone(),
 670                            }
 671                        };
 672
 673                        if let Some(task) = buffer.file_updated(Box::new(new_file), cx) {
 674                            task.detach();
 675                        }
 676                    }
 677                });
 678            } else {
 679                buffers_to_delete.push(*buffer_id);
 680            }
 681        }
 682
 683        for buffer_id in buffers_to_delete {
 684            match self {
 685                Self::Local(worktree) => {
 686                    worktree.open_buffers.remove(&buffer_id);
 687                }
 688                Self::Remote(worktree) => {
 689                    worktree.open_buffers.remove(&buffer_id);
 690                }
 691            }
 692        }
 693    }
 694
 695    fn send_buffer_update(
 696        &mut self,
 697        buffer_id: u64,
 698        operation: Operation,
 699        cx: &mut ModelContext<Self>,
 700    ) {
 701        if let Some((project_id, worktree_id, rpc)) = match self {
 702            Worktree::Local(worktree) => worktree
 703                .share
 704                .as_ref()
 705                .map(|share| (share.project_id, worktree.id(), worktree.client.clone())),
 706            Worktree::Remote(worktree) => Some((
 707                worktree.project_id,
 708                worktree.snapshot.id(),
 709                worktree.client.clone(),
 710            )),
 711        } {
 712            cx.spawn(|worktree, mut cx| async move {
 713                if let Err(error) = rpc
 714                    .request(proto::UpdateBuffer {
 715                        project_id,
 716                        worktree_id: worktree_id.0 as u64,
 717                        buffer_id,
 718                        operations: vec![language::proto::serialize_operation(&operation)],
 719                    })
 720                    .await
 721                {
 722                    worktree.update(&mut cx, |worktree, _| {
 723                        log::error!("error sending buffer operation: {}", error);
 724                        match worktree {
 725                            Worktree::Local(t) => &mut t.queued_operations,
 726                            Worktree::Remote(t) => &mut t.queued_operations,
 727                        }
 728                        .push((buffer_id, operation));
 729                    });
 730                }
 731            })
 732            .detach();
 733        }
 734    }
 735}
 736
 737impl WorktreeId {
 738    pub fn from_usize(handle_id: usize) -> Self {
 739        Self(handle_id)
 740    }
 741
 742    pub(crate) fn from_proto(id: u64) -> Self {
 743        Self(id as usize)
 744    }
 745
 746    pub fn to_proto(&self) -> u64 {
 747        self.0 as u64
 748    }
 749
 750    pub fn to_usize(&self) -> usize {
 751        self.0
 752    }
 753}
 754
 755impl fmt::Display for WorktreeId {
 756    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 757        self.0.fmt(f)
 758    }
 759}
 760
 761#[derive(Clone)]
 762pub struct Snapshot {
 763    id: WorktreeId,
 764    scan_id: usize,
 765    abs_path: Arc<Path>,
 766    root_name: String,
 767    root_char_bag: CharBag,
 768    ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
 769    entries_by_path: SumTree<Entry>,
 770    entries_by_id: SumTree<PathEntry>,
 771    removed_entry_ids: HashMap<u64, usize>,
 772    next_entry_id: Arc<AtomicUsize>,
 773}
 774
 775pub struct LocalWorktree {
 776    snapshot: Snapshot,
 777    config: WorktreeConfig,
 778    background_snapshot: Arc<Mutex<Snapshot>>,
 779    last_scan_state_rx: watch::Receiver<ScanState>,
 780    _background_scanner_task: Option<Task<()>>,
 781    poll_task: Option<Task<()>>,
 782    registration: Registration,
 783    share: Option<ShareState>,
 784    loading_buffers: LoadingBuffers,
 785    open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
 786    shared_buffers: HashMap<PeerId, HashMap<u64, ModelHandle<Buffer>>>,
 787    diagnostics: HashMap<Arc<Path>, Vec<DiagnosticEntry<PointUtf16>>>,
 788    diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
 789    queued_operations: Vec<(u64, Operation)>,
 790    client: Arc<Client>,
 791    user_store: ModelHandle<UserStore>,
 792    fs: Arc<dyn Fs>,
 793    weak: bool,
 794}
 795
 796#[derive(Debug, Eq, PartialEq)]
 797enum Registration {
 798    None,
 799    Pending,
 800    Done { project_id: u64 },
 801}
 802
 803struct ShareState {
 804    project_id: u64,
 805    snapshots_tx: Sender<Snapshot>,
 806    _maintain_remote_snapshot: Option<Task<()>>,
 807}
 808
 809pub struct RemoteWorktree {
 810    project_id: u64,
 811    snapshot: Snapshot,
 812    snapshot_rx: watch::Receiver<Snapshot>,
 813    client: Arc<Client>,
 814    updates_tx: postage::mpsc::Sender<proto::UpdateWorktree>,
 815    replica_id: ReplicaId,
 816    loading_buffers: LoadingBuffers,
 817    open_buffers: HashMap<usize, RemoteBuffer>,
 818    user_store: ModelHandle<UserStore>,
 819    queued_operations: Vec<(u64, Operation)>,
 820    diagnostic_summaries: TreeMap<PathKey, DiagnosticSummary>,
 821    weak: bool,
 822}
 823
 824type LoadingBuffers = HashMap<
 825    Arc<Path>,
 826    postage::watch::Receiver<
 827        Option<Result<(ModelHandle<Buffer>, Arc<AtomicBool>), Arc<anyhow::Error>>>,
 828    >,
 829>;
 830
 831#[derive(Default, Deserialize)]
 832struct WorktreeConfig {
 833    collaborators: Vec<String>,
 834}
 835
 836impl LocalWorktree {
 837    async fn new(
 838        client: Arc<Client>,
 839        user_store: ModelHandle<UserStore>,
 840        path: impl Into<Arc<Path>>,
 841        weak: bool,
 842        fs: Arc<dyn Fs>,
 843        cx: &mut AsyncAppContext,
 844    ) -> Result<(ModelHandle<Worktree>, Sender<ScanState>)> {
 845        let abs_path = path.into();
 846        let path: Arc<Path> = Arc::from(Path::new(""));
 847        let next_entry_id = AtomicUsize::new(0);
 848
 849        // After determining whether the root entry is a file or a directory, populate the
 850        // snapshot's "root name", which will be used for the purpose of fuzzy matching.
 851        let root_name = abs_path
 852            .file_name()
 853            .map_or(String::new(), |f| f.to_string_lossy().to_string());
 854        let root_char_bag = root_name.chars().map(|c| c.to_ascii_lowercase()).collect();
 855        let metadata = fs.metadata(&abs_path).await?;
 856
 857        let mut config = WorktreeConfig::default();
 858        if let Ok(zed_toml) = fs.load(&abs_path.join(".zed.toml")).await {
 859            if let Ok(parsed) = toml::from_str(&zed_toml) {
 860                config = parsed;
 861            }
 862        }
 863
 864        let (scan_states_tx, scan_states_rx) = smol::channel::unbounded();
 865        let (mut last_scan_state_tx, last_scan_state_rx) = watch::channel_with(ScanState::Scanning);
 866        let tree = cx.add_model(move |cx: &mut ModelContext<Worktree>| {
 867            let mut snapshot = Snapshot {
 868                id: WorktreeId::from_usize(cx.model_id()),
 869                scan_id: 0,
 870                abs_path,
 871                root_name: root_name.clone(),
 872                root_char_bag,
 873                ignores: Default::default(),
 874                entries_by_path: Default::default(),
 875                entries_by_id: Default::default(),
 876                removed_entry_ids: Default::default(),
 877                next_entry_id: Arc::new(next_entry_id),
 878            };
 879            if let Some(metadata) = metadata {
 880                snapshot.insert_entry(
 881                    Entry::new(
 882                        path.into(),
 883                        &metadata,
 884                        &snapshot.next_entry_id,
 885                        snapshot.root_char_bag,
 886                    ),
 887                    fs.as_ref(),
 888                );
 889            }
 890
 891            let tree = Self {
 892                snapshot: snapshot.clone(),
 893                config,
 894                background_snapshot: Arc::new(Mutex::new(snapshot)),
 895                last_scan_state_rx,
 896                _background_scanner_task: None,
 897                registration: Registration::None,
 898                share: None,
 899                poll_task: None,
 900                loading_buffers: Default::default(),
 901                open_buffers: Default::default(),
 902                shared_buffers: Default::default(),
 903                diagnostics: Default::default(),
 904                diagnostic_summaries: Default::default(),
 905                queued_operations: Default::default(),
 906                client,
 907                user_store,
 908                fs,
 909                weak,
 910            };
 911
 912            cx.spawn_weak(|this, mut cx| async move {
 913                while let Ok(scan_state) = scan_states_rx.recv().await {
 914                    if let Some(handle) = cx.read(|cx| this.upgrade(cx)) {
 915                        let to_send = handle.update(&mut cx, |this, cx| {
 916                            last_scan_state_tx.blocking_send(scan_state).ok();
 917                            this.poll_snapshot(cx);
 918                            let tree = this.as_local_mut().unwrap();
 919                            if !tree.is_scanning() {
 920                                if let Some(share) = tree.share.as_ref() {
 921                                    return Some((tree.snapshot(), share.snapshots_tx.clone()));
 922                                }
 923                            }
 924                            None
 925                        });
 926
 927                        if let Some((snapshot, snapshots_to_send_tx)) = to_send {
 928                            if let Err(err) = snapshots_to_send_tx.send(snapshot).await {
 929                                log::error!("error submitting snapshot to send {}", err);
 930                            }
 931                        }
 932                    } else {
 933                        break;
 934                    }
 935                }
 936            })
 937            .detach();
 938
 939            Worktree::Local(tree)
 940        });
 941
 942        Ok((tree, scan_states_tx))
 943    }
 944
 945    pub fn authorized_logins(&self) -> Vec<String> {
 946        self.config.collaborators.clone()
 947    }
 948
 949    fn get_open_buffer(
 950        &mut self,
 951        path: &Path,
 952        cx: &mut ModelContext<Worktree>,
 953    ) -> Option<ModelHandle<Buffer>> {
 954        let handle = cx.handle();
 955        let mut result = None;
 956        self.open_buffers.retain(|_buffer_id, buffer| {
 957            if let Some(buffer) = buffer.upgrade(cx) {
 958                if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
 959                    if file.worktree == handle && file.path().as_ref() == path {
 960                        result = Some(buffer);
 961                    }
 962                }
 963                true
 964            } else {
 965                false
 966            }
 967        });
 968        result
 969    }
 970
 971    fn open_buffer(
 972        &mut self,
 973        path: &Path,
 974        cx: &mut ModelContext<Worktree>,
 975    ) -> Task<Result<ModelHandle<Buffer>>> {
 976        let path = Arc::from(path);
 977        cx.spawn(move |this, mut cx| async move {
 978            let (file, contents) = this
 979                .update(&mut cx, |t, cx| t.as_local().unwrap().load(&path, cx))
 980                .await?;
 981
 982            let diagnostics = this.update(&mut cx, |this, _| {
 983                this.as_local_mut().unwrap().diagnostics.get(&path).cloned()
 984            });
 985
 986            let mut buffer_operations = Vec::new();
 987            let buffer = cx.add_model(|cx| {
 988                let mut buffer = Buffer::from_file(0, contents, Box::new(file), cx);
 989                if let Some(diagnostics) = diagnostics {
 990                    let op = buffer.update_diagnostics(None, diagnostics, cx).unwrap();
 991                    buffer_operations.push(op);
 992                }
 993                buffer
 994            });
 995
 996            this.update(&mut cx, |this, cx| {
 997                for op in buffer_operations {
 998                    this.send_buffer_update(buffer.read(cx).remote_id(), op, cx);
 999                }
1000                let this = this.as_local_mut().unwrap();
1001                this.open_buffers.insert(buffer.id(), buffer.downgrade());
1002            });
1003
1004            Ok(buffer)
1005        })
1006    }
1007
1008    pub fn open_remote_buffer(
1009        &mut self,
1010        peer_id: PeerId,
1011        buffer: ModelHandle<Buffer>,
1012        cx: &mut ModelContext<Worktree>,
1013    ) -> proto::OpenBufferResponse {
1014        self.shared_buffers
1015            .entry(peer_id)
1016            .or_default()
1017            .insert(buffer.id() as u64, buffer.clone());
1018        proto::OpenBufferResponse {
1019            buffer: Some(buffer.update(cx.as_mut(), |buffer, _| buffer.to_proto())),
1020        }
1021    }
1022
1023    pub fn close_remote_buffer(
1024        &mut self,
1025        envelope: TypedEnvelope<proto::CloseBuffer>,
1026        cx: &mut ModelContext<Worktree>,
1027    ) -> Result<()> {
1028        if let Some(shared_buffers) = self.shared_buffers.get_mut(&envelope.original_sender_id()?) {
1029            shared_buffers.remove(&envelope.payload.buffer_id);
1030            cx.notify();
1031        }
1032
1033        Ok(())
1034    }
1035
1036    pub fn remove_collaborator(
1037        &mut self,
1038        peer_id: PeerId,
1039        replica_id: ReplicaId,
1040        cx: &mut ModelContext<Worktree>,
1041    ) {
1042        self.shared_buffers.remove(&peer_id);
1043        for (_, buffer) in &self.open_buffers {
1044            if let Some(buffer) = buffer.upgrade(cx) {
1045                buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
1046            }
1047        }
1048        cx.notify();
1049    }
1050
1051    pub fn update_diagnostics(
1052        &mut self,
1053        worktree_path: Arc<Path>,
1054        params: lsp::PublishDiagnosticsParams,
1055        disk_based_sources: &HashSet<String>,
1056        cx: &mut ModelContext<Worktree>,
1057    ) -> Result<()> {
1058        let mut next_group_id = 0;
1059        let mut diagnostics = Vec::default();
1060        let mut primary_diagnostic_group_ids = HashMap::default();
1061        let mut sources_by_group_id = HashMap::default();
1062        let mut supporting_diagnostic_severities = HashMap::default();
1063        for diagnostic in &params.diagnostics {
1064            let source = diagnostic.source.as_ref();
1065            let code = diagnostic.code.as_ref().map(|code| match code {
1066                lsp::NumberOrString::Number(code) => code.to_string(),
1067                lsp::NumberOrString::String(code) => code.clone(),
1068            });
1069            let range = range_from_lsp(diagnostic.range);
1070            let is_supporting = diagnostic
1071                .related_information
1072                .as_ref()
1073                .map_or(false, |infos| {
1074                    infos.iter().any(|info| {
1075                        primary_diagnostic_group_ids.contains_key(&(
1076                            source,
1077                            code.clone(),
1078                            range_from_lsp(info.location.range),
1079                        ))
1080                    })
1081                });
1082
1083            if is_supporting {
1084                if let Some(severity) = diagnostic.severity {
1085                    supporting_diagnostic_severities
1086                        .insert((source, code.clone(), range), severity);
1087                }
1088            } else {
1089                let group_id = post_inc(&mut next_group_id);
1090                let is_disk_based =
1091                    source.map_or(false, |source| disk_based_sources.contains(source));
1092
1093                sources_by_group_id.insert(group_id, source);
1094                primary_diagnostic_group_ids
1095                    .insert((source, code.clone(), range.clone()), group_id);
1096
1097                diagnostics.push(DiagnosticEntry {
1098                    range,
1099                    diagnostic: Diagnostic {
1100                        code: code.clone(),
1101                        severity: diagnostic.severity.unwrap_or(DiagnosticSeverity::ERROR),
1102                        message: diagnostic.message.clone(),
1103                        group_id,
1104                        is_primary: true,
1105                        is_valid: true,
1106                        is_disk_based,
1107                    },
1108                });
1109                if let Some(infos) = &diagnostic.related_information {
1110                    for info in infos {
1111                        if info.location.uri == params.uri {
1112                            let range = range_from_lsp(info.location.range);
1113                            diagnostics.push(DiagnosticEntry {
1114                                range,
1115                                diagnostic: Diagnostic {
1116                                    code: code.clone(),
1117                                    severity: DiagnosticSeverity::INFORMATION,
1118                                    message: info.message.clone(),
1119                                    group_id,
1120                                    is_primary: false,
1121                                    is_valid: true,
1122                                    is_disk_based,
1123                                },
1124                            });
1125                        }
1126                    }
1127                }
1128            }
1129        }
1130
1131        for entry in &mut diagnostics {
1132            let diagnostic = &mut entry.diagnostic;
1133            if !diagnostic.is_primary {
1134                let source = *sources_by_group_id.get(&diagnostic.group_id).unwrap();
1135                if let Some(&severity) = supporting_diagnostic_severities.get(&(
1136                    source,
1137                    diagnostic.code.clone(),
1138                    entry.range.clone(),
1139                )) {
1140                    diagnostic.severity = severity;
1141                }
1142            }
1143        }
1144
1145        self.update_diagnostic_entries(worktree_path, params.version, diagnostics, cx)?;
1146        Ok(())
1147    }
1148
1149    pub fn update_diagnostic_entries(
1150        &mut self,
1151        worktree_path: Arc<Path>,
1152        version: Option<i32>,
1153        diagnostics: Vec<DiagnosticEntry<PointUtf16>>,
1154        cx: &mut ModelContext<Worktree>,
1155    ) -> Result<()> {
1156        for buffer in self.open_buffers.values() {
1157            if let Some(buffer) = buffer.upgrade(cx) {
1158                if buffer
1159                    .read(cx)
1160                    .file()
1161                    .map_or(false, |file| *file.path() == worktree_path)
1162                {
1163                    let (remote_id, operation) = buffer.update(cx, |buffer, cx| {
1164                        (
1165                            buffer.remote_id(),
1166                            buffer.update_diagnostics(version, diagnostics.clone(), cx),
1167                        )
1168                    });
1169                    self.send_buffer_update(remote_id, operation?, cx);
1170                    break;
1171                }
1172            }
1173        }
1174
1175        let summary = DiagnosticSummary::new(&diagnostics);
1176        self.diagnostic_summaries
1177            .insert(PathKey(worktree_path.clone()), summary.clone());
1178        self.diagnostics.insert(worktree_path.clone(), diagnostics);
1179
1180        if let Some(share) = self.share.as_ref() {
1181            cx.foreground()
1182                .spawn({
1183                    let client = self.client.clone();
1184                    let project_id = share.project_id;
1185                    let worktree_id = self.id().to_proto();
1186                    let path = worktree_path.to_string_lossy().to_string();
1187                    async move {
1188                        client
1189                            .send(proto::UpdateDiagnosticSummary {
1190                                project_id,
1191                                worktree_id,
1192                                summary: Some(proto::DiagnosticSummary {
1193                                    path,
1194                                    error_count: summary.error_count as u32,
1195                                    warning_count: summary.warning_count as u32,
1196                                    info_count: summary.info_count as u32,
1197                                    hint_count: summary.hint_count as u32,
1198                                }),
1199                            })
1200                            .await
1201                            .log_err()
1202                    }
1203                })
1204                .detach();
1205        }
1206
1207        Ok(())
1208    }
1209
1210    fn send_buffer_update(
1211        &mut self,
1212        buffer_id: u64,
1213        operation: Operation,
1214        cx: &mut ModelContext<Worktree>,
1215    ) -> Option<()> {
1216        let share = self.share.as_ref()?;
1217        let project_id = share.project_id;
1218        let worktree_id = self.id();
1219        let rpc = self.client.clone();
1220        cx.spawn(|worktree, mut cx| async move {
1221            if let Err(error) = rpc
1222                .request(proto::UpdateBuffer {
1223                    project_id,
1224                    worktree_id: worktree_id.0 as u64,
1225                    buffer_id,
1226                    operations: vec![language::proto::serialize_operation(&operation)],
1227                })
1228                .await
1229            {
1230                worktree.update(&mut cx, |worktree, _| {
1231                    log::error!("error sending buffer operation: {}", error);
1232                    worktree
1233                        .as_local_mut()
1234                        .unwrap()
1235                        .queued_operations
1236                        .push((buffer_id, operation));
1237                });
1238            }
1239        })
1240        .detach();
1241        None
1242    }
1243
1244    pub fn scan_complete(&self) -> impl Future<Output = ()> {
1245        let mut scan_state_rx = self.last_scan_state_rx.clone();
1246        async move {
1247            let mut scan_state = Some(scan_state_rx.borrow().clone());
1248            while let Some(ScanState::Scanning) = scan_state {
1249                scan_state = scan_state_rx.recv().await;
1250            }
1251        }
1252    }
1253
1254    fn is_scanning(&self) -> bool {
1255        if let ScanState::Scanning = *self.last_scan_state_rx.borrow() {
1256            true
1257        } else {
1258            false
1259        }
1260    }
1261
1262    pub fn snapshot(&self) -> Snapshot {
1263        self.snapshot.clone()
1264    }
1265
1266    pub fn abs_path(&self) -> &Arc<Path> {
1267        &self.snapshot.abs_path
1268    }
1269
1270    pub fn contains_abs_path(&self, path: &Path) -> bool {
1271        path.starts_with(&self.snapshot.abs_path)
1272    }
1273
1274    fn absolutize(&self, path: &Path) -> PathBuf {
1275        if path.file_name().is_some() {
1276            self.snapshot.abs_path.join(path)
1277        } else {
1278            self.snapshot.abs_path.to_path_buf()
1279        }
1280    }
1281
1282    fn load(&self, path: &Path, cx: &mut ModelContext<Worktree>) -> Task<Result<(File, String)>> {
1283        let handle = cx.handle();
1284        let path = Arc::from(path);
1285        let worktree_path = self.abs_path.clone();
1286        let abs_path = self.absolutize(&path);
1287        let background_snapshot = self.background_snapshot.clone();
1288        let fs = self.fs.clone();
1289        cx.spawn(|this, mut cx| async move {
1290            let text = fs.load(&abs_path).await?;
1291            // Eagerly populate the snapshot with an updated entry for the loaded file
1292            let entry = refresh_entry(fs.as_ref(), &background_snapshot, path, &abs_path).await?;
1293            this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
1294            Ok((
1295                File {
1296                    entry_id: Some(entry.id),
1297                    worktree: handle,
1298                    worktree_path,
1299                    path: entry.path,
1300                    mtime: entry.mtime,
1301                    is_local: true,
1302                },
1303                text,
1304            ))
1305        })
1306    }
1307
1308    pub fn save_buffer_as(
1309        &self,
1310        buffer_handle: ModelHandle<Buffer>,
1311        path: impl Into<Arc<Path>>,
1312        cx: &mut ModelContext<Worktree>,
1313    ) -> Task<Result<()>> {
1314        let buffer = buffer_handle.read(cx);
1315        let text = buffer.as_rope().clone();
1316        let version = buffer.version();
1317        let save = self.save(path, text, cx);
1318        cx.spawn(|this, mut cx| async move {
1319            let entry = save.await?;
1320            let file = this.update(&mut cx, |this, cx| {
1321                let this = this.as_local_mut().unwrap();
1322                this.open_buffers
1323                    .insert(buffer_handle.id(), buffer_handle.downgrade());
1324                File {
1325                    entry_id: Some(entry.id),
1326                    worktree: cx.handle(),
1327                    worktree_path: this.abs_path.clone(),
1328                    path: entry.path,
1329                    mtime: entry.mtime,
1330                    is_local: true,
1331                }
1332            });
1333
1334            buffer_handle.update(&mut cx, |buffer, cx| {
1335                buffer.did_save(version, file.mtime, Some(Box::new(file)), cx);
1336            });
1337
1338            Ok(())
1339        })
1340    }
1341
1342    fn save(
1343        &self,
1344        path: impl Into<Arc<Path>>,
1345        text: Rope,
1346        cx: &mut ModelContext<Worktree>,
1347    ) -> Task<Result<Entry>> {
1348        let path = path.into();
1349        let abs_path = self.absolutize(&path);
1350        let background_snapshot = self.background_snapshot.clone();
1351        let fs = self.fs.clone();
1352        let save = cx.background().spawn(async move {
1353            fs.save(&abs_path, &text).await?;
1354            refresh_entry(fs.as_ref(), &background_snapshot, path.clone(), &abs_path).await
1355        });
1356
1357        cx.spawn(|this, mut cx| async move {
1358            let entry = save.await?;
1359            this.update(&mut cx, |this, cx| this.poll_snapshot(cx));
1360            Ok(entry)
1361        })
1362    }
1363
1364    pub fn register(
1365        &mut self,
1366        project_id: u64,
1367        cx: &mut ModelContext<Worktree>,
1368    ) -> Task<anyhow::Result<()>> {
1369        if self.registration != Registration::None {
1370            return Task::ready(Ok(()));
1371        }
1372
1373        self.registration = Registration::Pending;
1374        let client = self.client.clone();
1375        let register_message = proto::RegisterWorktree {
1376            project_id,
1377            worktree_id: self.id().to_proto(),
1378            root_name: self.root_name().to_string(),
1379            authorized_logins: self.authorized_logins(),
1380        };
1381        cx.spawn(|this, mut cx| async move {
1382            let response = client.request(register_message).await;
1383            this.update(&mut cx, |this, _| {
1384                let worktree = this.as_local_mut().unwrap();
1385                match response {
1386                    Ok(_) => {
1387                        worktree.registration = Registration::Done { project_id };
1388                        Ok(())
1389                    }
1390                    Err(error) => {
1391                        worktree.registration = Registration::None;
1392                        Err(error)
1393                    }
1394                }
1395            })
1396        })
1397    }
1398
1399    pub fn share(&mut self, cx: &mut ModelContext<Worktree>) -> Task<anyhow::Result<()>> {
1400        let project_id = if let Registration::Done { project_id } = self.registration {
1401            project_id
1402        } else {
1403            return Task::ready(Err(anyhow!("cannot share worktree before registering it")));
1404        };
1405
1406        if self.share.is_some() {
1407            return Task::ready(Ok(()));
1408        }
1409
1410        let snapshot = self.snapshot();
1411        let rpc = self.client.clone();
1412        let worktree_id = cx.model_id() as u64;
1413        let (snapshots_to_send_tx, snapshots_to_send_rx) = smol::channel::unbounded::<Snapshot>();
1414        let maintain_remote_snapshot = cx.background().spawn({
1415            let rpc = rpc.clone();
1416            let snapshot = snapshot.clone();
1417            async move {
1418                let mut prev_snapshot = snapshot;
1419                while let Ok(snapshot) = snapshots_to_send_rx.recv().await {
1420                    let message =
1421                        snapshot.build_update(&prev_snapshot, project_id, worktree_id, false);
1422                    match rpc.send(message).await {
1423                        Ok(()) => prev_snapshot = snapshot,
1424                        Err(err) => log::error!("error sending snapshot diff {}", err),
1425                    }
1426                }
1427            }
1428        });
1429        self.share = Some(ShareState {
1430            project_id,
1431            snapshots_tx: snapshots_to_send_tx,
1432            _maintain_remote_snapshot: Some(maintain_remote_snapshot),
1433        });
1434
1435        let diagnostic_summaries = self.diagnostic_summaries.clone();
1436        let weak = self.weak;
1437        let share_message = cx.background().spawn(async move {
1438            proto::ShareWorktree {
1439                project_id,
1440                worktree: Some(snapshot.to_proto(&diagnostic_summaries, weak)),
1441            }
1442        });
1443
1444        cx.foreground().spawn(async move {
1445            rpc.request(share_message.await).await?;
1446            Ok(())
1447        })
1448    }
1449
1450    pub fn unshare(&mut self) {
1451        self.share.take();
1452    }
1453
1454    pub fn is_shared(&self) -> bool {
1455        self.share.is_some()
1456    }
1457}
1458
1459fn build_gitignore(abs_path: &Path, fs: &dyn Fs) -> Result<Gitignore> {
1460    let contents = smol::block_on(fs.load(&abs_path))?;
1461    let parent = abs_path.parent().unwrap_or(Path::new("/"));
1462    let mut builder = GitignoreBuilder::new(parent);
1463    for line in contents.lines() {
1464        builder.add_line(Some(abs_path.into()), line)?;
1465    }
1466    Ok(builder.build()?)
1467}
1468
1469impl Deref for Worktree {
1470    type Target = Snapshot;
1471
1472    fn deref(&self) -> &Self::Target {
1473        match self {
1474            Worktree::Local(worktree) => &worktree.snapshot,
1475            Worktree::Remote(worktree) => &worktree.snapshot,
1476        }
1477    }
1478}
1479
1480impl Deref for LocalWorktree {
1481    type Target = Snapshot;
1482
1483    fn deref(&self) -> &Self::Target {
1484        &self.snapshot
1485    }
1486}
1487
1488impl Deref for RemoteWorktree {
1489    type Target = Snapshot;
1490
1491    fn deref(&self) -> &Self::Target {
1492        &self.snapshot
1493    }
1494}
1495
1496impl fmt::Debug for LocalWorktree {
1497    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1498        self.snapshot.fmt(f)
1499    }
1500}
1501
1502impl RemoteWorktree {
1503    fn get_open_buffer(
1504        &mut self,
1505        path: &Path,
1506        cx: &mut ModelContext<Worktree>,
1507    ) -> Option<ModelHandle<Buffer>> {
1508        let handle = cx.handle();
1509        let mut existing_buffer = None;
1510        self.open_buffers.retain(|_buffer_id, buffer| {
1511            if let Some(buffer) = buffer.upgrade(cx.as_ref()) {
1512                if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
1513                    if file.worktree == handle && file.path().as_ref() == path {
1514                        existing_buffer = Some(buffer);
1515                    }
1516                }
1517                true
1518            } else {
1519                false
1520            }
1521        });
1522        existing_buffer
1523    }
1524
1525    fn open_buffer(
1526        &mut self,
1527        path: &Path,
1528        cx: &mut ModelContext<Worktree>,
1529    ) -> Task<Result<ModelHandle<Buffer>>> {
1530        let rpc = self.client.clone();
1531        let replica_id = self.replica_id;
1532        let project_id = self.project_id;
1533        let remote_worktree_id = self.id();
1534        let root_path = self.snapshot.abs_path.clone();
1535        let path: Arc<Path> = Arc::from(path);
1536        let path_string = path.to_string_lossy().to_string();
1537        cx.spawn_weak(move |this, mut cx| async move {
1538            let entry = this
1539                .upgrade(&cx)
1540                .ok_or_else(|| anyhow!("worktree was closed"))?
1541                .read_with(&cx, |tree, _| tree.entry_for_path(&path).cloned())
1542                .ok_or_else(|| anyhow!("file does not exist"))?;
1543            let response = rpc
1544                .request(proto::OpenBuffer {
1545                    project_id,
1546                    worktree_id: remote_worktree_id.to_proto(),
1547                    path: path_string,
1548                })
1549                .await?;
1550
1551            let this = this
1552                .upgrade(&cx)
1553                .ok_or_else(|| anyhow!("worktree was closed"))?;
1554            let file = File {
1555                entry_id: Some(entry.id),
1556                worktree: this.clone(),
1557                worktree_path: root_path,
1558                path: entry.path,
1559                mtime: entry.mtime,
1560                is_local: false,
1561            };
1562            let remote_buffer = response.buffer.ok_or_else(|| anyhow!("empty buffer"))?;
1563            let buffer_id = remote_buffer.id as usize;
1564            let buffer = cx.add_model(|cx| {
1565                Buffer::from_proto(replica_id, remote_buffer, Some(Box::new(file)), cx).unwrap()
1566            });
1567            this.update(&mut cx, move |this, cx| {
1568                let this = this.as_remote_mut().unwrap();
1569                if let Some(RemoteBuffer::Operations(pending_ops)) = this
1570                    .open_buffers
1571                    .insert(buffer_id, RemoteBuffer::Loaded(buffer.downgrade()))
1572                {
1573                    buffer.update(cx, |buf, cx| buf.apply_ops(pending_ops, cx))?;
1574                }
1575                Result::<_, anyhow::Error>::Ok(buffer)
1576            })
1577        })
1578    }
1579
1580    fn snapshot(&self) -> Snapshot {
1581        self.snapshot.clone()
1582    }
1583
1584    pub fn update_from_remote(
1585        &mut self,
1586        envelope: TypedEnvelope<proto::UpdateWorktree>,
1587        cx: &mut ModelContext<Worktree>,
1588    ) -> Result<()> {
1589        let mut tx = self.updates_tx.clone();
1590        let payload = envelope.payload.clone();
1591        cx.background()
1592            .spawn(async move {
1593                tx.send(payload).await.expect("receiver runs to completion");
1594            })
1595            .detach();
1596
1597        Ok(())
1598    }
1599
1600    pub fn update_diagnostic_summary(
1601        &mut self,
1602        path: Arc<Path>,
1603        summary: &proto::DiagnosticSummary,
1604    ) {
1605        self.diagnostic_summaries.insert(
1606            PathKey(path.clone()),
1607            DiagnosticSummary {
1608                error_count: summary.error_count as usize,
1609                warning_count: summary.warning_count as usize,
1610                info_count: summary.info_count as usize,
1611                hint_count: summary.hint_count as usize,
1612            },
1613        );
1614    }
1615
1616    pub fn remove_collaborator(&mut self, replica_id: ReplicaId, cx: &mut ModelContext<Worktree>) {
1617        for (_, buffer) in &self.open_buffers {
1618            if let Some(buffer) = buffer.upgrade(cx) {
1619                buffer.update(cx, |buffer, cx| buffer.remove_peer(replica_id, cx));
1620            }
1621        }
1622        cx.notify();
1623    }
1624}
1625
1626enum RemoteBuffer {
1627    Operations(Vec<Operation>),
1628    Loaded(WeakModelHandle<Buffer>),
1629}
1630
1631impl RemoteBuffer {
1632    fn upgrade(&self, cx: &impl UpgradeModelHandle) -> Option<ModelHandle<Buffer>> {
1633        match self {
1634            Self::Operations(_) => None,
1635            Self::Loaded(buffer) => buffer.upgrade(cx),
1636        }
1637    }
1638}
1639
1640impl Snapshot {
1641    pub fn id(&self) -> WorktreeId {
1642        self.id
1643    }
1644
1645    pub fn to_proto(
1646        &self,
1647        diagnostic_summaries: &TreeMap<PathKey, DiagnosticSummary>,
1648        weak: bool,
1649    ) -> proto::Worktree {
1650        let root_name = self.root_name.clone();
1651        proto::Worktree {
1652            id: self.id.0 as u64,
1653            root_name,
1654            entries: self
1655                .entries_by_path
1656                .iter()
1657                .filter(|e| !e.is_ignored)
1658                .map(Into::into)
1659                .collect(),
1660            diagnostic_summaries: diagnostic_summaries
1661                .iter()
1662                .map(|(path, summary)| summary.to_proto(path.0.clone()))
1663                .collect(),
1664            weak,
1665        }
1666    }
1667
1668    pub fn build_update(
1669        &self,
1670        other: &Self,
1671        project_id: u64,
1672        worktree_id: u64,
1673        include_ignored: bool,
1674    ) -> proto::UpdateWorktree {
1675        let mut updated_entries = Vec::new();
1676        let mut removed_entries = Vec::new();
1677        let mut self_entries = self
1678            .entries_by_id
1679            .cursor::<()>()
1680            .filter(|e| include_ignored || !e.is_ignored)
1681            .peekable();
1682        let mut other_entries = other
1683            .entries_by_id
1684            .cursor::<()>()
1685            .filter(|e| include_ignored || !e.is_ignored)
1686            .peekable();
1687        loop {
1688            match (self_entries.peek(), other_entries.peek()) {
1689                (Some(self_entry), Some(other_entry)) => {
1690                    match Ord::cmp(&self_entry.id, &other_entry.id) {
1691                        Ordering::Less => {
1692                            let entry = self.entry_for_id(self_entry.id).unwrap().into();
1693                            updated_entries.push(entry);
1694                            self_entries.next();
1695                        }
1696                        Ordering::Equal => {
1697                            if self_entry.scan_id != other_entry.scan_id {
1698                                let entry = self.entry_for_id(self_entry.id).unwrap().into();
1699                                updated_entries.push(entry);
1700                            }
1701
1702                            self_entries.next();
1703                            other_entries.next();
1704                        }
1705                        Ordering::Greater => {
1706                            removed_entries.push(other_entry.id as u64);
1707                            other_entries.next();
1708                        }
1709                    }
1710                }
1711                (Some(self_entry), None) => {
1712                    let entry = self.entry_for_id(self_entry.id).unwrap().into();
1713                    updated_entries.push(entry);
1714                    self_entries.next();
1715                }
1716                (None, Some(other_entry)) => {
1717                    removed_entries.push(other_entry.id as u64);
1718                    other_entries.next();
1719                }
1720                (None, None) => break,
1721            }
1722        }
1723
1724        proto::UpdateWorktree {
1725            project_id,
1726            worktree_id,
1727            root_name: self.root_name().to_string(),
1728            updated_entries,
1729            removed_entries,
1730        }
1731    }
1732
1733    fn apply_update(&mut self, update: proto::UpdateWorktree) -> Result<()> {
1734        self.scan_id += 1;
1735        let scan_id = self.scan_id;
1736
1737        let mut entries_by_path_edits = Vec::new();
1738        let mut entries_by_id_edits = Vec::new();
1739        for entry_id in update.removed_entries {
1740            let entry_id = entry_id as usize;
1741            let entry = self
1742                .entry_for_id(entry_id)
1743                .ok_or_else(|| anyhow!("unknown entry"))?;
1744            entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
1745            entries_by_id_edits.push(Edit::Remove(entry.id));
1746        }
1747
1748        for entry in update.updated_entries {
1749            let entry = Entry::try_from((&self.root_char_bag, entry))?;
1750            if let Some(PathEntry { path, .. }) = self.entries_by_id.get(&entry.id, &()) {
1751                entries_by_path_edits.push(Edit::Remove(PathKey(path.clone())));
1752            }
1753            entries_by_id_edits.push(Edit::Insert(PathEntry {
1754                id: entry.id,
1755                path: entry.path.clone(),
1756                is_ignored: entry.is_ignored,
1757                scan_id,
1758            }));
1759            entries_by_path_edits.push(Edit::Insert(entry));
1760        }
1761
1762        self.entries_by_path.edit(entries_by_path_edits, &());
1763        self.entries_by_id.edit(entries_by_id_edits, &());
1764
1765        Ok(())
1766    }
1767
1768    pub fn file_count(&self) -> usize {
1769        self.entries_by_path.summary().file_count
1770    }
1771
1772    pub fn visible_file_count(&self) -> usize {
1773        self.entries_by_path.summary().visible_file_count
1774    }
1775
1776    fn traverse_from_offset(
1777        &self,
1778        include_dirs: bool,
1779        include_ignored: bool,
1780        start_offset: usize,
1781    ) -> Traversal {
1782        let mut cursor = self.entries_by_path.cursor();
1783        cursor.seek(
1784            &TraversalTarget::Count {
1785                count: start_offset,
1786                include_dirs,
1787                include_ignored,
1788            },
1789            Bias::Right,
1790            &(),
1791        );
1792        Traversal {
1793            cursor,
1794            include_dirs,
1795            include_ignored,
1796        }
1797    }
1798
1799    fn traverse_from_path(
1800        &self,
1801        include_dirs: bool,
1802        include_ignored: bool,
1803        path: &Path,
1804    ) -> Traversal {
1805        let mut cursor = self.entries_by_path.cursor();
1806        cursor.seek(&TraversalTarget::Path(path), Bias::Left, &());
1807        Traversal {
1808            cursor,
1809            include_dirs,
1810            include_ignored,
1811        }
1812    }
1813
1814    pub fn files(&self, include_ignored: bool, start: usize) -> Traversal {
1815        self.traverse_from_offset(false, include_ignored, start)
1816    }
1817
1818    pub fn entries(&self, include_ignored: bool) -> Traversal {
1819        self.traverse_from_offset(true, include_ignored, 0)
1820    }
1821
1822    pub fn paths(&self) -> impl Iterator<Item = &Arc<Path>> {
1823        let empty_path = Path::new("");
1824        self.entries_by_path
1825            .cursor::<()>()
1826            .filter(move |entry| entry.path.as_ref() != empty_path)
1827            .map(|entry| &entry.path)
1828    }
1829
1830    fn child_entries<'a>(&'a self, parent_path: &'a Path) -> ChildEntriesIter<'a> {
1831        let mut cursor = self.entries_by_path.cursor();
1832        cursor.seek(&TraversalTarget::Path(parent_path), Bias::Right, &());
1833        let traversal = Traversal {
1834            cursor,
1835            include_dirs: true,
1836            include_ignored: true,
1837        };
1838        ChildEntriesIter {
1839            traversal,
1840            parent_path,
1841        }
1842    }
1843
1844    pub fn root_entry(&self) -> Option<&Entry> {
1845        self.entry_for_path("")
1846    }
1847
1848    pub fn root_name(&self) -> &str {
1849        &self.root_name
1850    }
1851
1852    pub fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
1853        let path = path.as_ref();
1854        self.traverse_from_path(true, true, path)
1855            .entry()
1856            .and_then(|entry| {
1857                if entry.path.as_ref() == path {
1858                    Some(entry)
1859                } else {
1860                    None
1861                }
1862            })
1863    }
1864
1865    pub fn entry_for_id(&self, id: usize) -> Option<&Entry> {
1866        let entry = self.entries_by_id.get(&id, &())?;
1867        self.entry_for_path(&entry.path)
1868    }
1869
1870    pub fn inode_for_path(&self, path: impl AsRef<Path>) -> Option<u64> {
1871        self.entry_for_path(path.as_ref()).map(|e| e.inode)
1872    }
1873
1874    fn insert_entry(&mut self, mut entry: Entry, fs: &dyn Fs) -> Entry {
1875        if !entry.is_dir() && entry.path.file_name() == Some(&GITIGNORE) {
1876            let abs_path = self.abs_path.join(&entry.path);
1877            match build_gitignore(&abs_path, fs) {
1878                Ok(ignore) => {
1879                    let ignore_dir_path = entry.path.parent().unwrap();
1880                    self.ignores
1881                        .insert(ignore_dir_path.into(), (Arc::new(ignore), self.scan_id));
1882                }
1883                Err(error) => {
1884                    log::error!(
1885                        "error loading .gitignore file {:?} - {:?}",
1886                        &entry.path,
1887                        error
1888                    );
1889                }
1890            }
1891        }
1892
1893        self.reuse_entry_id(&mut entry);
1894        self.entries_by_path.insert_or_replace(entry.clone(), &());
1895        self.entries_by_id.insert_or_replace(
1896            PathEntry {
1897                id: entry.id,
1898                path: entry.path.clone(),
1899                is_ignored: entry.is_ignored,
1900                scan_id: self.scan_id,
1901            },
1902            &(),
1903        );
1904        entry
1905    }
1906
1907    fn populate_dir(
1908        &mut self,
1909        parent_path: Arc<Path>,
1910        entries: impl IntoIterator<Item = Entry>,
1911        ignore: Option<Arc<Gitignore>>,
1912    ) {
1913        let mut parent_entry = self
1914            .entries_by_path
1915            .get(&PathKey(parent_path.clone()), &())
1916            .unwrap()
1917            .clone();
1918        if let Some(ignore) = ignore {
1919            self.ignores.insert(parent_path, (ignore, self.scan_id));
1920        }
1921        if matches!(parent_entry.kind, EntryKind::PendingDir) {
1922            parent_entry.kind = EntryKind::Dir;
1923        } else {
1924            unreachable!();
1925        }
1926
1927        let mut entries_by_path_edits = vec![Edit::Insert(parent_entry)];
1928        let mut entries_by_id_edits = Vec::new();
1929
1930        for mut entry in entries {
1931            self.reuse_entry_id(&mut entry);
1932            entries_by_id_edits.push(Edit::Insert(PathEntry {
1933                id: entry.id,
1934                path: entry.path.clone(),
1935                is_ignored: entry.is_ignored,
1936                scan_id: self.scan_id,
1937            }));
1938            entries_by_path_edits.push(Edit::Insert(entry));
1939        }
1940
1941        self.entries_by_path.edit(entries_by_path_edits, &());
1942        self.entries_by_id.edit(entries_by_id_edits, &());
1943    }
1944
1945    fn reuse_entry_id(&mut self, entry: &mut Entry) {
1946        if let Some(removed_entry_id) = self.removed_entry_ids.remove(&entry.inode) {
1947            entry.id = removed_entry_id;
1948        } else if let Some(existing_entry) = self.entry_for_path(&entry.path) {
1949            entry.id = existing_entry.id;
1950        }
1951    }
1952
1953    fn remove_path(&mut self, path: &Path) {
1954        let mut new_entries;
1955        let removed_entries;
1956        {
1957            let mut cursor = self.entries_by_path.cursor::<TraversalProgress>();
1958            new_entries = cursor.slice(&TraversalTarget::Path(path), Bias::Left, &());
1959            removed_entries = cursor.slice(&TraversalTarget::PathSuccessor(path), Bias::Left, &());
1960            new_entries.push_tree(cursor.suffix(&()), &());
1961        }
1962        self.entries_by_path = new_entries;
1963
1964        let mut entries_by_id_edits = Vec::new();
1965        for entry in removed_entries.cursor::<()>() {
1966            let removed_entry_id = self
1967                .removed_entry_ids
1968                .entry(entry.inode)
1969                .or_insert(entry.id);
1970            *removed_entry_id = cmp::max(*removed_entry_id, entry.id);
1971            entries_by_id_edits.push(Edit::Remove(entry.id));
1972        }
1973        self.entries_by_id.edit(entries_by_id_edits, &());
1974
1975        if path.file_name() == Some(&GITIGNORE) {
1976            if let Some((_, scan_id)) = self.ignores.get_mut(path.parent().unwrap()) {
1977                *scan_id = self.scan_id;
1978            }
1979        }
1980    }
1981
1982    fn ignore_stack_for_path(&self, path: &Path, is_dir: bool) -> Arc<IgnoreStack> {
1983        let mut new_ignores = Vec::new();
1984        for ancestor in path.ancestors().skip(1) {
1985            if let Some((ignore, _)) = self.ignores.get(ancestor) {
1986                new_ignores.push((ancestor, Some(ignore.clone())));
1987            } else {
1988                new_ignores.push((ancestor, None));
1989            }
1990        }
1991
1992        let mut ignore_stack = IgnoreStack::none();
1993        for (parent_path, ignore) in new_ignores.into_iter().rev() {
1994            if ignore_stack.is_path_ignored(&parent_path, true) {
1995                ignore_stack = IgnoreStack::all();
1996                break;
1997            } else if let Some(ignore) = ignore {
1998                ignore_stack = ignore_stack.append(Arc::from(parent_path), ignore);
1999            }
2000        }
2001
2002        if ignore_stack.is_path_ignored(path, is_dir) {
2003            ignore_stack = IgnoreStack::all();
2004        }
2005
2006        ignore_stack
2007    }
2008}
2009
2010impl fmt::Debug for Snapshot {
2011    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2012        for entry in self.entries_by_path.cursor::<()>() {
2013            for _ in entry.path.ancestors().skip(1) {
2014                write!(f, " ")?;
2015            }
2016            writeln!(f, "{:?} (inode: {})", entry.path, entry.inode)?;
2017        }
2018        Ok(())
2019    }
2020}
2021
2022#[derive(Clone, PartialEq)]
2023pub struct File {
2024    entry_id: Option<usize>,
2025    pub worktree: ModelHandle<Worktree>,
2026    worktree_path: Arc<Path>,
2027    pub path: Arc<Path>,
2028    pub mtime: SystemTime,
2029    is_local: bool,
2030}
2031
2032impl language::File for File {
2033    fn mtime(&self) -> SystemTime {
2034        self.mtime
2035    }
2036
2037    fn path(&self) -> &Arc<Path> {
2038        &self.path
2039    }
2040
2041    fn abs_path(&self) -> Option<PathBuf> {
2042        if self.is_local {
2043            Some(self.worktree_path.join(&self.path))
2044        } else {
2045            None
2046        }
2047    }
2048
2049    fn full_path(&self) -> PathBuf {
2050        let mut full_path = PathBuf::new();
2051        if let Some(worktree_name) = self.worktree_path.file_name() {
2052            full_path.push(worktree_name);
2053        }
2054        full_path.push(&self.path);
2055        full_path
2056    }
2057
2058    /// Returns the last component of this handle's absolute path. If this handle refers to the root
2059    /// of its worktree, then this method will return the name of the worktree itself.
2060    fn file_name<'a>(&'a self) -> Option<OsString> {
2061        self.path
2062            .file_name()
2063            .or_else(|| self.worktree_path.file_name())
2064            .map(Into::into)
2065    }
2066
2067    fn is_deleted(&self) -> bool {
2068        self.entry_id.is_none()
2069    }
2070
2071    fn save(
2072        &self,
2073        buffer_id: u64,
2074        text: Rope,
2075        version: clock::Global,
2076        cx: &mut MutableAppContext,
2077    ) -> Task<Result<(clock::Global, SystemTime)>> {
2078        let worktree_id = self.worktree.read(cx).id().to_proto();
2079        self.worktree.update(cx, |worktree, cx| match worktree {
2080            Worktree::Local(worktree) => {
2081                let rpc = worktree.client.clone();
2082                let project_id = worktree.share.as_ref().map(|share| share.project_id);
2083                let save = worktree.save(self.path.clone(), text, cx);
2084                cx.background().spawn(async move {
2085                    let entry = save.await?;
2086                    if let Some(project_id) = project_id {
2087                        rpc.send(proto::BufferSaved {
2088                            project_id,
2089                            worktree_id,
2090                            buffer_id,
2091                            version: (&version).into(),
2092                            mtime: Some(entry.mtime.into()),
2093                        })
2094                        .await?;
2095                    }
2096                    Ok((version, entry.mtime))
2097                })
2098            }
2099            Worktree::Remote(worktree) => {
2100                let rpc = worktree.client.clone();
2101                let project_id = worktree.project_id;
2102                cx.foreground().spawn(async move {
2103                    let response = rpc
2104                        .request(proto::SaveBuffer {
2105                            project_id,
2106                            worktree_id,
2107                            buffer_id,
2108                        })
2109                        .await?;
2110                    let version = response.version.try_into()?;
2111                    let mtime = response
2112                        .mtime
2113                        .ok_or_else(|| anyhow!("missing mtime"))?
2114                        .into();
2115                    Ok((version, mtime))
2116                })
2117            }
2118        })
2119    }
2120
2121    fn load_local(&self, cx: &AppContext) -> Option<Task<Result<String>>> {
2122        let worktree = self.worktree.read(cx).as_local()?;
2123        let abs_path = worktree.absolutize(&self.path);
2124        let fs = worktree.fs.clone();
2125        Some(
2126            cx.background()
2127                .spawn(async move { fs.load(&abs_path).await }),
2128        )
2129    }
2130
2131    fn format_remote(
2132        &self,
2133        buffer_id: u64,
2134        cx: &mut MutableAppContext,
2135    ) -> Option<Task<Result<()>>> {
2136        let worktree = self.worktree.read(cx);
2137        let worktree_id = worktree.id().to_proto();
2138        let worktree = worktree.as_remote()?;
2139        let rpc = worktree.client.clone();
2140        let project_id = worktree.project_id;
2141        Some(cx.foreground().spawn(async move {
2142            rpc.request(proto::FormatBuffer {
2143                project_id,
2144                worktree_id,
2145                buffer_id,
2146            })
2147            .await?;
2148            Ok(())
2149        }))
2150    }
2151
2152    fn buffer_updated(&self, buffer_id: u64, operation: Operation, cx: &mut MutableAppContext) {
2153        self.worktree.update(cx, |worktree, cx| {
2154            worktree.send_buffer_update(buffer_id, operation, cx);
2155        });
2156    }
2157
2158    fn buffer_removed(&self, buffer_id: u64, cx: &mut MutableAppContext) {
2159        self.worktree.update(cx, |worktree, cx| {
2160            if let Worktree::Remote(worktree) = worktree {
2161                let project_id = worktree.project_id;
2162                let worktree_id = worktree.id().to_proto();
2163                let rpc = worktree.client.clone();
2164                cx.background()
2165                    .spawn(async move {
2166                        if let Err(error) = rpc
2167                            .send(proto::CloseBuffer {
2168                                project_id,
2169                                worktree_id,
2170                                buffer_id,
2171                            })
2172                            .await
2173                        {
2174                            log::error!("error closing remote buffer: {}", error);
2175                        }
2176                    })
2177                    .detach();
2178            }
2179        });
2180    }
2181
2182    fn as_any(&self) -> &dyn Any {
2183        self
2184    }
2185}
2186
2187impl File {
2188    pub fn from_dyn(file: Option<&dyn language::File>) -> Option<&Self> {
2189        file.and_then(|f| f.as_any().downcast_ref())
2190    }
2191
2192    pub fn worktree_id(&self, cx: &AppContext) -> WorktreeId {
2193        self.worktree.read(cx).id()
2194    }
2195}
2196
2197#[derive(Clone, Debug)]
2198pub struct Entry {
2199    pub id: usize,
2200    pub kind: EntryKind,
2201    pub path: Arc<Path>,
2202    pub inode: u64,
2203    pub mtime: SystemTime,
2204    pub is_symlink: bool,
2205    pub is_ignored: bool,
2206}
2207
2208#[derive(Clone, Debug)]
2209pub enum EntryKind {
2210    PendingDir,
2211    Dir,
2212    File(CharBag),
2213}
2214
2215impl Entry {
2216    fn new(
2217        path: Arc<Path>,
2218        metadata: &fs::Metadata,
2219        next_entry_id: &AtomicUsize,
2220        root_char_bag: CharBag,
2221    ) -> Self {
2222        Self {
2223            id: next_entry_id.fetch_add(1, SeqCst),
2224            kind: if metadata.is_dir {
2225                EntryKind::PendingDir
2226            } else {
2227                EntryKind::File(char_bag_for_path(root_char_bag, &path))
2228            },
2229            path,
2230            inode: metadata.inode,
2231            mtime: metadata.mtime,
2232            is_symlink: metadata.is_symlink,
2233            is_ignored: false,
2234        }
2235    }
2236
2237    pub fn is_dir(&self) -> bool {
2238        matches!(self.kind, EntryKind::Dir | EntryKind::PendingDir)
2239    }
2240
2241    pub fn is_file(&self) -> bool {
2242        matches!(self.kind, EntryKind::File(_))
2243    }
2244}
2245
2246impl sum_tree::Item for Entry {
2247    type Summary = EntrySummary;
2248
2249    fn summary(&self) -> Self::Summary {
2250        let visible_count = if self.is_ignored { 0 } else { 1 };
2251        let file_count;
2252        let visible_file_count;
2253        if self.is_file() {
2254            file_count = 1;
2255            visible_file_count = visible_count;
2256        } else {
2257            file_count = 0;
2258            visible_file_count = 0;
2259        }
2260
2261        EntrySummary {
2262            max_path: self.path.clone(),
2263            count: 1,
2264            visible_count,
2265            file_count,
2266            visible_file_count,
2267        }
2268    }
2269}
2270
2271impl sum_tree::KeyedItem for Entry {
2272    type Key = PathKey;
2273
2274    fn key(&self) -> Self::Key {
2275        PathKey(self.path.clone())
2276    }
2277}
2278
2279#[derive(Clone, Debug)]
2280pub struct EntrySummary {
2281    max_path: Arc<Path>,
2282    count: usize,
2283    visible_count: usize,
2284    file_count: usize,
2285    visible_file_count: usize,
2286}
2287
2288impl Default for EntrySummary {
2289    fn default() -> Self {
2290        Self {
2291            max_path: Arc::from(Path::new("")),
2292            count: 0,
2293            visible_count: 0,
2294            file_count: 0,
2295            visible_file_count: 0,
2296        }
2297    }
2298}
2299
2300impl sum_tree::Summary for EntrySummary {
2301    type Context = ();
2302
2303    fn add_summary(&mut self, rhs: &Self, _: &()) {
2304        self.max_path = rhs.max_path.clone();
2305        self.visible_count += rhs.visible_count;
2306        self.file_count += rhs.file_count;
2307        self.visible_file_count += rhs.visible_file_count;
2308    }
2309}
2310
2311#[derive(Clone, Debug)]
2312struct PathEntry {
2313    id: usize,
2314    path: Arc<Path>,
2315    is_ignored: bool,
2316    scan_id: usize,
2317}
2318
2319impl sum_tree::Item for PathEntry {
2320    type Summary = PathEntrySummary;
2321
2322    fn summary(&self) -> Self::Summary {
2323        PathEntrySummary { max_id: self.id }
2324    }
2325}
2326
2327impl sum_tree::KeyedItem for PathEntry {
2328    type Key = usize;
2329
2330    fn key(&self) -> Self::Key {
2331        self.id
2332    }
2333}
2334
2335#[derive(Clone, Debug, Default)]
2336struct PathEntrySummary {
2337    max_id: usize,
2338}
2339
2340impl sum_tree::Summary for PathEntrySummary {
2341    type Context = ();
2342
2343    fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
2344        self.max_id = summary.max_id;
2345    }
2346}
2347
2348impl<'a> sum_tree::Dimension<'a, PathEntrySummary> for usize {
2349    fn add_summary(&mut self, summary: &'a PathEntrySummary, _: &()) {
2350        *self = summary.max_id;
2351    }
2352}
2353
2354#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
2355pub struct PathKey(Arc<Path>);
2356
2357impl Default for PathKey {
2358    fn default() -> Self {
2359        Self(Path::new("").into())
2360    }
2361}
2362
2363impl<'a> sum_tree::Dimension<'a, EntrySummary> for PathKey {
2364    fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2365        self.0 = summary.max_path.clone();
2366    }
2367}
2368
2369struct BackgroundScanner {
2370    fs: Arc<dyn Fs>,
2371    snapshot: Arc<Mutex<Snapshot>>,
2372    notify: Sender<ScanState>,
2373    executor: Arc<executor::Background>,
2374}
2375
2376impl BackgroundScanner {
2377    fn new(
2378        snapshot: Arc<Mutex<Snapshot>>,
2379        notify: Sender<ScanState>,
2380        fs: Arc<dyn Fs>,
2381        executor: Arc<executor::Background>,
2382    ) -> Self {
2383        Self {
2384            fs,
2385            snapshot,
2386            notify,
2387            executor,
2388        }
2389    }
2390
2391    fn abs_path(&self) -> Arc<Path> {
2392        self.snapshot.lock().abs_path.clone()
2393    }
2394
2395    fn snapshot(&self) -> Snapshot {
2396        self.snapshot.lock().clone()
2397    }
2398
2399    async fn run(mut self, events_rx: impl Stream<Item = Vec<fsevent::Event>>) {
2400        if self.notify.send(ScanState::Scanning).await.is_err() {
2401            return;
2402        }
2403
2404        if let Err(err) = self.scan_dirs().await {
2405            if self
2406                .notify
2407                .send(ScanState::Err(Arc::new(err)))
2408                .await
2409                .is_err()
2410            {
2411                return;
2412            }
2413        }
2414
2415        if self.notify.send(ScanState::Idle).await.is_err() {
2416            return;
2417        }
2418
2419        futures::pin_mut!(events_rx);
2420        while let Some(events) = events_rx.next().await {
2421            if self.notify.send(ScanState::Scanning).await.is_err() {
2422                break;
2423            }
2424
2425            if !self.process_events(events).await {
2426                break;
2427            }
2428
2429            if self.notify.send(ScanState::Idle).await.is_err() {
2430                break;
2431            }
2432        }
2433    }
2434
2435    async fn scan_dirs(&mut self) -> Result<()> {
2436        let root_char_bag;
2437        let next_entry_id;
2438        let is_dir;
2439        {
2440            let snapshot = self.snapshot.lock();
2441            root_char_bag = snapshot.root_char_bag;
2442            next_entry_id = snapshot.next_entry_id.clone();
2443            is_dir = snapshot.root_entry().map_or(false, |e| e.is_dir())
2444        };
2445
2446        if is_dir {
2447            let path: Arc<Path> = Arc::from(Path::new(""));
2448            let abs_path = self.abs_path();
2449            let (tx, rx) = channel::unbounded();
2450            tx.send(ScanJob {
2451                abs_path: abs_path.to_path_buf(),
2452                path,
2453                ignore_stack: IgnoreStack::none(),
2454                scan_queue: tx.clone(),
2455            })
2456            .await
2457            .unwrap();
2458            drop(tx);
2459
2460            self.executor
2461                .scoped(|scope| {
2462                    for _ in 0..self.executor.num_cpus() {
2463                        scope.spawn(async {
2464                            while let Ok(job) = rx.recv().await {
2465                                if let Err(err) = self
2466                                    .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2467                                    .await
2468                                {
2469                                    log::error!("error scanning {:?}: {}", job.abs_path, err);
2470                                }
2471                            }
2472                        });
2473                    }
2474                })
2475                .await;
2476        }
2477
2478        Ok(())
2479    }
2480
2481    async fn scan_dir(
2482        &self,
2483        root_char_bag: CharBag,
2484        next_entry_id: Arc<AtomicUsize>,
2485        job: &ScanJob,
2486    ) -> Result<()> {
2487        let mut new_entries: Vec<Entry> = Vec::new();
2488        let mut new_jobs: Vec<ScanJob> = Vec::new();
2489        let mut ignore_stack = job.ignore_stack.clone();
2490        let mut new_ignore = None;
2491
2492        let mut child_paths = self.fs.read_dir(&job.abs_path).await?;
2493        while let Some(child_abs_path) = child_paths.next().await {
2494            let child_abs_path = match child_abs_path {
2495                Ok(child_abs_path) => child_abs_path,
2496                Err(error) => {
2497                    log::error!("error processing entry {:?}", error);
2498                    continue;
2499                }
2500            };
2501            let child_name = child_abs_path.file_name().unwrap();
2502            let child_path: Arc<Path> = job.path.join(child_name).into();
2503            let child_metadata = match self.fs.metadata(&child_abs_path).await? {
2504                Some(metadata) => metadata,
2505                None => continue,
2506            };
2507
2508            // If we find a .gitignore, add it to the stack of ignores used to determine which paths are ignored
2509            if child_name == *GITIGNORE {
2510                match build_gitignore(&child_abs_path, self.fs.as_ref()) {
2511                    Ok(ignore) => {
2512                        let ignore = Arc::new(ignore);
2513                        ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2514                        new_ignore = Some(ignore);
2515                    }
2516                    Err(error) => {
2517                        log::error!(
2518                            "error loading .gitignore file {:?} - {:?}",
2519                            child_name,
2520                            error
2521                        );
2522                    }
2523                }
2524
2525                // Update ignore status of any child entries we've already processed to reflect the
2526                // ignore file in the current directory. Because `.gitignore` starts with a `.`,
2527                // there should rarely be too numerous. Update the ignore stack associated with any
2528                // new jobs as well.
2529                let mut new_jobs = new_jobs.iter_mut();
2530                for entry in &mut new_entries {
2531                    entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2532                    if entry.is_dir() {
2533                        new_jobs.next().unwrap().ignore_stack = if entry.is_ignored {
2534                            IgnoreStack::all()
2535                        } else {
2536                            ignore_stack.clone()
2537                        };
2538                    }
2539                }
2540            }
2541
2542            let mut child_entry = Entry::new(
2543                child_path.clone(),
2544                &child_metadata,
2545                &next_entry_id,
2546                root_char_bag,
2547            );
2548
2549            if child_metadata.is_dir {
2550                let is_ignored = ignore_stack.is_path_ignored(&child_path, true);
2551                child_entry.is_ignored = is_ignored;
2552                new_entries.push(child_entry);
2553                new_jobs.push(ScanJob {
2554                    abs_path: child_abs_path,
2555                    path: child_path,
2556                    ignore_stack: if is_ignored {
2557                        IgnoreStack::all()
2558                    } else {
2559                        ignore_stack.clone()
2560                    },
2561                    scan_queue: job.scan_queue.clone(),
2562                });
2563            } else {
2564                child_entry.is_ignored = ignore_stack.is_path_ignored(&child_path, false);
2565                new_entries.push(child_entry);
2566            };
2567        }
2568
2569        self.snapshot
2570            .lock()
2571            .populate_dir(job.path.clone(), new_entries, new_ignore);
2572        for new_job in new_jobs {
2573            job.scan_queue.send(new_job).await.unwrap();
2574        }
2575
2576        Ok(())
2577    }
2578
2579    async fn process_events(&mut self, mut events: Vec<fsevent::Event>) -> bool {
2580        let mut snapshot = self.snapshot();
2581        snapshot.scan_id += 1;
2582
2583        let root_abs_path = if let Ok(abs_path) = self.fs.canonicalize(&snapshot.abs_path).await {
2584            abs_path
2585        } else {
2586            return false;
2587        };
2588        let root_char_bag = snapshot.root_char_bag;
2589        let next_entry_id = snapshot.next_entry_id.clone();
2590
2591        events.sort_unstable_by(|a, b| a.path.cmp(&b.path));
2592        events.dedup_by(|a, b| a.path.starts_with(&b.path));
2593
2594        for event in &events {
2595            match event.path.strip_prefix(&root_abs_path) {
2596                Ok(path) => snapshot.remove_path(&path),
2597                Err(_) => {
2598                    log::error!(
2599                        "unexpected event {:?} for root path {:?}",
2600                        event.path,
2601                        root_abs_path
2602                    );
2603                    continue;
2604                }
2605            }
2606        }
2607
2608        let (scan_queue_tx, scan_queue_rx) = channel::unbounded();
2609        for event in events {
2610            let path: Arc<Path> = match event.path.strip_prefix(&root_abs_path) {
2611                Ok(path) => Arc::from(path.to_path_buf()),
2612                Err(_) => {
2613                    log::error!(
2614                        "unexpected event {:?} for root path {:?}",
2615                        event.path,
2616                        root_abs_path
2617                    );
2618                    continue;
2619                }
2620            };
2621
2622            match self.fs.metadata(&event.path).await {
2623                Ok(Some(metadata)) => {
2624                    let ignore_stack = snapshot.ignore_stack_for_path(&path, metadata.is_dir);
2625                    let mut fs_entry = Entry::new(
2626                        path.clone(),
2627                        &metadata,
2628                        snapshot.next_entry_id.as_ref(),
2629                        snapshot.root_char_bag,
2630                    );
2631                    fs_entry.is_ignored = ignore_stack.is_all();
2632                    snapshot.insert_entry(fs_entry, self.fs.as_ref());
2633                    if metadata.is_dir {
2634                        scan_queue_tx
2635                            .send(ScanJob {
2636                                abs_path: event.path,
2637                                path,
2638                                ignore_stack,
2639                                scan_queue: scan_queue_tx.clone(),
2640                            })
2641                            .await
2642                            .unwrap();
2643                    }
2644                }
2645                Ok(None) => {}
2646                Err(err) => {
2647                    // TODO - create a special 'error' entry in the entries tree to mark this
2648                    log::error!("error reading file on event {:?}", err);
2649                }
2650            }
2651        }
2652
2653        *self.snapshot.lock() = snapshot;
2654
2655        // Scan any directories that were created as part of this event batch.
2656        drop(scan_queue_tx);
2657        self.executor
2658            .scoped(|scope| {
2659                for _ in 0..self.executor.num_cpus() {
2660                    scope.spawn(async {
2661                        while let Ok(job) = scan_queue_rx.recv().await {
2662                            if let Err(err) = self
2663                                .scan_dir(root_char_bag, next_entry_id.clone(), &job)
2664                                .await
2665                            {
2666                                log::error!("error scanning {:?}: {}", job.abs_path, err);
2667                            }
2668                        }
2669                    });
2670                }
2671            })
2672            .await;
2673
2674        // Attempt to detect renames only over a single batch of file-system events.
2675        self.snapshot.lock().removed_entry_ids.clear();
2676
2677        self.update_ignore_statuses().await;
2678        true
2679    }
2680
2681    async fn update_ignore_statuses(&self) {
2682        let mut snapshot = self.snapshot();
2683
2684        let mut ignores_to_update = Vec::new();
2685        let mut ignores_to_delete = Vec::new();
2686        for (parent_path, (_, scan_id)) in &snapshot.ignores {
2687            if *scan_id == snapshot.scan_id && snapshot.entry_for_path(parent_path).is_some() {
2688                ignores_to_update.push(parent_path.clone());
2689            }
2690
2691            let ignore_path = parent_path.join(&*GITIGNORE);
2692            if snapshot.entry_for_path(ignore_path).is_none() {
2693                ignores_to_delete.push(parent_path.clone());
2694            }
2695        }
2696
2697        for parent_path in ignores_to_delete {
2698            snapshot.ignores.remove(&parent_path);
2699            self.snapshot.lock().ignores.remove(&parent_path);
2700        }
2701
2702        let (ignore_queue_tx, ignore_queue_rx) = channel::unbounded();
2703        ignores_to_update.sort_unstable();
2704        let mut ignores_to_update = ignores_to_update.into_iter().peekable();
2705        while let Some(parent_path) = ignores_to_update.next() {
2706            while ignores_to_update
2707                .peek()
2708                .map_or(false, |p| p.starts_with(&parent_path))
2709            {
2710                ignores_to_update.next().unwrap();
2711            }
2712
2713            let ignore_stack = snapshot.ignore_stack_for_path(&parent_path, true);
2714            ignore_queue_tx
2715                .send(UpdateIgnoreStatusJob {
2716                    path: parent_path,
2717                    ignore_stack,
2718                    ignore_queue: ignore_queue_tx.clone(),
2719                })
2720                .await
2721                .unwrap();
2722        }
2723        drop(ignore_queue_tx);
2724
2725        self.executor
2726            .scoped(|scope| {
2727                for _ in 0..self.executor.num_cpus() {
2728                    scope.spawn(async {
2729                        while let Ok(job) = ignore_queue_rx.recv().await {
2730                            self.update_ignore_status(job, &snapshot).await;
2731                        }
2732                    });
2733                }
2734            })
2735            .await;
2736    }
2737
2738    async fn update_ignore_status(&self, job: UpdateIgnoreStatusJob, snapshot: &Snapshot) {
2739        let mut ignore_stack = job.ignore_stack;
2740        if let Some((ignore, _)) = snapshot.ignores.get(&job.path) {
2741            ignore_stack = ignore_stack.append(job.path.clone(), ignore.clone());
2742        }
2743
2744        let mut entries_by_id_edits = Vec::new();
2745        let mut entries_by_path_edits = Vec::new();
2746        for mut entry in snapshot.child_entries(&job.path).cloned() {
2747            let was_ignored = entry.is_ignored;
2748            entry.is_ignored = ignore_stack.is_path_ignored(&entry.path, entry.is_dir());
2749            if entry.is_dir() {
2750                let child_ignore_stack = if entry.is_ignored {
2751                    IgnoreStack::all()
2752                } else {
2753                    ignore_stack.clone()
2754                };
2755                job.ignore_queue
2756                    .send(UpdateIgnoreStatusJob {
2757                        path: entry.path.clone(),
2758                        ignore_stack: child_ignore_stack,
2759                        ignore_queue: job.ignore_queue.clone(),
2760                    })
2761                    .await
2762                    .unwrap();
2763            }
2764
2765            if entry.is_ignored != was_ignored {
2766                let mut path_entry = snapshot.entries_by_id.get(&entry.id, &()).unwrap().clone();
2767                path_entry.scan_id = snapshot.scan_id;
2768                path_entry.is_ignored = entry.is_ignored;
2769                entries_by_id_edits.push(Edit::Insert(path_entry));
2770                entries_by_path_edits.push(Edit::Insert(entry));
2771            }
2772        }
2773
2774        let mut snapshot = self.snapshot.lock();
2775        snapshot.entries_by_path.edit(entries_by_path_edits, &());
2776        snapshot.entries_by_id.edit(entries_by_id_edits, &());
2777    }
2778}
2779
2780async fn refresh_entry(
2781    fs: &dyn Fs,
2782    snapshot: &Mutex<Snapshot>,
2783    path: Arc<Path>,
2784    abs_path: &Path,
2785) -> Result<Entry> {
2786    let root_char_bag;
2787    let next_entry_id;
2788    {
2789        let snapshot = snapshot.lock();
2790        root_char_bag = snapshot.root_char_bag;
2791        next_entry_id = snapshot.next_entry_id.clone();
2792    }
2793    let entry = Entry::new(
2794        path,
2795        &fs.metadata(abs_path)
2796            .await?
2797            .ok_or_else(|| anyhow!("could not read saved file metadata"))?,
2798        &next_entry_id,
2799        root_char_bag,
2800    );
2801    Ok(snapshot.lock().insert_entry(entry, fs))
2802}
2803
2804fn char_bag_for_path(root_char_bag: CharBag, path: &Path) -> CharBag {
2805    let mut result = root_char_bag;
2806    result.extend(
2807        path.to_string_lossy()
2808            .chars()
2809            .map(|c| c.to_ascii_lowercase()),
2810    );
2811    result
2812}
2813
2814struct ScanJob {
2815    abs_path: PathBuf,
2816    path: Arc<Path>,
2817    ignore_stack: Arc<IgnoreStack>,
2818    scan_queue: Sender<ScanJob>,
2819}
2820
2821struct UpdateIgnoreStatusJob {
2822    path: Arc<Path>,
2823    ignore_stack: Arc<IgnoreStack>,
2824    ignore_queue: Sender<UpdateIgnoreStatusJob>,
2825}
2826
2827pub trait WorktreeHandle {
2828    #[cfg(test)]
2829    fn flush_fs_events<'a>(
2830        &self,
2831        cx: &'a gpui::TestAppContext,
2832    ) -> futures::future::LocalBoxFuture<'a, ()>;
2833}
2834
2835impl WorktreeHandle for ModelHandle<Worktree> {
2836    // When the worktree's FS event stream sometimes delivers "redundant" events for FS changes that
2837    // occurred before the worktree was constructed. These events can cause the worktree to perfrom
2838    // extra directory scans, and emit extra scan-state notifications.
2839    //
2840    // This function mutates the worktree's directory and waits for those mutations to be picked up,
2841    // to ensure that all redundant FS events have already been processed.
2842    #[cfg(test)]
2843    fn flush_fs_events<'a>(
2844        &self,
2845        cx: &'a gpui::TestAppContext,
2846    ) -> futures::future::LocalBoxFuture<'a, ()> {
2847        use smol::future::FutureExt;
2848
2849        let filename = "fs-event-sentinel";
2850        let root_path = cx.read(|cx| self.read(cx).abs_path.clone());
2851        let tree = self.clone();
2852        async move {
2853            std::fs::write(root_path.join(filename), "").unwrap();
2854            tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_some())
2855                .await;
2856
2857            std::fs::remove_file(root_path.join(filename)).unwrap();
2858            tree.condition(&cx, |tree, _| tree.entry_for_path(filename).is_none())
2859                .await;
2860
2861            cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
2862                .await;
2863        }
2864        .boxed_local()
2865    }
2866}
2867
2868#[derive(Clone, Debug)]
2869struct TraversalProgress<'a> {
2870    max_path: &'a Path,
2871    count: usize,
2872    visible_count: usize,
2873    file_count: usize,
2874    visible_file_count: usize,
2875}
2876
2877impl<'a> TraversalProgress<'a> {
2878    fn count(&self, include_dirs: bool, include_ignored: bool) -> usize {
2879        match (include_ignored, include_dirs) {
2880            (true, true) => self.count,
2881            (true, false) => self.file_count,
2882            (false, true) => self.visible_count,
2883            (false, false) => self.visible_file_count,
2884        }
2885    }
2886}
2887
2888impl<'a> sum_tree::Dimension<'a, EntrySummary> for TraversalProgress<'a> {
2889    fn add_summary(&mut self, summary: &'a EntrySummary, _: &()) {
2890        self.max_path = summary.max_path.as_ref();
2891        self.count += summary.count;
2892        self.visible_count += summary.visible_count;
2893        self.file_count += summary.file_count;
2894        self.visible_file_count += summary.visible_file_count;
2895    }
2896}
2897
2898impl<'a> Default for TraversalProgress<'a> {
2899    fn default() -> Self {
2900        Self {
2901            max_path: Path::new(""),
2902            count: 0,
2903            visible_count: 0,
2904            file_count: 0,
2905            visible_file_count: 0,
2906        }
2907    }
2908}
2909
2910pub struct Traversal<'a> {
2911    cursor: sum_tree::Cursor<'a, Entry, TraversalProgress<'a>>,
2912    include_ignored: bool,
2913    include_dirs: bool,
2914}
2915
2916impl<'a> Traversal<'a> {
2917    pub fn advance(&mut self) -> bool {
2918        self.advance_to_offset(self.offset() + 1)
2919    }
2920
2921    pub fn advance_to_offset(&mut self, offset: usize) -> bool {
2922        self.cursor.seek_forward(
2923            &TraversalTarget::Count {
2924                count: offset,
2925                include_dirs: self.include_dirs,
2926                include_ignored: self.include_ignored,
2927            },
2928            Bias::Right,
2929            &(),
2930        )
2931    }
2932
2933    pub fn advance_to_sibling(&mut self) -> bool {
2934        while let Some(entry) = self.cursor.item() {
2935            self.cursor.seek_forward(
2936                &TraversalTarget::PathSuccessor(&entry.path),
2937                Bias::Left,
2938                &(),
2939            );
2940            if let Some(entry) = self.cursor.item() {
2941                if (self.include_dirs || !entry.is_dir())
2942                    && (self.include_ignored || !entry.is_ignored)
2943                {
2944                    return true;
2945                }
2946            }
2947        }
2948        false
2949    }
2950
2951    pub fn entry(&self) -> Option<&'a Entry> {
2952        self.cursor.item()
2953    }
2954
2955    pub fn offset(&self) -> usize {
2956        self.cursor
2957            .start()
2958            .count(self.include_dirs, self.include_ignored)
2959    }
2960}
2961
2962impl<'a> Iterator for Traversal<'a> {
2963    type Item = &'a Entry;
2964
2965    fn next(&mut self) -> Option<Self::Item> {
2966        if let Some(item) = self.entry() {
2967            self.advance();
2968            Some(item)
2969        } else {
2970            None
2971        }
2972    }
2973}
2974
2975#[derive(Debug)]
2976enum TraversalTarget<'a> {
2977    Path(&'a Path),
2978    PathSuccessor(&'a Path),
2979    Count {
2980        count: usize,
2981        include_ignored: bool,
2982        include_dirs: bool,
2983    },
2984}
2985
2986impl<'a, 'b> SeekTarget<'a, EntrySummary, TraversalProgress<'a>> for TraversalTarget<'b> {
2987    fn cmp(&self, cursor_location: &TraversalProgress<'a>, _: &()) -> Ordering {
2988        match self {
2989            TraversalTarget::Path(path) => path.cmp(&cursor_location.max_path),
2990            TraversalTarget::PathSuccessor(path) => {
2991                if !cursor_location.max_path.starts_with(path) {
2992                    Ordering::Equal
2993                } else {
2994                    Ordering::Greater
2995                }
2996            }
2997            TraversalTarget::Count {
2998                count,
2999                include_dirs,
3000                include_ignored,
3001            } => Ord::cmp(
3002                count,
3003                &cursor_location.count(*include_dirs, *include_ignored),
3004            ),
3005        }
3006    }
3007}
3008
3009struct ChildEntriesIter<'a> {
3010    parent_path: &'a Path,
3011    traversal: Traversal<'a>,
3012}
3013
3014impl<'a> Iterator for ChildEntriesIter<'a> {
3015    type Item = &'a Entry;
3016
3017    fn next(&mut self) -> Option<Self::Item> {
3018        if let Some(item) = self.traversal.entry() {
3019            if item.path.starts_with(&self.parent_path) {
3020                self.traversal.advance_to_sibling();
3021                return Some(item);
3022            }
3023        }
3024        None
3025    }
3026}
3027
3028impl<'a> From<&'a Entry> for proto::Entry {
3029    fn from(entry: &'a Entry) -> Self {
3030        Self {
3031            id: entry.id as u64,
3032            is_dir: entry.is_dir(),
3033            path: entry.path.to_string_lossy().to_string(),
3034            inode: entry.inode,
3035            mtime: Some(entry.mtime.into()),
3036            is_symlink: entry.is_symlink,
3037            is_ignored: entry.is_ignored,
3038        }
3039    }
3040}
3041
3042impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
3043    type Error = anyhow::Error;
3044
3045    fn try_from((root_char_bag, entry): (&'a CharBag, proto::Entry)) -> Result<Self> {
3046        if let Some(mtime) = entry.mtime {
3047            let kind = if entry.is_dir {
3048                EntryKind::Dir
3049            } else {
3050                let mut char_bag = root_char_bag.clone();
3051                char_bag.extend(entry.path.chars().map(|c| c.to_ascii_lowercase()));
3052                EntryKind::File(char_bag)
3053            };
3054            let path: Arc<Path> = Arc::from(Path::new(&entry.path));
3055            Ok(Entry {
3056                id: entry.id as usize,
3057                kind,
3058                path: path.clone(),
3059                inode: entry.inode,
3060                mtime: mtime.into(),
3061                is_symlink: entry.is_symlink,
3062                is_ignored: entry.is_ignored,
3063            })
3064        } else {
3065            Err(anyhow!(
3066                "missing mtime in remote worktree entry {:?}",
3067                entry.path
3068            ))
3069        }
3070    }
3071}
3072
3073#[cfg(test)]
3074mod tests {
3075    use super::*;
3076    use crate::fs::FakeFs;
3077    use anyhow::Result;
3078    use client::test::{FakeHttpClient, FakeServer};
3079    use fs::RealFs;
3080    use language::{Diagnostic, DiagnosticEntry};
3081    use lsp::Url;
3082    use rand::prelude::*;
3083    use serde_json::json;
3084    use std::{cell::RefCell, rc::Rc};
3085    use std::{
3086        env,
3087        fmt::Write,
3088        time::{SystemTime, UNIX_EPOCH},
3089    };
3090    use text::Point;
3091    use unindent::Unindent as _;
3092    use util::test::temp_tree;
3093
3094    #[gpui::test]
3095    async fn test_traversal(mut cx: gpui::TestAppContext) {
3096        let fs = FakeFs::new();
3097        fs.insert_tree(
3098            "/root",
3099            json!({
3100               ".gitignore": "a/b\n",
3101               "a": {
3102                   "b": "",
3103                   "c": "",
3104               }
3105            }),
3106        )
3107        .await;
3108
3109        let http_client = FakeHttpClient::with_404_response();
3110        let client = Client::new(http_client.clone());
3111        let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3112
3113        let tree = Worktree::open_local(
3114            client,
3115            user_store,
3116            Arc::from(Path::new("/root")),
3117            false,
3118            Arc::new(fs),
3119            &mut cx.to_async(),
3120        )
3121        .await
3122        .unwrap();
3123        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3124            .await;
3125
3126        tree.read_with(&cx, |tree, _| {
3127            assert_eq!(
3128                tree.entries(false)
3129                    .map(|entry| entry.path.as_ref())
3130                    .collect::<Vec<_>>(),
3131                vec![
3132                    Path::new(""),
3133                    Path::new(".gitignore"),
3134                    Path::new("a"),
3135                    Path::new("a/c"),
3136                ]
3137            );
3138        })
3139    }
3140
3141    #[gpui::test]
3142    async fn test_save_file(mut cx: gpui::TestAppContext) {
3143        let dir = temp_tree(json!({
3144            "file1": "the old contents",
3145        }));
3146
3147        let http_client = FakeHttpClient::with_404_response();
3148        let client = Client::new(http_client.clone());
3149        let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3150
3151        let tree = Worktree::open_local(
3152            client,
3153            user_store,
3154            dir.path(),
3155            false,
3156            Arc::new(RealFs),
3157            &mut cx.to_async(),
3158        )
3159        .await
3160        .unwrap();
3161        let (buffer, _) = tree
3162            .update(&mut cx, |tree, cx| tree.open_buffer("file1", cx))
3163            .await
3164            .unwrap();
3165        let save = buffer.update(&mut cx, |buffer, cx| {
3166            buffer.edit(Some(0..0), "a line of text.\n".repeat(10 * 1024), cx);
3167            buffer.save(cx)
3168        });
3169        save.await.unwrap();
3170
3171        let new_text = std::fs::read_to_string(dir.path().join("file1")).unwrap();
3172        assert_eq!(new_text, buffer.read_with(&cx, |buffer, _| buffer.text()));
3173    }
3174
3175    #[gpui::test]
3176    async fn test_save_in_single_file_worktree(mut cx: gpui::TestAppContext) {
3177        let dir = temp_tree(json!({
3178            "file1": "the old contents",
3179        }));
3180        let file_path = dir.path().join("file1");
3181
3182        let http_client = FakeHttpClient::with_404_response();
3183        let client = Client::new(http_client.clone());
3184        let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3185
3186        let tree = Worktree::open_local(
3187            client,
3188            user_store,
3189            file_path.clone(),
3190            false,
3191            Arc::new(RealFs),
3192            &mut cx.to_async(),
3193        )
3194        .await
3195        .unwrap();
3196        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3197            .await;
3198        cx.read(|cx| assert_eq!(tree.read(cx).file_count(), 1));
3199
3200        let (buffer, _) = tree
3201            .update(&mut cx, |tree, cx| tree.open_buffer("", cx))
3202            .await
3203            .unwrap();
3204        let save = buffer.update(&mut cx, |buffer, cx| {
3205            buffer.edit(Some(0..0), "a line of text.\n".repeat(10 * 1024), cx);
3206            buffer.save(cx)
3207        });
3208        save.await.unwrap();
3209
3210        let new_text = std::fs::read_to_string(file_path).unwrap();
3211        assert_eq!(new_text, buffer.read_with(&cx, |buffer, _| buffer.text()));
3212    }
3213
3214    #[gpui::test]
3215    async fn test_rescan_and_remote_updates(mut cx: gpui::TestAppContext) {
3216        let dir = temp_tree(json!({
3217            "a": {
3218                "file1": "",
3219                "file2": "",
3220                "file3": "",
3221            },
3222            "b": {
3223                "c": {
3224                    "file4": "",
3225                    "file5": "",
3226                }
3227            }
3228        }));
3229
3230        let user_id = 5;
3231        let http_client = FakeHttpClient::with_404_response();
3232        let mut client = Client::new(http_client.clone());
3233        let server = FakeServer::for_client(user_id, &mut client, &cx).await;
3234        let user_store = server.build_user_store(client.clone(), &mut cx).await;
3235        let tree = Worktree::open_local(
3236            client,
3237            user_store.clone(),
3238            dir.path(),
3239            false,
3240            Arc::new(RealFs),
3241            &mut cx.to_async(),
3242        )
3243        .await
3244        .unwrap();
3245
3246        let buffer_for_path = |path: &'static str, cx: &mut gpui::TestAppContext| {
3247            let buffer = tree.update(cx, |tree, cx| tree.open_buffer(path, cx));
3248            async move { buffer.await.unwrap().0 }
3249        };
3250        let id_for_path = |path: &'static str, cx: &gpui::TestAppContext| {
3251            tree.read_with(cx, |tree, _| {
3252                tree.entry_for_path(path)
3253                    .expect(&format!("no entry for path {}", path))
3254                    .id
3255            })
3256        };
3257
3258        let buffer2 = buffer_for_path("a/file2", &mut cx).await;
3259        let buffer3 = buffer_for_path("a/file3", &mut cx).await;
3260        let buffer4 = buffer_for_path("b/c/file4", &mut cx).await;
3261        let buffer5 = buffer_for_path("b/c/file5", &mut cx).await;
3262
3263        let file2_id = id_for_path("a/file2", &cx);
3264        let file3_id = id_for_path("a/file3", &cx);
3265        let file4_id = id_for_path("b/c/file4", &cx);
3266
3267        // Wait for the initial scan.
3268        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3269            .await;
3270
3271        // Create a remote copy of this worktree.
3272        let initial_snapshot = tree.read_with(&cx, |tree, _| tree.snapshot());
3273        let remote = Worktree::remote(
3274            1,
3275            1,
3276            initial_snapshot.to_proto(&Default::default(), Default::default()),
3277            Client::new(http_client.clone()),
3278            user_store,
3279            &mut cx.to_async(),
3280        )
3281        .await
3282        .unwrap();
3283
3284        cx.read(|cx| {
3285            assert!(!buffer2.read(cx).is_dirty());
3286            assert!(!buffer3.read(cx).is_dirty());
3287            assert!(!buffer4.read(cx).is_dirty());
3288            assert!(!buffer5.read(cx).is_dirty());
3289        });
3290
3291        // Rename and delete files and directories.
3292        tree.flush_fs_events(&cx).await;
3293        std::fs::rename(dir.path().join("a/file3"), dir.path().join("b/c/file3")).unwrap();
3294        std::fs::remove_file(dir.path().join("b/c/file5")).unwrap();
3295        std::fs::rename(dir.path().join("b/c"), dir.path().join("d")).unwrap();
3296        std::fs::rename(dir.path().join("a/file2"), dir.path().join("a/file2.new")).unwrap();
3297        tree.flush_fs_events(&cx).await;
3298
3299        let expected_paths = vec![
3300            "a",
3301            "a/file1",
3302            "a/file2.new",
3303            "b",
3304            "d",
3305            "d/file3",
3306            "d/file4",
3307        ];
3308
3309        cx.read(|app| {
3310            assert_eq!(
3311                tree.read(app)
3312                    .paths()
3313                    .map(|p| p.to_str().unwrap())
3314                    .collect::<Vec<_>>(),
3315                expected_paths
3316            );
3317
3318            assert_eq!(id_for_path("a/file2.new", &cx), file2_id);
3319            assert_eq!(id_for_path("d/file3", &cx), file3_id);
3320            assert_eq!(id_for_path("d/file4", &cx), file4_id);
3321
3322            assert_eq!(
3323                buffer2.read(app).file().unwrap().path().as_ref(),
3324                Path::new("a/file2.new")
3325            );
3326            assert_eq!(
3327                buffer3.read(app).file().unwrap().path().as_ref(),
3328                Path::new("d/file3")
3329            );
3330            assert_eq!(
3331                buffer4.read(app).file().unwrap().path().as_ref(),
3332                Path::new("d/file4")
3333            );
3334            assert_eq!(
3335                buffer5.read(app).file().unwrap().path().as_ref(),
3336                Path::new("b/c/file5")
3337            );
3338
3339            assert!(!buffer2.read(app).file().unwrap().is_deleted());
3340            assert!(!buffer3.read(app).file().unwrap().is_deleted());
3341            assert!(!buffer4.read(app).file().unwrap().is_deleted());
3342            assert!(buffer5.read(app).file().unwrap().is_deleted());
3343        });
3344
3345        // Update the remote worktree. Check that it becomes consistent with the
3346        // local worktree.
3347        remote.update(&mut cx, |remote, cx| {
3348            let update_message =
3349                tree.read(cx)
3350                    .snapshot()
3351                    .build_update(&initial_snapshot, 1, 1, true);
3352            remote
3353                .as_remote_mut()
3354                .unwrap()
3355                .snapshot
3356                .apply_update(update_message)
3357                .unwrap();
3358
3359            assert_eq!(
3360                remote
3361                    .paths()
3362                    .map(|p| p.to_str().unwrap())
3363                    .collect::<Vec<_>>(),
3364                expected_paths
3365            );
3366        });
3367    }
3368
3369    #[gpui::test]
3370    async fn test_rescan_with_gitignore(mut cx: gpui::TestAppContext) {
3371        let dir = temp_tree(json!({
3372            ".git": {},
3373            ".gitignore": "ignored-dir\n",
3374            "tracked-dir": {
3375                "tracked-file1": "tracked contents",
3376            },
3377            "ignored-dir": {
3378                "ignored-file1": "ignored contents",
3379            }
3380        }));
3381
3382        let http_client = FakeHttpClient::with_404_response();
3383        let client = Client::new(http_client.clone());
3384        let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3385
3386        let tree = Worktree::open_local(
3387            client,
3388            user_store,
3389            dir.path(),
3390            false,
3391            Arc::new(RealFs),
3392            &mut cx.to_async(),
3393        )
3394        .await
3395        .unwrap();
3396        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3397            .await;
3398        tree.flush_fs_events(&cx).await;
3399        cx.read(|cx| {
3400            let tree = tree.read(cx);
3401            let tracked = tree.entry_for_path("tracked-dir/tracked-file1").unwrap();
3402            let ignored = tree.entry_for_path("ignored-dir/ignored-file1").unwrap();
3403            assert_eq!(tracked.is_ignored, false);
3404            assert_eq!(ignored.is_ignored, true);
3405        });
3406
3407        std::fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
3408        std::fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
3409        tree.flush_fs_events(&cx).await;
3410        cx.read(|cx| {
3411            let tree = tree.read(cx);
3412            let dot_git = tree.entry_for_path(".git").unwrap();
3413            let tracked = tree.entry_for_path("tracked-dir/tracked-file2").unwrap();
3414            let ignored = tree.entry_for_path("ignored-dir/ignored-file2").unwrap();
3415            assert_eq!(tracked.is_ignored, false);
3416            assert_eq!(ignored.is_ignored, true);
3417            assert_eq!(dot_git.is_ignored, true);
3418        });
3419    }
3420
3421    #[gpui::test]
3422    async fn test_buffer_deduping(mut cx: gpui::TestAppContext) {
3423        let user_id = 100;
3424        let http_client = FakeHttpClient::with_404_response();
3425        let mut client = Client::new(http_client);
3426        let server = FakeServer::for_client(user_id, &mut client, &cx).await;
3427        let user_store = server.build_user_store(client.clone(), &mut cx).await;
3428
3429        let fs = Arc::new(FakeFs::new());
3430        fs.insert_tree(
3431            "/the-dir",
3432            json!({
3433                "a.txt": "a-contents",
3434                "b.txt": "b-contents",
3435            }),
3436        )
3437        .await;
3438
3439        let worktree = Worktree::open_local(
3440            client.clone(),
3441            user_store,
3442            "/the-dir".as_ref(),
3443            false,
3444            fs,
3445            &mut cx.to_async(),
3446        )
3447        .await
3448        .unwrap();
3449
3450        // Spawn multiple tasks to open paths, repeating some paths.
3451        let (buffer_a_1, buffer_b, buffer_a_2) = worktree.update(&mut cx, |worktree, cx| {
3452            (
3453                worktree.open_buffer("a.txt", cx),
3454                worktree.open_buffer("b.txt", cx),
3455                worktree.open_buffer("a.txt", cx),
3456            )
3457        });
3458
3459        let buffer_a_1 = buffer_a_1.await.unwrap().0;
3460        let buffer_a_2 = buffer_a_2.await.unwrap().0;
3461        let buffer_b = buffer_b.await.unwrap().0;
3462        assert_eq!(buffer_a_1.read_with(&cx, |b, _| b.text()), "a-contents");
3463        assert_eq!(buffer_b.read_with(&cx, |b, _| b.text()), "b-contents");
3464
3465        // There is only one buffer per path.
3466        let buffer_a_id = buffer_a_1.id();
3467        assert_eq!(buffer_a_2.id(), buffer_a_id);
3468
3469        // Open the same path again while it is still open.
3470        drop(buffer_a_1);
3471        let buffer_a_3 = worktree
3472            .update(&mut cx, |worktree, cx| worktree.open_buffer("a.txt", cx))
3473            .await
3474            .unwrap()
3475            .0;
3476
3477        // There's still only one buffer per path.
3478        assert_eq!(buffer_a_3.id(), buffer_a_id);
3479    }
3480
3481    #[gpui::test]
3482    async fn test_buffer_is_dirty(mut cx: gpui::TestAppContext) {
3483        use std::fs;
3484
3485        let dir = temp_tree(json!({
3486            "file1": "abc",
3487            "file2": "def",
3488            "file3": "ghi",
3489        }));
3490        let http_client = FakeHttpClient::with_404_response();
3491        let client = Client::new(http_client.clone());
3492        let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3493
3494        let tree = Worktree::open_local(
3495            client,
3496            user_store,
3497            dir.path(),
3498            false,
3499            Arc::new(RealFs),
3500            &mut cx.to_async(),
3501        )
3502        .await
3503        .unwrap();
3504        tree.flush_fs_events(&cx).await;
3505        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3506            .await;
3507
3508        let (buffer1, _) = tree
3509            .update(&mut cx, |tree, cx| tree.open_buffer("file1", cx))
3510            .await
3511            .unwrap();
3512        let events = Rc::new(RefCell::new(Vec::new()));
3513
3514        // initially, the buffer isn't dirty.
3515        buffer1.update(&mut cx, |buffer, cx| {
3516            cx.subscribe(&buffer1, {
3517                let events = events.clone();
3518                move |_, _, event, _| events.borrow_mut().push(event.clone())
3519            })
3520            .detach();
3521
3522            assert!(!buffer.is_dirty());
3523            assert!(events.borrow().is_empty());
3524
3525            buffer.edit(vec![1..2], "", cx);
3526        });
3527
3528        // after the first edit, the buffer is dirty, and emits a dirtied event.
3529        buffer1.update(&mut cx, |buffer, cx| {
3530            assert!(buffer.text() == "ac");
3531            assert!(buffer.is_dirty());
3532            assert_eq!(
3533                *events.borrow(),
3534                &[language::Event::Edited, language::Event::Dirtied]
3535            );
3536            events.borrow_mut().clear();
3537            buffer.did_save(buffer.version(), buffer.file().unwrap().mtime(), None, cx);
3538        });
3539
3540        // after saving, the buffer is not dirty, and emits a saved event.
3541        buffer1.update(&mut cx, |buffer, cx| {
3542            assert!(!buffer.is_dirty());
3543            assert_eq!(*events.borrow(), &[language::Event::Saved]);
3544            events.borrow_mut().clear();
3545
3546            buffer.edit(vec![1..1], "B", cx);
3547            buffer.edit(vec![2..2], "D", cx);
3548        });
3549
3550        // after editing again, the buffer is dirty, and emits another dirty event.
3551        buffer1.update(&mut cx, |buffer, cx| {
3552            assert!(buffer.text() == "aBDc");
3553            assert!(buffer.is_dirty());
3554            assert_eq!(
3555                *events.borrow(),
3556                &[
3557                    language::Event::Edited,
3558                    language::Event::Dirtied,
3559                    language::Event::Edited,
3560                ],
3561            );
3562            events.borrow_mut().clear();
3563
3564            // TODO - currently, after restoring the buffer to its
3565            // previously-saved state, the is still considered dirty.
3566            buffer.edit([1..3], "", cx);
3567            assert!(buffer.text() == "ac");
3568            assert!(buffer.is_dirty());
3569        });
3570
3571        assert_eq!(*events.borrow(), &[language::Event::Edited]);
3572
3573        // When a file is deleted, the buffer is considered dirty.
3574        let events = Rc::new(RefCell::new(Vec::new()));
3575        let (buffer2, _) = tree
3576            .update(&mut cx, |tree, cx| tree.open_buffer("file2", cx))
3577            .await
3578            .unwrap();
3579        buffer2.update(&mut cx, |_, cx| {
3580            cx.subscribe(&buffer2, {
3581                let events = events.clone();
3582                move |_, _, event, _| events.borrow_mut().push(event.clone())
3583            })
3584            .detach();
3585        });
3586
3587        fs::remove_file(dir.path().join("file2")).unwrap();
3588        buffer2.condition(&cx, |b, _| b.is_dirty()).await;
3589        assert_eq!(
3590            *events.borrow(),
3591            &[language::Event::Dirtied, language::Event::FileHandleChanged]
3592        );
3593
3594        // When a file is already dirty when deleted, we don't emit a Dirtied event.
3595        let events = Rc::new(RefCell::new(Vec::new()));
3596        let (buffer3, _) = tree
3597            .update(&mut cx, |tree, cx| tree.open_buffer("file3", cx))
3598            .await
3599            .unwrap();
3600        buffer3.update(&mut cx, |_, cx| {
3601            cx.subscribe(&buffer3, {
3602                let events = events.clone();
3603                move |_, _, event, _| events.borrow_mut().push(event.clone())
3604            })
3605            .detach();
3606        });
3607
3608        tree.flush_fs_events(&cx).await;
3609        buffer3.update(&mut cx, |buffer, cx| {
3610            buffer.edit(Some(0..0), "x", cx);
3611        });
3612        events.borrow_mut().clear();
3613        fs::remove_file(dir.path().join("file3")).unwrap();
3614        buffer3
3615            .condition(&cx, |_, _| !events.borrow().is_empty())
3616            .await;
3617        assert_eq!(*events.borrow(), &[language::Event::FileHandleChanged]);
3618        cx.read(|cx| assert!(buffer3.read(cx).is_dirty()));
3619    }
3620
3621    #[gpui::test]
3622    async fn test_buffer_file_changes_on_disk(mut cx: gpui::TestAppContext) {
3623        use std::fs;
3624
3625        let initial_contents = "aaa\nbbbbb\nc\n";
3626        let dir = temp_tree(json!({ "the-file": initial_contents }));
3627        let http_client = FakeHttpClient::with_404_response();
3628        let client = Client::new(http_client.clone());
3629        let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3630
3631        let tree = Worktree::open_local(
3632            client,
3633            user_store,
3634            dir.path(),
3635            false,
3636            Arc::new(RealFs),
3637            &mut cx.to_async(),
3638        )
3639        .await
3640        .unwrap();
3641        cx.read(|cx| tree.read(cx).as_local().unwrap().scan_complete())
3642            .await;
3643
3644        let abs_path = dir.path().join("the-file");
3645        let (buffer, _) = tree
3646            .update(&mut cx, |tree, cx| {
3647                tree.open_buffer(Path::new("the-file"), cx)
3648            })
3649            .await
3650            .unwrap();
3651
3652        // TODO
3653        // Add a cursor on each row.
3654        // let selection_set_id = buffer.update(&mut cx, |buffer, cx| {
3655        //     assert!(!buffer.is_dirty());
3656        //     buffer.add_selection_set(
3657        //         &(0..3)
3658        //             .map(|row| Selection {
3659        //                 id: row as usize,
3660        //                 start: Point::new(row, 1),
3661        //                 end: Point::new(row, 1),
3662        //                 reversed: false,
3663        //                 goal: SelectionGoal::None,
3664        //             })
3665        //             .collect::<Vec<_>>(),
3666        //         cx,
3667        //     )
3668        // });
3669
3670        // Change the file on disk, adding two new lines of text, and removing
3671        // one line.
3672        buffer.read_with(&cx, |buffer, _| {
3673            assert!(!buffer.is_dirty());
3674            assert!(!buffer.has_conflict());
3675        });
3676        let new_contents = "AAAA\naaa\nBB\nbbbbb\n";
3677        fs::write(&abs_path, new_contents).unwrap();
3678
3679        // Because the buffer was not modified, it is reloaded from disk. Its
3680        // contents are edited according to the diff between the old and new
3681        // file contents.
3682        buffer
3683            .condition(&cx, |buffer, _| buffer.text() == new_contents)
3684            .await;
3685
3686        buffer.update(&mut cx, |buffer, _| {
3687            assert_eq!(buffer.text(), new_contents);
3688            assert!(!buffer.is_dirty());
3689            assert!(!buffer.has_conflict());
3690
3691            // TODO
3692            // let cursor_positions = buffer
3693            //     .selection_set(selection_set_id)
3694            //     .unwrap()
3695            //     .selections::<Point>(&*buffer)
3696            //     .map(|selection| {
3697            //         assert_eq!(selection.start, selection.end);
3698            //         selection.start
3699            //     })
3700            //     .collect::<Vec<_>>();
3701            // assert_eq!(
3702            //     cursor_positions,
3703            //     [Point::new(1, 1), Point::new(3, 1), Point::new(4, 0)]
3704            // );
3705        });
3706
3707        // Modify the buffer
3708        buffer.update(&mut cx, |buffer, cx| {
3709            buffer.edit(vec![0..0], " ", cx);
3710            assert!(buffer.is_dirty());
3711            assert!(!buffer.has_conflict());
3712        });
3713
3714        // Change the file on disk again, adding blank lines to the beginning.
3715        fs::write(&abs_path, "\n\n\nAAAA\naaa\nBB\nbbbbb\n").unwrap();
3716
3717        // Because the buffer is modified, it doesn't reload from disk, but is
3718        // marked as having a conflict.
3719        buffer
3720            .condition(&cx, |buffer, _| buffer.has_conflict())
3721            .await;
3722    }
3723
3724    #[gpui::test]
3725    async fn test_grouped_diagnostics(mut cx: gpui::TestAppContext) {
3726        let fs = Arc::new(FakeFs::new());
3727        let http_client = FakeHttpClient::with_404_response();
3728        let client = Client::new(http_client.clone());
3729        let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http_client, cx));
3730
3731        fs.insert_tree(
3732            "/the-dir",
3733            json!({
3734                "a.rs": "
3735                    fn foo(mut v: Vec<usize>) {
3736                        for x in &v {
3737                            v.push(1);
3738                        }
3739                    }
3740                "
3741                .unindent(),
3742            }),
3743        )
3744        .await;
3745
3746        let worktree = Worktree::open_local(
3747            client.clone(),
3748            user_store,
3749            "/the-dir".as_ref(),
3750            false,
3751            fs,
3752            &mut cx.to_async(),
3753        )
3754        .await
3755        .unwrap();
3756
3757        let (buffer, _) = worktree
3758            .update(&mut cx, |tree, cx| tree.open_buffer("a.rs", cx))
3759            .await
3760            .unwrap();
3761
3762        let buffer_uri = Url::from_file_path("/the-dir/a.rs").unwrap();
3763        let message = lsp::PublishDiagnosticsParams {
3764            uri: buffer_uri.clone(),
3765            diagnostics: vec![
3766                lsp::Diagnostic {
3767                    range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
3768                    severity: Some(DiagnosticSeverity::WARNING),
3769                    message: "error 1".to_string(),
3770                    related_information: Some(vec![lsp::DiagnosticRelatedInformation {
3771                        location: lsp::Location {
3772                            uri: buffer_uri.clone(),
3773                            range: lsp::Range::new(
3774                                lsp::Position::new(1, 8),
3775                                lsp::Position::new(1, 9),
3776                            ),
3777                        },
3778                        message: "error 1 hint 1".to_string(),
3779                    }]),
3780                    ..Default::default()
3781                },
3782                lsp::Diagnostic {
3783                    range: lsp::Range::new(lsp::Position::new(1, 8), lsp::Position::new(1, 9)),
3784                    severity: Some(DiagnosticSeverity::HINT),
3785                    message: "error 1 hint 1".to_string(),
3786                    related_information: Some(vec![lsp::DiagnosticRelatedInformation {
3787                        location: lsp::Location {
3788                            uri: buffer_uri.clone(),
3789                            range: lsp::Range::new(
3790                                lsp::Position::new(1, 8),
3791                                lsp::Position::new(1, 9),
3792                            ),
3793                        },
3794                        message: "original diagnostic".to_string(),
3795                    }]),
3796                    ..Default::default()
3797                },
3798                lsp::Diagnostic {
3799                    range: lsp::Range::new(lsp::Position::new(2, 8), lsp::Position::new(2, 17)),
3800                    severity: Some(DiagnosticSeverity::ERROR),
3801                    message: "error 2".to_string(),
3802                    related_information: Some(vec![
3803                        lsp::DiagnosticRelatedInformation {
3804                            location: lsp::Location {
3805                                uri: buffer_uri.clone(),
3806                                range: lsp::Range::new(
3807                                    lsp::Position::new(1, 13),
3808                                    lsp::Position::new(1, 15),
3809                                ),
3810                            },
3811                            message: "error 2 hint 1".to_string(),
3812                        },
3813                        lsp::DiagnosticRelatedInformation {
3814                            location: lsp::Location {
3815                                uri: buffer_uri.clone(),
3816                                range: lsp::Range::new(
3817                                    lsp::Position::new(1, 13),
3818                                    lsp::Position::new(1, 15),
3819                                ),
3820                            },
3821                            message: "error 2 hint 2".to_string(),
3822                        },
3823                    ]),
3824                    ..Default::default()
3825                },
3826                lsp::Diagnostic {
3827                    range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
3828                    severity: Some(DiagnosticSeverity::HINT),
3829                    message: "error 2 hint 1".to_string(),
3830                    related_information: Some(vec![lsp::DiagnosticRelatedInformation {
3831                        location: lsp::Location {
3832                            uri: buffer_uri.clone(),
3833                            range: lsp::Range::new(
3834                                lsp::Position::new(2, 8),
3835                                lsp::Position::new(2, 17),
3836                            ),
3837                        },
3838                        message: "original diagnostic".to_string(),
3839                    }]),
3840                    ..Default::default()
3841                },
3842                lsp::Diagnostic {
3843                    range: lsp::Range::new(lsp::Position::new(1, 13), lsp::Position::new(1, 15)),
3844                    severity: Some(DiagnosticSeverity::HINT),
3845                    message: "error 2 hint 2".to_string(),
3846                    related_information: Some(vec![lsp::DiagnosticRelatedInformation {
3847                        location: lsp::Location {
3848                            uri: buffer_uri.clone(),
3849                            range: lsp::Range::new(
3850                                lsp::Position::new(2, 8),
3851                                lsp::Position::new(2, 17),
3852                            ),
3853                        },
3854                        message: "original diagnostic".to_string(),
3855                    }]),
3856                    ..Default::default()
3857                },
3858            ],
3859            version: None,
3860        };
3861
3862        worktree
3863            .update(&mut cx, |tree, cx| {
3864                tree.as_local_mut().unwrap().update_diagnostics(
3865                    Arc::from("a.rs".as_ref()),
3866                    message,
3867                    &Default::default(),
3868                    cx,
3869                )
3870            })
3871            .unwrap();
3872        let buffer = buffer.read_with(&cx, |buffer, _| buffer.snapshot());
3873
3874        assert_eq!(
3875            buffer
3876                .diagnostics_in_range::<_, Point>(0..buffer.len())
3877                .collect::<Vec<_>>(),
3878            &[
3879                DiagnosticEntry {
3880                    range: Point::new(1, 8)..Point::new(1, 9),
3881                    diagnostic: Diagnostic {
3882                        severity: DiagnosticSeverity::WARNING,
3883                        message: "error 1".to_string(),
3884                        group_id: 0,
3885                        is_primary: true,
3886                        ..Default::default()
3887                    }
3888                },
3889                DiagnosticEntry {
3890                    range: Point::new(1, 8)..Point::new(1, 9),
3891                    diagnostic: Diagnostic {
3892                        severity: DiagnosticSeverity::HINT,
3893                        message: "error 1 hint 1".to_string(),
3894                        group_id: 0,
3895                        is_primary: false,
3896                        ..Default::default()
3897                    }
3898                },
3899                DiagnosticEntry {
3900                    range: Point::new(1, 13)..Point::new(1, 15),
3901                    diagnostic: Diagnostic {
3902                        severity: DiagnosticSeverity::HINT,
3903                        message: "error 2 hint 1".to_string(),
3904                        group_id: 1,
3905                        is_primary: false,
3906                        ..Default::default()
3907                    }
3908                },
3909                DiagnosticEntry {
3910                    range: Point::new(1, 13)..Point::new(1, 15),
3911                    diagnostic: Diagnostic {
3912                        severity: DiagnosticSeverity::HINT,
3913                        message: "error 2 hint 2".to_string(),
3914                        group_id: 1,
3915                        is_primary: false,
3916                        ..Default::default()
3917                    }
3918                },
3919                DiagnosticEntry {
3920                    range: Point::new(2, 8)..Point::new(2, 17),
3921                    diagnostic: Diagnostic {
3922                        severity: DiagnosticSeverity::ERROR,
3923                        message: "error 2".to_string(),
3924                        group_id: 1,
3925                        is_primary: true,
3926                        ..Default::default()
3927                    }
3928                }
3929            ]
3930        );
3931
3932        assert_eq!(
3933            buffer.diagnostic_group::<Point>(0).collect::<Vec<_>>(),
3934            &[
3935                DiagnosticEntry {
3936                    range: Point::new(1, 8)..Point::new(1, 9),
3937                    diagnostic: Diagnostic {
3938                        severity: DiagnosticSeverity::WARNING,
3939                        message: "error 1".to_string(),
3940                        group_id: 0,
3941                        is_primary: true,
3942                        ..Default::default()
3943                    }
3944                },
3945                DiagnosticEntry {
3946                    range: Point::new(1, 8)..Point::new(1, 9),
3947                    diagnostic: Diagnostic {
3948                        severity: DiagnosticSeverity::HINT,
3949                        message: "error 1 hint 1".to_string(),
3950                        group_id: 0,
3951                        is_primary: false,
3952                        ..Default::default()
3953                    }
3954                },
3955            ]
3956        );
3957        assert_eq!(
3958            buffer.diagnostic_group::<Point>(1).collect::<Vec<_>>(),
3959            &[
3960                DiagnosticEntry {
3961                    range: Point::new(1, 13)..Point::new(1, 15),
3962                    diagnostic: Diagnostic {
3963                        severity: DiagnosticSeverity::HINT,
3964                        message: "error 2 hint 1".to_string(),
3965                        group_id: 1,
3966                        is_primary: false,
3967                        ..Default::default()
3968                    }
3969                },
3970                DiagnosticEntry {
3971                    range: Point::new(1, 13)..Point::new(1, 15),
3972                    diagnostic: Diagnostic {
3973                        severity: DiagnosticSeverity::HINT,
3974                        message: "error 2 hint 2".to_string(),
3975                        group_id: 1,
3976                        is_primary: false,
3977                        ..Default::default()
3978                    }
3979                },
3980                DiagnosticEntry {
3981                    range: Point::new(2, 8)..Point::new(2, 17),
3982                    diagnostic: Diagnostic {
3983                        severity: DiagnosticSeverity::ERROR,
3984                        message: "error 2".to_string(),
3985                        group_id: 1,
3986                        is_primary: true,
3987                        ..Default::default()
3988                    }
3989                }
3990            ]
3991        );
3992    }
3993
3994    #[gpui::test(iterations = 100)]
3995    fn test_random(mut rng: StdRng) {
3996        let operations = env::var("OPERATIONS")
3997            .map(|o| o.parse().unwrap())
3998            .unwrap_or(40);
3999        let initial_entries = env::var("INITIAL_ENTRIES")
4000            .map(|o| o.parse().unwrap())
4001            .unwrap_or(20);
4002
4003        let root_dir = tempdir::TempDir::new("worktree-test").unwrap();
4004        for _ in 0..initial_entries {
4005            randomly_mutate_tree(root_dir.path(), 1.0, &mut rng).unwrap();
4006        }
4007        log::info!("Generated initial tree");
4008
4009        let (notify_tx, _notify_rx) = smol::channel::unbounded();
4010        let fs = Arc::new(RealFs);
4011        let next_entry_id = Arc::new(AtomicUsize::new(0));
4012        let mut initial_snapshot = Snapshot {
4013            id: WorktreeId::from_usize(0),
4014            scan_id: 0,
4015            abs_path: root_dir.path().into(),
4016            entries_by_path: Default::default(),
4017            entries_by_id: Default::default(),
4018            removed_entry_ids: Default::default(),
4019            ignores: Default::default(),
4020            root_name: Default::default(),
4021            root_char_bag: Default::default(),
4022            next_entry_id: next_entry_id.clone(),
4023        };
4024        initial_snapshot.insert_entry(
4025            Entry::new(
4026                Path::new("").into(),
4027                &smol::block_on(fs.metadata(root_dir.path()))
4028                    .unwrap()
4029                    .unwrap(),
4030                &next_entry_id,
4031                Default::default(),
4032            ),
4033            fs.as_ref(),
4034        );
4035        let mut scanner = BackgroundScanner::new(
4036            Arc::new(Mutex::new(initial_snapshot.clone())),
4037            notify_tx,
4038            fs.clone(),
4039            Arc::new(gpui::executor::Background::new()),
4040        );
4041        smol::block_on(scanner.scan_dirs()).unwrap();
4042        scanner.snapshot().check_invariants();
4043
4044        let mut events = Vec::new();
4045        let mut snapshots = Vec::new();
4046        let mut mutations_len = operations;
4047        while mutations_len > 1 {
4048            if !events.is_empty() && rng.gen_bool(0.4) {
4049                let len = rng.gen_range(0..=events.len());
4050                let to_deliver = events.drain(0..len).collect::<Vec<_>>();
4051                log::info!("Delivering events: {:#?}", to_deliver);
4052                smol::block_on(scanner.process_events(to_deliver));
4053                scanner.snapshot().check_invariants();
4054            } else {
4055                events.extend(randomly_mutate_tree(root_dir.path(), 0.6, &mut rng).unwrap());
4056                mutations_len -= 1;
4057            }
4058
4059            if rng.gen_bool(0.2) {
4060                snapshots.push(scanner.snapshot());
4061            }
4062        }
4063        log::info!("Quiescing: {:#?}", events);
4064        smol::block_on(scanner.process_events(events));
4065        scanner.snapshot().check_invariants();
4066
4067        let (notify_tx, _notify_rx) = smol::channel::unbounded();
4068        let mut new_scanner = BackgroundScanner::new(
4069            Arc::new(Mutex::new(initial_snapshot)),
4070            notify_tx,
4071            scanner.fs.clone(),
4072            scanner.executor.clone(),
4073        );
4074        smol::block_on(new_scanner.scan_dirs()).unwrap();
4075        assert_eq!(
4076            scanner.snapshot().to_vec(true),
4077            new_scanner.snapshot().to_vec(true)
4078        );
4079
4080        for mut prev_snapshot in snapshots {
4081            let include_ignored = rng.gen::<bool>();
4082            if !include_ignored {
4083                let mut entries_by_path_edits = Vec::new();
4084                let mut entries_by_id_edits = Vec::new();
4085                for entry in prev_snapshot
4086                    .entries_by_id
4087                    .cursor::<()>()
4088                    .filter(|e| e.is_ignored)
4089                {
4090                    entries_by_path_edits.push(Edit::Remove(PathKey(entry.path.clone())));
4091                    entries_by_id_edits.push(Edit::Remove(entry.id));
4092                }
4093
4094                prev_snapshot
4095                    .entries_by_path
4096                    .edit(entries_by_path_edits, &());
4097                prev_snapshot.entries_by_id.edit(entries_by_id_edits, &());
4098            }
4099
4100            let update = scanner
4101                .snapshot()
4102                .build_update(&prev_snapshot, 0, 0, include_ignored);
4103            prev_snapshot.apply_update(update).unwrap();
4104            assert_eq!(
4105                prev_snapshot.to_vec(true),
4106                scanner.snapshot().to_vec(include_ignored)
4107            );
4108        }
4109    }
4110
4111    fn randomly_mutate_tree(
4112        root_path: &Path,
4113        insertion_probability: f64,
4114        rng: &mut impl Rng,
4115    ) -> Result<Vec<fsevent::Event>> {
4116        let root_path = root_path.canonicalize().unwrap();
4117        let (dirs, files) = read_dir_recursive(root_path.clone());
4118
4119        let mut events = Vec::new();
4120        let mut record_event = |path: PathBuf| {
4121            events.push(fsevent::Event {
4122                event_id: SystemTime::now()
4123                    .duration_since(UNIX_EPOCH)
4124                    .unwrap()
4125                    .as_secs(),
4126                flags: fsevent::StreamFlags::empty(),
4127                path,
4128            });
4129        };
4130
4131        if (files.is_empty() && dirs.len() == 1) || rng.gen_bool(insertion_probability) {
4132            let path = dirs.choose(rng).unwrap();
4133            let new_path = path.join(gen_name(rng));
4134
4135            if rng.gen() {
4136                log::info!("Creating dir {:?}", new_path.strip_prefix(root_path)?);
4137                std::fs::create_dir(&new_path)?;
4138            } else {
4139                log::info!("Creating file {:?}", new_path.strip_prefix(root_path)?);
4140                std::fs::write(&new_path, "")?;
4141            }
4142            record_event(new_path);
4143        } else if rng.gen_bool(0.05) {
4144            let ignore_dir_path = dirs.choose(rng).unwrap();
4145            let ignore_path = ignore_dir_path.join(&*GITIGNORE);
4146
4147            let (subdirs, subfiles) = read_dir_recursive(ignore_dir_path.clone());
4148            let files_to_ignore = {
4149                let len = rng.gen_range(0..=subfiles.len());
4150                subfiles.choose_multiple(rng, len)
4151            };
4152            let dirs_to_ignore = {
4153                let len = rng.gen_range(0..subdirs.len());
4154                subdirs.choose_multiple(rng, len)
4155            };
4156
4157            let mut ignore_contents = String::new();
4158            for path_to_ignore in files_to_ignore.chain(dirs_to_ignore) {
4159                write!(
4160                    ignore_contents,
4161                    "{}\n",
4162                    path_to_ignore
4163                        .strip_prefix(&ignore_dir_path)?
4164                        .to_str()
4165                        .unwrap()
4166                )
4167                .unwrap();
4168            }
4169            log::info!(
4170                "Creating {:?} with contents:\n{}",
4171                ignore_path.strip_prefix(&root_path)?,
4172                ignore_contents
4173            );
4174            std::fs::write(&ignore_path, ignore_contents).unwrap();
4175            record_event(ignore_path);
4176        } else {
4177            let old_path = {
4178                let file_path = files.choose(rng);
4179                let dir_path = dirs[1..].choose(rng);
4180                file_path.into_iter().chain(dir_path).choose(rng).unwrap()
4181            };
4182
4183            let is_rename = rng.gen();
4184            if is_rename {
4185                let new_path_parent = dirs
4186                    .iter()
4187                    .filter(|d| !d.starts_with(old_path))
4188                    .choose(rng)
4189                    .unwrap();
4190
4191                let overwrite_existing_dir =
4192                    !old_path.starts_with(&new_path_parent) && rng.gen_bool(0.3);
4193                let new_path = if overwrite_existing_dir {
4194                    std::fs::remove_dir_all(&new_path_parent).ok();
4195                    new_path_parent.to_path_buf()
4196                } else {
4197                    new_path_parent.join(gen_name(rng))
4198                };
4199
4200                log::info!(
4201                    "Renaming {:?} to {}{:?}",
4202                    old_path.strip_prefix(&root_path)?,
4203                    if overwrite_existing_dir {
4204                        "overwrite "
4205                    } else {
4206                        ""
4207                    },
4208                    new_path.strip_prefix(&root_path)?
4209                );
4210                std::fs::rename(&old_path, &new_path)?;
4211                record_event(old_path.clone());
4212                record_event(new_path);
4213            } else if old_path.is_dir() {
4214                let (dirs, files) = read_dir_recursive(old_path.clone());
4215
4216                log::info!("Deleting dir {:?}", old_path.strip_prefix(&root_path)?);
4217                std::fs::remove_dir_all(&old_path).unwrap();
4218                for file in files {
4219                    record_event(file);
4220                }
4221                for dir in dirs {
4222                    record_event(dir);
4223                }
4224            } else {
4225                log::info!("Deleting file {:?}", old_path.strip_prefix(&root_path)?);
4226                std::fs::remove_file(old_path).unwrap();
4227                record_event(old_path.clone());
4228            }
4229        }
4230
4231        Ok(events)
4232    }
4233
4234    fn read_dir_recursive(path: PathBuf) -> (Vec<PathBuf>, Vec<PathBuf>) {
4235        let child_entries = std::fs::read_dir(&path).unwrap();
4236        let mut dirs = vec![path];
4237        let mut files = Vec::new();
4238        for child_entry in child_entries {
4239            let child_path = child_entry.unwrap().path();
4240            if child_path.is_dir() {
4241                let (child_dirs, child_files) = read_dir_recursive(child_path);
4242                dirs.extend(child_dirs);
4243                files.extend(child_files);
4244            } else {
4245                files.push(child_path);
4246            }
4247        }
4248        (dirs, files)
4249    }
4250
4251    fn gen_name(rng: &mut impl Rng) -> String {
4252        (0..6)
4253            .map(|_| rng.sample(rand::distributions::Alphanumeric))
4254            .map(char::from)
4255            .collect()
4256    }
4257
4258    impl Snapshot {
4259        fn check_invariants(&self) {
4260            let mut files = self.files(true, 0);
4261            let mut visible_files = self.files(false, 0);
4262            for entry in self.entries_by_path.cursor::<()>() {
4263                if entry.is_file() {
4264                    assert_eq!(files.next().unwrap().inode, entry.inode);
4265                    if !entry.is_ignored {
4266                        assert_eq!(visible_files.next().unwrap().inode, entry.inode);
4267                    }
4268                }
4269            }
4270            assert!(files.next().is_none());
4271            assert!(visible_files.next().is_none());
4272
4273            let mut bfs_paths = Vec::new();
4274            let mut stack = vec![Path::new("")];
4275            while let Some(path) = stack.pop() {
4276                bfs_paths.push(path);
4277                let ix = stack.len();
4278                for child_entry in self.child_entries(path) {
4279                    stack.insert(ix, &child_entry.path);
4280                }
4281            }
4282
4283            let dfs_paths = self
4284                .entries_by_path
4285                .cursor::<()>()
4286                .map(|e| e.path.as_ref())
4287                .collect::<Vec<_>>();
4288            assert_eq!(bfs_paths, dfs_paths);
4289
4290            for (ignore_parent_path, _) in &self.ignores {
4291                assert!(self.entry_for_path(ignore_parent_path).is_some());
4292                assert!(self
4293                    .entry_for_path(ignore_parent_path.join(&*GITIGNORE))
4294                    .is_some());
4295            }
4296        }
4297
4298        fn to_vec(&self, include_ignored: bool) -> Vec<(&Path, u64, bool)> {
4299            let mut paths = Vec::new();
4300            for entry in self.entries_by_path.cursor::<()>() {
4301                if include_ignored || !entry.is_ignored {
4302                    paths.push((entry.path.as_ref(), entry.inode, entry.is_ignored));
4303                }
4304            }
4305            paths.sort_by(|a, b| a.0.cmp(&b.0));
4306            paths
4307        }
4308    }
4309}