diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 9d0e00ef8..637e4937f 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -45,9 +45,9 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("timeline.roll_to", "Roll History Marker", roll_to).icon("rollback") .params("position: number of features to keep active, or \"end\" (omit = end) | feature: id|name (the marker goes right after it)"), CommandSpec::new("timeline.reorder", "Reorder Feature", reorder).icon("reorder") - .params("feature: id|name, position: its new index, active?: bool (true = it ends up before the history marker, false = behind it; other features keep their state); refused when a feature that worked would fail"), + .params("feature: id|name, position: its new index, active?: bool (true = it ends up before the history marker, false = behind it; other features keep their state); refused when a feature that worked would fail, unless allow_errors?: bool (default false): then made, and broken: [{feature, error}] lists the features it broke"), CommandSpec::new("timeline.reorder_many", "Reorder Features", reorder_many).icon("reorder").params( - "features: [id|name], position: insertion slot in the current history; selected features keep their relative order; refused when a feature that worked would fail", + "features: [id|name], position: insertion slot in the current history; selected features keep their relative order; active?: bool (as timeline.reorder's); refused when a feature that worked would fail, unless allow_errors?: bool (default false): then made, and broken: [{feature, error}] lists the features it broke", ), CommandSpec::new("timeline.redefine", "Edit Feature", redefine).icon("edit") .params("feature: id|name, command: the feature's command id, params: its parameters (the feature is rebuilt in place)"), @@ -518,7 +518,7 @@ fn move_many(s: &mut Session, p: &Value) -> Result { sort_group_features(&mut next); let slot = num(p, "position").map(|x| x.max(0.0) as usize).ok_or_else(|| bad(cmd, "`position` must be a number"))?; let active = active_param(p, cmd)?; - let (mut next, ordered) = reorder_features(s, next, &moving, slot, cmd)?; + let (mut next, ordered, _) = reorder_features(s, next, &moving, slot, false, cmd)?; place_marker(s, &mut next, &moving, active, cmd)?; if next != *s.doc { *s.doc_mut() = next; @@ -1169,7 +1169,9 @@ fn reorder(s: &mut Session, p: &Value) -> Result { let mut after = s.model.clone(); after.evaluate(&next); let broken = newly_broken(&s.model, &after); - if !broken.is_empty() { + // `allow_errors`: made anyway, with what it broke said (#5962). + let allow = allow_errors(p, cmd)?; + if !broken.is_empty() && !allow { let list: Vec = broken.iter().map(|(n, e)| format!("{n} ({e})")).collect(); return Err(bad(cmd, format!("moving {name} there breaks {}", list.join("; ")))); } @@ -1177,7 +1179,25 @@ fn reorder(s: &mut Session, p: &Value) -> Result { if next != *s.doc { *s.doc_mut() = next; } - Ok(json!({"feature": id, "position": to})) + let mut out = json!({"feature": id, "position": to}); + if allow { + out["broken"] = broken_json(&broken); + } + Ok(out) +} + +/// The optional `allow_errors` of a move: make it even when it breaks features that worked. +fn allow_errors(p: &Value, cmd: &str) -> Result { + match p.get("allow_errors") { + None | Some(Value::Null) => Ok(false), + Some(Value::Bool(allow)) => Ok(*allow), + Some(_) => Err(bad(cmd, "`allow_errors` must be true or false")), + } +} + +/// The features a move broke, as `[{feature, error}]`. +fn broken_json(broken: &[(String, String)]) -> Value { + broken.iter().map(|(feature, error)| json!({"feature": feature, "error": error})).collect() } /// The optional `active` of a move: whether the moved features end up before the history marker. @@ -1210,11 +1230,18 @@ fn reorder_many(s: &mut Session, p: &Value) -> Result { let cmd = "timeline.reorder_many"; let ids = group_features(s, p, true, cmd)?; let slot = num(p, "position").map(|x| x.max(0.0) as usize).ok_or_else(|| bad(cmd, "`position` must be a number"))?; - let (next, ordered) = reorder_features(s, (*s.doc).clone(), &ids, slot, cmd)?; + let active = active_param(p, cmd)?; + let allow = allow_errors(p, cmd)?; + let (mut next, ordered, broken) = reorder_features(s, (*s.doc).clone(), &ids, slot, allow, cmd)?; + place_marker(s, &mut next, &ordered, active, cmd)?; if next != *s.doc { *s.doc_mut() = next; } - Ok(json!({"features": ordered, "position": slot.min(s.doc.features.len())})) + let mut out = json!({"features": ordered, "position": slot.min(s.doc.features.len())}); + if allow { + out["broken"] = broken_json(&broken); + } + Ok(out) } fn reorder_features( @@ -1222,8 +1249,9 @@ fn reorder_features( mut next: solidcraft_doc::Document, ids: &[u64], slot: usize, + allow_errors: bool, cmd: &str, -) -> Result<(solidcraft_doc::Document, Vec)> { +) -> Result<(solidcraft_doc::Document, Vec, Vec<(String, String)>)> { let n = next.features.len(); let slot = slot.min(n); let selected: std::collections::BTreeSet = ids.iter().copied().collect(); @@ -1248,16 +1276,17 @@ fn reorder_features( next.features.splice(insert_at..insert_at, moving); sort_group_features(&mut next); require_contiguous_groups(&next, cmd)?; + let mut broken = Vec::new(); if !unchanged { let mut after = solidcraft_doc::Model::new(); after.evaluate(&next); - let broken = newly_broken(&s.model, &after); - if !broken.is_empty() { + broken = newly_broken(&s.model, &after); + if !broken.is_empty() && !allow_errors { let list: Vec = broken.iter().map(|(name, error)| format!("{name} ({error})")).collect(); return Err(bad(cmd, format!("moving the selected features there breaks {}", list.join("; ")))); } } - Ok((next, ordered)) + Ok((next, ordered, broken)) } fn input_keys(kind: &FeatureKind) -> Vec<(String, solidcraft_doc::expr::Kind)> { diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs index 0e99580f3..a25861394 100644 --- a/crates/engine/src/cmd/edit_tests.rs +++ b/crates/engine/src/cmd/edit_tests.rs @@ -589,6 +589,43 @@ fn timeline_moves_across_the_marker_are_one_undo_step() { assert_eq!(s.undo.len(), undo, "a refused move adds no undo step"); } +/// #5962: a reorder that breaks a feature is refused unless `allow_errors`; with it the move is +/// made, the reply lists what broke, and one undo puts the order back. For both reorder commands. +#[test] +fn reorder_with_allow_errors_is_made_and_lists_what_broke() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "name": "Base"})); + run(&mut s, "solid.fillet", json!({"edges": [[10, 5, 10]], "radius": 1, "name": "Round"})); + let order = |s: &Session| s.doc.features.iter().map(|f| f.name.clone()).collect::>(); + let before = s.doc.clone(); + for (cmd, params) in + [("timeline.reorder", json!({"feature": "Round", "position": 0})), ("timeline.reorder_many", json!({"features": ["Round"], "position": 0}))] + { + let error = s.execute(cmd, ¶ms).unwrap_err().to_string(); + assert!(error.contains("breaks Round"), "{cmd}: {error}"); + assert_eq!(s.doc, before, "{cmd}: refused, nothing moved"); + let mut anyway = params.clone(); + anyway["allow_errors"] = json!(true); + let r = run(&mut s, cmd, anyway); + assert_eq!(order(&s), ["Round", "Base"], "{cmd}"); + let broken = r["broken"].as_array().unwrap(); + assert_eq!(broken.len(), 1, "{cmd}: {r}"); + assert_eq!(broken[0]["feature"], "Round", "{cmd}: {r}"); + assert!(broken[0]["error"].as_str().is_some_and(|e| !e.is_empty()), "{cmd}: {r}"); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc, before, "{cmd}: one undo puts the order back"); + } + // A move that breaks nothing says so, and refuses anything but a boolean. + run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [20, 0, 0], "operation": "new", "name": "Other"})); + let r = run(&mut s, "timeline.reorder_many", json!({"features": ["Other"], "position": 0, "allow_errors": true})); + assert_eq!(r["broken"], json!([])); + assert!(s.execute("timeline.reorder", &json!({"feature": "Other", "position": 2, "allow_errors": "yes"})).is_err()); + // `reorder_many` takes `active` too: the moved feature ends up behind the history marker. + run(&mut s, "timeline.reorder_many", json!({"features": ["Other"], "position": 3, "active": false})); + assert_eq!(order(&s).last().map(String::as_str), Some("Other")); + assert_eq!(s.doc.marker, Some(2)); +} + #[test] fn failed_timeline_move_many_keeps_folder_membership_and_order() { let mut s = Session::default();