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