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