@@ -127,6 +127,36 @@ where
130+/// Expands tilde `~` into users home directory if avilable, otherwise returns the path
131+/// unchanged. The tilde will only be expanded when present as the first component of the path
132+/// and only slash follows it.
133+pub fn expand_tilde(path: &Path) -> PathBuf {
134+ let mut components = path.components().peekable();
135+ if let Some(Component::Normal(c)) = components.peek() {
137+ if let Ok(home) = helix_core::home_dir() {
138+ // it's ok to unwrap, the path starts with `~`
139+ return home.join(path.strip_prefix("~").unwrap());
147+/// Replaces users home directory from `path` with tilde `~` if the directory
148+/// is available, otherwise returns the path unchanged.
149+pub fn fold_home_dir(path: &Path) -> PathBuf {
150+ if let Ok(home) = helix_core::home_dir() {
151+ if path.starts_with(&home) {
152+ // it's ok to unwrap, the path starts with home dir
153+ return PathBuf::from("~").join(path.strip_prefix(&home).unwrap());
130/// Normalize a path, removing things like `.` and `..`.
160/// Normalize a path, removing things like `.` and `..`.
132/// CAUTION: This does not resolve symlinks (unlike
162/// CAUTION: This does not resolve symlinks (unlike
@@ -137,6 +167,7 @@ where
137/// needs to improve on.
167/// needs to improve on.
138/// Copied from cargo: https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81
168/// Copied from cargo: https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81
139pub fn normalize_path(path: &Path) -> PathBuf {
169pub fn normalize_path(path: &Path) -> PathBuf {
170+ let path = expand_tilde(path);
140 let mut components = path.components().peekable();
171 let mut components = path.components().peekable();
141 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
172 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
142 components.next();
173 components.next();
@@ -163,12 +194,17 @@ pub fn normalize_path(path: &Path) -> PathBuf {
166−// Returns the canonical, absolute form of a path with all intermediate components normalized.
197+/// Returns the canonical, absolute form of a path with all intermediate components normalized.
168−// This function is used instead of `std::fs::canonicalize` because we don't want to verify
199+/// This function is used instead of `std::fs::canonicalize` because we don't want to verify
169−// here if the path exists, just normalize it's components.
200+/// here if the path exists, just normalize it's components.
170pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
201pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
202+ let normalized = normalize_path(path);
203+ if normalized.is_absolute() {
171− std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path)))
206+ std::env::current_dir().map(|current_dir| current_dir.join(normalized))
174use helix_lsp::lsp;
210use helix_lsp::lsp;
@@ -709,12 +745,19 @@ impl Document {
709 &self.selections[&view_id]
745 &self.selections[&view_id]
712− pub fn relative_path(&self) -> Option<&Path> {
748+ pub fn relative_path(&self) -> Option<PathBuf> {
713 let cwdir = std::env::current_dir().expect("couldn't determine current directory");
749 let cwdir = std::env::current_dir().expect("couldn't determine current directory");
715− self.path
751+ self.path.as_ref().map(|path| {
716− .as_ref()
752+ let path = fold_home_dir(path);
753+ if path.is_relative() {
717− .map(|path| path.strip_prefix(cwdir).unwrap_or(path))
756+ path.strip_prefix(cwdir)
757+ .map(|p| p.to_path_buf())
720 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {
763 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {