@@ -4,7 +4,7 @@ use std::cell::Cell;
44use std::collections::HashMap;
55use std::fmt::Display;
66use std::future::Future;
7−use std::path::{Component, Path, PathBuf};
7+use std::path::{Path, PathBuf};
88use std::str::FromStr;
99use std::sync::Arc;
1010
@@ -317,95 +317,6 @@ where
317317 }
318318}
319319
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() {
326− if c == &"~" {
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());
330− }
331− }
332− }
333−
334− path.to_path_buf()
335−}
336−
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());
344− }
345− }
346−
347− path.to_path_buf()
348−}
349−
350−/// Normalize a path, removing things like `.` and `..`.
351−///
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() {
363− components.next();
364− PathBuf::from(c.as_os_str())
365− } else {
366− PathBuf::new()
367− };
368−
369− for component in components {
370− match component {
371− Component::Prefix(..) => unreachable!(),
372− Component::RootDir => {
373− ret.push(component.as_os_str());
374− }
375− Component::CurDir => {}
376− Component::ParentDir => {
377− ret.pop();
378− }
379− Component::Normal(c) => {
380− ret.push(c);
381− }
382− }
383− }
384− ret
385−}
386−
387−/// Returns the canonical, absolute form of a path with all intermediate components normalized.
388−///
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))?
394− } else {
395− path.to_path_buf()
396− };
397−
398− Ok(normalize_path(&path))
399−}
400−
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)
405− };
406− fold_home_dir(path)
407−}
408−
409320use helix_lsp::lsp;
410321use url::Url;
411322
@@ -639,7 +550,7 @@ impl Document {
639550 }
640551
641552 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)?;
643554
644555 // if parent doesn't exist we still want to open the document
645556 // and error out when document is saved
@@ -944,7 +855,9 @@ impl Document {
944855 }
945856
946857 pub fn relative_path(&self) -> Option<PathBuf> {
947− self.path.as_deref().map(relative_path)
858+ self.path
859+ .as_deref()
860+ .map(helix_core::path::get_relative_path)
948861 }
949862
950863 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {