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