SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Save As: rename the design to the new file's name (#4907) (#10036)
Dahak Builds34c378c063b7
diff --git a/apps/solidcraft-cli/tests/run_results.rs b/apps/solidcraft-cli/tests/run_results.rsindex 51fe28534c..c6e44c6897 100644--- a/apps/solidcraft-cli/tests/run_results.rs+++ b/apps/solidcraft-cli/tests/run_results.rs@@ -229,7 +229,9 @@ fn known_pending_concepts_and_released_files_round_trip_with_inert_metadata() {assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));let reopened = io::read_design(&std::fs::read(&destination).unwrap()).unwrap();assert_eq!(reopened.metadata, document.metadata);- assert_eq!(reopened.name, document.name);+ // #4907: `run --save` maps to Save As, and a borrowed design name+ // follows the destination file's stem instead of keeping the old name.+ assert_eq!(reopened.name, format!("resaved-{index}"));assert_eq!(reopened.features, document.features);assert_eq!(std::fs::read(&source).unwrap(), bytes);}diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rsindex 9b47d6b447..77bf1b8f75 100644--- a/crates/engine/src/cmd/file.rs+++ b/crates/engine/src/cmd/file.rs@@ -81,6 +81,8 @@ pub static COMMANDS: &[CommandSpec] = &[fn new_doc(s: &mut Session, p: &Value) -> Result<Value> {let name = str_(p, "name").unwrap_or("Untitled");s.replace_design(Session::new(Document::new(name)));+ // A name given here is the user's own, so Save As keeps it (#4907); "Untitled" is none.+ s.user_named = !name.trim().is_empty() && name.trim() != DEFAULT_NAME;Ok(json!({"name": name}))}@@ -370,17 +372,22 @@ fn default_name_for(name: &str, path: &str) -> Option<String> {(!stem.is_empty()).then_some(stem)}-/// The name a design takes when saved to `path`, if it changes: its file's name while it has-/// none of its own ([`default_name_for`]), or while it still has the name it took from the file-/// it was saved as before, so Save As under another name renames it too (#1710). A name the-/// user chose is kept.-fn saved_name(name: &str, old_path: Option<&str>, path: &str) -> Option<String> {+/// The name a design takes when saved to `path`, if it changes. A design with no name of its+/// own takes the file's ([`default_name_for`]). Otherwise a name the user chose is kept, while+/// a borrowed one — taken from the file it was saved as before (#1710), from a sample, or from+/// a file that stored another design's name — follows a Save As to the new file's name, so+/// after Save As the open design *is* the new file (#4907). A plain Save to the same file+/// never renames.+fn saved_name(name: &str, old_path: Option<&str>, path: &str, user_named: bool) -> Option<String> {if let Some(stem) = default_name_for(name, path) {return Some(stem);}+ if user_named {+ return None;+ }let stem = |p: &str| std::path::Path::new(p).file_stem().map(|x| x.to_string_lossy().trim().to_string()).filter(|x| !x.is_empty());- let (old, new) = (stem(old_path?)?, stem(path)?);- (name.trim() == old && new != old).then_some(new)+ let new = stem(path)?;+ (old_path != Some(path) && name.trim() != new).then_some(new)}/// Rename the design, and the undo and redo states that still carry the old name, so undo@@ -417,9 +424,9 @@ fn write(s: &mut Session, path: &str, format: &str, compression: &str, preview:)));}}- // Saving an unnamed design, or one named after the file it was saved as, names it after its- // file (not an undoable edit).- let renamed = saved_name(&s.doc.name, s.path.as_deref(), path).map(|stem| (s.doc.name.clone(), stem));+ // Saving an unnamed or borrowed-named design to a new file names it after that file+ // (not an undoable edit); a name the user chose is kept (#4907).+ let renamed = saved_name(&s.doc.name, s.path.as_deref(), path, s.user_named).map(|stem| (s.doc.name.clone(), stem));if let Some((from, to)) = &renamed {rename_design(s, from, to);}@@ -1728,6 +1735,44 @@ mod tests {std::fs::remove_dir_all(dir).unwrap();}+ /// #4907: after Save As the open design *is* the new file — tab, browser and the name stored+ /// in the file — whenever its name didn't come from its current file: a sample's name, or a+ /// file renamed outside SolidCraft or named by someone else. A plain Save never renames.+ #[test]+ fn save_as_names_a_borrowed_name_after_the_new_file() {+ let dir = name_dir("borrowed");+ // The issue's steps: the sample "Sample Plate" saved as "Bracket v2.solidcraft".+ let mut s = Session::default();+ s.doc_mut().name = "Sample Plate".into();+ s.execute("solid.box", &json!({"length":10,"width":20,"height":30})).unwrap();+ let path = dir.join("Bracket v2.solidcraft");+ let r = s.execute("file.save_as", &json!({"path":path})).unwrap();+ assert_eq!(r["name"], "Bracket v2");+ assert_eq!(s.doc.name, "Bracket v2");+ assert!(!s.is_dirty(), "taking the new file's name is part of the save");+ assert_eq!(solidcraft_io::read_design(&std::fs::read(&path).unwrap()).unwrap().name, "Bracket v2");+ // Undoing the box does not bring the sample's name back.+ s.execute("edit.undo", &json!({})).unwrap();+ assert_eq!(s.doc.name, "Bracket v2");+ // Reopening shows the new name too.+ let mut reopened = Session::default();+ reopened.execute("doc.open", &json!({"path":path})).unwrap();+ assert_eq!(reopened.doc.name, "Bracket v2");+ // A file whose stored name is not its own (renamed outside SolidCraft, or saved by a+ // version before #1710): Save As names the design after the new file, while a plain+ // Save to the file it has leaves the name alone.+ let odd = dir.join("renamed.solidcraft");+ std::fs::write(&odd, solidcraft_io::write_design(&Document::new("Sample Plate"))).unwrap();+ let mut s = Session::default();+ s.execute("doc.open", &json!({"path":odd})).unwrap();+ assert_eq!(s.doc.name, "Sample Plate");+ s.execute("file.save", &json!({})).unwrap();+ assert_eq!(s.doc.name, "Sample Plate", "a plain Save does not rename behind the user's back");+ s.execute("file.save_as", &json!({"path":dir.join("Bracket v3.solidcraft")})).unwrap();+ assert_eq!(s.doc.name, "Bracket v3");+ std::fs::remove_dir_all(dir).unwrap();+ }+#[test]fn opening_an_untitled_file_shows_its_file_name() {let dir = name_dir("open");diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 11ffd16542..a906460bc5 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -450,6 +450,11 @@ pub struct Session {/// Opened read-only (`doc.open {read_only}`, the "open elsewhere" notice): saving to its own/// file is refused; Save As to another file is how its changes are kept.pub read_only: bool,+ /// Whether the design's name is one its user chose for it (`file.new {name}`) rather than+ /// one it borrowed — from a file's name, a sample's name, or a file that stored another+ /// design's name. A borrowed name follows Save As to the new file's name (#4907); a name+ /// the user chose is kept.+ pub user_named: bool,/// One-shot display-only notification during doc.open, before authoritative evaluation./// Interactive loaders may show it provisionally; it never becomes model state.pub open_preview: Option<Box<dyn Fn(solidcraft_io::DesignPreview) + Send + Sync>>,@@ -688,6 +693,7 @@ impl Session {uid: NEXT_UID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),disk: None,read_only: false,+ user_named: false,open_preview: None,log: Vec::new(),revision: 1,@@ -1649,6 +1655,7 @@ impl Session {uid: self.uid,disk: self.disk,read_only: self.read_only,+ user_named: self.user_named,open_preview: None,saved: self.saved.clone(),log: Vec::new(),