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