worktree.rs

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