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