@@ -4,7 +4,7 @@ use std::cell::Cell;
4use std::collections::HashMap;
4use std::collections::HashMap;
5use std::fmt::Display;
5use std::fmt::Display;
6use std::future::Future;
6use std::future::Future;
7−use std::path::{Component, Path, PathBuf};
7+use std::path::{Path, PathBuf};
8use std::str::FromStr;
8use std::str::FromStr;
9use std::sync::Arc;
9use std::sync::Arc;
@@ -317,95 +317,6 @@ where
320−/// Expands tilde `~` into users home directory if avilable, otherwise returns the path
321−/// unchanged. The tilde will only be expanded when present as the first component of the path
322−/// and only slash follows it.
323−pub fn expand_tilde(path: &Path) -> PathBuf {
324− let mut components = path.components().peekable();
325− if let Some(Component::Normal(c)) = components.peek() {
327− if let Ok(home) = helix_core::home_dir() {
328− // it's ok to unwrap, the path starts with `~`
329− return home.join(path.strip_prefix("~").unwrap());
337−/// Replaces users home directory from `path` with tilde `~` if the directory
338−/// is available, otherwise returns the path unchanged.
339−pub fn fold_home_dir(path: &Path) -> PathBuf {
340− if let Ok(home) = helix_core::home_dir() {
341− if path.starts_with(&home) {
342− // it's ok to unwrap, the path starts with home dir
343− return PathBuf::from("~").join(path.strip_prefix(&home).unwrap());
350−/// Normalize a path, removing things like `.` and `..`.
352−/// CAUTION: This does not resolve symlinks (unlike
353−/// [`std::fs::canonicalize`]). This may cause incorrect or surprising
354−/// behavior at times. This should be used carefully. Unfortunately,
355−/// [`std::fs::canonicalize`] can be hard to use correctly, since it can often
356−/// fail, or on Windows returns annoying device paths. This is a problem Cargo
357−/// needs to improve on.
358−/// Copied from cargo: <https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81>
359−pub fn normalize_path(path: &Path) -> PathBuf {
360− let path = expand_tilde(path);
361− let mut components = path.components().peekable();
362− let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
364− PathBuf::from(c.as_os_str())
369− for component in components {
371− Component::Prefix(..) => unreachable!(),
372− Component::RootDir => {
373− ret.push(component.as_os_str());
375− Component::CurDir => {}
376− Component::ParentDir => {
379− Component::Normal(c) => {
387−/// Returns the canonical, absolute form of a path with all intermediate components normalized.
389−/// This function is used instead of `std::fs::canonicalize` because we don't want to verify
390−/// here if the path exists, just normalize it's components.
391−pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
392− let path = if path.is_relative() {
393− std::env::current_dir().map(|current_dir| current_dir.join(path))?
398− Ok(normalize_path(&path))
401−pub fn relative_path(mut path: &Path) -> PathBuf {
402− let cwdir = std::env::current_dir().expect("couldn't determine current directory");
403− if path.is_absolute() {
404− path = path.strip_prefix(cwdir).unwrap_or(path)
409use helix_lsp::lsp;
320use helix_lsp::lsp;
410use url::Url;
321use url::Url;
@@ -639,7 +550,7 @@ impl Document {
641 pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
552 pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
642− let path = canonicalize_path(path)?;
553+ let path = helix_core::path::get_canonicalized_path(path)?;
644 // if parent doesn't exist we still want to open the document
555 // if parent doesn't exist we still want to open the document
645 // and error out when document is saved
556 // and error out when document is saved
@@ -944,7 +855,9 @@ impl Document {
946 pub fn relative_path(&self) -> Option<PathBuf> {
857 pub fn relative_path(&self) -> Option<PathBuf> {
947− self.path.as_deref().map(relative_path)
858+ self.path
860+ .map(helix_core::path::get_relative_path)
950 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {
863 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {