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