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 fs::{Fs, encodings::EncodingWrapper};
  13use gpui::{
  14    App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Subscription, Task, WeakEntity,
  15};
  16use language::{
  17    Buffer, BufferEvent, Capability, DiskState, File as _, Language, Operation,
  18    proto::{
  19        deserialize_line_ending, deserialize_version, serialize_line_ending, serialize_version,
  20        split_operations,
  21    },
  22};
  23use rpc::{
  24    AnyProtoClient, ErrorCode, ErrorExt as _, TypedEnvelope,
  25    proto::{self},
  26};
  27use smol::channel::Receiver;
  28use std::{io, pin::pin, sync::Arc, time::Instant};
  29use text::{BufferId, ReplicaId};
  30use util::{ResultExt as _, TryFutureExt, debug_panic, maybe, paths::PathStyle, rel_path::RelPath};
  31use worktree::{File, PathChange, ProjectEntryId, Worktree, WorktreeId};
  32
  33/// A set of open buffers.
  34pub struct BufferStore {
  35    state: BufferStoreState,
  36    #[allow(clippy::type_complexity)]
  37    loading_buffers: HashMap<ProjectPath, Shared<Task<Result<Entity<Buffer>, Arc<anyhow::Error>>>>>,
  38    worktree_store: Entity<WorktreeStore>,
  39    opened_buffers: HashMap<BufferId, OpenBuffer>,
  40    path_to_buffer_id: HashMap<ProjectPath, BufferId>,
  41    downstream_client: Option<(AnyProtoClient, u64)>,
  42    shared_buffers: HashMap<proto::PeerId, HashMap<BufferId, SharedBuffer>>,
  43    non_searchable_buffers: HashSet<BufferId>,
  44}
  45
  46#[derive(Hash, Eq, PartialEq, Clone)]
  47struct SharedBuffer {
  48    buffer: Entity<Buffer>,
  49    lsp_handle: Option<OpenLspBufferHandle>,
  50}
  51
  52enum BufferStoreState {
  53    Local(LocalBufferStore),
  54    Remote(RemoteBufferStore),
  55}
  56
  57struct RemoteBufferStore {
  58    shared_with_me: HashSet<Entity<Buffer>>,
  59    upstream_client: AnyProtoClient,
  60    project_id: u64,
  61    loading_remote_buffers_by_id: HashMap<BufferId, Entity<Buffer>>,
  62    remote_buffer_listeners:
  63        HashMap<BufferId, Vec<oneshot::Sender<anyhow::Result<Entity<Buffer>>>>>,
  64    worktree_store: Entity<WorktreeStore>,
  65}
  66
  67struct LocalBufferStore {
  68    local_buffer_ids_by_entry_id: HashMap<ProjectEntryId, BufferId>,
  69    worktree_store: Entity<WorktreeStore>,
  70    _subscription: Subscription,
  71}
  72
  73enum OpenBuffer {
  74    Complete { buffer: WeakEntity<Buffer> },
  75    Operations(Vec<Operation>),
  76}
  77
  78pub enum BufferStoreEvent {
  79    BufferAdded(Entity<Buffer>),
  80    BufferOpened {
  81        buffer: Entity<Buffer>,
  82        project_path: ProjectPath,
  83    },
  84    SharedBufferClosed(proto::PeerId, BufferId),
  85    BufferDropped(BufferId),
  86    BufferChangedFilePath {
  87        buffer: Entity<Buffer>,
  88        old_file: Option<Arc<dyn language::File>>,
  89    },
  90}
  91
  92#[derive(Default, Debug, Clone)]
  93pub struct ProjectTransaction(pub HashMap<Entity<Buffer>, language::Transaction>);
  94
  95impl PartialEq for ProjectTransaction {
  96    fn eq(&self, other: &Self) -> bool {
  97        self.0.len() == other.0.len()
  98            && self.0.iter().all(|(buffer, transaction)| {
  99                other.0.get(buffer).is_some_and(|t| t.id == transaction.id)
 100            })
 101    }
 102}
 103
 104impl EventEmitter<BufferStoreEvent> for BufferStore {}
 105
 106impl RemoteBufferStore {
 107    pub fn wait_for_remote_buffer(
 108        &mut self,
 109        id: BufferId,
 110        cx: &mut Context<BufferStore>,
 111    ) -> Task<Result<Entity<Buffer>>> {
 112        let (tx, rx) = oneshot::channel();
 113        self.remote_buffer_listeners.entry(id).or_default().push(tx);
 114
 115        cx.spawn(async move |this, cx| {
 116            if let Some(buffer) = this
 117                .read_with(cx, |buffer_store, _| buffer_store.get(id))
 118                .ok()
 119                .flatten()
 120            {
 121                return Ok(buffer);
 122            }
 123
 124            cx.background_spawn(async move { rx.await? }).await
 125        })
 126    }
 127
 128    fn save_remote_buffer(
 129        &self,
 130        buffer_handle: Entity<Buffer>,
 131        new_path: Option<proto::ProjectPath>,
 132        cx: &Context<BufferStore>,
 133    ) -> Task<Result<()>> {
 134        let buffer = buffer_handle.read(cx);
 135        let buffer_id = buffer.remote_id().into();
 136        let version = buffer.version();
 137        let rpc = self.upstream_client.clone();
 138        let project_id = self.project_id;
 139        cx.spawn(async move |_, cx| {
 140            let response = rpc
 141                .request(proto::SaveBuffer {
 142                    project_id,
 143                    buffer_id,
 144                    new_path,
 145                    version: serialize_version(&version),
 146                })
 147                .await?;
 148            let version = deserialize_version(&response.version);
 149            let mtime = response.mtime.map(|mtime| mtime.into());
 150
 151            buffer_handle.update(cx, |buffer, cx| {
 152                buffer.did_save(version.clone(), mtime, cx);
 153            })?;
 154
 155            Ok(())
 156        })
 157    }
 158
 159    pub fn handle_create_buffer_for_peer(
 160        &mut self,
 161        envelope: TypedEnvelope<proto::CreateBufferForPeer>,
 162        replica_id: ReplicaId,
 163        capability: Capability,
 164        cx: &mut Context<BufferStore>,
 165    ) -> Result<Option<Entity<Buffer>>> {
 166        match envelope.payload.variant.context("missing variant")? {
 167            proto::create_buffer_for_peer::Variant::State(mut state) => {
 168                let buffer_id = BufferId::new(state.id)?;
 169
 170                let buffer_result = maybe!({
 171                    let mut buffer_file = None;
 172                    if let Some(file) = state.file.take() {
 173                        let worktree_id = worktree::WorktreeId::from_proto(file.worktree_id);
 174                        let worktree = self
 175                            .worktree_store
 176                            .read(cx)
 177                            .worktree_for_id(worktree_id, cx)
 178                            .with_context(|| {
 179                                format!("no worktree found for id {}", file.worktree_id)
 180                            })?;
 181                        buffer_file = Some(Arc::new(File::from_proto(file, worktree, cx)?)
 182                            as Arc<dyn language::File>);
 183                    }
 184                    Buffer::from_proto(
 185                        replica_id,
 186                        capability,
 187                        state,
 188                        buffer_file,
 189                        cx.background_executor(),
 190                    )
 191                });
 192
 193                match buffer_result {
 194                    Ok(buffer) => {
 195                        let buffer = cx.new(|_| buffer);
 196                        self.loading_remote_buffers_by_id.insert(buffer_id, buffer);
 197                    }
 198                    Err(error) => {
 199                        if let Some(listeners) = self.remote_buffer_listeners.remove(&buffer_id) {
 200                            for listener in listeners {
 201                                listener.send(Err(anyhow!(error.cloned()))).ok();
 202                            }
 203                        }
 204                    }
 205                }
 206            }
 207            proto::create_buffer_for_peer::Variant::Chunk(chunk) => {
 208                let buffer_id = BufferId::new(chunk.buffer_id)?;
 209                let buffer = self
 210                    .loading_remote_buffers_by_id
 211                    .get(&buffer_id)
 212                    .cloned()
 213                    .with_context(|| {
 214                        format!(
 215                            "received chunk for buffer {} without initial state",
 216                            chunk.buffer_id
 217                        )
 218                    })?;
 219
 220                let result = maybe!({
 221                    let operations = chunk
 222                        .operations
 223                        .into_iter()
 224                        .map(language::proto::deserialize_operation)
 225                        .collect::<Result<Vec<_>>>()?;
 226                    buffer.update(cx, |buffer, cx| buffer.apply_ops(operations, cx));
 227                    anyhow::Ok(())
 228                });
 229
 230                if let Err(error) = result {
 231                    self.loading_remote_buffers_by_id.remove(&buffer_id);
 232                    if let Some(listeners) = self.remote_buffer_listeners.remove(&buffer_id) {
 233                        for listener in listeners {
 234                            listener.send(Err(error.cloned())).ok();
 235                        }
 236                    }
 237                } else if chunk.is_last {
 238                    self.loading_remote_buffers_by_id.remove(&buffer_id);
 239                    if self.upstream_client.is_via_collab() {
 240                        // retain buffers sent by peers to avoid races.
 241                        self.shared_with_me.insert(buffer.clone());
 242                    }
 243
 244                    if let Some(senders) = self.remote_buffer_listeners.remove(&buffer_id) {
 245                        for sender in senders {
 246                            sender.send(Ok(buffer.clone())).ok();
 247                        }
 248                    }
 249                    return Ok(Some(buffer));
 250                }
 251            }
 252        }
 253        Ok(None)
 254    }
 255
 256    pub fn incomplete_buffer_ids(&self) -> Vec<BufferId> {
 257        self.loading_remote_buffers_by_id
 258            .keys()
 259            .copied()
 260            .collect::<Vec<_>>()
 261    }
 262
 263    pub fn deserialize_project_transaction(
 264        &self,
 265        message: proto::ProjectTransaction,
 266        push_to_history: bool,
 267        cx: &mut Context<BufferStore>,
 268    ) -> Task<Result<ProjectTransaction>> {
 269        cx.spawn(async move |this, cx| {
 270            let mut project_transaction = ProjectTransaction::default();
 271            for (buffer_id, transaction) in message.buffer_ids.into_iter().zip(message.transactions)
 272            {
 273                let buffer_id = BufferId::new(buffer_id)?;
 274                let buffer = this
 275                    .update(cx, |this, cx| this.wait_for_remote_buffer(buffer_id, cx))?
 276                    .await?;
 277                let transaction = language::proto::deserialize_transaction(transaction)?;
 278                project_transaction.0.insert(buffer, transaction);
 279            }
 280
 281            for (buffer, transaction) in &project_transaction.0 {
 282                buffer
 283                    .update(cx, |buffer, _| {
 284                        buffer.wait_for_edits(transaction.edit_ids.iter().copied())
 285                    })?
 286                    .await?;
 287
 288                if push_to_history {
 289                    buffer.update(cx, |buffer, _| {
 290                        buffer.push_transaction(transaction.clone(), Instant::now());
 291                        buffer.finalize_last_transaction();
 292                    })?;
 293                }
 294            }
 295
 296            Ok(project_transaction)
 297        })
 298    }
 299
 300    fn open_buffer(
 301        &self,
 302        path: Arc<RelPath>,
 303        worktree: Entity<Worktree>,
 304        cx: &mut Context<BufferStore>,
 305    ) -> Task<Result<Entity<Buffer>>> {
 306        let worktree_id = worktree.read(cx).id().to_proto();
 307        let project_id = self.project_id;
 308        let client = self.upstream_client.clone();
 309        cx.spawn(async move |this, cx| {
 310            let response = client
 311                .request(proto::OpenBufferByPath {
 312                    project_id,
 313                    worktree_id,
 314                    path: path.to_proto(),
 315                })
 316                .await?;
 317            let buffer_id = BufferId::new(response.buffer_id)?;
 318
 319            let buffer = this
 320                .update(cx, {
 321                    |this, cx| this.wait_for_remote_buffer(buffer_id, cx)
 322                })?
 323                .await?;
 324
 325            Ok(buffer)
 326        })
 327    }
 328
 329    fn create_buffer(
 330        &self,
 331        project_searchable: bool,
 332        cx: &mut Context<BufferStore>,
 333    ) -> Task<Result<Entity<Buffer>>> {
 334        let create = self.upstream_client.request(proto::OpenNewBuffer {
 335            project_id: self.project_id,
 336        });
 337        cx.spawn(async move |this, cx| {
 338            let response = create.await?;
 339            let buffer_id = BufferId::new(response.buffer_id)?;
 340
 341            this.update(cx, |this, cx| {
 342                if !project_searchable {
 343                    this.non_searchable_buffers.insert(buffer_id);
 344                }
 345                this.wait_for_remote_buffer(buffer_id, cx)
 346            })?
 347            .await
 348        })
 349    }
 350
 351    fn reload_buffers(
 352        &self,
 353        buffers: HashSet<Entity<Buffer>>,
 354        push_to_history: bool,
 355        cx: &mut Context<BufferStore>,
 356    ) -> Task<Result<ProjectTransaction>> {
 357        let request = self.upstream_client.request(proto::ReloadBuffers {
 358            project_id: self.project_id,
 359            buffer_ids: buffers
 360                .iter()
 361                .map(|buffer| buffer.read(cx).remote_id().to_proto())
 362                .collect(),
 363        });
 364
 365        cx.spawn(async move |this, cx| {
 366            let response = request.await?.transaction.context("missing transaction")?;
 367            this.update(cx, |this, cx| {
 368                this.deserialize_project_transaction(response, push_to_history, cx)
 369            })?
 370            .await
 371        })
 372    }
 373}
 374
 375impl LocalBufferStore {
 376    fn save_local_buffer(
 377        &self,
 378        buffer_handle: Entity<Buffer>,
 379        worktree: Entity<Worktree>,
 380        path: Arc<RelPath>,
 381        mut has_changed_file: bool,
 382        cx: &mut Context<BufferStore>,
 383    ) -> Task<Result<()>> {
 384        let buffer = buffer_handle.read(cx);
 385
 386        let text = buffer.as_rope().clone();
 387        let line_ending = buffer.line_ending();
 388        let version = buffer.version();
 389        let buffer_id = buffer.remote_id();
 390        let file = buffer.file().cloned();
 391        let encoding = buffer.encoding.clone();
 392
 393        if file
 394            .as_ref()
 395            .is_some_and(|file| file.disk_state() == DiskState::New)
 396        {
 397            has_changed_file = true;
 398        }
 399
 400        let save = worktree.update(cx, |worktree, cx| {
 401            worktree.write_file(
 402                path.as_ref(),
 403                text,
 404                line_ending,
 405                cx,
 406                &encoding.lock().unwrap(),
 407            )
 408        });
 409
 410        cx.spawn(async move |this, cx| {
 411            let new_file = save.await?;
 412            let mtime = new_file.disk_state().mtime();
 413            this.update(cx, |this, cx| {
 414                if let Some((downstream_client, project_id)) = this.downstream_client.clone() {
 415                    if has_changed_file {
 416                        downstream_client
 417                            .send(proto::UpdateBufferFile {
 418                                project_id,
 419                                buffer_id: buffer_id.to_proto(),
 420                                file: Some(language::File::to_proto(&*new_file, cx)),
 421                            })
 422                            .log_err();
 423                    }
 424                    downstream_client
 425                        .send(proto::BufferSaved {
 426                            project_id,
 427                            buffer_id: buffer_id.to_proto(),
 428                            version: serialize_version(&version),
 429                            mtime: mtime.map(|time| time.into()),
 430                        })
 431                        .log_err();
 432                }
 433            })?;
 434            buffer_handle.update(cx, |buffer, cx| {
 435                if has_changed_file {
 436                    buffer.file_updated(new_file, cx);
 437                }
 438                buffer.did_save(version.clone(), mtime, cx);
 439            })
 440        })
 441    }
 442
 443    fn subscribe_to_worktree(
 444        &mut self,
 445        worktree: &Entity<Worktree>,
 446        cx: &mut Context<BufferStore>,
 447    ) {
 448        cx.subscribe(worktree, |this, worktree, event, cx| {
 449            if worktree.read(cx).is_local()
 450                && let worktree::Event::UpdatedEntries(changes) = event
 451            {
 452                Self::local_worktree_entries_changed(this, &worktree, changes, cx);
 453            }
 454        })
 455        .detach();
 456    }
 457
 458    fn local_worktree_entries_changed(
 459        this: &mut BufferStore,
 460        worktree_handle: &Entity<Worktree>,
 461        changes: &[(Arc<RelPath>, ProjectEntryId, PathChange)],
 462        cx: &mut Context<BufferStore>,
 463    ) {
 464        let snapshot = worktree_handle.read(cx).snapshot();
 465        for (path, entry_id, _) in changes {
 466            Self::local_worktree_entry_changed(
 467                this,
 468                *entry_id,
 469                path,
 470                worktree_handle,
 471                &snapshot,
 472                cx,
 473            );
 474        }
 475    }
 476
 477    fn local_worktree_entry_changed(
 478        this: &mut BufferStore,
 479        entry_id: ProjectEntryId,
 480        path: &Arc<RelPath>,
 481        worktree: &Entity<worktree::Worktree>,
 482        snapshot: &worktree::Snapshot,
 483        cx: &mut Context<BufferStore>,
 484    ) -> Option<()> {
 485        let project_path = ProjectPath {
 486            worktree_id: snapshot.id(),
 487            path: path.clone(),
 488        };
 489
 490        let buffer_id = this
 491            .as_local_mut()
 492            .and_then(|local| local.local_buffer_ids_by_entry_id.get(&entry_id))
 493            .copied()
 494            .or_else(|| this.path_to_buffer_id.get(&project_path).copied())?;
 495
 496        let buffer = if let Some(buffer) = this.get(buffer_id) {
 497            Some(buffer)
 498        } else {
 499            this.opened_buffers.remove(&buffer_id);
 500            this.non_searchable_buffers.remove(&buffer_id);
 501            None
 502        };
 503
 504        let buffer = if let Some(buffer) = buffer {
 505            buffer
 506        } else {
 507            this.path_to_buffer_id.remove(&project_path);
 508            let this = this.as_local_mut()?;
 509            this.local_buffer_ids_by_entry_id.remove(&entry_id);
 510            return None;
 511        };
 512
 513        let events = buffer.update(cx, |buffer, cx| {
 514            let file = buffer.file()?;
 515            let old_file = File::from_dyn(Some(file))?;
 516            if old_file.worktree != *worktree {
 517                return None;
 518            }
 519
 520            let snapshot_entry = old_file
 521                .entry_id
 522                .and_then(|entry_id| snapshot.entry_for_id(entry_id))
 523                .or_else(|| snapshot.entry_for_path(old_file.path.as_ref()));
 524
 525            let new_file = if let Some(entry) = snapshot_entry {
 526                File {
 527                    disk_state: match entry.mtime {
 528                        Some(mtime) => DiskState::Present { mtime },
 529                        None => old_file.disk_state,
 530                    },
 531                    is_local: true,
 532                    entry_id: Some(entry.id),
 533                    path: entry.path.clone(),
 534                    worktree: worktree.clone(),
 535                    is_private: entry.is_private,
 536                }
 537            } else {
 538                File {
 539                    disk_state: DiskState::Deleted,
 540                    is_local: true,
 541                    entry_id: old_file.entry_id,
 542                    path: old_file.path.clone(),
 543                    worktree: worktree.clone(),
 544                    is_private: old_file.is_private,
 545                }
 546            };
 547
 548            if new_file == *old_file {
 549                return None;
 550            }
 551
 552            let mut events = Vec::new();
 553            if new_file.path != old_file.path {
 554                this.path_to_buffer_id.remove(&ProjectPath {
 555                    path: old_file.path.clone(),
 556                    worktree_id: old_file.worktree_id(cx),
 557                });
 558                this.path_to_buffer_id.insert(
 559                    ProjectPath {
 560                        worktree_id: new_file.worktree_id(cx),
 561                        path: new_file.path.clone(),
 562                    },
 563                    buffer_id,
 564                );
 565                events.push(BufferStoreEvent::BufferChangedFilePath {
 566                    buffer: cx.entity(),
 567                    old_file: buffer.file().cloned(),
 568                });
 569            }
 570            let local = this.as_local_mut()?;
 571            if new_file.entry_id != old_file.entry_id {
 572                if let Some(entry_id) = old_file.entry_id {
 573                    local.local_buffer_ids_by_entry_id.remove(&entry_id);
 574                }
 575                if let Some(entry_id) = new_file.entry_id {
 576                    local
 577                        .local_buffer_ids_by_entry_id
 578                        .insert(entry_id, buffer_id);
 579                }
 580            }
 581
 582            if let Some((client, project_id)) = &this.downstream_client {
 583                client
 584                    .send(proto::UpdateBufferFile {
 585                        project_id: *project_id,
 586                        buffer_id: buffer_id.to_proto(),
 587                        file: Some(new_file.to_proto(cx)),
 588                    })
 589                    .ok();
 590            }
 591
 592            buffer.file_updated(Arc::new(new_file), cx);
 593            Some(events)
 594        })?;
 595
 596        for event in events {
 597            cx.emit(event);
 598        }
 599
 600        None
 601    }
 602
 603    fn save_buffer(
 604        &self,
 605        buffer: Entity<Buffer>,
 606        cx: &mut Context<BufferStore>,
 607    ) -> Task<Result<()>> {
 608        let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
 609            return Task::ready(Err(anyhow!("buffer doesn't have a file")));
 610        };
 611        let worktree = file.worktree.clone();
 612        self.save_local_buffer(buffer, worktree, file.path.clone(), false, cx)
 613    }
 614
 615    fn save_buffer_as(
 616        &self,
 617        buffer: Entity<Buffer>,
 618        path: ProjectPath,
 619        cx: &mut Context<BufferStore>,
 620    ) -> Task<Result<()>> {
 621        let Some(worktree) = self
 622            .worktree_store
 623            .read(cx)
 624            .worktree_for_id(path.worktree_id, cx)
 625        else {
 626            return Task::ready(Err(anyhow!("no such worktree")));
 627        };
 628        self.save_local_buffer(buffer, worktree, path.path, true, cx)
 629    }
 630
 631    fn open_buffer(
 632        &self,
 633        path: Arc<RelPath>,
 634        worktree: Entity<Worktree>,
 635        encoding: Option<EncodingWrapper>,
 636        cx: &mut Context<BufferStore>,
 637    ) -> Task<Result<Entity<Buffer>>> {
 638        let load_buffer = worktree.update(cx, |worktree, cx| {
 639            let load_file = worktree.load_file(path.as_ref(), encoding, cx);
 640            let reservation = cx.reserve_entity();
 641
 642            let buffer_id = BufferId::from(reservation.entity_id().as_non_zero_u64());
 643            let path = path.clone();
 644            cx.spawn(async move |_, cx| {
 645                let loaded = load_file.await.with_context(|| {
 646                    format!("Could not open path: {}", path.display(PathStyle::local()))
 647                })?;
 648                let text_buffer = cx
 649                    .background_spawn(async move {
 650                        text::Buffer::new(ReplicaId::LOCAL, buffer_id, loaded.text)
 651                    })
 652                    .await;
 653                cx.insert_entity(reservation, |_| {
 654                    Buffer::build(text_buffer, Some(loaded.file), Capability::ReadWrite)
 655                })
 656            })
 657        });
 658
 659        cx.spawn(async move |this, cx| {
 660            let buffer = match load_buffer.await {
 661                Ok(buffer) => Ok(buffer),
 662                Err(error) if is_not_found_error(&error) => cx.new(|cx| {
 663                    let buffer_id = BufferId::from(cx.entity_id().as_non_zero_u64());
 664                    let text_buffer = text::Buffer::new(
 665                        ReplicaId::LOCAL,
 666                        buffer_id,
 667                        "",
 668                        cx.background_executor(),
 669                    );
 670                    Buffer::build(
 671                        text_buffer,
 672                        Some(Arc::new(File {
 673                            worktree,
 674                            path,
 675                            disk_state: DiskState::New,
 676                            entry_id: None,
 677                            is_local: true,
 678                            is_private: false,
 679                        })),
 680                        Capability::ReadWrite,
 681                    )
 682                })?,
 683                Err(e) => return Err(e),
 684            };
 685            this.update(cx, |this, cx| {
 686                this.add_buffer(buffer.clone(), cx)?;
 687                let buffer_id = buffer.read(cx).remote_id();
 688                if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
 689                    this.path_to_buffer_id.insert(
 690                        ProjectPath {
 691                            worktree_id: file.worktree_id(cx),
 692                            path: file.path.clone(),
 693                        },
 694                        buffer_id,
 695                    );
 696                    let this = this.as_local_mut().unwrap();
 697                    if let Some(entry_id) = file.entry_id {
 698                        this.local_buffer_ids_by_entry_id
 699                            .insert(entry_id, buffer_id);
 700                    }
 701                }
 702
 703                anyhow::Ok(())
 704            })??;
 705
 706            Ok(buffer)
 707        })
 708    }
 709
 710    fn create_buffer(
 711        &self,
 712        project_searchable: bool,
 713        cx: &mut Context<BufferStore>,
 714    ) -> Task<Result<Entity<Buffer>>> {
 715        cx.spawn(async move |buffer_store, cx| {
 716            let buffer =
 717                cx.new(|cx| Buffer::local("", cx).with_language(language::PLAIN_TEXT.clone(), cx))?;
 718            buffer_store.update(cx, |buffer_store, cx| {
 719                buffer_store.add_buffer(buffer.clone(), cx).log_err();
 720                if !project_searchable {
 721                    buffer_store
 722                        .non_searchable_buffers
 723                        .insert(buffer.read(cx).remote_id());
 724                }
 725            })?;
 726            Ok(buffer)
 727        })
 728    }
 729
 730    fn reload_buffers(
 731        &self,
 732        buffers: HashSet<Entity<Buffer>>,
 733        push_to_history: bool,
 734        cx: &mut Context<BufferStore>,
 735    ) -> Task<Result<ProjectTransaction>> {
 736        cx.spawn(async move |_, cx| {
 737            let mut project_transaction = ProjectTransaction::default();
 738            for buffer in buffers {
 739                let transaction = buffer.update(cx, |buffer, cx| buffer.reload(cx))?.await?;
 740                buffer.update(cx, |buffer, cx| {
 741                    if let Some(transaction) = transaction {
 742                        if !push_to_history {
 743                            buffer.forget_transaction(transaction.id);
 744                        }
 745                        project_transaction.0.insert(cx.entity(), transaction);
 746                    }
 747                })?;
 748            }
 749
 750            Ok(project_transaction)
 751        })
 752    }
 753}
 754
 755impl BufferStore {
 756    pub fn init(client: &AnyProtoClient) {
 757        client.add_entity_message_handler(Self::handle_buffer_reloaded);
 758        client.add_entity_message_handler(Self::handle_buffer_saved);
 759        client.add_entity_message_handler(Self::handle_update_buffer_file);
 760        client.add_entity_request_handler(Self::handle_save_buffer);
 761        client.add_entity_request_handler(Self::handle_reload_buffers);
 762    }
 763
 764    /// Creates a buffer store, optionally retaining its buffers.
 765    pub fn local(worktree_store: Entity<WorktreeStore>, cx: &mut Context<Self>) -> Self {
 766        Self {
 767            state: BufferStoreState::Local(LocalBufferStore {
 768                local_buffer_ids_by_entry_id: Default::default(),
 769                worktree_store: worktree_store.clone(),
 770                _subscription: cx.subscribe(&worktree_store, |this, _, event, cx| {
 771                    if let WorktreeStoreEvent::WorktreeAdded(worktree) = event {
 772                        let this = this.as_local_mut().unwrap();
 773                        this.subscribe_to_worktree(worktree, cx);
 774                    }
 775                }),
 776            }),
 777            downstream_client: None,
 778            opened_buffers: Default::default(),
 779            path_to_buffer_id: Default::default(),
 780            shared_buffers: Default::default(),
 781            loading_buffers: Default::default(),
 782            non_searchable_buffers: Default::default(),
 783            worktree_store,
 784        }
 785    }
 786
 787    pub fn remote(
 788        worktree_store: Entity<WorktreeStore>,
 789        upstream_client: AnyProtoClient,
 790        remote_id: u64,
 791        _cx: &mut Context<Self>,
 792    ) -> Self {
 793        Self {
 794            state: BufferStoreState::Remote(RemoteBufferStore {
 795                shared_with_me: Default::default(),
 796                loading_remote_buffers_by_id: Default::default(),
 797                remote_buffer_listeners: Default::default(),
 798                project_id: remote_id,
 799                upstream_client,
 800                worktree_store: worktree_store.clone(),
 801            }),
 802            downstream_client: None,
 803            opened_buffers: Default::default(),
 804            path_to_buffer_id: Default::default(),
 805            loading_buffers: Default::default(),
 806            shared_buffers: Default::default(),
 807            non_searchable_buffers: Default::default(),
 808            worktree_store,
 809        }
 810    }
 811
 812    fn as_local_mut(&mut self) -> Option<&mut LocalBufferStore> {
 813        match &mut self.state {
 814            BufferStoreState::Local(state) => Some(state),
 815            _ => None,
 816        }
 817    }
 818
 819    fn as_remote_mut(&mut self) -> Option<&mut RemoteBufferStore> {
 820        match &mut self.state {
 821            BufferStoreState::Remote(state) => Some(state),
 822            _ => None,
 823        }
 824    }
 825
 826    fn as_remote(&self) -> Option<&RemoteBufferStore> {
 827        match &self.state {
 828            BufferStoreState::Remote(state) => Some(state),
 829            _ => None,
 830        }
 831    }
 832
 833    pub fn open_buffer(
 834        &mut self,
 835        project_path: ProjectPath,
 836        encoding: Option<EncodingWrapper>,
 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, encoding, 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}