diff --git a/crates/engine/src/cmd/blocks/refedit.rs b/crates/engine/src/cmd/blocks/refedit.rs index 5cd386a85..7e3398108 100644 --- a/crates/engine/src/cmd/blocks/refedit.rs +++ b/crates/engine/src/cmd/blocks/refedit.rs @@ -21,7 +21,8 @@ //! the file it wrote. //! //! The state is [`Drawing::refedit`]: it travels with undo, is never written to a file (files -//! are written from [`Drawing::committed`]) and ends with the drawing it belongs to. +//! are written from [`Drawing::committed`]) and ends with the drawing it belongs to. UNDO stops +//! at a REFCLOSE Save that wrote an xref's file ([`undo_barrier`]). //! //! Not yet: annotative scale contexts, attribute synchronisation and xrefs nested in xrefs (open //! those with XOPEN). @@ -1066,6 +1067,18 @@ pub(crate) fn after_command(s: &mut Session, before: &Arc, id: &str) -> None } +/// Why UNDO stops before bringing back `snap` while the drawing is `cur`: `snap` holds an edit of +/// an xref in place whose file REFCLOSE Save has written since (`cur` shows the xref loaded again +/// from the file as written, so its stamp is no longer the one the edit began with). Brought back, +/// that edit could never be saved (its file changed since it began) and the drawing would show the +/// xref as it was before the save. A discarded edit leaves the xref's stamp as it was: it comes +/// back with UNDO as any other step. +pub(crate) fn undo_barrier(cur: &Drawing, snap: &Drawing) -> Option { + let x = snap.refedit.as_ref()?.xref.as_ref()?; + let now = cur.block(&x.xref).and_then(|b| b.xref_stamp); + (now != x.stamp).then(|| format!("The reference edit was saved to {}, so it can't be undone; the drawing stays as saved.", x.file)) +} + /// Whether `h` may not be edited because a reference is being edited and it is not in the working set. pub(crate) fn locked(d: &Drawing, h: Handle) -> bool { d.refedit.as_ref().is_some_and(|r| !r.is_working(h)) diff --git a/crates/engine/src/session/execute.rs b/crates/engine/src/session/execute.rs index 74e2a9f6e..2e5a94c7f 100644 --- a/crates/engine/src/session/execute.rs +++ b/crates/engine/src/session/execute.rs @@ -108,6 +108,9 @@ impl Session { return Ok(Some(label)); } let st = self.state_mut()?; + if let Some(why) = st.undo.last().and_then(|snap| cmd::blocks::refedit::undo_barrier(&st.doc, &snap.doc)) { + return Err(EngineError::Other(why)); + } let Some(snap) = st.undo.pop() else { return Ok(None) }; let cur = st.swap_in(snap); let label = cur.label.clone(); diff --git a/crates/engine/tests/it/refedit_xref.rs b/crates/engine/tests/it/refedit_xref.rs index dde9dd24a..91aad9e43 100644 --- a/crates/engine/tests/it/refedit_xref.rs +++ b/crates/engine/tests/it/refedit_xref.rs @@ -1003,3 +1003,32 @@ fn an_xref_file_dated_before_1970_can_be_edited_in_place() { s.execute("refclose", &json!({ "option": "save" })).unwrap(); assert_eq!(read(&file).model.len(), 3, "the edit is written"); } + +/// Review of #3352: UNDO after a saved REFCLOSE brought the edit back, with the file's stamp from +/// before the save, so it could never be saved again. The save is an undo barrier: UNDO says the +/// edit was saved to its file and leaves the drawing as saved. A discarded edit still comes back. +#[test] +fn undo_after_a_saved_xref_edit_does_not_bring_the_edit_back() { + let (mut s, dir, _, insert) = attached("undo-saved"); + let file = dir.join("site.dxf"); + s.execute("line", &json!({ "points": [[0, 50], [10, 50]] })).unwrap(); + s.execute("refedit", &json!({ "handle": insert })).unwrap(); + s.execute("line", &json!({ "points": [[100, 20], [110, 20]] })).unwrap(); + s.execute("refclose", &json!({ "option": "discard" })).unwrap(); + s.execute("undo", &json!({})).unwrap(); + assert!(s.doc().unwrap().refedit.is_some(), "a discarded edit comes back with UNDO"); + + s.execute("refclose", &json!({ "option": "save" })).unwrap(); + let saved = std::fs::read(&file).unwrap(); + let e = s.execute("undo", &json!({})).unwrap_err().to_string(); + assert!(e.contains("saved to") && e.contains("site.dxf"), "{e}"); + let d = s.doc().unwrap(); + assert!(d.refedit.is_none(), "the edit doesn't come back"); + assert_eq!(d.block("site").unwrap().entities.len(), 3, "the host shows the xref as saved"); + assert_eq!(std::fs::read(&file).unwrap(), saved, "the file is as saved"); + // The xref can be edited and saved again. + s.execute("refedit", &json!({ "handle": insert })).unwrap(); + s.execute("line", &json!({ "points": [[100, 30], [110, 30]] })).unwrap(); + s.execute("refclose", &json!({ "option": "save" })).unwrap(); + assert_eq!(read(&file).model.len(), 4); +}