buffer_store.rs

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