worktree.rs

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