From 78bcd82885e3c1d29f5e389b19424e84ca8a9ab3 Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:31:29 +0530 Subject: [PATCH 1/8] fix: expand Windows tilde paths --- src/util.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 6ee92fe..5b9ebe2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -300,11 +300,16 @@ pub fn home_dir() -> Option { /// expand `~` ourselves. the shell is not involved when a user pastes a path /// into a text box, so somebody has to do it. +fn tilde_suffix(path: &str, windows: bool) -> Option<&str> { + path.strip_prefix("~/") + .or_else(|| windows.then(|| path.strip_prefix("~\\")).flatten()) +} + pub fn expand_tilde(path: &str) -> PathBuf { if path == "~" { return home_dir().unwrap_or_else(|| PathBuf::from("~")); } - if let Some(rest) = path.strip_prefix("~/") + if let Some(rest) = tilde_suffix(path, cfg!(windows)) && let Some(home) = home_dir() { return home.join(rest); @@ -753,6 +758,13 @@ mod tests { assert_eq!(common_prefix(&[]), ""); } + #[test] + fn tilde_paths_accept_native_windows_separators() { + assert_eq!(tilde_suffix("~/Documents", false), Some("Documents")); + assert_eq!(tilde_suffix("~\\Documents", true), Some("Documents")); + assert_eq!(tilde_suffix("~\\Documents", false), None); + } + #[test] fn user_directories_are_platform_shaped() { let cache = cache_home(); From 88b933f9eceb0f5d56a670830075d46bba7448e3 Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:31:41 +0530 Subject: [PATCH 2/8] style: simplify Windows tilde separator handling --- src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 5b9ebe2..eb4ab91 100644 --- a/src/util.rs +++ b/src/util.rs @@ -302,7 +302,7 @@ pub fn home_dir() -> Option { /// into a text box, so somebody has to do it. fn tilde_suffix(path: &str, windows: bool) -> Option<&str> { path.strip_prefix("~/") - .or_else(|| windows.then(|| path.strip_prefix("~\\")).flatten()) + .or_else(|| if windows { path.strip_prefix("~\\") } else { None }) } pub fn expand_tilde(path: &str) -> PathBuf { From 5e110cbb434723395a14b528411cf46214bb445a Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:33:06 +0530 Subject: [PATCH 3/8] style: format tilde separator helper --- src/util.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/util.rs b/src/util.rs index eb4ab91..7982d27 100644 --- a/src/util.rs +++ b/src/util.rs @@ -301,8 +301,13 @@ pub fn home_dir() -> Option { /// expand `~` ourselves. the shell is not involved when a user pastes a path /// into a text box, so somebody has to do it. fn tilde_suffix(path: &str, windows: bool) -> Option<&str> { - path.strip_prefix("~/") - .or_else(|| if windows { path.strip_prefix("~\\") } else { None }) + path.strip_prefix("~/").or_else(|| { + if windows { + path.strip_prefix("~\\") + } else { + None + } + }) } pub fn expand_tilde(path: &str) -> PathBuf { From 78fb0504ec7e6cee4822526f99ef185cfee05042 Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:34:16 +0530 Subject: [PATCH 4/8] fix: normalize Windows home-relative separators --- src/util.rs | 54 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/util.rs b/src/util.rs index 7982d27..30098ee 100644 --- a/src/util.rs +++ b/src/util.rs @@ -301,27 +301,47 @@ pub fn home_dir() -> Option { /// expand `~` ourselves. the shell is not involved when a user pastes a path /// into a text box, so somebody has to do it. fn tilde_suffix(path: &str, windows: bool) -> Option<&str> { - path.strip_prefix("~/").or_else(|| { - if windows { - path.strip_prefix("~\\") - } else { - None + if windows { + let rest = path.strip_prefix('~')?; + if !rest.starts_with('/') && !rest.starts_with('\\') { + return None; } - }) + Some(rest.trim_start_matches(['/', '\\'])) + } else { + path.strip_prefix("~/") + .map(|rest| rest.trim_start_matches('/')) + } } -pub fn expand_tilde(path: &str) -> PathBuf { +fn with_trailing_separator(home: PathBuf) -> PathBuf { + let mut value = home.into_os_string(); + let text = value.to_string_lossy(); + if !text.ends_with('/') && !text.ends_with('\\') { + value.push(std::path::MAIN_SEPARATOR.to_string()); + } + PathBuf::from(value) +} + +fn expand_tilde_from(path: &str, home: Option, windows: bool) -> PathBuf { if path == "~" { - return home_dir().unwrap_or_else(|| PathBuf::from("~")); + return home.unwrap_or_else(|| PathBuf::from("~")); } - if let Some(rest) = tilde_suffix(path, cfg!(windows)) - && let Some(home) = home_dir() + if let Some(rest) = tilde_suffix(path, windows) + && let Some(home) = home { - return home.join(rest); + return if rest.is_empty() { + with_trailing_separator(home) + } else { + home.join(rest) + }; } PathBuf::from(path) } +pub fn expand_tilde(path: &str) -> PathBuf { + expand_tilde_from(path, home_dir(), cfg!(windows)) +} + /// one path suggestion, with everything the UI needs to render and rank it. #[derive(Clone, Debug, PartialEq, Eq)] pub struct CompletionCandidate { @@ -767,9 +787,21 @@ mod tests { fn tilde_paths_accept_native_windows_separators() { assert_eq!(tilde_suffix("~/Documents", false), Some("Documents")); assert_eq!(tilde_suffix("~\\Documents", true), Some("Documents")); + assert_eq!(tilde_suffix("~\\\\Documents", true), Some("Documents")); + assert_eq!(tilde_suffix("~\\", true), Some("")); assert_eq!(tilde_suffix("~\\Documents", false), None); } + #[test] + fn tilde_root_keeps_a_trailing_separator_for_completion() { + let home = PathBuf::from("C:\\Users\\person"); + let expanded = expand_tilde_from("~\\", Some(home), true); + assert_eq!(expanded.to_string_lossy(), "C:\\Users\\person\\"); + let (dir, prefix) = split_completion_path(&expanded.to_string_lossy(), true); + assert_eq!(dir, "C:\\Users\\person\\"); + assert_eq!(prefix, ""); + } + #[test] fn user_directories_are_platform_shaped() { let cache = cache_home(); From 736e5d4ea7c32ad0d49fb29fdcacf90d83e02df1 Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:34:29 +0530 Subject: [PATCH 5/8] fix: avoid borrowing the home path while appending separator --- src/util.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/util.rs b/src/util.rs index 30098ee..9149a81 100644 --- a/src/util.rs +++ b/src/util.rs @@ -315,8 +315,11 @@ fn tilde_suffix(path: &str, windows: bool) -> Option<&str> { fn with_trailing_separator(home: PathBuf) -> PathBuf { let mut value = home.into_os_string(); - let text = value.to_string_lossy(); - if !text.ends_with('/') && !text.ends_with('\\') { + let has_separator = { + let text = value.to_string_lossy(); + text.ends_with('/') || text.ends_with('\\') + }; + if !has_separator { value.push(std::path::MAIN_SEPARATOR.to_string()); } PathBuf::from(value) From ea33d8af75e443827e94e1cab9a8aa7a2ae50ce5 Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:34:43 +0530 Subject: [PATCH 6/8] fix: preserve home directory boundary for tilde completion --- src/util.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 9149a81..1050152 100644 --- a/src/util.rs +++ b/src/util.rs @@ -327,7 +327,9 @@ fn with_trailing_separator(home: PathBuf) -> PathBuf { fn expand_tilde_from(path: &str, home: Option, windows: bool) -> PathBuf { if path == "~" { - return home.unwrap_or_else(|| PathBuf::from("~")); + return home + .map(with_trailing_separator) + .unwrap_or_else(|| PathBuf::from("~")); } if let Some(rest) = tilde_suffix(path, windows) && let Some(home) = home @@ -803,6 +805,9 @@ mod tests { let (dir, prefix) = split_completion_path(&expanded.to_string_lossy(), true); assert_eq!(dir, "C:\\Users\\person\\"); assert_eq!(prefix, ""); + + let expanded = expand_tilde_from("~", Some(PathBuf::from("C:\\Users\\person")), true); + assert_eq!(expanded.to_string_lossy(), "C:\\Users\\person\\"); } #[test] From 82d126dc5ac1e6544c15dde559a663aae8ef092a Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:35:49 +0530 Subject: [PATCH 7/8] test: keep expanded path alive during split --- src/util.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 1050152..388d1ea 100644 --- a/src/util.rs +++ b/src/util.rs @@ -802,7 +802,8 @@ mod tests { let home = PathBuf::from("C:\\Users\\person"); let expanded = expand_tilde_from("~\\", Some(home), true); assert_eq!(expanded.to_string_lossy(), "C:\\Users\\person\\"); - let (dir, prefix) = split_completion_path(&expanded.to_string_lossy(), true); + let expanded_text = expanded.to_string_lossy(); + let (dir, prefix) = split_completion_path(&expanded_text, true); assert_eq!(dir, "C:\\Users\\person\\"); assert_eq!(prefix, ""); From 243770a25437a0d80c8b50032b0aad254741e860 Mon Sep 17 00:00:00 2001 From: soumalya das Date: Sat, 28 Nov 2026 09:37:57 +0530 Subject: [PATCH 8/8] fix: use the requested platform separator in home paths --- src/util.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util.rs b/src/util.rs index 388d1ea..d013ce4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -313,14 +313,14 @@ fn tilde_suffix(path: &str, windows: bool) -> Option<&str> { } } -fn with_trailing_separator(home: PathBuf) -> PathBuf { +fn with_trailing_separator(home: PathBuf, windows: bool) -> PathBuf { let mut value = home.into_os_string(); let has_separator = { let text = value.to_string_lossy(); text.ends_with('/') || text.ends_with('\\') }; if !has_separator { - value.push(std::path::MAIN_SEPARATOR.to_string()); + value.push(if windows { "\\" } else { "/" }); } PathBuf::from(value) } @@ -328,14 +328,14 @@ fn with_trailing_separator(home: PathBuf) -> PathBuf { fn expand_tilde_from(path: &str, home: Option, windows: bool) -> PathBuf { if path == "~" { return home - .map(with_trailing_separator) + .map(|home| with_trailing_separator(home, windows)) .unwrap_or_else(|| PathBuf::from("~")); } if let Some(rest) = tilde_suffix(path, windows) && let Some(home) = home { return if rest.is_empty() { - with_trailing_separator(home) + with_trailing_separator(home, windows) } else { home.join(rest) };