buffer_store.rs

   1use crate::{
   2    lsp_store::OpenLspBufferHandle,
   3    search::SearchQuery,
   4    worktree_store::{WorktreeStore, WorktreeStoreEvent},
   5    ProjectItem as _, ProjectPath,
   6};
   7use ::git::{parse_git_remote_url, BuildPermalinkParams, GitHostingProviderRegistry};
   8use anyhow::{anyhow, bail, Context as _, Result};
   9use client::Client;
  10use collections::{hash_map, HashMap, HashSet};
  11use fs::Fs;
  12use futures::{channel::oneshot, future::Shared, Future, FutureExt as _, StreamExt};
  13use git::{blame::Blame, repository::RepoPath};
  14use gpui::{
  15    App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Subscription, Task, WeakEntity,
  16};
  17use language::{
  18    proto::{
  19        deserialize_line_ending, deserialize_version, serialize_line_ending, serialize_version,
  20        split_operations,
  21    },
  22    Buffer, BufferEvent, Capability, DiskState, File as _, Language, Operation,
  23};
  24use rpc::{
  25    proto::{self, ToProto},
  26    AnyProtoClient, ErrorExt as _, TypedEnvelope,
  27};
  28use serde::Deserialize;
  29use smol::channel::Receiver;
  30use std::{
  31    io,
  32    ops::Range,
  33    path::{Path, PathBuf},
  34    pin::pin,
  35    sync::Arc,
  36    time::Instant,
  37};
  38use text::BufferId;
  39use util::{debug_panic, maybe, ResultExt as _, TryFutureExt};
  40use worktree::{File, PathChange, ProjectEntryId, Worktree, WorktreeId};
  41
  42/// A set of open buffers.
  43pub struct BufferStore {
  44    state: BufferStoreState,
  45    #[allow(clippy::type_complexity)]
  46    loading_buffers: HashMap<ProjectPath, Shared<Task<Result<Entity<Buffer>, Arc<anyhow::Error>>>>>,
  47    worktree_store: Entity<WorktreeStore>,
  48    opened_buffers: HashMap<BufferId, OpenBuffer>,
  49    downstream_client: Option<(AnyProtoClient, u64)>,
  50    shared_buffers: HashMap<proto::PeerId, HashMap<BufferId, SharedBuffer>>,
  51}
  52
  53#[derive(Hash, Eq, PartialEq, Clone)]
  54struct SharedBuffer {
  55    buffer: Entity<Buffer>,
  56    lsp_handle: Option<OpenLspBufferHandle>,
  57}
  58
  59enum BufferStoreState {
  60    Local(LocalBufferStore),
  61    Remote(RemoteBufferStore),
  62}
  63
  64struct RemoteBufferStore {
  65    shared_with_me: HashSet<Entity<Buffer>>,
  66    upstream_client: AnyProtoClient,
  67    project_id: u64,
  68    loading_remote_buffers_by_id: HashMap<BufferId, Entity<Buffer>>,
  69    remote_buffer_listeners:
  70        HashMap<BufferId, Vec<oneshot::Sender<Result<Entity<Buffer>, anyhow::Error>>>>,
  71    worktree_store: Entity<WorktreeStore>,
  72}
  73
  74struct LocalBufferStore {
  75    local_buffer_ids_by_path: HashMap<ProjectPath, BufferId>,
  76    local_buffer_ids_by_entry_id: HashMap<ProjectEntryId, BufferId>,
  77    worktree_store: Entity<WorktreeStore>,
  78    _subscription: Subscription,
  79}
  80
  81enum OpenBuffer {
  82    Complete { buffer: WeakEntity<Buffer> },
  83    Operations(Vec<Operation>),
  84}
  85
  86pub enum BufferStoreEvent {
  87    BufferAdded(Entity<Buffer>),
  88    SharedBufferClosed(proto::PeerId, BufferId),
  89    BufferDropped(BufferId),
  90    BufferChangedFilePath {
  91        buffer: Entity<Buffer>,
  92        old_file: Option<Arc<dyn language::File>>,
  93    },
  94}
  95
  96#[derive(Default, Debug)]
  97pub struct ProjectTransaction(pub HashMap<Entity<Buffer>, language::Transaction>);
  98
  99impl EventEmitter<BufferStoreEvent> for BufferStore {}
 100
 101impl RemoteBufferStore {
 102    pub fn wait_for_remote_buffer(
 103        &mut self,
 104        id: BufferId,
 105        cx: &mut Context<BufferStore>,
 106    ) -> Task<Result<Entity<Buffer>>> {
 107        let (tx, rx) = oneshot::channel();
 108        self.remote_buffer_listeners.entry(id).or_default().push(tx);
 109
 110        cx.spawn(|this, cx| async move {
 111            if let Some(buffer) = this
 112                .read_with(&cx, |buffer_store, _| buffer_store.get(id))
 113                .ok()
 114                .flatten()
 115            {
 116                return Ok(buffer);
 117            }
 118
 119            cx.background_spawn(async move { rx.await? }).await
 120        })
 121    }
 122
 123    fn save_remote_buffer(
 124        &self,
 125        buffer_handle: Entity<Buffer>,
 126        new_path: Option<proto::ProjectPath>,
 127        cx: &Context<BufferStore>,
 128    ) -> Task<Result<()>> {
 129        let buffer = buffer_handle.read(cx);
 130        let buffer_id = buffer.remote_id().into();
 131        let version = buffer.version();
 132        let rpc = self.upstream_client.clone();
 133        let project_id = self.project_id;
 134        cx.spawn(move |_, mut cx| async move {
 135            let response = rpc
 136                .request(proto::SaveBuffer {
 137                    project_id,
 138                    buffer_id,
 139                    new_path,
 140                    version: serialize_version(&version),
 141                })
 142                .await?;
 143            let version = deserialize_version(&response.version);
 144            let mtime = response.mtime.map(|mtime| mtime.into());
 145
 146            buffer_handle.update(&mut cx, |buffer, cx| {
 147                buffer.did_save(version.clone(), mtime, cx);
 148            })?;
 149
 150            Ok(())
 151        })
 152    }
 153
 154    pub fn handle_create_buffer_for_peer(
 155        &mut self,
 156        envelope: TypedEnvelope<proto::CreateBufferForPeer>,
 157        replica_id: u16,
 158        capability: Capability,
 159        cx: &mut Context<BufferStore>,
 160    ) -> Result<Option<Entity<Buffer>>> {
 161        match envelope
 162            .payload
 163            .variant
 164            .ok_or_else(|| anyhow!("missing variant"))?
 165        {
 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                            .ok_or_else(|| {
 178                                anyhow!("no worktree found for id {}", file.worktree_id)
 179                            })?;
 180                        buffer_file = Some(Arc::new(File::from_proto(file, worktree.clone(), 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                    .ok_or_else(|| {
 207                        anyhow!(
 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        return 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(|this, mut cx| async move {
 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(&mut cx, |this, cx| {
 269                        this.wait_for_remote_buffer(buffer_id, cx)
 270                    })?
 271                    .await?;
 272                let transaction = language::proto::deserialize_transaction(transaction)?;
 273                project_transaction.0.insert(buffer, transaction);
 274            }
 275
 276            for (buffer, transaction) in &project_transaction.0 {
 277                buffer
 278                    .update(&mut cx, |buffer, _| {
 279                        buffer.wait_for_edits(transaction.edit_ids.iter().copied())
 280                    })?
 281                    .await?;
 282
 283                if push_to_history {
 284                    buffer.update(&mut cx, |buffer, _| {
 285                        buffer.push_transaction(transaction.clone(), Instant::now());
 286                    })?;
 287                }
 288            }
 289
 290            Ok(project_transaction)
 291        })
 292    }
 293
 294    fn open_buffer(
 295        &self,
 296        path: Arc<Path>,
 297        worktree: Entity<Worktree>,
 298        cx: &mut Context<BufferStore>,
 299    ) -> Task<Result<Entity<Buffer>>> {
 300        let worktree_id = worktree.read(cx).id().to_proto();
 301        let project_id = self.project_id;
 302        let client = self.upstream_client.clone();
 303        cx.spawn(move |this, mut cx| async move {
 304            let response = client
 305                .request(proto::OpenBufferByPath {
 306                    project_id,
 307                    worktree_id,
 308                    path: path.to_proto(),
 309                })
 310                .await?;
 311            let buffer_id = BufferId::new(response.buffer_id)?;
 312
 313            let buffer = this
 314                .update(&mut cx, {
 315                    |this, cx| this.wait_for_remote_buffer(buffer_id, cx)
 316                })?
 317                .await?;
 318
 319            Ok(buffer)
 320        })
 321    }
 322
 323    fn create_buffer(&self, cx: &mut Context<BufferStore>) -> Task<Result<Entity<Buffer>>> {
 324        let create = self.upstream_client.request(proto::OpenNewBuffer {
 325            project_id: self.project_id,
 326        });
 327        cx.spawn(|this, mut cx| async move {
 328            let response = create.await?;
 329            let buffer_id = BufferId::new(response.buffer_id)?;
 330
 331            this.update(&mut cx, |this, cx| {
 332                this.wait_for_remote_buffer(buffer_id, cx)
 333            })?
 334            .await
 335        })
 336    }
 337
 338    fn reload_buffers(
 339        &self,
 340        buffers: HashSet<Entity<Buffer>>,
 341        push_to_history: bool,
 342        cx: &mut Context<BufferStore>,
 343    ) -> Task<Result<ProjectTransaction>> {
 344        let request = self.upstream_client.request(proto::ReloadBuffers {
 345            project_id: self.project_id,
 346            buffer_ids: buffers
 347                .iter()
 348                .map(|buffer| buffer.read(cx).remote_id().to_proto())
 349                .collect(),
 350        });
 351
 352        cx.spawn(|this, mut cx| async move {
 353            let response = request
 354                .await?
 355                .transaction
 356                .ok_or_else(|| anyhow!("missing transaction"))?;
 357            this.update(&mut cx, |this, cx| {
 358                this.deserialize_project_transaction(response, push_to_history, cx)
 359            })?
 360            .await
 361        })
 362    }
 363}
 364
 365impl LocalBufferStore {
 366    fn save_local_buffer(
 367        &self,
 368        buffer_handle: Entity<Buffer>,
 369        worktree: Entity<Worktree>,
 370        path: Arc<Path>,
 371        mut has_changed_file: bool,
 372        cx: &mut Context<BufferStore>,
 373    ) -> Task<Result<()>> {
 374        let buffer = buffer_handle.read(cx);
 375
 376        let text = buffer.as_rope().clone();
 377        let line_ending = buffer.line_ending();
 378        let version = buffer.version();
 379        let buffer_id = buffer.remote_id();
 380        if buffer
 381            .file()
 382            .is_some_and(|file| file.disk_state() == DiskState::New)
 383        {
 384            has_changed_file = true;
 385        }
 386
 387        let save = worktree.update(cx, |worktree, cx| {
 388            worktree.write_file(path.as_ref(), text, line_ending, cx)
 389        });
 390
 391        cx.spawn(move |this, mut cx| async move {
 392            let new_file = save.await?;
 393            let mtime = new_file.disk_state().mtime();
 394            this.update(&mut cx, |this, cx| {
 395                if let Some((downstream_client, project_id)) = this.downstream_client.clone() {
 396                    if has_changed_file {
 397                        downstream_client
 398                            .send(proto::UpdateBufferFile {
 399                                project_id,
 400                                buffer_id: buffer_id.to_proto(),
 401                                file: Some(language::File::to_proto(&*new_file, cx)),
 402                            })
 403                            .log_err();
 404                    }
 405                    downstream_client
 406                        .send(proto::BufferSaved {
 407                            project_id,
 408                            buffer_id: buffer_id.to_proto(),
 409                            version: serialize_version(&version),
 410                            mtime: mtime.map(|time| time.into()),
 411                        })
 412                        .log_err();
 413                }
 414            })?;
 415            buffer_handle.update(&mut cx, |buffer, cx| {
 416                if has_changed_file {
 417                    buffer.file_updated(new_file, cx);
 418                }
 419                buffer.did_save(version.clone(), mtime, cx);
 420            })
 421        })
 422    }
 423
 424    fn subscribe_to_worktree(
 425        &mut self,
 426        worktree: &Entity<Worktree>,
 427        cx: &mut Context<BufferStore>,
 428    ) {
 429        cx.subscribe(worktree, |this, worktree, event, cx| {
 430            if worktree.read(cx).is_local() {
 431                match event {
 432                    worktree::Event::UpdatedEntries(changes) => {
 433                        Self::local_worktree_entries_changed(this, &worktree, changes, cx);
 434                    }
 435                    _ => {}
 436                }
 437            }
 438        })
 439        .detach();
 440    }
 441
 442    fn local_worktree_entries_changed(
 443        this: &mut BufferStore,
 444        worktree_handle: &Entity<Worktree>,
 445        changes: &[(Arc<Path>, ProjectEntryId, PathChange)],
 446        cx: &mut Context<BufferStore>,
 447    ) {
 448        let snapshot = worktree_handle.read(cx).snapshot();
 449        for (path, entry_id, _) in changes {
 450            Self::local_worktree_entry_changed(
 451                this,
 452                *entry_id,
 453                path,
 454                worktree_handle,
 455                &snapshot,
 456                cx,
 457            );
 458        }
 459    }
 460
 461    fn local_worktree_entry_changed(
 462        this: &mut BufferStore,
 463        entry_id: ProjectEntryId,
 464        path: &Arc<Path>,
 465        worktree: &Entity<worktree::Worktree>,
 466        snapshot: &worktree::Snapshot,
 467        cx: &mut Context<BufferStore>,
 468    ) -> Option<()> {
 469        let project_path = ProjectPath {
 470            worktree_id: snapshot.id(),
 471            path: path.clone(),
 472        };
 473
 474        let buffer_id = {
 475            let local = this.as_local_mut()?;
 476            match local.local_buffer_ids_by_entry_id.get(&entry_id) {
 477                Some(&buffer_id) => buffer_id,
 478                None => local.local_buffer_ids_by_path.get(&project_path).copied()?,
 479            }
 480        };
 481
 482        let buffer = if let Some(buffer) = this.get(buffer_id) {
 483            Some(buffer)
 484        } else {
 485            this.opened_buffers.remove(&buffer_id);
 486            None
 487        };
 488
 489        let buffer = if let Some(buffer) = buffer {
 490            buffer
 491        } else {
 492            let this = this.as_local_mut()?;
 493            this.local_buffer_ids_by_path.remove(&project_path);
 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 local = this.as_local_mut()?;
 500            let file = buffer.file()?;
 501            let old_file = File::from_dyn(Some(file))?;
 502            if old_file.worktree != *worktree {
 503                return None;
 504            }
 505
 506            let snapshot_entry = old_file
 507                .entry_id
 508                .and_then(|entry_id| snapshot.entry_for_id(entry_id))
 509                .or_else(|| snapshot.entry_for_path(old_file.path.as_ref()));
 510
 511            let new_file = if let Some(entry) = snapshot_entry {
 512                File {
 513                    disk_state: match entry.mtime {
 514                        Some(mtime) => DiskState::Present { mtime },
 515                        None => old_file.disk_state,
 516                    },
 517                    is_local: true,
 518                    entry_id: Some(entry.id),
 519                    path: entry.path.clone(),
 520                    worktree: worktree.clone(),
 521                    is_private: entry.is_private,
 522                }
 523            } else {
 524                File {
 525                    disk_state: DiskState::Deleted,
 526                    is_local: true,
 527                    entry_id: old_file.entry_id,
 528                    path: old_file.path.clone(),
 529                    worktree: worktree.clone(),
 530                    is_private: old_file.is_private,
 531                }
 532            };
 533
 534            if new_file == *old_file {
 535                return None;
 536            }
 537
 538            let mut events = Vec::new();
 539            if new_file.path != old_file.path {
 540                local.local_buffer_ids_by_path.remove(&ProjectPath {
 541                    path: old_file.path.clone(),
 542                    worktree_id: old_file.worktree_id(cx),
 543                });
 544                local.local_buffer_ids_by_path.insert(
 545                    ProjectPath {
 546                        worktree_id: new_file.worktree_id(cx),
 547                        path: new_file.path.clone(),
 548                    },
 549                    buffer_id,
 550                );
 551                events.push(BufferStoreEvent::BufferChangedFilePath {
 552                    buffer: cx.entity(),
 553                    old_file: buffer.file().cloned(),
 554                });
 555            }
 556
 557            if new_file.entry_id != old_file.entry_id {
 558                if let Some(entry_id) = old_file.entry_id {
 559                    local.local_buffer_ids_by_entry_id.remove(&entry_id);
 560                }
 561                if let Some(entry_id) = new_file.entry_id {
 562                    local
 563                        .local_buffer_ids_by_entry_id
 564                        .insert(entry_id, buffer_id);
 565                }
 566            }
 567
 568            if let Some((client, project_id)) = &this.downstream_client {
 569                client
 570                    .send(proto::UpdateBufferFile {
 571                        project_id: *project_id,
 572                        buffer_id: buffer_id.to_proto(),
 573                        file: Some(new_file.to_proto(cx)),
 574                    })
 575                    .ok();
 576            }
 577
 578            buffer.file_updated(Arc::new(new_file), cx);
 579            Some(events)
 580        })?;
 581
 582        for event in events {
 583            cx.emit(event);
 584        }
 585
 586        None
 587    }
 588
 589    fn buffer_changed_file(&mut self, buffer: Entity<Buffer>, cx: &mut App) -> Option<()> {
 590        let file = File::from_dyn(buffer.read(cx).file())?;
 591
 592        let remote_id = buffer.read(cx).remote_id();
 593        if let Some(entry_id) = file.entry_id {
 594            match self.local_buffer_ids_by_entry_id.get(&entry_id) {
 595                Some(_) => {
 596                    return None;
 597                }
 598                None => {
 599                    self.local_buffer_ids_by_entry_id
 600                        .insert(entry_id, remote_id);
 601                }
 602            }
 603        };
 604        self.local_buffer_ids_by_path.insert(
 605            ProjectPath {
 606                worktree_id: file.worktree_id(cx),
 607                path: file.path.clone(),
 608            },
 609            remote_id,
 610        );
 611
 612        Some(())
 613    }
 614
 615    fn save_buffer(
 616        &self,
 617        buffer: Entity<Buffer>,
 618        cx: &mut Context<BufferStore>,
 619    ) -> Task<Result<()>> {
 620        let Some(file) = File::from_dyn(buffer.read(cx).file()) else {
 621            return Task::ready(Err(anyhow!("buffer doesn't have a file")));
 622        };
 623        let worktree = file.worktree.clone();
 624        self.save_local_buffer(buffer, worktree, file.path.clone(), false, cx)
 625    }
 626
 627    fn save_buffer_as(
 628        &self,
 629        buffer: Entity<Buffer>,
 630        path: ProjectPath,
 631        cx: &mut Context<BufferStore>,
 632    ) -> Task<Result<()>> {
 633        let Some(worktree) = self
 634            .worktree_store
 635            .read(cx)
 636            .worktree_for_id(path.worktree_id, cx)
 637        else {
 638            return Task::ready(Err(anyhow!("no such worktree")));
 639        };
 640        self.save_local_buffer(buffer, worktree, path.path.clone(), true, cx)
 641    }
 642
 643    fn open_buffer(
 644        &self,
 645        path: Arc<Path>,
 646        worktree: Entity<Worktree>,
 647        cx: &mut Context<BufferStore>,
 648    ) -> Task<Result<Entity<Buffer>>> {
 649        let load_buffer = worktree.update(cx, |worktree, cx| {
 650            let load_file = worktree.load_file(path.as_ref(), cx);
 651            let reservation = cx.reserve_entity();
 652            let buffer_id = BufferId::from(reservation.entity_id().as_non_zero_u64());
 653            cx.spawn(move |_, mut cx| async move {
 654                let loaded = load_file.await?;
 655                let text_buffer = cx
 656                    .background_spawn(async move { text::Buffer::new(0, buffer_id, loaded.text) })
 657                    .await;
 658                cx.insert_entity(reservation, |_| {
 659                    Buffer::build(text_buffer, Some(loaded.file), Capability::ReadWrite)
 660                })
 661            })
 662        });
 663
 664        cx.spawn(move |this, mut cx| async move {
 665            let buffer = match load_buffer.await {
 666                Ok(buffer) => Ok(buffer),
 667                Err(error) if is_not_found_error(&error) => cx.new(|cx| {
 668                    let buffer_id = BufferId::from(cx.entity_id().as_non_zero_u64());
 669                    let text_buffer = text::Buffer::new(0, buffer_id, "".into());
 670                    Buffer::build(
 671                        text_buffer,
 672                        Some(Arc::new(File {
 673                            worktree,
 674                            path,
 675                            disk_state: DiskState::New,
 676                            entry_id: None,
 677                            is_local: true,
 678                            is_private: false,
 679                        })),
 680                        Capability::ReadWrite,
 681                    )
 682                }),
 683                Err(e) => Err(e),
 684            }?;
 685            this.update(&mut cx, |this, cx| {
 686                this.add_buffer(buffer.clone(), cx)?;
 687                let buffer_id = buffer.read(cx).remote_id();
 688                if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
 689                    let this = this.as_local_mut().unwrap();
 690                    this.local_buffer_ids_by_path.insert(
 691                        ProjectPath {
 692                            worktree_id: file.worktree_id(cx),
 693                            path: file.path.clone(),
 694                        },
 695                        buffer_id,
 696                    );
 697
 698                    if let Some(entry_id) = file.entry_id {
 699                        this.local_buffer_ids_by_entry_id
 700                            .insert(entry_id, buffer_id);
 701                    }
 702                }
 703
 704                anyhow::Ok(())
 705            })??;
 706
 707            Ok(buffer)
 708        })
 709    }
 710
 711    fn create_buffer(&self, cx: &mut Context<BufferStore>) -> Task<Result<Entity<Buffer>>> {
 712        cx.spawn(|buffer_store, mut cx| async move {
 713            let buffer =
 714                cx.new(|cx| Buffer::local("", cx).with_language(language::PLAIN_TEXT.clone(), cx))?;
 715            buffer_store.update(&mut cx, |buffer_store, cx| {
 716                buffer_store.add_buffer(buffer.clone(), cx).log_err();
 717            })?;
 718            Ok(buffer)
 719        })
 720    }
 721
 722    fn reload_buffers(
 723        &self,
 724        buffers: HashSet<Entity<Buffer>>,
 725        push_to_history: bool,
 726        cx: &mut Context<BufferStore>,
 727    ) -> Task<Result<ProjectTransaction>> {
 728        cx.spawn(move |_, mut cx| async move {
 729            let mut project_transaction = ProjectTransaction::default();
 730            for buffer in buffers {
 731                let transaction = buffer
 732                    .update(&mut cx, |buffer, cx| buffer.reload(cx))?
 733                    .await?;
 734                buffer.update(&mut cx, |buffer, cx| {
 735                    if let Some(transaction) = transaction {
 736                        if !push_to_history {
 737                            buffer.forget_transaction(transaction.id);
 738                        }
 739                        project_transaction.0.insert(cx.entity(), transaction);
 740                    }
 741                })?;
 742            }
 743
 744            Ok(project_transaction)
 745        })
 746    }
 747}
 748
 749impl BufferStore {
 750    pub fn init(client: &AnyProtoClient) {
 751        client.add_entity_message_handler(Self::handle_buffer_reloaded);
 752        client.add_entity_message_handler(Self::handle_buffer_saved);
 753        client.add_entity_message_handler(Self::handle_update_buffer_file);
 754        client.add_entity_request_handler(Self::handle_save_buffer);
 755        client.add_entity_request_handler(Self::handle_blame_buffer);
 756        client.add_entity_request_handler(Self::handle_reload_buffers);
 757        client.add_entity_request_handler(Self::handle_get_permalink_to_line);
 758    }
 759
 760    /// Creates a buffer store, optionally retaining its buffers.
 761    pub fn local(worktree_store: Entity<WorktreeStore>, cx: &mut Context<Self>) -> Self {
 762        Self {
 763            state: BufferStoreState::Local(LocalBufferStore {
 764                local_buffer_ids_by_path: Default::default(),
 765                local_buffer_ids_by_entry_id: Default::default(),
 766                worktree_store: worktree_store.clone(),
 767                _subscription: cx.subscribe(&worktree_store, |this, _, event, cx| {
 768                    if let WorktreeStoreEvent::WorktreeAdded(worktree) = event {
 769                        let this = this.as_local_mut().unwrap();
 770                        this.subscribe_to_worktree(worktree, cx);
 771                    }
 772                }),
 773            }),
 774            downstream_client: None,
 775            opened_buffers: Default::default(),
 776            shared_buffers: Default::default(),
 777            loading_buffers: Default::default(),
 778            worktree_store,
 779        }
 780    }
 781
 782    pub fn remote(
 783        worktree_store: Entity<WorktreeStore>,
 784        upstream_client: AnyProtoClient,
 785        remote_id: u64,
 786        _cx: &mut Context<Self>,
 787    ) -> Self {
 788        Self {
 789            state: BufferStoreState::Remote(RemoteBufferStore {
 790                shared_with_me: Default::default(),
 791                loading_remote_buffers_by_id: Default::default(),
 792                remote_buffer_listeners: Default::default(),
 793                project_id: remote_id,
 794                upstream_client,
 795                worktree_store: worktree_store.clone(),
 796            }),
 797            downstream_client: None,
 798            opened_buffers: Default::default(),
 799            loading_buffers: Default::default(),
 800            shared_buffers: Default::default(),
 801            worktree_store,
 802        }
 803    }
 804
 805    fn as_local_mut(&mut self) -> Option<&mut LocalBufferStore> {
 806        match &mut self.state {
 807            BufferStoreState::Local(state) => Some(state),
 808            _ => None,
 809        }
 810    }
 811
 812    fn as_remote_mut(&mut self) -> Option<&mut RemoteBufferStore> {
 813        match &mut self.state {
 814            BufferStoreState::Remote(state) => Some(state),
 815            _ => None,
 816        }
 817    }
 818
 819    fn as_remote(&self) -> Option<&RemoteBufferStore> {
 820        match &self.state {
 821            BufferStoreState::Remote(state) => Some(state),
 822            _ => None,
 823        }
 824    }
 825
 826    pub fn open_buffer(
 827        &mut self,
 828        project_path: ProjectPath,
 829        cx: &mut Context<Self>,
 830    ) -> Task<Result<Entity<Buffer>>> {
 831        if let Some(buffer) = self.get_by_path(&project_path, cx) {
 832            return Task::ready(Ok(buffer));
 833        }
 834
 835        let task = match self.loading_buffers.entry(project_path.clone()) {
 836            hash_map::Entry::Occupied(e) => e.get().clone(),
 837            hash_map::Entry::Vacant(entry) => {
 838                let path = project_path.path.clone();
 839                let Some(worktree) = self
 840                    .worktree_store
 841                    .read(cx)
 842                    .worktree_for_id(project_path.worktree_id, cx)
 843                else {
 844                    return Task::ready(Err(anyhow!("no such worktree")));
 845                };
 846                let load_buffer = match &self.state {
 847                    BufferStoreState::Local(this) => this.open_buffer(path, worktree, cx),
 848                    BufferStoreState::Remote(this) => this.open_buffer(path, worktree, cx),
 849                };
 850
 851                entry
 852                    .insert(
 853                        cx.spawn(move |this, mut cx| async move {
 854                            let load_result = load_buffer.await;
 855                            this.update(&mut cx, |this, _cx| {
 856                                // Record the fact that the buffer is no longer loading.
 857                                this.loading_buffers.remove(&project_path);
 858                            })
 859                            .ok();
 860                            load_result.map_err(Arc::new)
 861                        })
 862                        .shared(),
 863                    )
 864                    .clone()
 865            }
 866        };
 867
 868        cx.background_spawn(async move { task.await.map_err(|e| anyhow!("{e}")) })
 869    }
 870
 871    pub(crate) fn worktree_for_buffer(
 872        &self,
 873        buffer: &Entity<Buffer>,
 874        cx: &App,
 875    ) -> Option<(Entity<Worktree>, Arc<Path>)> {
 876        let file = buffer.read(cx).file()?;
 877        let worktree_id = file.worktree_id(cx);
 878        let path = file.path().clone();
 879        let worktree = self
 880            .worktree_store
 881            .read(cx)
 882            .worktree_for_id(worktree_id, cx)?;
 883        Some((worktree, path))
 884    }
 885
 886    pub fn create_buffer(&mut self, cx: &mut Context<Self>) -> Task<Result<Entity<Buffer>>> {
 887        match &self.state {
 888            BufferStoreState::Local(this) => this.create_buffer(cx),
 889            BufferStoreState::Remote(this) => this.create_buffer(cx),
 890        }
 891    }
 892
 893    pub fn save_buffer(
 894        &mut self,
 895        buffer: Entity<Buffer>,
 896        cx: &mut Context<Self>,
 897    ) -> Task<Result<()>> {
 898        match &mut self.state {
 899            BufferStoreState::Local(this) => this.save_buffer(buffer, cx),
 900            BufferStoreState::Remote(this) => this.save_remote_buffer(buffer.clone(), None, cx),
 901        }
 902    }
 903
 904    pub fn save_buffer_as(
 905        &mut self,
 906        buffer: Entity<Buffer>,
 907        path: ProjectPath,
 908        cx: &mut Context<Self>,
 909    ) -> Task<Result<()>> {
 910        let old_file = buffer.read(cx).file().cloned();
 911        let task = match &self.state {
 912            BufferStoreState::Local(this) => this.save_buffer_as(buffer.clone(), path, cx),
 913            BufferStoreState::Remote(this) => {
 914                this.save_remote_buffer(buffer.clone(), Some(path.to_proto()), cx)
 915            }
 916        };
 917        cx.spawn(|this, mut cx| async move {
 918            task.await?;
 919            this.update(&mut cx, |_, cx| {
 920                cx.emit(BufferStoreEvent::BufferChangedFilePath { buffer, old_file });
 921            })
 922        })
 923    }
 924
 925    pub fn blame_buffer(
 926        &self,
 927        buffer: &Entity<Buffer>,
 928        version: Option<clock::Global>,
 929        cx: &App,
 930    ) -> Task<Result<Option<Blame>>> {
 931        let buffer = buffer.read(cx);
 932        let Some(file) = File::from_dyn(buffer.file()) else {
 933            return Task::ready(Err(anyhow!("buffer has no file")));
 934        };
 935
 936        match file.worktree.clone().read(cx) {
 937            Worktree::Local(worktree) => {
 938                let worktree = worktree.snapshot();
 939                let blame_params = maybe!({
 940                    let local_repo = match worktree.local_repo_for_path(&file.path) {
 941                        Some(repo_for_path) => repo_for_path,
 942                        None => return Ok(None),
 943                    };
 944
 945                    let relative_path = local_repo
 946                        .relativize(&file.path)
 947                        .context("failed to relativize buffer path")?;
 948
 949                    let repo = local_repo.repo().clone();
 950
 951                    let content = match version {
 952                        Some(version) => buffer.rope_for_version(&version).clone(),
 953                        None => buffer.as_rope().clone(),
 954                    };
 955
 956                    anyhow::Ok(Some((repo, relative_path, content)))
 957                });
 958
 959                cx.spawn(|cx| async move {
 960                    let Some((repo, relative_path, content)) = blame_params? else {
 961                        return Ok(None);
 962                    };
 963                    repo.blame(relative_path.clone(), content, cx)
 964                        .await
 965                        .with_context(|| format!("Failed to blame {:?}", relative_path.0))
 966                        .map(Some)
 967                })
 968            }
 969            Worktree::Remote(worktree) => {
 970                let buffer_id = buffer.remote_id();
 971                let version = buffer.version();
 972                let project_id = worktree.project_id();
 973                let client = worktree.client();
 974                cx.spawn(|_| async move {
 975                    let response = client
 976                        .request(proto::BlameBuffer {
 977                            project_id,
 978                            buffer_id: buffer_id.into(),
 979                            version: serialize_version(&version),
 980                        })
 981                        .await?;
 982                    Ok(deserialize_blame_buffer_response(response))
 983                })
 984            }
 985        }
 986    }
 987
 988    pub fn get_permalink_to_line(
 989        &self,
 990        buffer: &Entity<Buffer>,
 991        selection: Range<u32>,
 992        cx: &App,
 993    ) -> Task<Result<url::Url>> {
 994        let buffer = buffer.read(cx);
 995        let Some(file) = File::from_dyn(buffer.file()) else {
 996            return Task::ready(Err(anyhow!("buffer has no file")));
 997        };
 998
 999        match file.worktree.read(cx) {
1000            Worktree::Local(worktree) => {
1001                let worktree_path = worktree.abs_path().clone();
1002                let Some((repo_entry, repo)) =
1003                    worktree.repository_for_path(file.path()).and_then(|entry| {
1004                        let repo = worktree.get_local_repo(&entry)?.repo().clone();
1005                        Some((entry, repo))
1006                    })
1007                else {
1008                    // If we're not in a Git repo, check whether this is a Rust source
1009                    // file in the Cargo registry (presumably opened with go-to-definition
1010                    // from a normal Rust file). If so, we can put together a permalink
1011                    // using crate metadata.
1012                    if buffer
1013                        .language()
1014                        .is_none_or(|lang| lang.name() != "Rust".into())
1015                    {
1016                        return Task::ready(Err(anyhow!("no permalink available")));
1017                    }
1018                    let file_path = worktree_path.join(file.path());
1019                    return cx.spawn(|cx| async move {
1020                        let provider_registry =
1021                            cx.update(GitHostingProviderRegistry::default_global)?;
1022                        get_permalink_in_rust_registry_src(provider_registry, file_path, selection)
1023                            .map_err(|_| anyhow!("no permalink available"))
1024                    });
1025                };
1026
1027                let path = match repo_entry.relativize(file.path()) {
1028                    Ok(RepoPath(path)) => path,
1029                    Err(e) => return Task::ready(Err(e)),
1030                };
1031
1032                let remote = repo_entry
1033                    .branch()
1034                    .and_then(|b| b.upstream.as_ref())
1035                    .and_then(|b| b.remote_name())
1036                    .unwrap_or("origin")
1037                    .to_string();
1038
1039                cx.spawn(|cx| async move {
1040                    let origin_url = repo
1041                        .remote_url(&remote)
1042                        .ok_or_else(|| anyhow!("remote \"{remote}\" not found"))?;
1043
1044                    let sha = repo
1045                        .head_sha()
1046                        .ok_or_else(|| anyhow!("failed to read HEAD SHA"))?;
1047
1048                    let provider_registry =
1049                        cx.update(GitHostingProviderRegistry::default_global)?;
1050
1051                    let (provider, remote) =
1052                        parse_git_remote_url(provider_registry, &origin_url)
1053                            .ok_or_else(|| anyhow!("failed to parse Git remote URL"))?;
1054
1055                    let path = path
1056                        .to_str()
1057                        .ok_or_else(|| anyhow!("failed to convert path to string"))?;
1058
1059                    Ok(provider.build_permalink(
1060                        remote,
1061                        BuildPermalinkParams {
1062                            sha: &sha,
1063                            path,
1064                            selection: Some(selection),
1065                        },
1066                    ))
1067                })
1068            }
1069            Worktree::Remote(worktree) => {
1070                let buffer_id = buffer.remote_id();
1071                let project_id = worktree.project_id();
1072                let client = worktree.client();
1073                cx.spawn(|_| async move {
1074                    let response = client
1075                        .request(proto::GetPermalinkToLine {
1076                            project_id,
1077                            buffer_id: buffer_id.into(),
1078                            selection: Some(proto::Range {
1079                                start: selection.start as u64,
1080                                end: selection.end as u64,
1081                            }),
1082                        })
1083                        .await?;
1084
1085                    url::Url::parse(&response.permalink).context("failed to parse permalink")
1086                })
1087            }
1088        }
1089    }
1090
1091    fn add_buffer(&mut self, buffer_entity: Entity<Buffer>, cx: &mut Context<Self>) -> Result<()> {
1092        let buffer = buffer_entity.read(cx);
1093        let remote_id = buffer.remote_id();
1094        let is_remote = buffer.replica_id() != 0;
1095        let open_buffer = OpenBuffer::Complete {
1096            buffer: buffer_entity.downgrade(),
1097        };
1098
1099        let handle = cx.entity().downgrade();
1100        buffer_entity.update(cx, move |_, cx| {
1101            cx.on_release(move |buffer, cx| {
1102                handle
1103                    .update(cx, |_, cx| {
1104                        cx.emit(BufferStoreEvent::BufferDropped(buffer.remote_id()))
1105                    })
1106                    .ok();
1107            })
1108            .detach()
1109        });
1110
1111        match self.opened_buffers.entry(remote_id) {
1112            hash_map::Entry::Vacant(entry) => {
1113                entry.insert(open_buffer);
1114            }
1115            hash_map::Entry::Occupied(mut entry) => {
1116                if let OpenBuffer::Operations(operations) = entry.get_mut() {
1117                    buffer_entity.update(cx, |b, cx| b.apply_ops(operations.drain(..), cx));
1118                } else if entry.get().upgrade().is_some() {
1119                    if is_remote {
1120                        return Ok(());
1121                    } else {
1122                        debug_panic!("buffer {} was already registered", remote_id);
1123                        Err(anyhow!("buffer {} was already registered", remote_id))?;
1124                    }
1125                }
1126                entry.insert(open_buffer);
1127            }
1128        }
1129
1130        cx.subscribe(&buffer_entity, Self::on_buffer_event).detach();
1131        cx.emit(BufferStoreEvent::BufferAdded(buffer_entity));
1132        Ok(())
1133    }
1134
1135    pub fn buffers(&self) -> impl '_ + Iterator<Item = Entity<Buffer>> {
1136        self.opened_buffers
1137            .values()
1138            .filter_map(|buffer| buffer.upgrade())
1139    }
1140
1141    pub fn loading_buffers(
1142        &self,
1143    ) -> impl Iterator<Item = (&ProjectPath, impl Future<Output = Result<Entity<Buffer>>>)> {
1144        self.loading_buffers.iter().map(|(path, task)| {
1145            let task = task.clone();
1146            (path, async move { task.await.map_err(|e| anyhow!("{e}")) })
1147        })
1148    }
1149
1150    pub fn get_by_path(&self, path: &ProjectPath, cx: &App) -> Option<Entity<Buffer>> {
1151        self.buffers().find_map(|buffer| {
1152            let file = File::from_dyn(buffer.read(cx).file())?;
1153            if file.worktree_id(cx) == path.worktree_id && file.path == path.path {
1154                Some(buffer)
1155            } else {
1156                None
1157            }
1158        })
1159    }
1160
1161    pub fn get(&self, buffer_id: BufferId) -> Option<Entity<Buffer>> {
1162        self.opened_buffers.get(&buffer_id)?.upgrade()
1163    }
1164
1165    pub fn get_existing(&self, buffer_id: BufferId) -> Result<Entity<Buffer>> {
1166        self.get(buffer_id)
1167            .ok_or_else(|| anyhow!("unknown buffer id {}", buffer_id))
1168    }
1169
1170    pub fn get_possibly_incomplete(&self, buffer_id: BufferId) -> Option<Entity<Buffer>> {
1171        self.get(buffer_id).or_else(|| {
1172            self.as_remote()
1173                .and_then(|remote| remote.loading_remote_buffers_by_id.get(&buffer_id).cloned())
1174        })
1175    }
1176
1177    pub fn buffer_version_info(&self, cx: &App) -> (Vec<proto::BufferVersion>, Vec<BufferId>) {
1178        let buffers = self
1179            .buffers()
1180            .map(|buffer| {
1181                let buffer = buffer.read(cx);
1182                proto::BufferVersion {
1183                    id: buffer.remote_id().into(),
1184                    version: language::proto::serialize_version(&buffer.version),
1185                }
1186            })
1187            .collect();
1188        let incomplete_buffer_ids = self
1189            .as_remote()
1190            .map(|remote| remote.incomplete_buffer_ids())
1191            .unwrap_or_default();
1192        (buffers, incomplete_buffer_ids)
1193    }
1194
1195    pub fn disconnected_from_host(&mut self, cx: &mut App) {
1196        for open_buffer in self.opened_buffers.values_mut() {
1197            if let Some(buffer) = open_buffer.upgrade() {
1198                buffer.update(cx, |buffer, _| buffer.give_up_waiting());
1199            }
1200        }
1201
1202        for buffer in self.buffers() {
1203            buffer.update(cx, |buffer, cx| {
1204                buffer.set_capability(Capability::ReadOnly, cx)
1205            });
1206        }
1207
1208        if let Some(remote) = self.as_remote_mut() {
1209            // Wake up all futures currently waiting on a buffer to get opened,
1210            // to give them a chance to fail now that we've disconnected.
1211            remote.remote_buffer_listeners.clear()
1212        }
1213    }
1214
1215    pub fn shared(&mut self, remote_id: u64, downstream_client: AnyProtoClient, _cx: &mut App) {
1216        self.downstream_client = Some((downstream_client, remote_id));
1217    }
1218
1219    pub fn unshared(&mut self, _cx: &mut Context<Self>) {
1220        self.downstream_client.take();
1221        self.forget_shared_buffers();
1222    }
1223
1224    pub fn discard_incomplete(&mut self) {
1225        self.opened_buffers
1226            .retain(|_, buffer| !matches!(buffer, OpenBuffer::Operations(_)));
1227    }
1228
1229    pub fn find_search_candidates(
1230        &mut self,
1231        query: &SearchQuery,
1232        mut limit: usize,
1233        fs: Arc<dyn Fs>,
1234        cx: &mut Context<Self>,
1235    ) -> Receiver<Entity<Buffer>> {
1236        let (tx, rx) = smol::channel::unbounded();
1237        let mut open_buffers = HashSet::default();
1238        let mut unnamed_buffers = Vec::new();
1239        for handle in self.buffers() {
1240            let buffer = handle.read(cx);
1241            if let Some(entry_id) = buffer.entry_id(cx) {
1242                open_buffers.insert(entry_id);
1243            } else {
1244                limit = limit.saturating_sub(1);
1245                unnamed_buffers.push(handle)
1246            };
1247        }
1248
1249        const MAX_CONCURRENT_BUFFER_OPENS: usize = 64;
1250        let project_paths_rx = self
1251            .worktree_store
1252            .update(cx, |worktree_store, cx| {
1253                worktree_store.find_search_candidates(query.clone(), limit, open_buffers, fs, cx)
1254            })
1255            .chunks(MAX_CONCURRENT_BUFFER_OPENS);
1256
1257        cx.spawn(|this, mut cx| async move {
1258            for buffer in unnamed_buffers {
1259                tx.send(buffer).await.ok();
1260            }
1261
1262            let mut project_paths_rx = pin!(project_paths_rx);
1263            while let Some(project_paths) = project_paths_rx.next().await {
1264                let buffers = this.update(&mut cx, |this, cx| {
1265                    project_paths
1266                        .into_iter()
1267                        .map(|project_path| this.open_buffer(project_path, cx))
1268                        .collect::<Vec<_>>()
1269                })?;
1270                for buffer_task in buffers {
1271                    if let Some(buffer) = buffer_task.await.log_err() {
1272                        if tx.send(buffer).await.is_err() {
1273                            return anyhow::Ok(());
1274                        }
1275                    }
1276                }
1277            }
1278            anyhow::Ok(())
1279        })
1280        .detach();
1281        rx
1282    }
1283
1284    fn on_buffer_event(
1285        &mut self,
1286        buffer: Entity<Buffer>,
1287        event: &BufferEvent,
1288        cx: &mut Context<Self>,
1289    ) {
1290        match event {
1291            BufferEvent::FileHandleChanged => {
1292                if let Some(local) = self.as_local_mut() {
1293                    local.buffer_changed_file(buffer, cx);
1294                }
1295            }
1296            BufferEvent::Reloaded => {
1297                let Some((downstream_client, project_id)) = self.downstream_client.as_ref() else {
1298                    return;
1299                };
1300                let buffer = buffer.read(cx);
1301                downstream_client
1302                    .send(proto::BufferReloaded {
1303                        project_id: *project_id,
1304                        buffer_id: buffer.remote_id().to_proto(),
1305                        version: serialize_version(&buffer.version()),
1306                        mtime: buffer.saved_mtime().map(|t| t.into()),
1307                        line_ending: serialize_line_ending(buffer.line_ending()) as i32,
1308                    })
1309                    .log_err();
1310            }
1311            BufferEvent::LanguageChanged => {}
1312            _ => {}
1313        }
1314    }
1315
1316    pub async fn handle_update_buffer(
1317        this: Entity<Self>,
1318        envelope: TypedEnvelope<proto::UpdateBuffer>,
1319        mut cx: AsyncApp,
1320    ) -> Result<proto::Ack> {
1321        let payload = envelope.payload.clone();
1322        let buffer_id = BufferId::new(payload.buffer_id)?;
1323        let ops = payload
1324            .operations
1325            .into_iter()
1326            .map(language::proto::deserialize_operation)
1327            .collect::<Result<Vec<_>, _>>()?;
1328        this.update(&mut cx, |this, cx| {
1329            match this.opened_buffers.entry(buffer_id) {
1330                hash_map::Entry::Occupied(mut e) => match e.get_mut() {
1331                    OpenBuffer::Operations(operations) => operations.extend_from_slice(&ops),
1332                    OpenBuffer::Complete { buffer, .. } => {
1333                        if let Some(buffer) = buffer.upgrade() {
1334                            buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx));
1335                        }
1336                    }
1337                },
1338                hash_map::Entry::Vacant(e) => {
1339                    e.insert(OpenBuffer::Operations(ops));
1340                }
1341            }
1342            Ok(proto::Ack {})
1343        })?
1344    }
1345
1346    pub fn register_shared_lsp_handle(
1347        &mut self,
1348        peer_id: proto::PeerId,
1349        buffer_id: BufferId,
1350        handle: OpenLspBufferHandle,
1351    ) {
1352        if let Some(shared_buffers) = self.shared_buffers.get_mut(&peer_id) {
1353            if let Some(buffer) = shared_buffers.get_mut(&buffer_id) {
1354                buffer.lsp_handle = Some(handle);
1355                return;
1356            }
1357        }
1358        debug_panic!("tried to register shared lsp handle, but buffer was not shared")
1359    }
1360
1361    pub fn handle_synchronize_buffers(
1362        &mut self,
1363        envelope: TypedEnvelope<proto::SynchronizeBuffers>,
1364        cx: &mut Context<Self>,
1365        client: Arc<Client>,
1366    ) -> Result<proto::SynchronizeBuffersResponse> {
1367        let project_id = envelope.payload.project_id;
1368        let mut response = proto::SynchronizeBuffersResponse {
1369            buffers: Default::default(),
1370        };
1371        let Some(guest_id) = envelope.original_sender_id else {
1372            anyhow::bail!("missing original_sender_id on SynchronizeBuffers request");
1373        };
1374
1375        self.shared_buffers.entry(guest_id).or_default().clear();
1376        for buffer in envelope.payload.buffers {
1377            let buffer_id = BufferId::new(buffer.id)?;
1378            let remote_version = language::proto::deserialize_version(&buffer.version);
1379            if let Some(buffer) = self.get(buffer_id) {
1380                self.shared_buffers
1381                    .entry(guest_id)
1382                    .or_default()
1383                    .entry(buffer_id)
1384                    .or_insert_with(|| SharedBuffer {
1385                        buffer: buffer.clone(),
1386                        lsp_handle: None,
1387                    });
1388
1389                let buffer = buffer.read(cx);
1390                response.buffers.push(proto::BufferVersion {
1391                    id: buffer_id.into(),
1392                    version: language::proto::serialize_version(&buffer.version),
1393                });
1394
1395                let operations = buffer.serialize_ops(Some(remote_version), cx);
1396                let client = client.clone();
1397                if let Some(file) = buffer.file() {
1398                    client
1399                        .send(proto::UpdateBufferFile {
1400                            project_id,
1401                            buffer_id: buffer_id.into(),
1402                            file: Some(file.to_proto(cx)),
1403                        })
1404                        .log_err();
1405                }
1406
1407                // TODO(max): do something
1408                // client
1409                //     .send(proto::UpdateStagedText {
1410                //         project_id,
1411                //         buffer_id: buffer_id.into(),
1412                //         diff_base: buffer.diff_base().map(ToString::to_string),
1413                //     })
1414                //     .log_err();
1415
1416                client
1417                    .send(proto::BufferReloaded {
1418                        project_id,
1419                        buffer_id: buffer_id.into(),
1420                        version: language::proto::serialize_version(buffer.saved_version()),
1421                        mtime: buffer.saved_mtime().map(|time| time.into()),
1422                        line_ending: language::proto::serialize_line_ending(buffer.line_ending())
1423                            as i32,
1424                    })
1425                    .log_err();
1426
1427                cx.background_spawn(
1428                    async move {
1429                        let operations = operations.await;
1430                        for chunk in split_operations(operations) {
1431                            client
1432                                .request(proto::UpdateBuffer {
1433                                    project_id,
1434                                    buffer_id: buffer_id.into(),
1435                                    operations: chunk,
1436                                })
1437                                .await?;
1438                        }
1439                        anyhow::Ok(())
1440                    }
1441                    .log_err(),
1442                )
1443                .detach();
1444            }
1445        }
1446        Ok(response)
1447    }
1448
1449    pub fn handle_create_buffer_for_peer(
1450        &mut self,
1451        envelope: TypedEnvelope<proto::CreateBufferForPeer>,
1452        replica_id: u16,
1453        capability: Capability,
1454        cx: &mut Context<Self>,
1455    ) -> Result<()> {
1456        let Some(remote) = self.as_remote_mut() else {
1457            return Err(anyhow!("buffer store is not a remote"));
1458        };
1459
1460        if let Some(buffer) =
1461            remote.handle_create_buffer_for_peer(envelope, replica_id, capability, cx)?
1462        {
1463            self.add_buffer(buffer, cx)?;
1464        }
1465
1466        Ok(())
1467    }
1468
1469    pub async fn handle_update_buffer_file(
1470        this: Entity<Self>,
1471        envelope: TypedEnvelope<proto::UpdateBufferFile>,
1472        mut cx: AsyncApp,
1473    ) -> Result<()> {
1474        let buffer_id = envelope.payload.buffer_id;
1475        let buffer_id = BufferId::new(buffer_id)?;
1476
1477        this.update(&mut cx, |this, cx| {
1478            let payload = envelope.payload.clone();
1479            if let Some(buffer) = this.get_possibly_incomplete(buffer_id) {
1480                let file = payload.file.ok_or_else(|| anyhow!("invalid file"))?;
1481                let worktree = this
1482                    .worktree_store
1483                    .read(cx)
1484                    .worktree_for_id(WorktreeId::from_proto(file.worktree_id), cx)
1485                    .ok_or_else(|| anyhow!("no such worktree"))?;
1486                let file = File::from_proto(file, worktree, cx)?;
1487                let old_file = buffer.update(cx, |buffer, cx| {
1488                    let old_file = buffer.file().cloned();
1489                    let new_path = file.path.clone();
1490                    buffer.file_updated(Arc::new(file), cx);
1491                    if old_file
1492                        .as_ref()
1493                        .map_or(true, |old| *old.path() != new_path)
1494                    {
1495                        Some(old_file)
1496                    } else {
1497                        None
1498                    }
1499                });
1500                if let Some(old_file) = old_file {
1501                    cx.emit(BufferStoreEvent::BufferChangedFilePath { buffer, old_file });
1502                }
1503            }
1504            if let Some((downstream_client, project_id)) = this.downstream_client.as_ref() {
1505                downstream_client
1506                    .send(proto::UpdateBufferFile {
1507                        project_id: *project_id,
1508                        buffer_id: buffer_id.into(),
1509                        file: envelope.payload.file,
1510                    })
1511                    .log_err();
1512            }
1513            Ok(())
1514        })?
1515    }
1516
1517    pub async fn handle_save_buffer(
1518        this: Entity<Self>,
1519        envelope: TypedEnvelope<proto::SaveBuffer>,
1520        mut cx: AsyncApp,
1521    ) -> Result<proto::BufferSaved> {
1522        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1523        let (buffer, project_id) = this.update(&mut cx, |this, _| {
1524            anyhow::Ok((
1525                this.get_existing(buffer_id)?,
1526                this.downstream_client
1527                    .as_ref()
1528                    .map(|(_, project_id)| *project_id)
1529                    .context("project is not shared")?,
1530            ))
1531        })??;
1532        buffer
1533            .update(&mut cx, |buffer, _| {
1534                buffer.wait_for_version(deserialize_version(&envelope.payload.version))
1535            })?
1536            .await?;
1537        let buffer_id = buffer.update(&mut cx, |buffer, _| buffer.remote_id())?;
1538
1539        if let Some(new_path) = envelope.payload.new_path {
1540            let new_path = ProjectPath::from_proto(new_path);
1541            this.update(&mut cx, |this, cx| {
1542                this.save_buffer_as(buffer.clone(), new_path, cx)
1543            })?
1544            .await?;
1545        } else {
1546            this.update(&mut cx, |this, cx| this.save_buffer(buffer.clone(), cx))?
1547                .await?;
1548        }
1549
1550        buffer.update(&mut cx, |buffer, _| proto::BufferSaved {
1551            project_id,
1552            buffer_id: buffer_id.into(),
1553            version: serialize_version(buffer.saved_version()),
1554            mtime: buffer.saved_mtime().map(|time| time.into()),
1555        })
1556    }
1557
1558    pub async fn handle_close_buffer(
1559        this: Entity<Self>,
1560        envelope: TypedEnvelope<proto::CloseBuffer>,
1561        mut cx: AsyncApp,
1562    ) -> Result<()> {
1563        let peer_id = envelope.sender_id;
1564        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1565        this.update(&mut cx, |this, cx| {
1566            if let Some(shared) = this.shared_buffers.get_mut(&peer_id) {
1567                if shared.remove(&buffer_id).is_some() {
1568                    cx.emit(BufferStoreEvent::SharedBufferClosed(peer_id, buffer_id));
1569                    if shared.is_empty() {
1570                        this.shared_buffers.remove(&peer_id);
1571                    }
1572                    return;
1573                }
1574            }
1575            debug_panic!(
1576                "peer_id {} closed buffer_id {} which was either not open or already closed",
1577                peer_id,
1578                buffer_id
1579            )
1580        })
1581    }
1582
1583    pub async fn handle_buffer_saved(
1584        this: Entity<Self>,
1585        envelope: TypedEnvelope<proto::BufferSaved>,
1586        mut cx: AsyncApp,
1587    ) -> Result<()> {
1588        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1589        let version = deserialize_version(&envelope.payload.version);
1590        let mtime = envelope.payload.mtime.clone().map(|time| time.into());
1591        this.update(&mut cx, move |this, cx| {
1592            if let Some(buffer) = this.get_possibly_incomplete(buffer_id) {
1593                buffer.update(cx, |buffer, cx| {
1594                    buffer.did_save(version, mtime, cx);
1595                });
1596            }
1597
1598            if let Some((downstream_client, project_id)) = this.downstream_client.as_ref() {
1599                downstream_client
1600                    .send(proto::BufferSaved {
1601                        project_id: *project_id,
1602                        buffer_id: buffer_id.into(),
1603                        mtime: envelope.payload.mtime,
1604                        version: envelope.payload.version,
1605                    })
1606                    .log_err();
1607            }
1608        })
1609    }
1610
1611    pub async fn handle_buffer_reloaded(
1612        this: Entity<Self>,
1613        envelope: TypedEnvelope<proto::BufferReloaded>,
1614        mut cx: AsyncApp,
1615    ) -> Result<()> {
1616        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1617        let version = deserialize_version(&envelope.payload.version);
1618        let mtime = envelope.payload.mtime.clone().map(|time| time.into());
1619        let line_ending = deserialize_line_ending(
1620            proto::LineEnding::from_i32(envelope.payload.line_ending)
1621                .ok_or_else(|| anyhow!("missing line ending"))?,
1622        );
1623        this.update(&mut cx, |this, cx| {
1624            if let Some(buffer) = this.get_possibly_incomplete(buffer_id) {
1625                buffer.update(cx, |buffer, cx| {
1626                    buffer.did_reload(version, line_ending, mtime, cx);
1627                });
1628            }
1629
1630            if let Some((downstream_client, project_id)) = this.downstream_client.as_ref() {
1631                downstream_client
1632                    .send(proto::BufferReloaded {
1633                        project_id: *project_id,
1634                        buffer_id: buffer_id.into(),
1635                        mtime: envelope.payload.mtime,
1636                        version: envelope.payload.version,
1637                        line_ending: envelope.payload.line_ending,
1638                    })
1639                    .log_err();
1640            }
1641        })
1642    }
1643
1644    pub async fn handle_blame_buffer(
1645        this: Entity<Self>,
1646        envelope: TypedEnvelope<proto::BlameBuffer>,
1647        mut cx: AsyncApp,
1648    ) -> Result<proto::BlameBufferResponse> {
1649        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1650        let version = deserialize_version(&envelope.payload.version);
1651        let buffer = this.read_with(&cx, |this, _| this.get_existing(buffer_id))??;
1652        buffer
1653            .update(&mut cx, |buffer, _| {
1654                buffer.wait_for_version(version.clone())
1655            })?
1656            .await?;
1657        let blame = this
1658            .update(&mut cx, |this, cx| {
1659                this.blame_buffer(&buffer, Some(version), cx)
1660            })?
1661            .await?;
1662        Ok(serialize_blame_buffer_response(blame))
1663    }
1664
1665    pub async fn handle_get_permalink_to_line(
1666        this: Entity<Self>,
1667        envelope: TypedEnvelope<proto::GetPermalinkToLine>,
1668        mut cx: AsyncApp,
1669    ) -> Result<proto::GetPermalinkToLineResponse> {
1670        let buffer_id = BufferId::new(envelope.payload.buffer_id)?;
1671        // let version = deserialize_version(&envelope.payload.version);
1672        let selection = {
1673            let proto_selection = envelope
1674                .payload
1675                .selection
1676                .context("no selection to get permalink for defined")?;
1677            proto_selection.start as u32..proto_selection.end as u32
1678        };
1679        let buffer = this.read_with(&cx, |this, _| this.get_existing(buffer_id))??;
1680        let permalink = this
1681            .update(&mut cx, |this, cx| {
1682                this.get_permalink_to_line(&buffer, selection, cx)
1683            })?
1684            .await?;
1685        Ok(proto::GetPermalinkToLineResponse {
1686            permalink: permalink.to_string(),
1687        })
1688    }
1689
1690    pub fn reload_buffers(
1691        &self,
1692        buffers: HashSet<Entity<Buffer>>,
1693        push_to_history: bool,
1694        cx: &mut Context<Self>,
1695    ) -> Task<Result<ProjectTransaction>> {
1696        if buffers.is_empty() {
1697            return Task::ready(Ok(ProjectTransaction::default()));
1698        }
1699        match &self.state {
1700            BufferStoreState::Local(this) => this.reload_buffers(buffers, push_to_history, cx),
1701            BufferStoreState::Remote(this) => this.reload_buffers(buffers, push_to_history, cx),
1702        }
1703    }
1704
1705    async fn handle_reload_buffers(
1706        this: Entity<Self>,
1707        envelope: TypedEnvelope<proto::ReloadBuffers>,
1708        mut cx: AsyncApp,
1709    ) -> Result<proto::ReloadBuffersResponse> {
1710        let sender_id = envelope.original_sender_id().unwrap_or_default();
1711        let reload = this.update(&mut cx, |this, cx| {
1712            let mut buffers = HashSet::default();
1713            for buffer_id in &envelope.payload.buffer_ids {
1714                let buffer_id = BufferId::new(*buffer_id)?;
1715                buffers.insert(this.get_existing(buffer_id)?);
1716            }
1717            Ok::<_, anyhow::Error>(this.reload_buffers(buffers, false, cx))
1718        })??;
1719
1720        let project_transaction = reload.await?;
1721        let project_transaction = this.update(&mut cx, |this, cx| {
1722            this.serialize_project_transaction_for_peer(project_transaction, sender_id, cx)
1723        })?;
1724        Ok(proto::ReloadBuffersResponse {
1725            transaction: Some(project_transaction),
1726        })
1727    }
1728
1729    pub fn create_buffer_for_peer(
1730        &mut self,
1731        buffer: &Entity<Buffer>,
1732        peer_id: proto::PeerId,
1733        cx: &mut Context<Self>,
1734    ) -> Task<Result<()>> {
1735        let buffer_id = buffer.read(cx).remote_id();
1736        let shared_buffers = self.shared_buffers.entry(peer_id).or_default();
1737        if shared_buffers.contains_key(&buffer_id) {
1738            return Task::ready(Ok(()));
1739        }
1740        shared_buffers.insert(
1741            buffer_id,
1742            SharedBuffer {
1743                buffer: buffer.clone(),
1744                lsp_handle: None,
1745            },
1746        );
1747
1748        let Some((client, project_id)) = self.downstream_client.clone() else {
1749            return Task::ready(Ok(()));
1750        };
1751
1752        cx.spawn(|this, mut cx| async move {
1753            let Some(buffer) = this.update(&mut cx, |this, _| this.get(buffer_id))? else {
1754                return anyhow::Ok(());
1755            };
1756
1757            let operations = buffer.update(&mut cx, |b, cx| b.serialize_ops(None, cx))?;
1758            let operations = operations.await;
1759            let state = buffer.update(&mut cx, |buffer, cx| buffer.to_proto(cx))?;
1760
1761            let initial_state = proto::CreateBufferForPeer {
1762                project_id,
1763                peer_id: Some(peer_id),
1764                variant: Some(proto::create_buffer_for_peer::Variant::State(state)),
1765            };
1766
1767            if client.send(initial_state).log_err().is_some() {
1768                let client = client.clone();
1769                cx.background_spawn(async move {
1770                    let mut chunks = split_operations(operations).peekable();
1771                    while let Some(chunk) = chunks.next() {
1772                        let is_last = chunks.peek().is_none();
1773                        client.send(proto::CreateBufferForPeer {
1774                            project_id,
1775                            peer_id: Some(peer_id),
1776                            variant: Some(proto::create_buffer_for_peer::Variant::Chunk(
1777                                proto::BufferChunk {
1778                                    buffer_id: buffer_id.into(),
1779                                    operations: chunk,
1780                                    is_last,
1781                                },
1782                            )),
1783                        })?;
1784                    }
1785                    anyhow::Ok(())
1786                })
1787                .await
1788                .log_err();
1789            }
1790            Ok(())
1791        })
1792    }
1793
1794    pub fn forget_shared_buffers(&mut self) {
1795        self.shared_buffers.clear();
1796    }
1797
1798    pub fn forget_shared_buffers_for(&mut self, peer_id: &proto::PeerId) {
1799        self.shared_buffers.remove(peer_id);
1800    }
1801
1802    pub fn update_peer_id(&mut self, old_peer_id: &proto::PeerId, new_peer_id: proto::PeerId) {
1803        if let Some(buffers) = self.shared_buffers.remove(old_peer_id) {
1804            self.shared_buffers.insert(new_peer_id, buffers);
1805        }
1806    }
1807
1808    pub fn has_shared_buffers(&self) -> bool {
1809        !self.shared_buffers.is_empty()
1810    }
1811
1812    pub fn create_local_buffer(
1813        &mut self,
1814        text: &str,
1815        language: Option<Arc<Language>>,
1816        cx: &mut Context<Self>,
1817    ) -> Entity<Buffer> {
1818        let buffer = cx.new(|cx| {
1819            Buffer::local(text, cx)
1820                .with_language(language.unwrap_or_else(|| language::PLAIN_TEXT.clone()), cx)
1821        });
1822
1823        self.add_buffer(buffer.clone(), cx).log_err();
1824        let buffer_id = buffer.read(cx).remote_id();
1825
1826        let this = self
1827            .as_local_mut()
1828            .expect("local-only method called in a non-local context");
1829        if let Some(file) = File::from_dyn(buffer.read(cx).file()) {
1830            this.local_buffer_ids_by_path.insert(
1831                ProjectPath {
1832                    worktree_id: file.worktree_id(cx),
1833                    path: file.path.clone(),
1834                },
1835                buffer_id,
1836            );
1837
1838            if let Some(entry_id) = file.entry_id {
1839                this.local_buffer_ids_by_entry_id
1840                    .insert(entry_id, buffer_id);
1841            }
1842        }
1843        buffer
1844    }
1845
1846    pub fn deserialize_project_transaction(
1847        &mut self,
1848        message: proto::ProjectTransaction,
1849        push_to_history: bool,
1850        cx: &mut Context<Self>,
1851    ) -> Task<Result<ProjectTransaction>> {
1852        if let Some(this) = self.as_remote_mut() {
1853            this.deserialize_project_transaction(message, push_to_history, cx)
1854        } else {
1855            debug_panic!("not a remote buffer store");
1856            Task::ready(Err(anyhow!("not a remote buffer store")))
1857        }
1858    }
1859
1860    pub fn wait_for_remote_buffer(
1861        &mut self,
1862        id: BufferId,
1863        cx: &mut Context<BufferStore>,
1864    ) -> Task<Result<Entity<Buffer>>> {
1865        if let Some(this) = self.as_remote_mut() {
1866            this.wait_for_remote_buffer(id, cx)
1867        } else {
1868            debug_panic!("not a remote buffer store");
1869            Task::ready(Err(anyhow!("not a remote buffer store")))
1870        }
1871    }
1872
1873    pub fn serialize_project_transaction_for_peer(
1874        &mut self,
1875        project_transaction: ProjectTransaction,
1876        peer_id: proto::PeerId,
1877        cx: &mut Context<Self>,
1878    ) -> proto::ProjectTransaction {
1879        let mut serialized_transaction = proto::ProjectTransaction {
1880            buffer_ids: Default::default(),
1881            transactions: Default::default(),
1882        };
1883        for (buffer, transaction) in project_transaction.0 {
1884            self.create_buffer_for_peer(&buffer, peer_id, cx)
1885                .detach_and_log_err(cx);
1886            serialized_transaction
1887                .buffer_ids
1888                .push(buffer.read(cx).remote_id().into());
1889            serialized_transaction
1890                .transactions
1891                .push(language::proto::serialize_transaction(&transaction));
1892        }
1893        serialized_transaction
1894    }
1895}
1896
1897impl OpenBuffer {
1898    fn upgrade(&self) -> Option<Entity<Buffer>> {
1899        match self {
1900            OpenBuffer::Complete { buffer, .. } => buffer.upgrade(),
1901            OpenBuffer::Operations(_) => None,
1902        }
1903    }
1904}
1905
1906fn is_not_found_error(error: &anyhow::Error) -> bool {
1907    error
1908        .root_cause()
1909        .downcast_ref::<io::Error>()
1910        .is_some_and(|err| err.kind() == io::ErrorKind::NotFound)
1911}
1912
1913fn serialize_blame_buffer_response(blame: Option<git::blame::Blame>) -> proto::BlameBufferResponse {
1914    let Some(blame) = blame else {
1915        return proto::BlameBufferResponse {
1916            blame_response: None,
1917        };
1918    };
1919
1920    let entries = blame
1921        .entries
1922        .into_iter()
1923        .map(|entry| proto::BlameEntry {
1924            sha: entry.sha.as_bytes().into(),
1925            start_line: entry.range.start,
1926            end_line: entry.range.end,
1927            original_line_number: entry.original_line_number,
1928            author: entry.author.clone(),
1929            author_mail: entry.author_mail.clone(),
1930            author_time: entry.author_time,
1931            author_tz: entry.author_tz.clone(),
1932            committer: entry.committer_name.clone(),
1933            committer_mail: entry.committer_email.clone(),
1934            committer_time: entry.committer_time,
1935            committer_tz: entry.committer_tz.clone(),
1936            summary: entry.summary.clone(),
1937            previous: entry.previous.clone(),
1938            filename: entry.filename.clone(),
1939        })
1940        .collect::<Vec<_>>();
1941
1942    let messages = blame
1943        .messages
1944        .into_iter()
1945        .map(|(oid, message)| proto::CommitMessage {
1946            oid: oid.as_bytes().into(),
1947            message,
1948        })
1949        .collect::<Vec<_>>();
1950
1951    proto::BlameBufferResponse {
1952        blame_response: Some(proto::blame_buffer_response::BlameResponse {
1953            entries,
1954            messages,
1955            remote_url: blame.remote_url,
1956        }),
1957    }
1958}
1959
1960fn deserialize_blame_buffer_response(
1961    response: proto::BlameBufferResponse,
1962) -> Option<git::blame::Blame> {
1963    let response = response.blame_response?;
1964    let entries = response
1965        .entries
1966        .into_iter()
1967        .filter_map(|entry| {
1968            Some(git::blame::BlameEntry {
1969                sha: git::Oid::from_bytes(&entry.sha).ok()?,
1970                range: entry.start_line..entry.end_line,
1971                original_line_number: entry.original_line_number,
1972                committer_name: entry.committer,
1973                committer_time: entry.committer_time,
1974                committer_tz: entry.committer_tz,
1975                committer_email: entry.committer_mail,
1976                author: entry.author,
1977                author_mail: entry.author_mail,
1978                author_time: entry.author_time,
1979                author_tz: entry.author_tz,
1980                summary: entry.summary,
1981                previous: entry.previous,
1982                filename: entry.filename,
1983            })
1984        })
1985        .collect::<Vec<_>>();
1986
1987    let messages = response
1988        .messages
1989        .into_iter()
1990        .filter_map(|message| Some((git::Oid::from_bytes(&message.oid).ok()?, message.message)))
1991        .collect::<HashMap<_, _>>();
1992
1993    Some(Blame {
1994        entries,
1995        messages,
1996        remote_url: response.remote_url,
1997    })
1998}
1999
2000fn get_permalink_in_rust_registry_src(
2001    provider_registry: Arc<GitHostingProviderRegistry>,
2002    path: PathBuf,
2003    selection: Range<u32>,
2004) -> Result<url::Url> {
2005    #[derive(Deserialize)]
2006    struct CargoVcsGit {
2007        sha1: String,
2008    }
2009
2010    #[derive(Deserialize)]
2011    struct CargoVcsInfo {
2012        git: CargoVcsGit,
2013        path_in_vcs: String,
2014    }
2015
2016    #[derive(Deserialize)]
2017    struct CargoPackage {
2018        repository: String,
2019    }
2020
2021    #[derive(Deserialize)]
2022    struct CargoToml {
2023        package: CargoPackage,
2024    }
2025
2026    let Some((dir, cargo_vcs_info_json)) = path.ancestors().skip(1).find_map(|dir| {
2027        let json = std::fs::read_to_string(dir.join(".cargo_vcs_info.json")).ok()?;
2028        Some((dir, json))
2029    }) else {
2030        bail!("No .cargo_vcs_info.json found in parent directories")
2031    };
2032    let cargo_vcs_info = serde_json::from_str::<CargoVcsInfo>(&cargo_vcs_info_json)?;
2033    let cargo_toml = std::fs::read_to_string(dir.join("Cargo.toml"))?;
2034    let manifest = toml::from_str::<CargoToml>(&cargo_toml)?;
2035    let (provider, remote) = parse_git_remote_url(provider_registry, &manifest.package.repository)
2036        .ok_or_else(|| anyhow!("Failed to parse package.repository field of manifest"))?;
2037    let path = PathBuf::from(cargo_vcs_info.path_in_vcs).join(path.strip_prefix(dir).unwrap());
2038    let permalink = provider.build_permalink(
2039        remote,
2040        BuildPermalinkParams {
2041            sha: &cargo_vcs_info.git.sha1,
2042            path: &path.to_string_lossy(),
2043            selection: Some(selection),
2044        },
2045    );
2046    Ok(permalink)
2047}