on master help sign in
Fix expansion of `~` (#284)
41b0748Wojciech Kępka
helix-core/src/lib.rs+2
@@ -89,6 +89,8 @@ pub fn cache_dir() -> std::path::PathBuf {
89 path
89 path
90}
90}
91
91
92+pub use etcetera::home_dir;
93+
92use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
94use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
93
95
94pub use ropey::{Rope, RopeSlice};
96pub use ropey::{Rope, RopeSlice};
helix-term/src/commands.rs+3−3
@@ -1188,8 +1188,8 @@ mod cmd {
1188 .filter(|doc| doc.is_modified())
1188 .filter(|doc| doc.is_modified())
1189 .map(|doc| {
1189 .map(|doc| {
1190 doc.relative_path()
1190 doc.relative_path()
1191− .and_then(|path| path.to_str())
1191+ .map(|path| path.to_string_lossy().to_string())
1192− .unwrap_or("[scratch]")
1192+ .unwrap_or_else(|| "[scratch]".into())
1193 })
1193 })
1194 .collect();
1194 .collect();
1195 if !modified.is_empty() {
1195 if !modified.is_empty() {
@@ -1487,7 +1487,7 @@ fn buffer_picker(cx: &mut Context) {
1487 cx.editor
1487 cx.editor
1488 .documents
1488 .documents
1489 .iter()
1489 .iter()
1490− .map(|(id, doc)| (id, doc.relative_path().map(Path::to_path_buf)))
1490+ .map(|(id, doc)| (id, doc.relative_path()))
1491 .collect(),
1491 .collect(),
1492 move |(id, path): &(DocumentId, Option<PathBuf>)| {
1492 move |(id, path): &(DocumentId, Option<PathBuf>)| {
1493 // format_fn
1493 // format_fn
helix-term/src/ui/mod.rs+14−4
@@ -126,10 +126,11 @@ pub mod completers {
126 use ignore::WalkBuilder;
126 use ignore::WalkBuilder;
127 use std::path::{Path, PathBuf};
127 use std::path::{Path, PathBuf};
128
128
129+ let is_tilde = input.starts_with('~') && input.len() == 1;
129− let path = Path::new(input);
130+ let path = helix_view::document::expand_tilde(Path::new(input));
130
131
131 let (dir, file_name) = if input.ends_with('/') {
132 let (dir, file_name) = if input.ends_with('/') {
132− (path.into(), None)
133+ (path, None)
133 } else {
134 } else {
134 let file_name = path
135 let file_name = path
135 .file_name()
136 .file_name()
@@ -154,7 +155,16 @@ pub mod completers {
154 let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
155 let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
155
156
156 let path = entry.path();
157 let path = entry.path();
158+ let mut path = if is_tilde {
159+ // if it's a single tilde an absolute path is displayed so that when `TAB` is pressed on
160+ // one of the directories the tilde will be replaced with a valid path not with a relative
161+ // home directory name.
162+ // ~ -> <TAB> -> /home/user
163+ // ~/ -> <TAB> -> ~/first_entry
164+ path.to_path_buf()
165+ } else {
157− let mut path = path.strip_prefix(&dir).unwrap_or(path).to_path_buf();
166+ path.strip_prefix(&dir).unwrap_or(path).to_path_buf()
167+ };
158
168
159 if is_dir {
169 if is_dir {
160 path.push("");
170 path.push("");
@@ -184,7 +194,7 @@ pub mod completers {
184 })
194 })
185 .collect();
195 .collect();
186
196
187− let range = ((input.len() - file_name.len())..);
197+ let range = ((input.len().saturating_sub(file_name.len()))..);
188
198
189 matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
199 matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
190 files = matches
200 files = matches
helix-view/src/document.rs+52−9
@@ -127,6 +127,36 @@ where
127 }
127 }
128}
128}
129
129
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+
130/// Normalize a path, removing things like `.` and `..`.
160/// Normalize a path, removing things like `.` and `..`.
131///
161///
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 {
163 ret
194 ret
164}
195}
165
196
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.
167−//
198+///
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() {
204+ Ok(normalized)
205+ } else {
171− std::env::current_dir().map(|current_dir| normalize_path(&current_dir.join(path)))
206+ std::env::current_dir().map(|current_dir| current_dir.join(normalized))
207+ }
172}
208}
173
209
174use helix_lsp::lsp;
210use helix_lsp::lsp;
@@ -709,12 +745,19 @@ impl Document {
709 &self.selections[&view_id]
745 &self.selections[&view_id]
710 }
746 }
711
747
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");
714
750
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() {
754+ path
755+ } else {
717− .map(|path| path.strip_prefix(cwdir).unwrap_or(path))
756+ path.strip_prefix(cwdir)
757+ .map(|p| p.to_path_buf())
758+ .unwrap_or(path)
759+ }
760+ })
718 }
761 }
719
762
720 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {
763 // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {