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