worktree.rs

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