worktree.rs

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