@@ -127,6 +127,36 @@ where
127127 }
128128}
129129
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() {
136+ if c == &"~" {
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());
140+ }
141+ }
142+ }
143+
144+ path.to_path_buf()
145+}
146+
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());
154+ }
155+ }
156+
157+ path.to_path_buf()
158+}
159+
130160/// Normalize a path, removing things like `.` and `..`.
131161///
132162/// CAUTION: This does not resolve symlinks (unlike
@@ -137,6 +167,7 @@ where
137167/// needs to improve on.
138168/// Copied from cargo: https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81
139169pub fn normalize_path(path: &Path) -> PathBuf {
170+ let path = expand_tilde(path);
140171 let mut components = path.components().peekable();
141172 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
142173 components.next();
@@ -163,12 +194,17 @@ pub fn normalize_path(path: &Path) -> PathBuf {
163194 ret
164195}
165196
166−// Returns the canonical, absolute form of a path with all intermediate components normalized.
167−//
168−// 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.
197+/// Returns the canonical, absolute form of a path with all intermediate components normalized.
198+///
199+/// This function is used instead of `std::fs::canonicalize` because we don't want to verify
200+/// here if the path exists, just normalize it's components.
170201pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
171− std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path)))
202+ let normalized = normalize_path(path);
203+ if normalized.is_absolute() {
204+ Ok(normalized)
205+ } else {
206+ std::env::current_dir().map(|current_dir| current_dir.join(normalized))
207+ }
172208}
173209
174210use helix_lsp::lsp;
@@ -709,12 +745,19 @@ impl Document {
709745 &self.selections[&view_id]
710746 }
711747
712− pub fn relative_path(&self) -> Option<&Path> {
748+ pub fn relative_path(&self) -> Option<PathBuf> {
713749 let cwdir = std::env::current_dir().expect("couldn't determine current directory");
714750
715− self.path
716− .as_ref()
717− .map(|path| path.strip_prefix(cwdir).unwrap_or(path))
751+ self.path.as_ref().map(|path| {
752+ let path = fold_home_dir(path);
753+ if path.is_relative() {
754+ path
755+ } else {
756+ path.strip_prefix(cwdir)
757+ .map(|p| p.to_path_buf())
758+ .unwrap_or(path)
759+ }
760+ })
718761 }
719762
720763 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {