diff --git a/crates/engine/src/session/interactive.rs b/crates/engine/src/session/interactive.rs index fb33d761d..a4fc53690 100644 --- a/crates/engine/src/session/interactive.rs +++ b/crates/engine/src/session/interactive.rs @@ -122,11 +122,15 @@ impl Session { return self.input(Input::Keyword("Undo".into())); } if !nested { - self.commands_started = self.commands_started.wrapping_add(1); - self.command_origin = origin; + // The command this one replaces is cancelled first: its cancellation can run a paused + // LISP routine's `*error*` handler, and a handler that starts a command records that + // command's Program origin. Origin and number go in after it, so they describe this + // start and the factory below still sees the origin this command was started with. if self.running.is_some() { self.cancel(); } + self.commands_started = self.commands_started.wrapping_add(1); + self.command_origin = origin; } (spec.enabled)(self).map_err(|m| EngineError::Disabled(spec.id.into(), m))?; if let Some(why) = cmd::blocks::bedit::refused_in_editor(self, spec.id) { diff --git a/crates/engine/tests/it/insert_attrib_entry.rs b/crates/engine/tests/it/insert_attrib_entry.rs index 394f05826..882958eab 100644 --- a/crates/engine/tests/it/insert_attrib_entry.rs +++ b/crates/engine/tests/it/insert_attrib_entry.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use cadcraft_engine::doc::{Attrib, AttribModes, Block, Entity, EntityKind, Handle, Text}; use cadcraft_engine::geom::{Vec2, Vec3}; -use cadcraft_engine::{AttribAsk, Input, Session}; +use cadcraft_engine::{AttribAsk, Input, Origin, Session}; use serde_json::json; fn def(tag: &str, prompt: &str, default: &str, y: f64, constant: bool, modes: AttribModes) -> Attrib { @@ -310,6 +310,52 @@ fn a_person_typing_a_transparent_insert_still_asks_in_the_dialog() { s.cancel(); } +/// A person typing `'NAME T …` while a routine waits in `(command "LINE" "0,0" pause)` +/// cancels it; the routine's `*error*` handler then starts a LINE of its own. The handler's +/// command is the program's, and it must not take the origin or number of the person's start. +/// `extra` answers MINSERT's rows and columns. +fn person_replaces_paused_lisp(spelling: &str, extra: &str) -> Session { + let mut s = session(None, Some(true)); + s.cmdline("(defun *error* (msg) (command \"LINE\" \"1,1\" \"2,2\" \"\"))").unwrap(); + s.cmdline("(command \"LINE\" \"0,0\" pause)").unwrap(); + assert!(s.running.is_some(), "the routine's LINE waits for the person: {}", s.prompt_text()); + // The person replaces the waiting routine with their own command. + s.cmdline(&format!("'{spelling} T 0,50 1 1 0{extra}")).unwrap(); + assert!(s.running.is_some(), "{}", s.prompt_text()); + // The handler's LINE is the only one drawn: the paused LINE was cancelled at its first + // point, and the handler's own command ran as the program's (nobody typed 1,1 or 2,2). + let lines: Vec<(Vec2, Vec2)> = s + .doc() + .unwrap() + .model + .iter() + .filter_map(|e| match &e.kind { + EntityKind::Line(l) => Some((l.a.xy(), l.b.xy())), + _ => None, + }) + .collect(); + assert_eq!(lines, vec![(Vec2::new(1.0, 1.0), Vec2::new(2.0, 2.0))], "the error handler's LINE is drawn, and the paused one added nothing"); + s +} + +#[test] +fn a_person_replacing_paused_lisp_keeps_their_insert_dialog() { + let s = person_replaces_paused_lisp("-INSERT", ""); + assert_eq!(s.command_origin(), Origin::Person, "the person's INSERT is not the handler's command"); + let e = s.attribute_entry().expect("ATTDIA 1: the person's INSERT asks in the dialog"); + // The handler's LINE finished as a command of its own; the INSERT's answers belong to a + // later number, so the dialog a cancelled command left behind is not taken. + assert_eq!(s.last_finished.map(|(command, _)| command), Some(e.command.wrapping_sub(1)), "the handler's LINE keeps its own number"); +} + +#[test] +fn a_person_replacing_paused_lisp_keeps_their_minsert_dialog() { + let s = person_replaces_paused_lisp("MINSERT", " 1 1"); + assert_eq!(s.command_origin(), Origin::Person, "the person's MINSERT is not the handler's command"); + let e = s.attribute_entry().expect("ATTDIA 1: the person's MINSERT asks in the dialog"); + assert_eq!(s.last_finished.map(|(command, _)| command), Some(e.command.wrapping_sub(1)), "the handler's LINE keeps its own number"); +} + #[test] fn an_entry_belongs_to_the_drawing_and_command_that_asked() { let mut s = session(None, Some(true)); diff --git a/crates/ui-egui/tests/it/insert_attrib_dialog.rs b/crates/ui-egui/tests/it/insert_attrib_dialog.rs index a20a9fdf3..7ee2484e5 100644 --- a/crates/ui-egui/tests/it/insert_attrib_dialog.rs +++ b/crates/ui-egui/tests/it/insert_attrib_dialog.rs @@ -221,7 +221,7 @@ fn a_new_insert_does_not_inherit_a_cancelled_draft() { h.run_steps(2); assert_eq!(h.get_by_label("Room").value().as_deref(), Some("101"), "the replacement's own default"); assert_eq!(h.get_by_label("Floor").value().as_deref(), Some("1"), "the replacement's own default"); - // The confirmation left over from the old dialog submits what this one shows. + // A confirmation made once the replacement has been drawn completes it with what it shows. h.get_by_label("OK").click(); h.run_steps(3); let app = h.state(); @@ -335,6 +335,33 @@ fn a_person_typing_a_transparent_insert_still_opens_the_dialog() { assert!(h.state().session.attribute_entry().is_some()); } +#[test] +fn a_person_replacing_paused_lisp_keeps_their_insert_dialog() { + // A routine waits in `(command "LINE" "0,0" pause)`; the person's `'-INSERT` cancels it, + // and the routine's `*error*` handler starts a LINE of its own. That handler command is the + // program's; the dialog still belongs to the person's INSERT. + let mut a = app(); + a.cmdline("(defun *error* (msg) (command \"LINE\" \"1,1\" \"2,2\" \"\"))"); + a.cmdline("(command \"LINE\" \"0,0\" pause)"); + a.cmdline("'-INSERT T 50,0 1 1 0"); + assert!(a.session.attribute_entry().is_some(), "the person's INSERT asks in the dialog: {}", a.session.prompt_text()); + let h = harness(a); + h.get_by_label("Edit Attributes"); + assert_eq!(h.state().session.command_origin(), cadcraft_engine::Origin::Person); +} + +#[test] +fn a_person_replacing_paused_lisp_keeps_their_minsert_dialog() { + let mut a = app(); + a.cmdline("(defun *error* (msg) (command \"LINE\" \"1,1\" \"2,2\" \"\"))"); + a.cmdline("(command \"LINE\" \"0,0\" pause)"); + a.cmdline("'-MINSERT T 50,0 1 1 0 1 1"); + assert!(a.session.attribute_entry().is_some(), "the person's MINSERT asks in the dialog: {}", a.session.prompt_text()); + let h = harness(a); + h.get_by_label("Edit Attributes"); + assert_eq!(h.state().session.command_origin(), cadcraft_engine::Origin::Person); +} + #[test] fn a_typed_or_menu_insert_still_opens_the_dialog() { // Typed at the command line.