-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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).
- Loading branch information
1 parent
af8e524
commit 1f916e6
Showing
27 changed files
with
163 additions
and
111 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ members = [ | |
"helix-loader", | ||
"helix-vcs", | ||
"helix-parsec", | ||
"helix-stdx", | ||
"xtask", | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "helix-stdx" | ||
description = "Standard library extensions" | ||
include = ["src/**/*", "README.md"] | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
rust-version.workspace = true | ||
categories.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
|
||
[dependencies] | ||
dunce = "1.0" | ||
etcetera = "0.8" | ||
|
||
[dev-dependencies] | ||
tempfile = "3.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::{ | ||
path::{Path, PathBuf}, | ||
sync::RwLock, | ||
}; | ||
|
||
static CWD: RwLock<Option<PathBuf>> = RwLock::new(None); | ||
|
||
// 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() | ||
.map(crate::path::normalize) | ||
.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 = crate::path::canonicalize(path); | ||
std::env::set_current_dir(&path)?; | ||
let mut cwd = CWD.write().unwrap(); | ||
*cwd = Some(path); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{current_working_dir, set_current_working_dir}; | ||
|
||
#[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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod env; | ||
pub mod path; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.