SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Timeline: reordering a feature says when nothing moved and returns its new index (#12171)
SS-EN1b521865243a
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rsindex 232bfbd8c2..7aaa154bc2 100644--- a/crates/engine/src/cmd/edit.rs+++ b/crates/engine/src/cmd/edit.rs@@ -87,7 +87,7 @@ 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, unless allow_errors?: bool (default false): then made, and broken: [{feature, error}] lists the features it broke"),+ .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 → feature, position: its index now, from: its index before, moved: bool, note?: why nothing moved, or that a position past the end meant the last slot"),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; 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",),@@ -1521,11 +1521,15 @@ fn reorder(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "timeline.reorder";let id = feature_id(s, p.get("feature"), cmd)?;let n = s.doc.features.len();- let to = num(p, "position").map(|x| x.max(0.0) as usize).ok_or_else(|| bad(cmd, "`position` must be a number"))?.min(n.saturating_sub(1));+ let asked = num(p, "position").map(|x| x.max(0.0) as usize).ok_or_else(|| bad(cmd, "`position` must be a number"))?;+ // A position past the end means the last slot; the reply says so (#12140).+ let to = asked.min(n.saturating_sub(1));+ let capped = (asked != to).then(|| format!("position {asked} is past the end, so the last slot, {to}, was used"));let from = s.doc.feature_index(id).ok_or_else(|| bad(cmd, "feature"))?;let active = active_param(p, cmd)?;if from == to && active.is_none() {- return Ok(json!({"feature": id, "position": to}));+ let why = capped.map_or_else(|| format!("it is already at position {to}"), |c| format!("{c}, where it already is"));+ return Ok(json!({"feature": id, "position": to, "from": from, "moved": false, "note": format!("nothing moved: {why}")}));}let mut next = (*s.doc).clone();let f = next.features.remove(from);@@ -1540,11 +1544,27 @@ fn reorder(s: &mut Session, p: &Value) -> Result<Value> {// `allow_errors`: made anyway, with what it broke said (#5962).let allow = allow_errors(p, cmd)?;if !broken.is_empty() && !allow {+ // The moved feature itself failing: say why, and only when it moved earlier suggest that+ // it now comes before something it uses, such as an extrude ahead of its sketch (#12140).+ // Moved later, it fails for another reason, such as a feature that removed its body.+ if let Some((_, error)) = broken.iter().find(|(n, _)| *n == name) {+ let hint = if to < from {+ "; if it uses a feature that would now come after it, move that feature earlier first"+ } else {+ "; a feature it would now come after changes what it works on"+ };+ return Err(bad(cmd, format!("moving {name} to position {to} breaks {name} ({error}){hint}")));+ }let list: Vec<String> = broken.iter().map(|(n, e)| format!("{n} ({e})")).collect();return Err(bad(cmd, format!("moving {name} there breaks {}", list.join("; "))));}let marker = next.marker;place_marker(s, &mut next, &[id], active, cmd)?;+ // Never report a move that didn't happen (#12140): the reply gives where the feature is now.+ let now = next.feature_index(id).ok_or_else(|| bad(cmd, "feature"))?;+ if now != to {+ return Err(bad(cmd, format!("{name} would stay at position {now}, not {to}")));+ }if next != *s.doc {// The check evaluated the new order: keep its results, so setting the design doesn't// evaluate the moved part of the timeline a second time (#6186). Placing the marker@@ -1554,7 +1574,16 @@ fn reorder(s: &mut Session, p: &Value) -> Result<Value> {}*s.doc_mut() = next;}- let mut out = json!({"feature": id, "position": to});+ let mut out = json!({"feature": id, "position": now, "from": from, "moved": now != from});+ // With `active`, a feature left in place may still cross the history marker: say which.+ let stayed = (now == from).then(|| {+ let what = if s.doc.marker == marker { "nothing moved" } else { "only the history marker moved" };+ format!("{what}: {name} is already at position {now}")+ });+ let note: Vec<String> = [capped, stayed].into_iter().flatten().collect();+ if !note.is_empty() {+ out["note"] = json!(note.join("; "));+ }if allow {out["broken"] = broken_json(&broken);}diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rsindex 3d750a7659..5f3fa8a935 100644--- a/crates/engine/src/cmd/edit_tests.rs+++ b/crates/engine/src/cmd/edit_tests.rs@@ -1688,3 +1688,46 @@ fn assistant_toggle_is_refused_headlessly_without_changing_the_document() {let spec = crate::find_command("ui.assistant").unwrap();assert_eq!((spec.shortcut, spec.tab, spec.undoable), (Some("Ctrl+Shift+A"), "", false));}++#[test]+fn reorder_reports_where_the_feature_is_or_why_it_did_not_move() {+ // #12140: a reorder never reports success while the feature stays put without saying so.+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [50, 0, 0], "operation": "new_body", "name": "Block"}));+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Profile"}));+ run(&mut s, "sketch.line", json!({"points": [[0, 0], [10, 0], [10, 10], [0, 10]], "closed": true}));+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.extrude", json!({"distance": 5, "operation": "new_body", "name": "Pad"}));+ let names = |s: &Session| s.doc.features.iter().map(|f| f.name.clone()).collect::<Vec<_>>();+ // A position past the end, where the feature already is: nothing moved, and the note says why.+ let out = run(&mut s, "timeline.reorder", json!({"feature": "Pad", "position": 9}));+ assert_eq!((out["position"].as_u64(), out["from"].as_u64(), out["moved"].as_bool()), (Some(2), Some(2), Some(false)));+ assert!(out["note"].as_str().is_some_and(|n| n.contains("nothing moved") && n.contains("past the end")), "{out}");+ // Ahead of its own sketch: refused, naming the cause.+ let e = s.execute("timeline.reorder", &json!({"feature": "Pad", "position": 0})).unwrap_err().to_string();+ assert!(e.contains("breaks Pad") && e.contains("move that feature earlier first"), "{e}");+ assert_eq!(names(&s), ["Block", "Profile", "Pad"], "a refused move leaves the order");+ // Moving the sketch first lets the extrude follow; the reply gives the real index.+ let out = run(&mut s, "timeline.reorder", json!({"feature": "Profile", "position": 0}));+ assert_eq!((out["position"].as_u64(), out["from"].as_u64(), out["moved"].as_bool()), (Some(0), Some(1), Some(true)));+ let out = run(&mut s, "timeline.reorder", json!({"feature": "Pad", "position": 1}));+ assert_eq!((out["position"].as_u64(), out["moved"].as_bool()), (Some(1), Some(true)));+ assert!(out.get("note").is_none(), "{out}");+ assert_eq!(names(&s), ["Profile", "Pad", "Block"]);+ // `active` on a feature left in place: the note says only the marker moved.+ run(&mut s, "timeline.reorder", json!({"feature": "Block", "position": 2, "active": false}));+ let out = run(&mut s, "timeline.reorder", json!({"feature": "Block", "position": 2, "active": true}));+ assert_eq!((out["moved"].as_bool(), s.doc.marker), (Some(false), None));+ assert!(out["note"].as_str().is_some_and(|n| n.contains("only the history marker moved")), "{out}");+}++#[test]+fn reorder_refusal_after_a_feature_that_removes_its_body_does_not_blame_the_order_of_its_inputs() {+ // A fillet moved behind a cut that removes its whole body fails though what it uses is earlier.+ 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"}));+ run(&mut s, "solid.box", json!({"length": 30, "width": 30, "height": 30, "corner": [-10, -10, -10], "operation": "cut", "name": "Clear"}));+ let e = s.execute("timeline.reorder", &json!({"feature": "Round", "position": 2})).unwrap_err().to_string();+ assert!(e.contains("breaks Round") && e.contains("changes what it works on") && !e.contains("earlier first"), "{e}");+}