aboutsummaryrefslogtreecommitdiff
path: root/helix-loader
diff options
context:
space:
mode:
authorMichael Davis2024-01-16 18:59:48 +0000
committerBlaž Hrastnik2024-01-18 01:57:53 +0000
commit1f916e65cff4459698d465b2f4558da1e1bf6e44 (patch)
tree5b6768e3069085bc5ba995efa95fdc73241ed0f6 /helix-loader
parentaf8e524a7d06253fa854bf8954f64312e11d0ea0 (diff)
Create helix-stdx crate for stdlib extensions
helix-stdx is meant to carry extensions to the stdlib or low-level dependencies that are useful in all other crates. This commit starts with all of the path functions from helix-core and the CWD tracking that lived in helix-loader. The CWD tracking in helix-loader was previously unable to call the canonicalization functions in helix-core. Switching to our custom canonicalization code should make no noticeable difference though since `std::env::current_dir` returns a canonicalized path with symlinks resolved (at least on unix).
Diffstat (limited to 'helix-loader')
-rw-r--r--helix-loader/src/lib.rs44
1 files changed, 3 insertions, 41 deletions
diff --git a/helix-loader/src/lib.rs b/helix-loader/src/lib.rs
index 5337d602..991504fb 100644
--- a/helix-loader/src/lib.rs
+++ b/helix-loader/src/lib.rs
@@ -1,14 +1,13 @@
pub mod config;
pub mod grammar;
+use helix_stdx::{env::current_working_dir, path};
+
use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
use std::path::{Path, PathBuf};
-use std::sync::RwLock;
pub const VERSION_AND_GIT_HASH: &str = env!("VERSION_AND_GIT_HASH");
-static CWD: RwLock<Option<PathBuf>> = RwLock::new(None);
-
static RUNTIME_DIRS: once_cell::sync::Lazy<Vec<PathBuf>> =
once_cell::sync::Lazy::new(prioritize_runtime_dirs);
@@ -16,31 +15,6 @@ static CONFIG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCe
static LOG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
-// Get the current working directory.
-// This information is managed internally as the call to std::env::current_dir
-// might fail if the cwd has been deleted.
-pub fn current_working_dir() -> PathBuf {
- if let Some(path) = &*CWD.read().unwrap() {
- return path.clone();
- }
-
- let path = std::env::current_dir()
- .and_then(dunce::canonicalize)
- .expect("Couldn't determine current working directory");
- let mut cwd = CWD.write().unwrap();
- *cwd = Some(path.clone());
-
- path
-}
-
-pub fn set_current_working_dir(path: impl AsRef<Path>) -> std::io::Result<()> {
- let path = dunce::canonicalize(path)?;
- std::env::set_current_dir(&path)?;
- let mut cwd = CWD.write().unwrap();
- *cwd = Some(path);
- Ok(())
-}
-
pub fn initialize_config_file(specified_file: Option<PathBuf>) {
let config_file = specified_file.unwrap_or_else(default_config_file);
ensure_parent_dir(&config_file);
@@ -280,22 +254,10 @@ fn ensure_parent_dir(path: &Path) {
mod merge_toml_tests {
use std::str;
- use super::{current_working_dir, merge_toml_values, set_current_working_dir};
+ use super::merge_toml_values;
use toml::Value;
#[test]
- fn current_dir_is_set() {
- let new_path = dunce::canonicalize(std::env::temp_dir()).unwrap();
- let cwd = current_working_dir();
- assert_ne!(cwd, new_path);
-
- set_current_working_dir(&new_path).expect("Couldn't set new path");
-
- let cwd = current_working_dir();
- assert_eq!(cwd, new_path);
- }
-
- #[test]
fn language_toml_map_merges() {
const USER: &str = r#"
[[language]]