R3.1: keep \?\ for file IO; simplify a save target only as a whole

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-12 18:56:57 -04:00
1 parent d0b5aede95
commit d929a807c4
7 files changed
+105 -8

No files matched your search

+10 -5
View File
@@ -14,17 +14,19 @@ use crate::{After, Closing, Dialog, Engine, Host, Opening, Tab, Tabs, in_window,
/// The file a path names, links resolved.
fn identity(path: &Path) -> io::Result<PathBuf> {
match dunce::canonicalize(path) {
Ok(real) => Ok(real),
let real = match std::fs::canonicalize(path) {
Ok(real) => real,
Err(error) if error.kind() == io::ErrorKind::NotFound => {
let parent = path.parent().unwrap_or(Path::new("."));
let name = path
.file_name()
.ok_or_else(|| io::Error::other("no file name"))?;
Ok(dunce::canonicalize(parent)?.join(name))
std::fs::canonicalize(parent)?.join(name)
}
Err(error) => Err(error),
}
Err(error) => return Err(error),
};
// Whole path only: a name may need `\\?\`.
Ok(dunce::simplified(&real).to_path_buf())
}
pub trait Saving: Host {
@@ -478,3 +480,6 @@ pub fn folder<E, G: Engine>(tab: &Tab<E, G>) -> Option<PathBuf> {
.map(PathBuf::from)
})
}
#[cfg(all(test, windows))]
mod tests;
+35
View File
@@ -0,0 +1,35 @@
use std::os::windows::ffi::OsStrExt as _;
use std::path::{Path, PathBuf};
use super::identity;
fn verbatim(path: &Path) -> bool {
path.to_string_lossy().starts_with(r"\\?\")
}
#[test]
fn a_short_path_reads_plain() {
let root = tempfile::tempdir().expect("a temp dir");
let new = identity(&root.path().join("new.md")).expect("a target");
assert!(!verbatim(&new), "{}", new.display());
}
#[test]
fn a_trailing_space_name_stays_verbatim() {
let root = tempfile::tempdir().expect("a temp dir");
let real = std::fs::canonicalize(root.path()).expect("real");
let spaced = identity(&PathBuf::from(format!("{} ", real.join("guard.md").display())))
.expect("a target");
assert!(verbatim(&spaced), "{}", spaced.display());
assert!(spaced.to_string_lossy().ends_with("guard.md "));
}
#[test]
fn a_long_new_name_stays_verbatim() {
let root = tempfile::tempdir().expect("a temp dir");
let dir = root.path().join("d".repeat(150));
std::fs::create_dir(&dir).expect("a folder");
let long = identity(&dir.join(format!("{}.md", "x".repeat(120)))).expect("a target");
assert!(long.as_os_str().encode_wide().count() > 260);
assert!(verbatim(&long), "{}", long.display());
}