diff options
Diffstat (limited to 'helix-core/src')
-rw-r--r-- | helix-core/src/lib.rs | 1 | ||||
-rw-r--r-- | helix-core/src/path.rs | 181 |
2 files changed, 0 insertions, 182 deletions
diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 0acdb238..94802eba 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -17,7 +17,6 @@ pub mod macros; pub mod match_brackets; pub mod movement; pub mod object; -pub mod path; mod position; pub mod search; pub mod selection; diff --git a/helix-core/src/path.rs b/helix-core/src/path.rs deleted file mode 100644 index 0cf6f812..00000000 --- a/helix-core/src/path.rs +++ /dev/null @@ -1,181 +0,0 @@ -use etcetera::home_dir; -use std::path::{Component, Path, PathBuf}; - -/// Replaces users home directory from `path` with tilde `~` if the directory -/// is available, otherwise returns the path unchanged. -pub fn fold_home_dir(path: &Path) -> PathBuf { - if let Ok(home) = home_dir() { - if let Ok(stripped) = path.strip_prefix(&home) { - return PathBuf::from("~").join(stripped); - } - } - - path.to_path_buf() -} - -/// Expands tilde `~` into users home directory if available, otherwise returns the path -/// unchanged. The tilde will only be expanded when present as the first component of the path -/// and only slash follows it. -pub fn expand_tilde(path: &Path) -> PathBuf { - let mut components = path.components().peekable(); - if let Some(Component::Normal(c)) = components.peek() { - if c == &"~" { - if let Ok(home) = home_dir() { - // it's ok to unwrap, the path starts with `~` - return home.join(path.strip_prefix("~").unwrap()); - } - } - } - - path.to_path_buf() -} - -/// Normalize a path without resolving symlinks. -// Strategy: start from the first component and move up. Cannonicalize previous path, -// join component, cannonicalize new path, strip prefix and join to the final result. -pub fn get_normalized_path(path: &Path) -> PathBuf { - let mut components = path.components().peekable(); - let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { - components.next(); - PathBuf::from(c.as_os_str()) - } else { - PathBuf::new() - }; - - for component in components { - match component { - Component::Prefix(..) => unreachable!(), - Component::RootDir => { - ret.push(component.as_os_str()); - } - Component::CurDir => {} - #[cfg(not(windows))] - Component::ParentDir => { - ret.pop(); - } - #[cfg(windows)] - Component::ParentDir => { - if let Some(head) = ret.components().next_back() { - match head { - Component::Prefix(_) | Component::RootDir => {} - Component::CurDir => unreachable!(), - // If we left previous component as ".." it means we met a symlink before and we can't pop path. - Component::ParentDir => { - ret.push(".."); - } - Component::Normal(_) => { - if ret.is_symlink() { - ret.push(".."); - } else { - ret.pop(); - } - } - } - } - } - #[cfg(not(windows))] - Component::Normal(c) => { - ret.push(c); - } - #[cfg(windows)] - Component::Normal(c) => 'normal: { - use std::fs::canonicalize; - - let new_path = ret.join(c); - if new_path.is_symlink() { - ret = new_path; - break 'normal; - } - let (can_new, can_old) = (canonicalize(&new_path), canonicalize(&ret)); - match (can_new, can_old) { - (Ok(can_new), Ok(can_old)) => { - let striped = can_new.strip_prefix(can_old); - ret.push(striped.unwrap_or_else(|_| c.as_ref())); - } - _ => ret.push(c), - } - } - } - } - dunce::simplified(&ret).to_path_buf() -} - -/// Returns the canonical, absolute form of a path with all intermediate components normalized. -/// -/// This function is used instead of [`std::fs::canonicalize`] because we don't want to verify -/// here if the path exists, just normalize it's components. -pub fn get_canonicalized_path(path: &Path) -> PathBuf { - let path = expand_tilde(path); - let path = if path.is_relative() { - helix_loader::current_working_dir().join(path) - } else { - path - }; - - get_normalized_path(path.as_path()) -} - -pub fn get_relative_path(path: &Path) -> PathBuf { - let path = PathBuf::from(path); - let path = if path.is_absolute() { - let cwdir = get_normalized_path(&helix_loader::current_working_dir()); - get_normalized_path(&path) - .strip_prefix(cwdir) - .map(PathBuf::from) - .unwrap_or(path) - } else { - path - }; - fold_home_dir(&path) -} - -/// Returns a truncated filepath where the basepart of the path is reduced to the first -/// char of the folder and the whole filename appended. -/// -/// Also strip the current working directory from the beginning of the path. -/// Note that this function does not check if the truncated path is unambiguous. -/// -/// ``` -/// use helix_core::path::get_truncated_path; -/// use std::path::Path; -/// -/// assert_eq!( -/// get_truncated_path("/home/cnorris/documents/jokes.txt").as_path(), -/// Path::new("/h/c/d/jokes.txt") -/// ); -/// assert_eq!( -/// get_truncated_path("jokes.txt").as_path(), -/// Path::new("jokes.txt") -/// ); -/// assert_eq!( -/// get_truncated_path("/jokes.txt").as_path(), -/// Path::new("/jokes.txt") -/// ); -/// assert_eq!( -/// get_truncated_path("/h/c/d/jokes.txt").as_path(), -/// Path::new("/h/c/d/jokes.txt") -/// ); -/// assert_eq!(get_truncated_path("").as_path(), Path::new("")); -/// ``` -/// -pub fn get_truncated_path<P: AsRef<Path>>(path: P) -> PathBuf { - let cwd = helix_loader::current_working_dir(); - let path = path - .as_ref() - .strip_prefix(cwd) - .unwrap_or_else(|_| path.as_ref()); - let file = path.file_name().unwrap_or_default(); - let base = path.parent().unwrap_or_else(|| Path::new("")); - let mut ret = PathBuf::new(); - for d in base { - ret.push( - d.to_string_lossy() - .chars() - .next() - .unwrap_or_default() - .to_string(), - ); - } - ret.push(file); - ret -} |