buffer_store.rs

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