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