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