diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index d83d776c2..481901874 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -518,9 +518,14 @@ 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, false, cmd)?; + let (mut next, ordered, _, checked) = reorder_features(s, next, &moving, slot, false, cmd)?; + let marker = next.marker; place_marker(s, &mut next, &moving, active, cmd)?; + let checked = checked.filter(|_| next.marker == marker); if next != *s.doc { + if let Some(model) = checked { + s.model = model; + } *s.doc_mut() = next; } Ok(json!({"features": ordered, "groups": groups, "group": target, "position": slot})) @@ -1175,8 +1180,15 @@ fn reorder(s: &mut Session, p: &Value) -> Result { let list: Vec = 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)?; 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 + // elsewhere makes them stale. + if next.marker == marker { + s.model = after; + } *s.doc_mut() = next; } let mut out = json!({"feature": id, "position": to}); @@ -1232,9 +1244,15 @@ fn reorder_many(s: &mut Session, p: &Value) -> Result { 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 allow = allow_errors(p, cmd)?; - let (mut next, ordered, broken) = reorder_features(s, (*s.doc).clone(), &ids, slot, allow, cmd)?; + let (mut next, ordered, broken, checked) = reorder_features(s, (*s.doc).clone(), &ids, slot, allow, cmd)?; + let marker = next.marker; place_marker(s, &mut next, &ordered, active, cmd)?; + // The checked results stand only when placing the marker left the design as they were made for. + let checked = checked.filter(|_| next.marker == marker); if next != *s.doc { + if let Some(model) = checked { + s.model = model; + } *s.doc_mut() = next; } let mut out = json!({"features": ordered, "position": slot.min(s.doc.features.len())}); @@ -1244,6 +1262,8 @@ fn reorder_many(s: &mut Session, p: &Value) -> Result { Ok(out) } +/// The reordered design, the moved features in order, and the model the move was checked on +/// (none when the order didn't change), for the caller to keep (#6186). fn reorder_features( s: &Session, mut next: solidcraft_doc::Document, @@ -1251,7 +1271,7 @@ fn reorder_features( slot: usize, allow_errors: bool, cmd: &str, -) -> Result<(solidcraft_doc::Document, Vec, Vec<(String, String)>)> { +) -> Result<(solidcraft_doc::Document, Vec, Vec<(String, String)>, Option)> { let n = next.features.len(); let slot = slot.min(n); let selected: std::collections::BTreeSet = ids.iter().copied().collect(); @@ -1276,17 +1296,19 @@ 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); - 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("; ")))); - } + if unchanged { + return Ok((next, ordered, Vec::new(), None)); } - Ok((next, ordered, broken)) + // From the session's own results: the features before the first one moved are kept, not + // evaluated again from an empty model (#6186). + let mut after = s.model.clone(); + after.evaluate(&next); + let 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, broken, Some(after))) } 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 a25861394..10c675094 100644 --- a/crates/engine/src/cmd/edit_tests.rs +++ b/crates/engine/src/cmd/edit_tests.rs @@ -1009,3 +1009,27 @@ fn renaming_a_plane_keeps_its_projections_canvases_and_hidden_state() { assert_eq!(s.visibility.hidden_origin, ["Rail"]); assert_eq!(plane_link(&s), "Ledge"); } + +/// #6186: a reorder evaluates the design once. The model the move was checked on is the one the +/// session keeps, so setting the reordered design finds every result ready: nothing is +/// recomputed or taken from the cache again. The same for `timeline.reorder_many`, whose check +/// starts from the session's results rather than an empty model. +#[test] +fn a_reorder_keeps_the_model_it_was_checked_on() { + let mut s = Session::default(); + for i in 0..4 { + run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5 + i, "corner": [i * 10, 0, 0], "operation": "new"})); + } + let bodies = |s: &Session| s.model.state().bodies.len(); + run(&mut s, "timeline.reorder", json!({"feature": "Box2", "position": 0})); + assert_eq!((s.model.last_recomputed, s.model.last_restored), (0, 0), "the checked model was kept"); + assert_eq!(s.doc.features.first().map(|f| f.name.as_str()), Some("Box2")); + assert_eq!(bodies(&s), 4); + run(&mut s, "timeline.reorder_many", json!({"features": ["Box4"], "position": 2})); + assert_eq!((s.model.last_recomputed, s.model.last_restored), (0, 0), "the checked model was kept"); + assert_eq!(s.doc.features.get(2).map(|f| f.name.as_str()), Some("Box4")); + // The kept model is the design's: evaluating it from scratch gives the same bodies. + let mut fresh = solidcraft_doc::Model::new(); + fresh.evaluate(&s.doc); + assert_eq!(fresh.state().bodies.len(), bodies(&s)); +}