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