UNDO stops at a REFCLOSE Save that wrote an xref's file, instead of bringing back an edit it can't save

This commit is contained in:
Tanish Manku
2026-11-23 13:30:21 -07:00
parent 39fca9e2a7
commit cda891fb4e
3 changed files with 46 additions and 1 deletions
+14 -1
View File
@@ -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<Drawing>, 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<String> {
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))
+3
View File
@@ -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();
+29
View File
@@ -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);
}