paths: Replace lazy_static! with OnceLock (#13213)

This PR replaces the `lazy_static!` usages in the `paths` crate with
`OnceLock` from the standard library.

This allows us to drop the `lazy_static` dependency from this crate.

The paths are now exposed as accessor functions that reference a private
static value.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-06-18 12:22:37 -04:00 committed by GitHub
parent ba59e66314
commit 81475ac4cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 322 additions and 172 deletions

View file

@ -13,5 +13,4 @@ path = "src/paths.rs"
[dependencies]
dirs.workspace = true
lazy_static.workspace = true
util.workspace = true

View file

@ -1,92 +1,243 @@
//! Paths to locations used by Zed.
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use util::paths::HOME;
lazy_static::lazy_static! {
pub static ref CONFIG_DIR: PathBuf = if cfg!(target_os = "windows") {
dirs::config_dir()
.expect("failed to determine RoamingAppData directory")
.join("Zed")
} else if cfg!(target_os = "linux") {
if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
flatpak_xdg_config.into()
} else {
dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
}.join("zed")
} else {
/// Returns the path to the configuration directory used by Zed.
pub fn config_dir() -> &'static PathBuf {
static CONFIG_DIR: OnceLock<PathBuf> = OnceLock::new();
CONFIG_DIR.get_or_init(|| {
if cfg!(target_os = "windows") {
return dirs::config_dir()
.expect("failed to determine RoamingAppData directory")
.join("Zed");
}
if cfg!(target_os = "linux") {
return if let Ok(flatpak_xdg_config) = std::env::var("FLATPAK_XDG_CONFIG_HOME") {
flatpak_xdg_config.into()
} else {
dirs::config_dir().expect("failed to determine XDG_CONFIG_HOME directory")
}
.join("zed");
}
HOME.join(".config").join("zed")
};
pub static ref CONTEXTS_DIR: PathBuf = if cfg!(target_os = "macos") {
CONFIG_DIR.join("conversations")
} else {
SUPPORT_DIR.join("conversations")
};
pub static ref PROMPTS_DIR: PathBuf = if cfg!(target_os = "macos") {
CONFIG_DIR.join("prompts")
} else {
SUPPORT_DIR.join("prompts")
};
pub static ref EMBEDDINGS_DIR: PathBuf = if cfg!(target_os = "macos") {
CONFIG_DIR.join("embeddings")
} else {
SUPPORT_DIR.join("embeddings")
};
pub static ref THEMES_DIR: PathBuf = CONFIG_DIR.join("themes");
pub static ref SUPPORT_DIR: PathBuf = if cfg!(target_os = "macos") {
HOME.join("Library/Application Support/Zed")
} else if cfg!(target_os = "linux") {
if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
flatpak_xdg_data.into()
} else {
dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
}.join("zed")
} else if cfg!(target_os = "windows") {
dirs::data_local_dir()
.expect("failed to determine LocalAppData directory")
.join("Zed")
} else {
CONFIG_DIR.clone()
};
pub static ref LOGS_DIR: PathBuf = if cfg!(target_os = "macos") {
HOME.join("Library/Logs/Zed")
} else {
SUPPORT_DIR.join("logs")
};
pub static ref EXTENSIONS_DIR: PathBuf = SUPPORT_DIR.join("extensions");
pub static ref LANGUAGES_DIR: PathBuf = SUPPORT_DIR.join("languages");
pub static ref COPILOT_DIR: PathBuf = SUPPORT_DIR.join("copilot");
pub static ref SUPERMAVEN_DIR: PathBuf = SUPPORT_DIR.join("supermaven");
pub static ref DEFAULT_PRETTIER_DIR: PathBuf = SUPPORT_DIR.join("prettier");
pub static ref DB_DIR: PathBuf = SUPPORT_DIR.join("db");
pub static ref CRASHES_DIR: Option<PathBuf> = cfg!(target_os = "macos")
.then_some(HOME.join("Library/Logs/DiagnosticReports"));
pub static ref CRASHES_RETIRED_DIR: Option<PathBuf> = CRASHES_DIR
.as_ref()
.map(|dir| dir.join("Retired"));
pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
pub static ref TASKS: PathBuf = CONFIG_DIR.join("tasks.json");
pub static ref LAST_USERNAME: PathBuf = CONFIG_DIR.join("last-username.txt");
pub static ref LOG: PathBuf = LOGS_DIR.join("Zed.log");
pub static ref OLD_LOG: PathBuf = LOGS_DIR.join("Zed.log.old");
pub static ref LOCAL_SETTINGS_RELATIVE_PATH: &'static Path = Path::new(".zed/settings.json");
pub static ref LOCAL_TASKS_RELATIVE_PATH: &'static Path = Path::new(".zed/tasks.json");
pub static ref LOCAL_VSCODE_TASKS_RELATIVE_PATH: &'static Path = Path::new(".vscode/tasks.json");
pub static ref TEMP_DIR: PathBuf = if cfg!(target_os = "windows") {
dirs::cache_dir()
.expect("failed to determine LocalAppData directory")
.join("Zed")
} else if cfg!(target_os = "linux") {
if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
flatpak_xdg_cache.into()
} else {
dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
}.join("zed")
} else {
HOME.join(".cache").join("zed")
};
})
}
/// Returns the path to the support directory used by Zed.
pub fn support_dir() -> &'static PathBuf {
static SUPPORT_DIR: OnceLock<PathBuf> = OnceLock::new();
SUPPORT_DIR.get_or_init(|| {
if cfg!(target_os = "macos") {
return HOME.join("Library/Application Support/Zed");
}
if cfg!(target_os = "linux") {
return if let Ok(flatpak_xdg_data) = std::env::var("FLATPAK_XDG_DATA_HOME") {
flatpak_xdg_data.into()
} else {
dirs::data_local_dir().expect("failed to determine XDG_DATA_HOME directory")
}
.join("zed");
}
if cfg!(target_os = "windows") {
return dirs::data_local_dir()
.expect("failed to determine LocalAppData directory")
.join("Zed");
}
config_dir().clone()
})
}
/// Returns the path to the temp directory used by Zed.
pub fn temp_dir() -> &'static PathBuf {
static TEMP_DIR: OnceLock<PathBuf> = OnceLock::new();
TEMP_DIR.get_or_init(|| {
if cfg!(target_os = "windows") {
return dirs::cache_dir()
.expect("failed to determine LocalAppData directory")
.join("Zed");
}
if cfg!(target_os = "linux") {
return if let Ok(flatpak_xdg_cache) = std::env::var("FLATPAK_XDG_CACHE_HOME") {
flatpak_xdg_cache.into()
} else {
dirs::cache_dir().expect("failed to determine XDG_CACHE_HOME directory")
}
.join("zed");
}
HOME.join(".cache").join("zed")
})
}
/// Returns the path to the logs directory.
pub fn logs_dir() -> &'static PathBuf {
static LOGS_DIR: OnceLock<PathBuf> = OnceLock::new();
LOGS_DIR.get_or_init(|| {
if cfg!(target_os = "macos") {
HOME.join("Library/Logs/Zed")
} else {
support_dir().join("logs")
}
})
}
/// Returns the path to the `Zed.log` file.
pub fn log_file() -> &'static PathBuf {
static LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
LOG_FILE.get_or_init(|| logs_dir().join("Zed.log"))
}
/// Returns the path to the `Zed.log.old` file.
pub fn old_log_file() -> &'static PathBuf {
static OLD_LOG_FILE: OnceLock<PathBuf> = OnceLock::new();
OLD_LOG_FILE.get_or_init(|| logs_dir().join("Zed.log.old"))
}
/// Returns the path to the database directory.
pub fn database_dir() -> &'static PathBuf {
static DATABASE_DIR: OnceLock<PathBuf> = OnceLock::new();
DATABASE_DIR.get_or_init(|| support_dir().join("db"))
}
/// Returns the path to the crashes directory, if it exists for the current platform.
pub fn crashes_dir() -> &'static Option<PathBuf> {
static CRASHES_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
CRASHES_DIR.get_or_init(|| {
cfg!(target_os = "macos").then_some(HOME.join("Library/Logs/DiagnosticReports"))
})
}
/// Returns the path to the retired crashes directory, if it exists for the current platform.
pub fn crashes_retired_dir() -> &'static Option<PathBuf> {
static CRASHES_RETIRED_DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
CRASHES_RETIRED_DIR.get_or_init(|| crashes_dir().as_ref().map(|dir| dir.join("Retired")))
}
/// Returns the path to the `settings.json` file.
pub fn settings_file() -> &'static PathBuf {
static SETTINGS_FILE: OnceLock<PathBuf> = OnceLock::new();
SETTINGS_FILE.get_or_init(|| config_dir().join("settings.json"))
}
/// Returns the path to the `keymap.json` file.
pub fn keymap_file() -> &'static PathBuf {
static KEYMAP_FILE: OnceLock<PathBuf> = OnceLock::new();
KEYMAP_FILE.get_or_init(|| config_dir().join("keymap.json"))
}
/// Returns the path to the `tasks.json` file.
pub fn tasks_file() -> &'static PathBuf {
static TASKS_FILE: OnceLock<PathBuf> = OnceLock::new();
TASKS_FILE.get_or_init(|| config_dir().join("tasks.json"))
}
/// Returns the path to the extensions directory.
///
/// This is where installed extensions are stored.
pub fn extensions_dir() -> &'static PathBuf {
static EXTENSIONS_DIR: OnceLock<PathBuf> = OnceLock::new();
EXTENSIONS_DIR.get_or_init(|| support_dir().join("extensions"))
}
/// Returns the path to the themes directory.
///
/// This is where themes that are not provided by extensions are stored.
pub fn themes_dir() -> &'static PathBuf {
static THEMES_DIR: OnceLock<PathBuf> = OnceLock::new();
THEMES_DIR.get_or_init(|| config_dir().join("themes"))
}
/// Returns the path to the contexts directory.
///
/// This is where the saved contexts from the Assistant are stored.
pub fn contexts_dir() -> &'static PathBuf {
static CONTEXTS_DIR: OnceLock<PathBuf> = OnceLock::new();
CONTEXTS_DIR.get_or_init(|| {
if cfg!(target_os = "macos") {
config_dir().join("conversations")
} else {
support_dir().join("conversations")
}
})
}
/// Returns the path to the contexts directory.
///
/// This is where the prompts for use with the Assistant are stored.
pub fn prompts_dir() -> &'static PathBuf {
static PROMPTS_DIR: OnceLock<PathBuf> = OnceLock::new();
PROMPTS_DIR.get_or_init(|| {
if cfg!(target_os = "macos") {
config_dir().join("prompts")
} else {
support_dir().join("prompts")
}
})
}
/// Returns the path to the semantic search's embeddings directory.
///
/// This is where the embeddings used to power semantic search are stored.
pub fn embeddings_dir() -> &'static PathBuf {
static EMBEDDINGS_DIR: OnceLock<PathBuf> = OnceLock::new();
EMBEDDINGS_DIR.get_or_init(|| {
if cfg!(target_os = "macos") {
config_dir().join("embeddings")
} else {
support_dir().join("embeddings")
}
})
}
/// Returns the path to the languages directory.
///
/// This is where language servers are downloaded to for languages built-in to Zed.
pub fn languages_dir() -> &'static PathBuf {
static LANGUAGES_DIR: OnceLock<PathBuf> = OnceLock::new();
LANGUAGES_DIR.get_or_init(|| support_dir().join("languages"))
}
/// Returns the path to the Copilot directory.
pub fn copilot_dir() -> &'static PathBuf {
static COPILOT_DIR: OnceLock<PathBuf> = OnceLock::new();
COPILOT_DIR.get_or_init(|| support_dir().join("copilot"))
}
/// Returns the path to the Supermaven directory.
pub fn supermaven_dir() -> &'static PathBuf {
static SUPERMAVEN_DIR: OnceLock<PathBuf> = OnceLock::new();
SUPERMAVEN_DIR.get_or_init(|| support_dir().join("supermaven"))
}
/// Returns the path to the default Prettier directory.
pub fn default_prettier_dir() -> &'static PathBuf {
static DEFAULT_PRETTIER_DIR: OnceLock<PathBuf> = OnceLock::new();
DEFAULT_PRETTIER_DIR.get_or_init(|| support_dir().join("prettier"))
}
/// Returns the relative path to a `settings.json` file within a project.
pub fn local_settings_file_relative_path() -> &'static Path {
static LOCAL_SETTINGS_FILE_RELATIVE_PATH: OnceLock<&Path> = OnceLock::new();
LOCAL_SETTINGS_FILE_RELATIVE_PATH.get_or_init(|| Path::new(".zed/settings.json"))
}
/// Returns the relative path to a `tasks.json` file within a project.
pub fn local_tasks_file_relative_path() -> &'static Path {
static LOCAL_TASKS_FILE_RELATIVE_PATH: OnceLock<&Path> = OnceLock::new();
LOCAL_TASKS_FILE_RELATIVE_PATH.get_or_init(|| Path::new(".zed/tasks.json"))
}
/// Returns the relative path to a `.vscode/tasks.json` file within a project.
pub fn local_vscode_tasks_file_relative_path() -> &'static Path {
static LOCAL_VSCODE_TASKS_FILE_RELATIVE_PATH: OnceLock<&Path> = OnceLock::new();
LOCAL_VSCODE_TASKS_FILE_RELATIVE_PATH.get_or_init(|| Path::new(".vscode/tasks.json"))
}