buffer_store.rs

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