buffer_store.rs

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