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