From d36ebc8c7463b68501aad9f5568d2458a595aec3 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 23 Jul 2024 17:59:55 -0400 Subject: [PATCH] Add global `Fs` instance (#15044) This PR adds a global `Fs` instance to the app context. This will make it easier to access the filesystem in some cases instead of having to thread it around. Release Notes: - N/A --- crates/fs/Cargo.toml | 2 +- crates/fs/src/fs.rs | 17 +++++++++++++++++ crates/zed/src/main.rs | 4 +++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/fs/Cargo.toml b/crates/fs/Cargo.toml index 98ce465711..105b11b775 100644 --- a/crates/fs/Cargo.toml +++ b/crates/fs/Cargo.toml @@ -19,7 +19,7 @@ collections.workspace = true futures.workspace = true git.workspace = true git2.workspace = true -gpui = { workspace = true, optional = true } +gpui.workspace = true lazy_static.workspace = true libc.workspace = true parking_lot.workspace = true diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rs index 6ee5f05148..924fb9a215 100644 --- a/crates/fs/src/fs.rs +++ b/crates/fs/src/fs.rs @@ -12,6 +12,7 @@ use std::os::unix::fs::MetadataExt; use async_tar::Archive; use futures::{future::BoxFuture, AsyncRead, Stream, StreamExt}; use git::repository::{GitRepository, RealGitRepository}; +use gpui::{AppContext, Global, ReadGlobal}; use rope::Rope; use smol::io::AsyncWriteExt; use std::{ @@ -102,6 +103,22 @@ pub trait Fs: Send + Sync { } } +struct GlobalFs(Arc); + +impl Global for GlobalFs {} + +impl dyn Fs { + /// Returns the global [`Fs`]. + pub fn global(cx: &AppContext) -> Arc { + GlobalFs::global(cx).0.clone() + } + + /// Sets the global [`Fs`]. + pub fn set_global(fs: Arc, cx: &mut AppContext) { + cx.set_global(GlobalFs(fs)); + } +} + #[derive(Copy, Clone, Default)] pub struct CreateOptions { pub overwrite: bool, diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index c9efc2a8d6..0532180927 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -14,7 +14,7 @@ use collab_ui::channel_view::ChannelView; use db::kvp::KEY_VALUE_STORE; use editor::Editor; use env_logger::Builder; -use fs::RealFs; +use fs::{Fs, RealFs}; use futures::{future, StreamExt}; use git::GitHostingProviderRegistry; use gpui::{ @@ -399,6 +399,8 @@ fn main() { AppCommitSha::set_global(AppCommitSha(build_sha.into()), cx); } + ::set_global(fs.clone(), cx); + GitHostingProviderRegistry::set_global(git_hosting_provider_registry, cx); git_hosting_providers::init(cx);