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