buffer_store.rs

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