fix: Expand tilde first, then deal with relative paths

Otherwise the ~ gets treated as a relative path.

Fixes #1107
pull/1124/head
Blaž Hrastnik 3 years ago
parent f2b4ff23ba
commit 2b7c086653

@ -40,7 +40,6 @@ pub fn expand_tilde(path: &Path) -> PathBuf {
/// 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 get_normalized_path(path: &Path) -> PathBuf { pub fn get_normalized_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();
@ -72,10 +71,11 @@ pub fn get_normalized_path(path: &Path) -> PathBuf {
/// 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 get_canonicalized_path(path: &Path) -> std::io::Result<PathBuf> { pub fn get_canonicalized_path(path: &Path) -> std::io::Result<PathBuf> {
let path = expand_tilde(path);
let path = if path.is_relative() { let path = if path.is_relative() {
std::env::current_dir().map(|current_dir| current_dir.join(path))? std::env::current_dir().map(|current_dir| current_dir.join(path))?
} else { } else {
path.to_path_buf() path
}; };
Ok(get_normalized_path(path.as_path())) Ok(get_normalized_path(path.as_path()))

@ -1767,11 +1767,8 @@ mod cmd {
args: &[&str], args: &[&str],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
use helix_core::path::expand_tilde;
let path = args.get(0).context("wrong argument count")?; let path = args.get(0).context("wrong argument count")?;
let _ = cx let _ = cx.editor.open(path.into(), Action::Replace)?;
.editor
.open(expand_tilde(Path::new(path)), Action::Replace)?;
Ok(()) Ok(())
} }

Loading…
Cancel
Save