diff --git a/crates/engine/src/cmd/textedit.rs b/crates/engine/src/cmd/textedit.rs index d220deac73..2252ba63e6 100644 --- a/crates/engine/src/cmd/textedit.rs +++ b/crates/engine/src/cmd/textedit.rs @@ -2249,6 +2249,7 @@ fn discard_empty(s: &mut Session, p: &Value) -> Result { st.selection.prune(&st.doc); super::globaledit::follow_history(st); st.revision += 1; + st.rollbacks += 1; return Ok(json!({"removed": true})); } } diff --git a/crates/engine/src/doc_state.rs b/crates/engine/src/doc_state.rs index 8dc15f5aac..b557bb128d 100644 --- a/crates/engine/src/doc_state.rs +++ b/crates/engine/src/doc_state.rs @@ -121,8 +121,9 @@ pub struct DocState { /// Advances when Undo or Redo restores an older document snapshot. The same node ID can name different objects /// across that boundary. pub history_restores: u64, - /// Advances when a canceled undo group, undo scope or interaction, or Exit Symbol Editing Mode taking - /// back the entering step, puts back an older document snapshot. A canceled plug-in run advances it + /// Advances when a canceled undo group, undo scope or interaction, Exit Symbol Editing Mode taking + /// back the entering step, Discard Empty Type taking back the step that made the text, or a command + /// that panicked puts back an older document snapshot. A canceled plug-in run advances it /// when its calls changed the document. A preview advances it when it discards an earlier preview /// that allocated node IDs, or fails after discarding an earlier preview's document change. /// A command run during a tool interaction advances it when it sets aside a preview that allocated diff --git a/crates/engine/src/session/execute.rs b/crates/engine/src/session/execute.rs index 25a9b278b1..88db34c705 100644 --- a/crates/engine/src/session/execute.rs +++ b/crates/engine/src/session/execute.rs @@ -473,6 +473,9 @@ impl Session { for (uid, doc, selection, interaction, history) in snapshot { // A document the command closed stays closed. let Some(st) = self.docs.iter_mut().find(|d| d.uid == uid) else { continue }; + if !std::sync::Arc::ptr_eq(&st.doc, &doc) { + st.rollbacks += 1; + } st.doc = doc; st.selection = selection; st.interaction = interaction; diff --git a/crates/engine/src/tests/journal.rs b/crates/engine/src/tests/journal.rs index 1be7fb69b2..4031910307 100644 --- a/crates/engine/src/tests/journal.rs +++ b/crates/engine/src/tests/journal.rs @@ -877,6 +877,49 @@ fn leaving_symbol_editing_advances_rollbacks_only_when_taking_back_the_entering_ } } +/// Discard Empty Type advances `rollbacks` when it takes back the step that made the empty text, which frees the +/// text's node ID; when it removes the text as a new step instead, it leaves the count as it is. +#[test] +fn discard_empty_type_advances_rollbacks_only_when_taking_back_the_step_that_made_the_text() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let made = s.execute("text.create", &json!({"x": 10, "y": 40, "text": ""})).unwrap(); + let rollbacks = s.doc().unwrap().rollbacks; + s.execute("text.discardEmpty", &json!({"id": made["id"]})).unwrap(); + assert!(s.doc().unwrap().rollbacks > rollbacks, "taking back the step that made the text advances the rollback count"); + let kept = s.execute("text.create", &json!({"x": 10, "y": 80, "text": ""})).unwrap(); + s.execute("shape.rectangle", &rect_step(10)["params"]).unwrap(); + let rollbacks = s.doc().unwrap().rollbacks; + s.execute("text.discardEmpty", &json!({"id": kept["id"]})).unwrap(); + assert!(s.doc().unwrap().doc.node(NodeId(kept["id"].as_u64().unwrap())).is_none(), "the empty text is removed"); + assert_eq!(s.doc().unwrap().rollbacks, rollbacks, "removing the text as a new step keeps the rollback count"); +} + +/// A command that panics after changing a document advances that document's `rollbacks` when the document is put +/// back; one that panics before changing it leaves the count as it is. +#[test] +fn a_panicking_command_advances_rollbacks_only_when_it_changed_the_document() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + let a = NodeId(s.execute("shape.rectangle", &rect_step(10)["params"]).unwrap()["id"].as_u64().unwrap()); + for changed in [false, true] { + let rollbacks = s.doc().unwrap().rollbacks; + let r = s.run_guarded("test.panic", |s| { + if changed { + let st = s.doc_mut()?; + std::sync::Arc::make_mut(&mut st.doc).remove(a)?; + } + s.run_nested(|_| panic!("boom")) + }); + assert!(matches!(r, Err(EngineError::Internal { .. })), "the panic is returned as an internal error"); + if changed { + assert!(s.doc().unwrap().rollbacks > rollbacks, "putting back the changed document advances the rollback count"); + } else { + assert_eq!(s.doc().unwrap().rollbacks, rollbacks, "a panic before any change keeps the rollback count"); + } + } +} + /// Canceling an interaction whose preview changed the document advances `rollbacks`; canceling one that changed /// nothing leaves it as it is. #[test] diff --git a/crates/ui-egui/src/panels/transparency.rs b/crates/ui-egui/src/panels/transparency.rs index a5066539d0..1f9c09340e 100644 --- a/crates/ui-egui/src/panels/transparency.rs +++ b/crates/ui-egui/src/panels/transparency.rs @@ -762,6 +762,23 @@ mod tests { assert_eq!(opacity_of(&panel, &b), 0.01, "the canceled group drops the unfinished value: 1%, not 20+1"); } + /// Discard Empty Type drops an unfinished Opacity expression when it takes back the step that made the empty + /// text, though the object made and selected after it has the same id and opacity. + #[test] + fn unfinished_opacity_is_dropped_by_discard_empty_type() { + let mut app = new_app(); + let a = run_cmd(&mut app, "text.create", json!({"x": 10, "y": 40, "text": ""}))["id"].clone(); + run_cmd(&mut app, "select.set", json!({"ids": [a]})); + let mut panel = Panel::new(app, show); + unfinished_opacity(&mut panel); + run_cmd(&mut panel.app(), "text.discardEmpty", json!({"id": a})); + let b = run_cmd(&mut panel.app(), "shape.rectangle", json!({"x": 50, "y": 0, "width": 30, "height": 30}))["id"].clone(); + assert_eq!(b, a, "the new rectangle has the discarded text's id"); + run_cmd(&mut panel.app(), "select.set", json!({"ids": [b]})); + finish_opacity(&mut panel); + assert_eq!(opacity_of(&panel, &b), 0.01, "Discard Empty Type drops the unfinished value: 1%, not 20+1"); + } + /// The same objects selected in another order keep an unfinished Opacity expression. #[test] fn unfinished_opacity_stays_when_the_same_objects_are_selected_in_another_order() {