Skip to content

Commit

Permalink
attempt ancestor canonicalization in get_normalized_path
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzfool committed Mar 14, 2023
1 parent ea1903b commit c3be3ff
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions helix-core/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ pub fn expand_tilde(path: &Path) -> PathBuf {
/// needs to improve on.
/// Copied from cargo: <https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81>
pub fn get_normalized_path(path: &Path) -> PathBuf {
// normalization strategy is to canonicalize first ancestor path that exists (i.e., canonicalize as much as possible),
// then run handrolled normalization on the non-existent remainder
let (base, path) = path
.ancestors()
.find(|path| path.exists())
.and_then(|path| Some((path, dunce::canonicalize(path).ok()?)))
.map(|(base, canonicalized)| {
(
canonicalized,
path.strip_prefix(base).unwrap(/* base is an ancestor of path */).into(),
)
})
.unwrap_or_else(|| (PathBuf::new(), PathBuf::from(path)));

if path.as_os_str().is_empty() {
return base;
}

let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
Expand All @@ -63,7 +81,7 @@ pub fn get_normalized_path(path: &Path) -> PathBuf {
}
}
}
ret
base.join(ret)
}

/// Returns the canonical, absolute form of a path with all intermediate components normalized.
Expand All @@ -82,19 +100,13 @@ pub fn get_canonicalized_path(path: &Path) -> std::io::Result<PathBuf> {
}

pub fn get_relative_path(path: &Path) -> PathBuf {
let path = PathBuf::from(path);
let path = if path.is_absolute() {
let cwdir = std::env::current_dir()
.and_then(dunce::canonicalize)
.expect("couldn't determine current directory");
dunce::canonicalize(&path)
.ok()
.and_then(|path| path.strip_prefix(cwdir).map(PathBuf::from).ok())
.unwrap_or(path)
let cwdir = std::env::current_dir().expect("couldn't determine current directory");
path.strip_prefix(cwdir).unwrap_or(path)
} else {
path
};
fold_home_dir(&path)
fold_home_dir(path)
}

/// Returns a truncated filepath where the basepart of the path is reduced to the first
Expand Down

0 comments on commit c3be3ff

Please sign in to comment.