buffer_store.rs

   1use crate::{
   2    ProjectItem as _, ProjectPath,
   3    lsp_store::OpenLspBufferHandle,
   4    search::SearchQuery,
   5    worktree_store::{WorktreeStore, WorktreeStoreEvent},
   6};
   7use anyhow::{Context as _, Result, anyhow};
   8use client::Client;
   9use collections::{HashMap, HashSet, hash_map};
  10use fs::Fs;
  11use futures::{Future, FutureExt as _, StreamExt, channel::oneshot, future::Shared};
  12use gpui::{
  13    App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Subscription, Task, WeakEntity,
  14};
  15use language::{
  16    Buffer, BufferEvent, Capability, DiskState, File as _, Language, Operation,
  17    proto::{
  18        deserialize_line_ending, deserialize_version, serialize_line_ending, serialize_version,
  19        split_operations,
  20    },
  21};
  22use rpc::{
  23    AnyProtoClient, ErrorCode, ErrorExt as _, TypedEnvelope,
  24    proto::{self},
  25};
  26use smol::channel::Receiver;
  27use std::{io, pin::pin, sync::Arc, time::Instant};
  28use text::{BufferId, ReplicaId};
  29use util::{ResultExt as _, TryFutureExt, debug_panic, maybe, paths::PathStyle, rel_path::RelPath};
  30use worktree::{File, PathChange, ProjectEntryId, Worktree, WorktreeId};
  31
  32/// A set of open buffers.
  33pub struct BufferStore {
  34    state: BufferStoreState,
  35    #[allow(clippy::type_complexity)]
  36    loading_buffers: HashMap<ProjectPath, Shared<Task<Result<Entity<Buffer>, Arc<anyhow::Error>>>>>,
  37    worktree_store: Entity<WorktreeStore>,
  38    opened_buffers: HashMap<BufferId, OpenBuffer>,
  39    path_to_buffer_id: HashMap<ProjectPath, BufferId>,
  40    downstream_client: Option<(AnyProtoClient, u64)>,
  41    shared_buffers: HashMap<proto::PeerId, HashMap<BufferId, SharedBuffer>>,
  42    non_searchable_buffers: HashSet<BufferId>,
  43}
  44
  45#[derive(Hash, Eq, PartialEq, Clone)]
  46struct SharedBuffer {
  47    buffer: Entity<Buffer>,
  48    lsp_handle: Option<OpenLspBufferHandle>,
  49}
  50
  51enum BufferStoreState {
  52    Local(LocalBufferStore),
  53    Remote(RemoteBufferStore),
  54}
  55
  56struct RemoteBufferStore {
  57    shared_with_me: HashSet<Entity<Buffer>>,
  58    upstream_client: AnyProtoClient,
  59    project_id: u64,
  60    loading_remote_buffers_by_id: HashMap<BufferId, Entity<Buffer>>,
  61    remote_buffer_listeners:
  62        HashMap<BufferId, Vec<oneshot::Sender<anyhow::Result<Entity<Buffer>>>>>,
  63    worktree_store: Entity<WorktreeStore>,
  64}
  65
  66struct LocalBufferStore {
  67    local_buffer_ids_by_entry_id: HashMap<ProjectEntryId, BufferId>,
  68    worktree_store: Entity<WorktreeStore>,
  69    _subscription: Subscription,
  70}
  71
  72enum OpenBuffer {
  73    Complete { buffer: WeakEntity<Buffer> },
  74    Operations(Vec<Operation>),
  75}
  76
  77pub enum BufferStoreEvent {
  78    BufferAdded(Entity<Buffer>),
  79    BufferOpened {
  80        buffer: Entity<Buffer>,
  81        project_path: ProjectPath,
  82    },
  83    SharedBufferClosed(proto::PeerId, BufferId),
  84    BufferDropped(BufferId),
  85    BufferChangedFilePath {
  86        buffer: Entity<Buffer>,
  87        old_file: Option<Arc<dyn language::File>>,
  88    },
  89}
  90
  91#[derive(Default, Debug, Clone)]
  92pub struct ProjectTransaction(pub HashMap<Entity<Buffer>, language::Transaction>);
  93
  94impl PartialEq for ProjectTransaction {
  95    fn eq(&self, other: &Self) -> bool {
  96        self.0.len() == other.0.len()
  97            && self.0.iter().all(|(buffer, transaction)| {
  98                other.0.get(buffer).is_some_and(|t| t.id == transaction.id)
  99            })
 100    }
 101}
 102
 103impl EventEmitter<BufferStoreEvent> for BufferStore {}
 104
 105impl RemoteBufferStore {
 106    pub fn wait_for_remote_buffer(
 107        &mut self,
 108        id: BufferId,
 109        cx: &mut Context<BufferStore>,
 110    ) -> Task<Result<Entity<Buffer>>> {
 111        let (tx, rx) = oneshot::channel();
 112        self.remote_buffer_listeners.entry(id).or_default().push(tx);
 113
 114        cx.spawn(async move |this, cx| {
 115            if let Some(buffer) = this
 116                .read_with(cx, |buffer_store, _| buffer_store.get(id))
 117                .ok()
 118                .flatten()
 119            {
 120                return Ok(buffer);
 121            }
 122
 123            cx.background_spawn(async move { rx.await? }).await
 124        })
 125    }
 126
 127    fn save_remote_buffer(
 128        &self,
 129        buffer_handle: Entity<Buffer>,
 130        new_path: Option<proto::ProjectPath>,
 131        cx: &Context<BufferStore>,
 132    ) -> Task<Result<()>> {
 133        let buffer = buffer_handle.read(cx);
 134        let buffer_id = buffer.remote_id().into();
 135        let version = buffer.version();
 136        let rpc = self.upstream_client.clone();
 137        let project_id = self.project_id;
 138        cx.spawn(async move |_, cx| {
 139            let response = rpc
 140                .request(proto::SaveBuffer {
 141                    project_id,
 142                    buffer_id,
 143                    new_path,
 144                    version: serialize_version(&version),
 145                })
 146                .await?;
 147            let version = deserialize_version(&response.version);
 148            let mtime = response.mtime.map(|mtime| mtime.into());
 149
 150            buffer_handle.update(cx, |buffer, cx| {
 151                buffer.did_save(version.clone(), mtime, cx);
 152            })?;
 153
 154            Ok(())
 155        })
 156    }
 157
 158    pub fn handle_create_buffer_for_peer(
 159        &mut self,
 160        envelope: TypedEnvelope<proto::CreateBufferForPeer>,
 161        replica_id: ReplicaId,
 162        capability: Capability,
 163        cx: &mut Context<BufferStore>,
 164    ) -> Result<Option<Entity<Buffer>>> {
 165        match envelope.payload.variant.context("missing variant")? {
 166            proto::create_buffer_for_peer::Variant::State(mut state) => {
 167                let buffer_id = BufferId::new(state.id)?;
 168
 169                let buffer_result = maybe!({
 170                    let mut buffer_file = None;
 171                    if let Some(file) = state.file.take() {
 172                        let worktree_id = worktree::WorktreeId::from_proto(file.worktree_id);
 173                        let worktree = self
 174                            .worktree_store
 175                            .read(cx)
 176                            .worktree_for_id(worktree_id, cx)
 177                            .with_context(|| {
 178                                format!("no worktree found for id {}", file.worktree_id)
 179                            })?;
 180                        buffer_file = Some(Arc::new(File::from_proto(file, worktree, cx)?)
 181                            as Arc<dyn language::File>);
 182                    }
 183                    Buffer::from_proto(
 184                        replica_id,
 185                        capability,
 186                        state,
 187                        buffer_file,
 188                        cx.background_executor(),
 189                    )
 190                });
 191
 192                match buffer_result {
 193                    Ok(buffer) => {
 194                        let buffer = cx.new(|_| buffer);
 195                        self.loading_remote_buffers_by_id.insert(buffer_id, buffer);
 196                    }
 197                    Err(error) => {
 198                        if let Some(listeners) = self.remote_buffer_listeners.remove(&buffer_id) {
 199                            for listener in listeners {
 200                                listener.send(Err(anyhow!(error.cloned()))).ok();
 201                            }
 202                        }
 203                    }
 204                }
 205            }
 206            proto::create_buffer_for_peer::Variant::Chunk(chunk) => {
 207                let buffer_id = BufferId::new(chunk.buffer_id)?;
 208                let buffer = self
 209                    .loading_remote_buffers_by_id
 210                    .get(&buffer_id)
 211                    .cloned()
 212                    .with_context(|| {
 213                        format!(
 214                            "received chunk for buffer {} without initial state",
 215                            chunk.buffer_id
 216                        )
 217                    })?;
 218
 219                let result = maybe!({
 220                    let operations = chunk
 221                        .operations
 222                        .into_iter()
 223                        .map(language::proto::deserialize_operation)
 224                        .collect::<Result<Vec<_>>>()?;
 225                    buffer.update(cx, |buffer, cx| buffer.apply_ops(operations, cx));
 226                    anyhow::Ok(())
 227                });
 228
 229                if let Err(error) = result {
 230                    self.loading_remote_buffers_by_id.remove(&buffer_id);
 231                    if let Some(listeners) = self.remote_buffer_listeners.remove(&buffer_id) {
 232                        for listener in listeners {
 233                            listener.send(Err(error.cloned())).ok();
 234                        }
 235                    }
 236                } else if chunk.is_last {
 237                    self.loading_remote_buffers_by_id.remove(&buffer_id);
 238                    if self.upstream_client.is_via_collab() {
 239                        // retain buffers sent by peers to avoid races.
 240                        self.shared_with_me.insert(buffer.clone());
 241                    }
 242
 243                    if let Some(senders) = self.remote_buffer_listeners.remove(&buffer_id) {
 244                        for sender in senders {
 245                            sender.send(Ok(buffer.clone())).ok();
 246                        }
 247                    }
 248                    return Ok(Some(buffer));
 249                }
 250            }
 251        }
 252        Ok(None)
 253    }
 254
 255    pub fn incomplete_buffer_ids(&self) -> Vec<BufferId> {
 256        self.loading_remote_buffers_by_id
 257            .keys()
 258            .copied()
 259            .collect::<Vec<_>>()
 260    }
 261
 262    pub fn deserialize_project_transaction(
 263        &self,
 264        message: proto::ProjectTransaction,
 265        push_to_history: bool,
 266        cx: &mut Context<BufferStore>,
 267    ) -> Task<Result<ProjectTransaction>> {
 268        cx.spawn(async move |this, cx| {
 269            let mut project_transaction = ProjectTransaction::default();
 270            for (buffer_id, transaction) in message.buffer_ids.into_iter().zip(message.transactions)
 271            {
 272                let buffer_id = BufferId::new(buffer_id)?;
 273                let buffer = this
 274                    .update(cx, |this, cx| this.wait_for_remote_buffer(buffer_id, cx))?
 275                    .await?;
 276                let transaction = language::proto::deserialize_transaction(transaction)?;
 277                project_transaction.0.insert(buffer, transaction);
 278            }
 279
 280            for (buffer, transaction) in &project_transaction.0 {
 281                buffer
 282                    .update(cx, |buffer, _| {
 283                        buffer.wait_for_edits(transaction.edit_ids.iter().copied())
 284                    })?
 285                    .await?;
 286
 287                if push_to_history {
 288                    buffer.update(cx, |buffer, _| {
 289                        buffer.push_transaction(transaction.clone(), Instant::now());
 290                        buffer.finalize_last_transaction();
 291                    })?;
 292                }
 293            }
 294
 295            Ok(project_transaction)
 296        })
 297    }
 298
 299    fn open_buffer(
 300        &self,
 301        path: Arc<RelPath>,
 302        worktree: Entity<Worktree>,
 303        cx: &mut Context<BufferStore>,
 304    ) -> Task<Result<Entity<Buffer>>> {
 305        let worktree_id = worktree.read(cx).id().to_proto();
 306        let project_id = self.project_id;
 307        let client = self.upstream_client.clone();
 308        cx.spawn(async move |this, cx| {
 309            let response = client
 310                .request(proto::OpenBufferByPath {
 311                    project_id,
 312                    worktree_id,
 313                    path: path.to_proto(),
 314                })
 315                .await?;
 316            let buffer_id = BufferId::new(response.buffer_id)?;
 317
 318            let buffer = this
 319                .update(cx, {
 320                    |this, cx| this.wait_for_remote_buffer(buffer_id, cx)
 321                })?
 322                .await?;
 323
 324            Ok(buffer)
 325        })
 326    }
 327
 328    fn create_buffer(
 329        &self,
 330        project_searchable: bool,
 331        cx: &mut Context<BufferStore>,
 332    ) -> Task<Result<Entity<Buffer>>> {
 333        let create = self.upstream_client.request(proto::OpenNewBuffer {
 334            project_id: self.project_id,
 335        });
 336        cx.spawn(async move |this, cx| {
 337            let response = create.await?;
 338            let buffer_id = BufferId::new(response.buffer_id)?;
 339
 340            this.update(cx, |this, cx| {
 341                if !project_searchable {
 342                    this.non_searchable_buffers.insert(buffer_id);
 343                }
 344                this.wait_for_remote_buffer(buffer_id, cx)
 345            })?
 346            .await
 347        })
 348    }
 349
 350    fn reload_buffers(
 351        &self,
 352        buffers: HashSet<Entity<Buffer>>,
 353        push_to_history: bool,
 354        cx: &mut Context<BufferStore>,
 355    ) -> Task<Result<ProjectTransaction>> {
 356        let request = self.upstream_client.request(proto::ReloadBuffers {
 357            project_id: self.project_id,
 358            buffer_ids: buffers
 359                .iter()
 360                .map(|buffer| buffer.read(cx).remote_id().to_proto())
 361                .collect(),
 362        });
 363
 364        cx.spawn(async move |this, cx| {
 365            let response = request.await?.transaction.context("missing transaction")?;
 366            this.update(cx, |this, cx| {
 367                this.deserialize_project_transaction(response, push_to_history, cx)
 368            })?
 369            .await
 370        })
 371    }
 372}
 373
 374impl LocalBufferStore {
 375    fn save_local_buffer(
 376        &self,
 377        buffer_handle: Entity<Buffer>,
 378        worktree: Entity<Worktree>,
 379        path: Arc<RelPath>,
 380        mut has_changed_file: bool,
 381        cx: &mut Context<BufferStore>,
 382    ) -> Task<Result<()>> {
 383        let buffer = buffer_handle.read(cx);
 384
 385        let text = buffer.as_rope().clone();
 386        let line_ending = buffer.line_ending();
 387        let version = buffer.version();
 388        let buffer_id = buffer.remote_id();
 389        let file = buffer.file().cloned();
 390        let encoding = buffer.encoding.clone();
 391
 392        if file
 393            .as_ref()
 394            .is_some_and(|file| file.disk_state() == DiskState::New)
 395        {
 396            has_changed_file = true;
 397        }
 398
 399        let save = worktree.update(cx, |worktree, cx| {
 400            worktree.write_file(
 401                path.as_ref(),
 402                text,
 403                line_ending,
 404                cx,
 405                &encoding.lock().unwrap(),
 406            )
 407        });
 408
 409        cx.spawn(async move |this, cx| {
 410            let new_file = save.await?;
 411            let mtime = new_file.disk_state().mtime();
 412            this.update(cx, |this, cx| {
 413                if let Some((downstream_client, project_id)) = this.downstream_client.clone() {
 414                    if has_changed_file {
 415                        downstream_client
 416                            .send(proto::UpdateBufferFile {
 417                                project_id,
 418                                buffer_id: buffer_id.to_proto(),
 419                                file: Some(language::File::to_proto(&*new_file, cx)),
 420                            })
 421                            .log_err();
 422                    }
 423                    downstream_client
 424                        .send(proto::BufferSaved {
 425                            project_id,
 426                            buffer_id: buffer_id.to_proto(),
 427                            version: serialize_version(&version),
 428                            mtime: mtime.map(|time| time.into()),
 429                        })
 430                        .log_err();
 431                }
 432            })?;
 433            buffer_handle.update(cx, |buffer, cx| {
 434                if has_changed_file {
 435                    buffer.file_updated(new_file, cx);
 436                }
 437                buffer.did_save(version.clone(), mtime, cx);
 438            })
 439        })
 440    }
 441
 442    fn subscribe_to_worktree(
 443        &mut self,
 444        worktree: &Entity<Worktree>,
 445        cx: &mut Context<BufferStore>,
 446    ) {
 447        cx.subscribe(worktree, |this, worktree, event, cx| {
 448            if worktree.read(cx).is_local()
 449                && let worktree::Event::UpdatedEntries(changes) = event
 450            {
 451                Self::local_worktree_entries_changed(this, &worktree, changes, cx);
 452            }
 453        })
 454        .detach();
 455    }
 456
 457    fn local_worktree_entries_changed(
 458        this: &mut BufferStore,
 459        worktree_handle: &Entity<Worktree>,
 460        changes: &[(Arc<RelPath>, ProjectEntryId, PathChange)],
 461        cx: &mut Context<BufferStore>,
 462    ) {
 463        let snapshot = worktree_handle.read(cx).snapshot();
 464        for (path, entry_id, _) in changes {
 465            Self::local_worktree_entry_changed(
 466                this,
 467                *entry_id,
 468                path,
 469                worktree_handle,
 470                &snapshot,
 471                cx,
 472            );
 473        }
 474    }
 475
 476    fn local_worktree_entry_changed(
 477        this: &mut BufferStore,
 478        entry_id: ProjectEntryId,
 479        path: &Arc<RelPath>,
 480        worktree: &Entity<worktree::Worktree>,
 481        snapshot: &worktree::Snapshot,
 482        cx: &mut Context<BufferStore>,
 483    ) -> Option<()> {
 484        let project_path = ProjectPath {
 485            worktree_id: snapshot.id(),
 486            path: path.clone(),
 487        };
 488
 489        let buffer_id = this
 490            .as_local_mut()
 491            .and_then(|local| local.local_buffer_ids_by_entry_id.get(&entry_id))
 492            .copied()
 493            .or_else(|| this.path_to_buffer_id.get(&project_path).copied())?;
 494
 495        let buffer = if let Some(buffer) = this.get(buffer_id) {
 496            Some(buffer)
 497        } else {
 498            this.opened_buffers.remove(&buffer_id);
 499            this.non_searchable_buffers.remove(&buffer_id);
 500            None
 501        };
 502
 503        let buffer = if let Some(buffer) = buffer {
 504            buffer
 505        } else {
 506            this.path_to_buffer_id.remove(&project_path);
 507            let this = this.as_local_mut()?;
 508            this.local_buffer_ids_by_entry_id.remove(&entry_id);
 509            return None;
 510        };
 511
 512        let events = buffer.update(cx, |buffer, cx| {
 513            let file = buffer.file()?;
 514            let old_file = File::from_dyn(Some(file))?;
 515            if old_file.worktree != *worktree {
 516                return None;
 517            }
 518
 519            let snapshot_entry = old_file
 520                .entry_id
 521                .and_then(|entry_id| snapshot.entry_for_id(entry_id))
 522                .or_else(|| snapshot.entry_for_path(old_file.path.as_ref()));
 523
 524            let new_file = if let Some(entry) = snapshot_entry {
 525                File {
 526                    disk_state: match entry.mtime {
 527                        Some(mtime) => DiskState::Present { mtime },
 528                        None => old_file.disk_state,
 529                    },
 530                    is_local: true,
 531                    entry_id: Some(entry.id),
 532                    path: entry.path.clone(),
 533                    worktree: worktree.clone(),
 534                    is_private: entry.is_private,
 535                }
 536            } else {
 537                File {
 538                    disk_state: DiskState::Deleted,
 539                    is_local: true,
 540                    entry_id: old_file.entry_id,
 541                    path: old_file.path.clone(),
 542                    worktree: worktree.clone(),
 543                    is_private: old_file.is_private,
 544                }
 545            };
 546
 547            if new_file == *old_file {
 548                return None;
 549            }
 550
 551            let mut events = Vec::new();
 552            if new_file.path != old_file.path {
 553                this.path_to_buffer_id.remove(&ProjectPath {
 554                    path: old_file.path.clone(),
 555                    worktree_id: old_file.worktree_id(cx),
 556                });
 557                this.path_to_buffer_id.insert(
 558                    ProjectPath {
 559                        worktree_id: new_file.worktree_id(cx),
 560                        path: new_file.path.clone(),
 561                    },
 562                    buffer_id,
 563                );
 564                events.push(BufferStoreEvent::BufferChangedFilePath {
 565                    buffer: cx.entity(),
 566                    old_file: buffer.file().cloned(),
 567                });
 568            }
 569            let local = this.as_local_mut()?;
 570            if new_file.entry_id != old_file.entry_id {
 571                if let Some(entry_id) = old_file.entry_id {
 572                    local.local_buffer_ids_by_entry_id.remove(&entry_id);
 573                }
 574                if let Some(entry_id) = new_file.entry_id {
 575                    local
 576                        .local_buffer_ids_by_entry_id
 577                        .insert(entry_id, buffer_id);
 578                }
 579            }
 580
 581            if let Some((client, project_id)) = &this.downstream_client {
 582                client
 583                    .send(proto::UpdateBufferFile {
 584                        project_id: *project_id,
 585                        buffer_id: buffer_id.to_proto(),
 586                        file: Some(new_file.to_proto(cx)),
 587                    })
 588                    .ok();
 589            }
 590
 591            buffer.file_updated(Arc::new(new_file), cx);
 592            Some(events)
 593        })?;
 594
 595        for event in events {
 596            cx.emit(event);
 597        }
 598
 599        None
 600    }
 601
 602    fn save_buffer(
 603        &self,
 604        buffer: Entity<Buffer>,
 605        cx: &mut Context<BufferStore>,
 606    ) -> Task<Result<()>> {
 607        let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
 608            return Task::ready(Err(anyhow!("buffer doesn't have a file")));
 609        };
 610        let worktree = file.worktree.clone();
 611        self.save_local_buffer(buffer, worktree, file.path.clone(), false, cx)
 612    }
 613
 614    fn save_buffer_as(
 615        &self,
 616        buffer: Entity<Buffer>,
 617        path: ProjectPath,
 618        cx: &mut Context<BufferStore>,
 619    ) -> Task<Result<()>> {
 620        let Some(worktree) = self
 621            .worktree_store
 622            .read(cx)
 623            .worktree_for_id(path.worktree_id, cx)
 624        else {
 625            return Task::ready(Err(anyhow!("no such worktree")));
 626        };
 627        self.save_local_buffer(buffer, worktree, path.path, true, cx)
 628    }
 629
 630    fn open_buffer(
 631        &self,
 632        path: Arc<RelPath>,
 633        worktree: Entity<Worktree>,
 634        cx: &mut Context<BufferStore>,
 635    ) -> Task<Result<Entity<Buffer>>> {
 636        let load_buffer = worktree.update(cx, |worktree, cx| {
 637            let load_file = worktree.load_file(path.as_ref(), None, cx);
 638            let reservation = cx.reserve_entity();
 639            let buffer_id = BufferId::from(reservation.entity_id().as_non_zero_u64());
 640            let path = path.clone();
 641            cx.spawn(async move |_, cx| {
 642                let loaded = load_file.await.with_context(|| {
 643                    format!("Could not open path: {}", path.display(PathStyle::local()))
 644                })?;
 645                let text_buffer = cx
 646                    .background_spawn(async move {
 647                        text::Buffer::new(ReplicaId::LOCAL, buffer_id, loaded.text)
 648                    })
 649                    .await;
 650                cx.insert_entity(reservation, |_| {
 651                    Buffer::build(text_buffer, Some(loaded.file), Capability::ReadWrite)
 652                })
 653            })
 654        });
 655
 656        cx.spawn(async move |this, cx| {
 657            let buffer = match load_buffer.await {
 658                Ok(buffer) => {
 659                    // Reload the buffer to trigger UTF-16 detection
 660                    buffer.update(cx, |buffer, cx| buffer.reload(cx))?.await?;
 661                    Ok(buffer)
 662                }
 663                Err(error) if is_not_found_error(&error) => cx.new(|cx| {
 664                    let buffer_id = BufferId::from(cx.entity_id().as_non_zero_u64());
 665                    let text_buffer = text::Buffer::new(
 666                        ReplicaId::LOCAL,
 667                        buffer_id,
 668                        "",
 669                        cx.background_executor(),
 670                    );
 671                    Buffer::build(
 672                        text_buffer,
 673                        Some(Arc::new(File {
 674                            worktree,
 675                            path,
 676                            disk_state: DiskState::New,
 677                            entry_id: None,
 678                            is_local: true,
 679                            is_private: false,
 680                        })),
 681                        Capability::ReadWrite,
 682                    )
 683                })?,
 684                Err(e) => return Err(e),
 685            };
 686            this.update(cx, |this, cx| {
 687                this.add_buffer(buffer.clone(), cx)?;
 688                let buffer_id = buffer.read(cx).remote_id();
 689                if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
 690                    this.path_to_buffer_id.insert(
 691                        ProjectPath {
 692                            worktree_id: file.worktree_id(cx),
 693                            path: file.path.clone(),
 694                        },
 695                        buffer_id,
 696                    );
 697                    let this = this.as_local_mut().unwrap();
 698                    if let Some(entry_id) = file.entry_id {
 699                        this.local_buffer_ids_by_entry_id
 700                            .insert(entry_id, buffer_id);
 701                    }
 702                }
 703
 704                anyhow::Ok(())
 705            })??;
 706
 707            Ok(buffer)
 708        })
 709    }
 710
 711    fn create_buffer(
 712        &self,
 713        project_searchable: bool,
 714        cx: &mut Context<BufferStore>,
 715    ) -> Task<Result<Entity<Buffer>>> {
 716        cx.spawn(async move |buffer_store, cx| {
 717            let buffer =
 718                cx.new(|cx| Buffer::local("", cx).with_language(language::PLAIN_TEXT.clone(), cx))?;
 719            buffer_store.update(cx, |buffer_store, cx| {
 720                buffer_store.add_buffer(buffer.clone(), cx).log_err();
 721                if !project_searchable {
 722                    buffer_store
 723                        .non_searchable_buffers
 724                        .insert(buffer.read(cx).remote_id());
 725                }
 726            })?;
 727            Ok(buffer)
 728        })
 729    }
 730
 731    fn reload_buffers(
 732        &self,
 733        buffers: HashSet<Entity<Buffer>>,
 734        push_to_history: bool,
 735        cx: &mut Context<BufferStore>,
 736    ) -> Task<Result<ProjectTransaction>> {
 737        cx.spawn(async move |_, cx| {
 738            let mut project_transaction = ProjectTransaction::default();
 739            for buffer in buffers {
 740                let transaction = buffer.update(cx, |buffer, cx| buffer.reload(cx))?.await?;
 741                buffer.update(cx, |buffer, cx| {
 742                    if let Some(transaction) = transaction {
 743                        if !push_to_history {
 744                            buffer.forget_transaction(transaction.id);
 745                        }
 746                        project_transaction.0.insert(cx.entity(), transaction);
 747                    }
 748                })?;
 749            }
 750
 751            Ok(project_transaction)
 752        })
 753    }
 754}
 755
 756impl BufferStore {
 757    pub fn init(client: &AnyProtoClient) {
 758        client.add_entity_message_handler(Self::handle_buffer_reloaded);
 759        client.add_entity_message_handler(Self::handle_buffer_saved);
 760        client.add_entity_message_handler(Self::handle_update_buffer_file);
 761        client.add_entity_request_handler(Self::handle_save_buffer);
 762        client.add_entity_request_handler(Self::handle_reload_buffers);
 763    }
 764
 765    /// Creates a buffer store, optionally retaining its buffers.
 766    pub fn local(worktree_store: Entity<WorktreeStore>, cx: &mut Context<Self>) -> Self {
 767        Self {
 768            state: BufferStoreState::Local(LocalBufferStore {
 769                local_buffer_ids_by_entry_id: Default::default(),
 770                worktree_store: worktree_store.clone(),
 771                _subscription: cx.subscribe(&worktree_store, |this, _, event, cx| {
 772                    if let WorktreeStoreEvent::WorktreeAdded(worktree) = event {
 773                        let this = this.as_local_mut().unwrap();
 774                        this.subscribe_to_worktree(worktree, cx);
 775                    }
 776                }),
 777            }),
 778            downstream_client: None,
 779            opened_buffers: Default::default(),
 780            path_to_buffer_id: Default::default(),
 781            shared_buffers: Default::default(),
 782            loading_buffers: Default::default(),
 783            non_searchable_buffers: Default::default(),
 784            worktree_store,
 785        }
 786    }
 787
 788    pub fn remote(
 789        worktree_store: Entity<WorktreeStore>,
 790        upstream_client: AnyProtoClient,
 791        remote_id: u64,
 792        _cx: &mut Context<Self>,
 793    ) -> Self {
 794        Self {
 795            state: BufferStoreState::Remote(RemoteBufferStore {
 796                shared_with_me: Default::default(),
 797                loading_remote_buffers_by_id: Default::default(),
 798                remote_buffer_listeners: Default::default(),
 799                project_id: remote_id,
 800                upstream_client,
 801                worktree_store: worktree_store.clone(),
 802            }),
 803            downstream_client: None,
 804            opened_buffers: Default::default(),
 805            path_to_buffer_id: Default::default(),
 806            loading_buffers: Default::default(),
 807            shared_buffers: Default::default(),
 808            non_searchable_buffers: Default::default(),
 809            worktree_store,
 810        }
 811    }
 812
 813    fn as_local_mut(&mut self) -> Option<&mut LocalBufferStore> {
 814        match &mut self.state {
 815            BufferStoreState::Local(state) => Some(state),
 816            _ => None,
 817        }
 818    }
 819
 820    fn as_remote_mut(&mut self) -> Option<&mut RemoteBufferStore> {
 821        match &mut self.state {
 822            BufferStoreState::Remote(state) => Some(state),
 823            _ => None,
 824        }
 825    }
 826
 827    fn as_remote(&self) -> Option<&RemoteBufferStore> {
 828        match &self.state {
 829            BufferStoreState::Remote(state) => Some(state),
 830            _ => None,
 831        }
 832    }
 833
 834    pub fn open_buffer(
 835        &mut self,
 836        project_path: ProjectPath,
 837        cx: &mut Context<Self>,
 838    ) -> Task<Result<Entity<Buffer>>> {
 839        if let Some(buffer) = self.get_by_path(&project_path) {
 840            cx.emit(BufferStoreEvent::BufferOpened {
 841                buffer: buffer.clone(),
 842                project_path,
 843            });
 844
 845            return Task::ready(Ok(buffer));
 846        }
 847
 848        let task = match self.loading_buffers.entry(project_path.clone()) {
 849            hash_map::Entry::Occupied(e) => e.get().clone(),
 850            hash_map::Entry::Vacant(entry) => {
 851                let path = project_path.path.clone();
 852                let Some(worktree) = self
 853                    .worktree_store
 854                    .read(cx)
 855                    .worktree_for_id(project_path.worktree_id, cx)
 856                else {
 857                    return Task::ready(Err(anyhow!("no such worktree")));
 858                };
 859                let load_buffer = match &self.state {
 860                    BufferStoreState::Local(this) => this.open_buffer(path, worktree, cx),
 861                    BufferStoreState::Remote(this) => this.open_buffer(path, worktree, cx),
 862                };
 863
 864                entry
 865                    .insert(
 866                        // todo(lw): hot foreground spawn
 867                        cx.spawn(async move |this, cx| {
 868                            let load_result = load_buffer.await;
 869                            this.update(cx, |this, cx| {
 870                                // Record the fact that the buffer is no longer loading.
 871                                this.loading_buffers.remove(&project_path);
 872
 873                                let buffer = load_result.map_err(Arc::new)?;
 874                                cx.emit(BufferStoreEvent::BufferOpened {
 875                                    buffer: buffer.clone(),
 876                                    project_path,
 877                                });
 878
 879                                Ok(buffer)
 880                            })?
 881                        })
 882                        .shared(),
 883                    )
 884                    .clone()
 885            }
 886        };
 887
 888        cx.background_spawn(async move {
 889            task.await.map_err(|e| {
 890                if e.error_code() != ErrorCode::Internal {
 891                    anyhow!(e.error_code())
 892                } else {
 893                    anyhow!("{e}")
 894                }
 895            })
 896        })
 897    }
 898
 899    pub fn create_buffer(
 900        &mut self,
 901        project_searchable: bool,
 902        cx: &mut Context<Self>,
 903    ) -> Task<Result<Entity<Buffer>>> {
 904        match &self.state {
 905            BufferStoreState::Local(this) => this.create_buffer(project_searchable, cx),
 906            BufferStoreState::Remote(this) => this.create_buffer(project_searchable, cx),
 907        }
 908    }
 909
 910    pub fn save_buffer(
 911        &mut self,
 912        buffer: Entity<Buffer>,
 913        cx: &mut Context<Self>,
 914    ) -> Task<Result<()>> {
 915        match &mut self.state {
 916            BufferStoreState::Local(this) => this.save_buffer(buffer, cx),
 917            BufferStoreState::Remote(this) => this.save_remote_buffer(buffer, None, cx),
 918        }
 919    }
 920
 921    pub fn save_buffer_as(
 922        &mut self,
 923        buffer: Entity<Buffer>,
 924        path: ProjectPath,
 925        cx: &mut Context<Self>,
 926    ) -> Task<Result<()>> {
 927        let old_file = buffer.read(cx).file().cloned();
 928        let task = match &self.state {
 929            BufferStoreState::Local(this) => this.save_buffer_as(buffer.clone(), path, cx),
 930            BufferStoreState::Remote(this) => {
 931                this.save_remote_buffer(buffer.clone(), Some(path.to_proto()), cx)
 932            }
 933        };
 934        cx.spawn(async move |this, cx| {
 935            task.await?;
 936            this.update(cx, |this, cx| {
 937                old_file.clone().and_then(|file| {
 938                    this.path_to_buffer_id.remove(&ProjectPath {
 939                        worktree_id: file.worktree_id(cx),
 940                        path: file.path().clone(),
 941                    })
 942                });
 943
 944                cx.emit(BufferStoreEvent::BufferChangedFilePath { buffer, old_file });
 945            })
 946        })
 947    }
 948
 949    fn add_buffer(&mut self, buffer_entity: Entity<Buffer>, cx: &mut Context<Self>) -> Result<()> {
 950        let buffer = buffer_entity.read(cx);
 951        let remote_id = buffer.remote_id();
 952        let path = File::from_dyn(buffer.file()).map(|file| ProjectPath {
 953            path: file.path.clone(),
 954            worktree_id: file.worktree_id(cx),
 955        });
 956        let is_remote = buffer.replica_id().is_remote();
 957        let open_buffer = OpenBuffer::Complete {
 958            buffer: buffer_entity.downgrade(),
 959        };
 960
 961        let handle = cx.entity().downgrade();
 962        buffer_entity.update(cx, move |_, cx| {
 963            cx.on_release(move |buffer, cx| {
 964                handle
 965                    .update(cx, |_, cx| {
 966                        cx.emit(BufferStoreEvent::BufferDropped(buffer.remote_id()))
 967                    })
 968                    .ok();
 969            })
 970            .detach()
 971        });
 972        let _expect_path_to_exist;
 973        match self.opened_buffers.entry(remote_id) {
 974            hash_map::Entry::Vacant(entry) => {
 975                entry.insert(open_buffer);
 976                _expect_path_to_exist = false;
 977            }
 978            hash_map::Entry::Occupied(mut entry) => {
 979                if let OpenBuffer::Operations(operations) = entry.get_mut() {
 980                    buffer_entity.update(cx, |b, cx| b.apply_ops(operations.drain(..), cx));
 981                } else if entry.get().upgrade().is_some() {
 982                    if is_remote {
 983                        return Ok(());
 984                    } else {
 985                        debug_panic!("buffer {remote_id} was already registered");
 986                        anyhow::bail!("buffer {remote_id} was already registered");
 987                    }
 988                }
 989                entry.insert(open_buffer);
 990                _expect_path_to_exist = true;
 991            }
 992        }
 993
 994        if let Some(path) = path {
 995            self.path_to_buffer_id.insert(path, remote_id);
 996        }
 997
 998        cx.subscribe(&buffer_entity, Self::on_buffer_event).detach();
 999        cx.emit(BufferStoreEvent::BufferAdded(buffer_entity));
1000        Ok(())
1001    }
1002
1003    pub fn buffers(&self) -> impl '_ + Iterator<Item = Entity<Buffer>> {
1004        self.opened_buffers
1005            .values()
1006            .filter_map(|buffer| buffer.upgrade())
1007    }
1008
1009    pub fn loading_buffers(
1010        &self,
1011    ) -> impl Iterator<Item = (&ProjectPath, impl Future<Output = Result<Entity<Buffer>>>)> {
1012        self.loading_buffers.iter().map(|(path, task)| {
1013            let task = task.clone();
1014            (path, async move {
1015                task.await.map_err(|e| {
1016                    if e.error_code() != ErrorCode::Internal {
1017                        anyhow!(e.error_code())
1018                    } else {
1019                        anyhow!("{e}")
1020                    }
1021                })
1022            })
1023        })
1024    }
1025
1026    pub fn buffer_id_for_project_path(&self, project_path: &ProjectPath) -> Option<&BufferId> {
1027        self.path_to_buffer_id.get(project_path)
1028    }
1029
1030    pub fn get_by_path(&self, path: &ProjectPath) -> Option<Entity<Buffer>> {
1031        self.path_to_buffer_id
1032            .get(path)
1033            .and_then(|buffer_id| self.get(*buffer_id))
1034    }
1035
1036    pub fn get(&self, buffer_id: BufferId) -> Option<Entity<Buffer>> {
1037        self.opened_buffers.get(&buffer_id)?.upgrade()
1038    }
1039
1040    pub fn get_existing(&self, buffer_id: BufferId) -> Result<Entity<Buffer>> {
1041        self.get(buffer_id)
1042            .with_context(|| format!("unknown buffer id {buffer_id}"))
1043    }
1044
1045    pub fn get_possibly_incomplete(&self, buffer_id: BufferId) -> Option<Entity<Buffer>> {
1046        self.get(buffer_id).or_else(|| {
1047            self.as_remote()
1048                .and_then(|remote| remote.loading_remote_buffers_by_id.get(&buffer_id).cloned())
1049        })
1050    }
1051
1052    pub fn buffer_version_info(&self, cx: &App) -> (Vec<proto::BufferVersion>, Vec<BufferId>) {
1053        let buffers = self
1054            .buffers()
1055            .map(|buffer| {
1056                let buffer = buffer.read(cx);
1057                proto::BufferVersion {
1058                    id: buffer.remote_id().into(),
1059                    version: language::proto::serialize_version(&buffer.version),
1060                }
1061            })
1062            .collect();
1063        let incomplete_buffer_ids = self
1064            .as_remote()
1065            .map(|remote| remote.incomplete_buffer_ids())
1066            .unwrap_or_default();
1067        (buffers, incomplete_buffer_ids)
1068    }
1069
1070    pub fn disconnected_from_host(&mut self, cx: &mut App) {
1071        for open_buffer in self.opened_buffers.values_mut() {
1072            if let Some(buffer) = open_buffer.upgrade() {
1073                buffer.update(cx, |buffer, _| buffer.give_up_waiting());
1074            }
1075        }
1076
1077        for buffer in self.buffers() {
1078            buffer.update(cx, |buffer, cx| {
1079                buffer.set_capability(Capability::ReadOnly, cx)
1080            });
1081        }
1082
1083        if let Some(remote) = self.as_remote_mut() {
1084            // Wake up all futures currently waiting on a buffer to get opened,
1085            // to give them a chance to fail now that we've disconnected.
1086            remote.remote_buffer_listeners.clear()
1087        }
1088    }
1089
1090    pub fn shared(&mut self, remote_id: u64, downstream_client: AnyProtoClient, _cx: &mut App) {
1091        self.downstream_client = Some((downstream_client, remote_id));
1092    }
1093
1094    pub fn unshared(&mut self, _cx: &mut Context<Self>) {
1095        self.downstream_client.take();
1096        self.forget_shared_buffers();
1097    }
1098
1099    pub fn discard_incomplete(&mut self) {
1100        self.opened_buffers
1101            .retain(|_, buffer| !matches!(buffer, OpenBuffer::Operations(_)));
1102    }
1103
1104    fn buffer_changed_file(&mut self, buffer: Entity<Buffer>, cx: &mut App) -> Option<()> {
1105        let file = File::from_dyn(buffer.read(cx).file())?;
1106
1107        let remote_id = buffer.read(cx).remote_id();
1108        if let Some(entry_id) = file.entry_id {
1109            if let Some(local) = self.as_local_mut() {
1110                match local.local_buffer_ids_by_entry_id.get(&entry_id) {
1111                    Some(_) => {
1112                        return None;
1113                    }
1114                    None => {
1115                        local
1116                            .local_buffer_ids_by_entry_id
1117                            .insert(entry_id, remote_id);
1118                    }
1119                }
1120            }
1121            self.path_to_buffer_id.insert(
1122                ProjectPath {
1123                    worktree_id: file.worktree_id(cx),
1124                    path: file.path.clone(),
1125                },
1126                remote_id,
1127            );
1128        };
1129
1130        Some(())
1131    }
1132
1133    pub fn find_search_candidates(
1134        &mut self,
1135        query: &SearchQuery,
1136        mut limit: usize,
1137        fs: Arc<dyn Fs>,
1138        cx: &mut Context<Self>,
1139    ) -> Receiver<Entity<Buffer>> {
1140        let (tx, rx) = smol::channel::unbounded();
1141        let mut open_buffers = HashSet::default();
1142        let mut unnamed_buffers = Vec::new();
1143        for handle in self.buffers() {
1144            let buffer = handle.read(cx);
1145            if self.non_searchable_buffers.contains(&buffer.remote_id()) {
1146                continue;
1147            } else if let Some(entry_id) = buffer.entry_id(cx) {
1148                open_buffers.insert(entry_id);
1149            } else {
1150                limit = limit.saturating_sub(1);
1151                unnamed_buffers.push(handle)
1152            };
1153        }
1154
1155        const MAX_CONCURRENT_BUFFER_OPENS: usize = 64;
1156        let project_paths_rx = self
1157            .worktree_store
1158            .update(cx, |worktree_store, cx| {
1159                worktree_store.find_search_candidates(query.clone(), limit, open_buffers, fs, cx)
1160            })
1161            .chunks(MAX_CONCURRENT_BUFFER_OPENS);
1162
1163        cx.spawn(async move |this, cx| {
1164            for buffer in unnamed_buffers {
1165                tx.send(buffer).await.ok();
1166            }
1167
1168            let mut project_paths_rx = pin!(project_paths_rx);
1169            while let Some(project_paths) = project_paths_rx.next().await {
1170                let buffers = this.update(cx, |this, cx| {
1171                    project_paths
1172                        .into_iter()
1173                        .map(|project_path| this.open_buffer(project_path, cx))
1174                        .collect::<Vec<_>>()
1175                })?;
1176                for buffer_task in buffers {
1177                    if let Some(buffer) = buffer_task.await.log_err()
1178                        && tx.send(buffer).await.is_err()
1179                    {
1180                        return anyhow::Ok(());
1181                    }
1182                }
1183            }
1184            anyhow::Ok(())
1185        })
1186        .detach();
1187        rx
1188    }
1189
1190    fn on_buffer_event(
1191        &mut self,
1192        buffer: Entity<Buffer>,
1193        event: &BufferEvent,
1194        cx: &mut Context<Self>,
1195    ) {
1196        match event {
1197            BufferEvent::FileHandleChanged => {
1198                self.buffer_changed_file(buffer, cx);
1199            }
1200            BufferEvent::Reloaded => {
1201                let Some((downstream_client, project_id)) = self.downstream_client.as_ref() else {
1202                    return;
1203                };
1204                let buffer = buffer.read(cx);
1205                downstream_client
1206                    .send(proto::BufferReloaded {
1207                        project_id: *project_id,
1208                        buffer_id: buffer.remote_id().to_proto(),
1209                        version: serialize_version(&buffer.version()),
1210                        mtime: buffer.saved_mtime().map(|t| t.into()),
1211                        line_ending: serialize_line_ending(buffer.line_ending()) as i32,
1212                    })
1213                    .log_err();
1214            }
1215            BufferEvent::LanguageChanged => {}
1216            _ => {}
1217        }
1218    }
1219
1220    pub async fn handle_update_buffer(
1221        this: Entity<Self>,
1222        envelope: TypedEnvelope<proto::UpdateBuffer>,
1223        mut cx: AsyncApp,
1224    ) -> Result<proto::Ack> {
1225        let payload = envelope.payload;
1226        let buffer_id = BufferId::new(payload.buffer_id)?;
1227        let ops = payload
1228            .operations
1229            .into_iter()
1230            .map(language::proto::deserialize_operation)
1231            .collect::<Result<Vec<_>, _>>()?;
1232        this.update(&mut cx, |this, cx| {
1233            match this.opened_buffers.entry(buffer_id) {
1234                hash_map::Entry::Occupied(mut e) => match e.get_mut() {
1235                    OpenBuffer::Operations(operations) => operations.extend_from_slice(&ops),
1236                    OpenBuffer::Complete { buffer, .. } => {
1237                        if let Some(buffer) = buffer.upgrade() {
1238                            buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx));
1239                        }
1240                    }
1241                },
1242                hash_map::Entry::Vacant(e) => {
1243                    e.insert(OpenBuffer::Operations(ops));
1244                }
1245            }
1246            Ok(proto::Ack {})
1247        })?
1248    }
1249
1250    pub fn register_shared_lsp_handle(
1251        &mut self,
1252        peer_id: proto::PeerId,
1253        buffer_id: BufferId,
1254        handle: OpenLspBufferHandle,
1255    ) {
1256        if let Some(shared_buffers) = self.shared_buffers.get_mut(&peer_id)
1257            && let Some(buffer) = shared_buffers.get_mut(&buffer_id)
1258        {
1259            buffer.lsp_handle = Some(handle);
1260            return;
1261        }
1262        debug_panic!("tried to register shared lsp handle, but buffer was not shared")
1263    }
1264
1265    pub fn handle_synchronize_buffers(
1266        &mut self,
1267        envelope: TypedEnvelope<proto::SynchronizeBuffers>,
1268        cx: &mut Context<Self>,
1269        client: Arc<Client>,
1270    ) -> Result<proto::SynchronizeBuffersResponse> {
1271        let project_id = envelope.payload.project_id;
1272        let mut response = proto::SynchronizeBuffersResponse {
1273            buffers: Default::default(),
1274        };
1275        let Some(guest_id) = envelope.original_sender_id else {
1276            anyhow::bail!("missing original_sender_id on SynchronizeBuffers request");
1277        };
1278
1279        self.shared_buffers.entry(guest_id).or_default().clear();
1280        for buffer in envelope.payload.buffers {
1281            let buffer_id = BufferId::new(buffer.id)?;
1282            let remote_version = language::proto::deserialize_version(&buffer.version);
1283            if let Some(buffer) = self.get(buffer_id) {
1284                self.shared_buffers
1285                    .entry(guest_id)
1286                    .or_default()
1287                    .entry(buffer_id)
1288                    .or_insert_with(|| SharedBuffer {
1289                        buffer: buffer.clone(),
1290                        lsp_handle: None,
1291                    });
1292
1293                let buffer = buffer.read(cx);
1294                response.buffers.push(proto::BufferVersion {
1295                    id: buffer_id.into(),
1296                    version: language::proto::serialize_version(&buffer.version),
1297                });
1298
1299                let operations = buffer.serialize_ops(Some(remote_version), cx);
1300                let client = client.clone();
1301                if let Some(file) = buffer.file() {
1302                    client
1303                        .send(proto::UpdateBufferFile {
1304                            project_id,
1305                            buffer_id: buffer_id.into(),
1306                            file: Some(file.to_proto(cx)),
1307                        })
1308                        .log_err();
1309                }
1310
1311                // TODO(max): do something
1312                // client
1313                //     .send(proto::UpdateStagedText {
1314                //         project_id,
1315                //         buffer_id: buffer_id.into(),
1316                //         diff_base: buffer.diff_base().map(ToString::to_string),
1317                //     })
1318                //     .log_err();
1319
1320                client
1321                    .send(proto::BufferReloaded {
1322                        project_id,
1323                        buffer_id: buffer_id.into(),
1324                        version: language::proto::serialize_version(buffer.saved_version()),
1325                        mtime: buffer.saved_mtime().map(|time| time.into()),
1326                        line_ending: language::proto::serialize_line_ending(buffer.line_ending())
1327                            as i32,
1328                    })
1329                    .log_err();
1330
1331                cx.background_spawn(
1332                    async move {
1333                        let operations = operations.await;
1334                        for chunk in split_operations(operations) {
1335                            client
1336                                .request(proto::UpdateBuffer {
1337                                    project_id,
1338                                    buffer_id: buffer_id.into(),
1339                                    operations: chunk,
1340                                })
1341                                .await?;
1342                        }
1343                        anyhow::Ok(())
1344                    }
1345                    .log_err(),
1346                )
1347                .detach();
1348            }
1349        }
1350        Ok(response)
1351    }
1352
1353    pub fn handle_create_buffer_for_peer(
1354        &mut self,
1355        envelope: TypedEnvelope<proto::CreateBufferForPeer>,
1356        replica_id: ReplicaId,
1357        capability: Capability,
1358        cx: &mut Context<Self>,
1359    ) -> Result<()> {
1360        let remote = self
1361            .as_remote_mut()
1362            .context("buffer store is not a remote")?;
1363
1364        if let Some(buffer) =
1365            remote.handle_create_buffer_for_peer(envelope, replica_id, capability, cx)?
1366        {
1367            self.add_buffer(buffer, cx)?;
1368        }
1369
1370        Ok(())
1371    }
1372
1373    pub async fn handle_update_buffer_file(
1374        this: Entity<Self>,
1375        envelope: TypedEnvelope<proto::UpdateBufferFile>,
1376        mut cx: AsyncApp,
1377    ) -> Result<()> {
1378        let buffer_id = envelope.payload.buffer_id;
1379        let buffer_id = BufferId::new(buffer_id)?;
1380
1381        this.update(&mut cx, |this, cx| {
1382            let payload = envelope.payload.clone();
1383            if let Some(buffer) = this.get_possibly_incomplete(buffer_id) {
1384                let file = payload.file.context("invalid file")?;
1385                let worktree = this
1386                    .worktree_store
1387                    .read(cx)
1388                    .worktree_for_id(WorktreeId::from_proto(file.worktree_id), cx)
1389                    .context("no such worktree")?;
1390                let file = File::from_proto(file, worktree, cx)?;
1391                let old_file = buffer.update(cx, |buffer, cx| {
1392                    let old_file = buffer.file().cloned();
1393                    let new_path = file.path.clone();
1394
1395                    buffer.file_updated(Arc::new(file), cx);
1396                    if old_file.as_ref().is_none_or(|old| *old.path() != new_path) {
1397                        Some(old_file)
1398                    } else {
1399                        None
1400                    }
1401                });
1402                if let Some(old_file) = old_file {
1403                    cx.emit(BufferStoreEvent::BufferChangedFilePath { buffer, old_file });
1404                }
1405            }
1406            if let Some((downstream_client, project_id)) = this.downstream_client.as_ref() {
1407                downstream_client
1408                    .send(proto::UpdateBufferFile {
1409                        project_id: *project_id,
1410                        buffer_id: buffer_id.into(),
1411                        file: envelope.payload.file,
1412                    })
1413                    .log_err();
1414            }
1415            Ok(())
1416        })?
1417    }
1418
1419    pub async fn handle_save_buffer(
1420        this: Entity<Self>,
1421        envelope: TypedEnvelope<proto::SaveBuffer>,
1422        mut cx: AsyncApp,
1423    ) -> Result<proto::BufferSaved> {
1424        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1425        let (buffer, project_id) = this.read_with(&cx, |this, _| {
1426            anyhow::Ok((
1427                this.get_existing(buffer_id)?,
1428                this.downstream_client
1429                    .as_ref()
1430                    .map(|(_, project_id)| *project_id)
1431                    .context("project is not shared")?,
1432            ))
1433        })??;
1434        buffer
1435            .update(&mut cx, |buffer, _| {
1436                buffer.wait_for_version(deserialize_version(&envelope.payload.version))
1437            })?
1438            .await?;
1439        let buffer_id = buffer.read_with(&cx, |buffer, _| buffer.remote_id())?;
1440
1441        if let Some(new_path) = envelope.payload.new_path
1442            && let Some(new_path) = ProjectPath::from_proto(new_path)
1443        {
1444            this.update(&mut cx, |this, cx| {
1445                this.save_buffer_as(buffer.clone(), new_path, cx)
1446            })?
1447            .await?;
1448        } else {
1449            this.update(&mut cx, |this, cx| this.save_buffer(buffer.clone(), cx))?
1450                .await?;
1451        }
1452
1453        buffer.read_with(&cx, |buffer, _| proto::BufferSaved {
1454            project_id,
1455            buffer_id: buffer_id.into(),
1456            version: serialize_version(buffer.saved_version()),
1457            mtime: buffer.saved_mtime().map(|time| time.into()),
1458        })
1459    }
1460
1461    pub async fn handle_close_buffer(
1462        this: Entity<Self>,
1463        envelope: TypedEnvelope<proto::CloseBuffer>,
1464        mut cx: AsyncApp,
1465    ) -> Result<()> {
1466        let peer_id = envelope.sender_id;
1467        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1468        this.update(&mut cx, |this, cx| {
1469            if let Some(shared) = this.shared_buffers.get_mut(&peer_id)
1470                && shared.remove(&buffer_id).is_some()
1471            {
1472                cx.emit(BufferStoreEvent::SharedBufferClosed(peer_id, buffer_id));
1473                if shared.is_empty() {
1474                    this.shared_buffers.remove(&peer_id);
1475                }
1476                return;
1477            }
1478            debug_panic!(
1479                "peer_id {} closed buffer_id {} which was either not open or already closed",
1480                peer_id,
1481                buffer_id
1482            )
1483        })
1484    }
1485
1486    pub async fn handle_buffer_saved(
1487        this: Entity<Self>,
1488        envelope: TypedEnvelope<proto::BufferSaved>,
1489        mut cx: AsyncApp,
1490    ) -> Result<()> {
1491        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1492        let version = deserialize_version(&envelope.payload.version);
1493        let mtime = envelope.payload.mtime.clone().map(|time| time.into());
1494        this.update(&mut cx, move |this, cx| {
1495            if let Some(buffer) = this.get_possibly_incomplete(buffer_id) {
1496                buffer.update(cx, |buffer, cx| {
1497                    buffer.did_save(version, mtime, cx);
1498                });
1499            }
1500
1501            if let Some((downstream_client, project_id)) = this.downstream_client.as_ref() {
1502                downstream_client
1503                    .send(proto::BufferSaved {
1504                        project_id: *project_id,
1505                        buffer_id: buffer_id.into(),
1506                        mtime: envelope.payload.mtime,
1507                        version: envelope.payload.version,
1508                    })
1509                    .log_err();
1510            }
1511        })
1512    }
1513
1514    pub async fn handle_buffer_reloaded(
1515        this: Entity<Self>,
1516        envelope: TypedEnvelope<proto::BufferReloaded>,
1517        mut cx: AsyncApp,
1518    ) -> Result<()> {
1519        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1520        let version = deserialize_version(&envelope.payload.version);
1521        let mtime = envelope.payload.mtime.clone().map(|time| time.into());
1522        let line_ending = deserialize_line_ending(
1523            proto::LineEnding::from_i32(envelope.payload.line_ending)
1524                .context("missing line ending")?,
1525        );
1526        this.update(&mut cx, |this, cx| {
1527            if let Some(buffer) = this.get_possibly_incomplete(buffer_id) {
1528                buffer.update(cx, |buffer, cx| {
1529                    buffer.did_reload(version, line_ending, mtime, cx);
1530                });
1531            }
1532
1533            if let Some((downstream_client, project_id)) = this.downstream_client.as_ref() {
1534                downstream_client
1535                    .send(proto::BufferReloaded {
1536                        project_id: *project_id,
1537                        buffer_id: buffer_id.into(),
1538                        mtime: envelope.payload.mtime,
1539                        version: envelope.payload.version,
1540                        line_ending: envelope.payload.line_ending,
1541                    })
1542                    .log_err();
1543            }
1544        })
1545    }
1546
1547    pub fn reload_buffers(
1548        &self,
1549        buffers: HashSet<Entity<Buffer>>,
1550        push_to_history: bool,
1551        cx: &mut Context<Self>,
1552    ) -> Task<Result<ProjectTransaction>> {
1553        if buffers.is_empty() {
1554            return Task::ready(Ok(ProjectTransaction::default()));
1555        }
1556        match &self.state {
1557            BufferStoreState::Local(this) => this.reload_buffers(buffers, push_to_history, cx),
1558            BufferStoreState::Remote(this) => this.reload_buffers(buffers, push_to_history, cx),
1559        }
1560    }
1561
1562    async fn handle_reload_buffers(
1563        this: Entity<Self>,
1564        envelope: TypedEnvelope<proto::ReloadBuffers>,
1565        mut cx: AsyncApp,
1566    ) -> Result<proto::ReloadBuffersResponse> {
1567        let sender_id = envelope.original_sender_id().unwrap_or_default();
1568        let reload = this.update(&mut cx, |this, cx| {
1569            let mut buffers = HashSet::default();
1570            for buffer_id in &envelope.payload.buffer_ids {
1571                let buffer_id = BufferId::new(*buffer_id)?;
1572                buffers.insert(this.get_existing(buffer_id)?);
1573            }
1574            anyhow::Ok(this.reload_buffers(buffers, false, cx))
1575        })??;
1576
1577        let project_transaction = reload.await?;
1578        let project_transaction = this.update(&mut cx, |this, cx| {
1579            this.serialize_project_transaction_for_peer(project_transaction, sender_id, cx)
1580        })?;
1581        Ok(proto::ReloadBuffersResponse {
1582            transaction: Some(project_transaction),
1583        })
1584    }
1585
1586    pub fn create_buffer_for_peer(
1587        &mut self,
1588        buffer: &Entity<Buffer>,
1589        peer_id: proto::PeerId,
1590        cx: &mut Context<Self>,
1591    ) -> Task<Result<()>> {
1592        let buffer_id = buffer.read(cx).remote_id();
1593        let shared_buffers = self.shared_buffers.entry(peer_id).or_default();
1594        if shared_buffers.contains_key(&buffer_id) {
1595            return Task::ready(Ok(()));
1596        }
1597        shared_buffers.insert(
1598            buffer_id,
1599            SharedBuffer {
1600                buffer: buffer.clone(),
1601                lsp_handle: None,
1602            },
1603        );
1604
1605        let Some((client, project_id)) = self.downstream_client.clone() else {
1606            return Task::ready(Ok(()));
1607        };
1608
1609        cx.spawn(async move |this, cx| {
1610            let Some(buffer) = this.read_with(cx, |this, _| this.get(buffer_id))? else {
1611                return anyhow::Ok(());
1612            };
1613
1614            let operations = buffer.update(cx, |b, cx| b.serialize_ops(None, cx))?;
1615            let operations = operations.await;
1616            let state = buffer.update(cx, |buffer, cx| buffer.to_proto(cx))?;
1617
1618            let initial_state = proto::CreateBufferForPeer {
1619                project_id,
1620                peer_id: Some(peer_id),
1621                variant: Some(proto::create_buffer_for_peer::Variant::State(state)),
1622            };
1623
1624            if client.send(initial_state).log_err().is_some() {
1625                let client = client.clone();
1626                cx.background_spawn(async move {
1627                    let mut chunks = split_operations(operations).peekable();
1628                    while let Some(chunk) = chunks.next() {
1629                        let is_last = chunks.peek().is_none();
1630                        client.send(proto::CreateBufferForPeer {
1631                            project_id,
1632                            peer_id: Some(peer_id),
1633                            variant: Some(proto::create_buffer_for_peer::Variant::Chunk(
1634                                proto::BufferChunk {
1635                                    buffer_id: buffer_id.into(),
1636                                    operations: chunk,
1637                                    is_last,
1638                                },
1639                            )),
1640                        })?;
1641                    }
1642                    anyhow::Ok(())
1643                })
1644                .await
1645                .log_err();
1646            }
1647            Ok(())
1648        })
1649    }
1650
1651    pub fn forget_shared_buffers(&mut self) {
1652        self.shared_buffers.clear();
1653    }
1654
1655    pub fn forget_shared_buffers_for(&mut self, peer_id: &proto::PeerId) {
1656        self.shared_buffers.remove(peer_id);
1657    }
1658
1659    pub fn update_peer_id(&mut self, old_peer_id: &proto::PeerId, new_peer_id: proto::PeerId) {
1660        if let Some(buffers) = self.shared_buffers.remove(old_peer_id) {
1661            self.shared_buffers.insert(new_peer_id, buffers);
1662        }
1663    }
1664
1665    pub fn has_shared_buffers(&self) -> bool {
1666        !self.shared_buffers.is_empty()
1667    }
1668
1669    pub fn create_local_buffer(
1670        &mut self,
1671        text: &str,
1672        language: Option<Arc<Language>>,
1673        project_searchable: bool,
1674        cx: &mut Context<Self>,
1675    ) -> Entity<Buffer> {
1676        let buffer = cx.new(|cx| {
1677            Buffer::local(text, cx)
1678                .with_language(language.unwrap_or_else(|| language::PLAIN_TEXT.clone()), cx)
1679        });
1680
1681        self.add_buffer(buffer.clone(), cx).log_err();
1682        let buffer_id = buffer.read(cx).remote_id();
1683        if !project_searchable {
1684            self.non_searchable_buffers.insert(buffer_id);
1685        }
1686
1687        if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
1688            self.path_to_buffer_id.insert(
1689                ProjectPath {
1690                    worktree_id: file.worktree_id(cx),
1691                    path: file.path.clone(),
1692                },
1693                buffer_id,
1694            );
1695            let this = self
1696                .as_local_mut()
1697                .expect("local-only method called in a non-local context");
1698            if let Some(entry_id) = file.entry_id {
1699                this.local_buffer_ids_by_entry_id
1700                    .insert(entry_id, buffer_id);
1701            }
1702        }
1703        buffer
1704    }
1705
1706    pub fn deserialize_project_transaction(
1707        &mut self,
1708        message: proto::ProjectTransaction,
1709        push_to_history: bool,
1710        cx: &mut Context<Self>,
1711    ) -> Task<Result<ProjectTransaction>> {
1712        if let Some(this) = self.as_remote_mut() {
1713            this.deserialize_project_transaction(message, push_to_history, cx)
1714        } else {
1715            debug_panic!("not a remote buffer store");
1716            Task::ready(Err(anyhow!("not a remote buffer store")))
1717        }
1718    }
1719
1720    pub fn wait_for_remote_buffer(
1721        &mut self,
1722        id: BufferId,
1723        cx: &mut Context<BufferStore>,
1724    ) -> Task<Result<Entity<Buffer>>> {
1725        if let Some(this) = self.as_remote_mut() {
1726            this.wait_for_remote_buffer(id, cx)
1727        } else {
1728            debug_panic!("not a remote buffer store");
1729            Task::ready(Err(anyhow!("not a remote buffer store")))
1730        }
1731    }
1732
1733    pub fn serialize_project_transaction_for_peer(
1734        &mut self,
1735        project_transaction: ProjectTransaction,
1736        peer_id: proto::PeerId,
1737        cx: &mut Context<Self>,
1738    ) -> proto::ProjectTransaction {
1739        let mut serialized_transaction = proto::ProjectTransaction {
1740            buffer_ids: Default::default(),
1741            transactions: Default::default(),
1742        };
1743        for (buffer, transaction) in project_transaction.0 {
1744            self.create_buffer_for_peer(&buffer, peer_id, cx)
1745                .detach_and_log_err(cx);
1746            serialized_transaction
1747                .buffer_ids
1748                .push(buffer.read(cx).remote_id().into());
1749            serialized_transaction
1750                .transactions
1751                .push(language::proto::serialize_transaction(&transaction));
1752        }
1753        serialized_transaction
1754    }
1755}
1756
1757impl OpenBuffer {
1758    fn upgrade(&self) -> Option<Entity<Buffer>> {
1759        match self {
1760            OpenBuffer::Complete { buffer, .. } => buffer.upgrade(),
1761            OpenBuffer::Operations(_) => None,
1762        }
1763    }
1764}
1765
1766fn is_not_found_error(error: &anyhow::Error) -> bool {
1767    error
1768        .root_cause()
1769        .downcast_ref::<io::Error>()
1770        .is_some_and(|err| err.kind() == io::ErrorKind::NotFound)
1771}