Fix expansion of `~` (#284)

* Fix expansion of `~`, dont use directory relative to cwd.

* Add `expand_tilde`

* Bring back `canonicalize_path`, use `expand_tilde` to `normalize`

* Make `:open ~` completion work

* Fix clippy

* Fold home dir into tilde in Document `realitve_path`
pull/296/head
Wojciech Kępka 3 years ago committed by GitHub
parent 42142cf680
commit 41b07486ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -89,6 +89,8 @@ pub fn cache_dir() -> std::path::PathBuf {
path path
} }
pub use etcetera::home_dir;
use etcetera::base_strategy::{choose_base_strategy, BaseStrategy}; use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
pub use ropey::{Rope, RopeSlice}; pub use ropey::{Rope, RopeSlice};

@ -1188,8 +1188,8 @@ mod cmd {
.filter(|doc| doc.is_modified()) .filter(|doc| doc.is_modified())
.map(|doc| { .map(|doc| {
doc.relative_path() doc.relative_path()
.and_then(|path| path.to_str()) .map(|path| path.to_string_lossy().to_string())
.unwrap_or("[scratch]") .unwrap_or_else(|| "[scratch]".into())
}) })
.collect(); .collect();
if !modified.is_empty() { if !modified.is_empty() {
@ -1487,7 +1487,7 @@ fn buffer_picker(cx: &mut Context) {
cx.editor cx.editor
.documents .documents
.iter() .iter()
.map(|(id, doc)| (id, doc.relative_path().map(Path::to_path_buf))) .map(|(id, doc)| (id, doc.relative_path()))
.collect(), .collect(),
move |(id, path): &(DocumentId, Option<PathBuf>)| { move |(id, path): &(DocumentId, Option<PathBuf>)| {
// format_fn // format_fn

@ -126,10 +126,11 @@ pub mod completers {
use ignore::WalkBuilder; use ignore::WalkBuilder;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
let path = Path::new(input); let is_tilde = input.starts_with('~') && input.len() == 1;
let path = helix_view::document::expand_tilde(Path::new(input));
let (dir, file_name) = if input.ends_with('/') { let (dir, file_name) = if input.ends_with('/') {
(path.into(), None) (path, None)
} else { } else {
let file_name = path let file_name = path
.file_name() .file_name()
@ -154,7 +155,16 @@ pub mod completers {
let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir()); let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
let path = entry.path(); let path = entry.path();
let mut path = path.strip_prefix(&dir).unwrap_or(path).to_path_buf(); let mut path = if is_tilde {
// if it's a single tilde an absolute path is displayed so that when `TAB` is pressed on
// one of the directories the tilde will be replaced with a valid path not with a relative
// home directory name.
// ~ -> <TAB> -> /home/user
// ~/ -> <TAB> -> ~/first_entry
path.to_path_buf()
} else {
path.strip_prefix(&dir).unwrap_or(path).to_path_buf()
};
if is_dir { if is_dir {
path.push(""); path.push("");
@ -184,7 +194,7 @@ pub mod completers {
}) })
.collect(); .collect();
let range = ((input.len() - file_name.len())..); let range = ((input.len().saturating_sub(file_name.len()))..);
matches.sort_unstable_by_key(|(_file, score)| Reverse(*score)); matches.sort_unstable_by_key(|(_file, score)| Reverse(*score));
files = matches files = matches

@ -127,6 +127,36 @@ where
} }
} }
/// Expands tilde `~` into users home directory if avilable, otherwise returns the path
/// unchanged. The tilde will only be expanded when present as the first component of the path
/// and only slash follows it.
pub fn expand_tilde(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
if let Some(Component::Normal(c)) = components.peek() {
if c == &"~" {
if let Ok(home) = helix_core::home_dir() {
// it's ok to unwrap, the path starts with `~`
return home.join(path.strip_prefix("~").unwrap());
}
}
}
path.to_path_buf()
}
/// Replaces users home directory from `path` with tilde `~` if the directory
/// is available, otherwise returns the path unchanged.
pub fn fold_home_dir(path: &Path) -> PathBuf {
if let Ok(home) = helix_core::home_dir() {
if path.starts_with(&home) {
// it's ok to unwrap, the path starts with home dir
return PathBuf::from("~").join(path.strip_prefix(&home).unwrap());
}
}
path.to_path_buf()
}
/// Normalize a path, removing things like `.` and `..`. /// Normalize a path, removing things like `.` and `..`.
/// ///
/// CAUTION: This does not resolve symlinks (unlike /// CAUTION: This does not resolve symlinks (unlike
@ -137,6 +167,7 @@ where
/// needs to improve on. /// needs to improve on.
/// Copied from cargo: https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81 /// Copied from cargo: https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81
pub fn normalize_path(path: &Path) -> PathBuf { pub fn normalize_path(path: &Path) -> PathBuf {
let path = expand_tilde(path);
let mut components = path.components().peekable(); let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next(); components.next();
@ -163,12 +194,17 @@ pub fn normalize_path(path: &Path) -> PathBuf {
ret ret
} }
// Returns the canonical, absolute form of a path with all intermediate components normalized. /// Returns the canonical, absolute form of a path with all intermediate components normalized.
// ///
// This function is used instead of `std::fs::canonicalize` because we don't want to verify /// This function is used instead of `std::fs::canonicalize` because we don't want to verify
// here if the path exists, just normalize it's components. /// here if the path exists, just normalize it's components.
pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> { pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
std::env::current_dir().map(|current_dir| normalize_path(&current_dir.join(path))) let normalized = normalize_path(path);
if normalized.is_absolute() {
Ok(normalized)
} else {
std::env::current_dir().map(|current_dir| current_dir.join(normalized))
}
} }
use helix_lsp::lsp; use helix_lsp::lsp;
@ -709,12 +745,19 @@ impl Document {
&self.selections[&view_id] &self.selections[&view_id]
} }
pub fn relative_path(&self) -> Option<&Path> { pub fn relative_path(&self) -> Option<PathBuf> {
let cwdir = std::env::current_dir().expect("couldn't determine current directory"); let cwdir = std::env::current_dir().expect("couldn't determine current directory");
self.path self.path.as_ref().map(|path| {
.as_ref() let path = fold_home_dir(path);
.map(|path| path.strip_prefix(cwdir).unwrap_or(path)) if path.is_relative() {
path
} else {
path.strip_prefix(cwdir)
.map(|p| p.to_path_buf())
.unwrap_or(path)
}
})
} }
// pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds { // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds {

Loading…
Cancel
Save