Rename Body: a pasted placement's body renames by the name the browser shows (#10017) (#10026)

* Rename Body: a pasted placement's body renames by the name the browser shows (#10017)

The browser lists the bodies of a component's second placement by their
placed names (`KB (K:2)`) and the context menu's Rename sends that name,
but body.rename looked it up only among the model's own bodies and
refused it ("no body"). It now takes a placed copy's name as the body it
shows, as appearance.assign does (#9740): the body is renamed in its
component, so every placement shows the new name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Rename Body: test renaming a pasted placement's body by its placed name (#10017)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Rename Body: the test also hides the copy first and undoes the rename (#10017)

A copy hidden by its placed name stays hidden under its new name, and
one undo brings both names back (checks from the review on #10026).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
semmelrinde
2026-10-31 13:48:14 +00:00
committed by GitHub
parent 24ce89e0fc
commit 59efc86826
+30 -1
View File
@@ -1013,7 +1013,11 @@ pub(crate) fn counted(f: &solidcraft_doc::Feature, i: usize) -> bool {
fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "body.rename";
let old = str_(p, "body").ok_or_else(|| bad(cmd, "`body` must be a body name"))?.to_string();
let given = str_(p, "body").ok_or_else(|| bad(cmd, "`body` must be a body name"))?;
// A placed copy's name (`KB (K:2)`, the name the browser lists a pasted placement's body by)
// means the body it shows, as appearance.assign takes it (#9740): renaming it renames that body,
// in every placement of its component (#10017).
let old = s.own_body_name(given).ok_or_else(|| bad(cmd, format!("no body `{given}`")))?;
let name =
str_(p, "name").map(str::trim).filter(|n| !n.is_empty() && n.len() <= 128).ok_or_else(|| bad(cmd, "`name` must be 1…128 characters"))?;
if name == old {
@@ -1269,6 +1273,31 @@ mod tests {
s.model.state().bodies.iter().map(|b| b.name.clone()).collect()
}
/// #10017: a pasted placement's body is listed by its placed name (`KB (K:2)`), and Rename
/// sends that name. It renames the body that placement shows, so both placements show the new
/// name; a name that is neither a body's nor a placed copy's is still refused.
#[test]
fn a_placed_copys_body_renames_by_its_placed_name() {
let mut s = Session::default();
run(&mut s, "component.create", json!({"name": "K"}));
run(&mut s, "solid.box", json!({"body_name": "KB", "length": 10, "width": 10, "height": 10}));
run(&mut s, "occurrence.copy", json!({"component": "K", "translate": [30, 0, 0]}));
// The copy hidden first stays hidden under its new name, and one undo brings both names
// back.
run(&mut s, "browser.visibility", json!({"items": ["KB (K:2)"], "visible": false}));
let r = run(&mut s, "body.rename", json!({"body": "KB (K:2)", "name": "Lid"}));
assert_eq!((r["body"].as_str(), r["name"].as_str()), (Some("KB"), Some("Lid")), "{r}");
let world = |s: &Session| s.world_state().bodies.iter().map(|b| b.name.clone()).collect::<Vec<_>>();
let renamed = world(&s);
assert!(renamed.iter().any(|n| n == "Lid") && renamed.iter().any(|n| n == "Lid (K:2)"), "{renamed:?}");
assert!(!renamed.iter().any(|n| n.starts_with("KB")), "{renamed:?}");
assert!(!s.body_visible("Lid (K:2)") && s.body_visible("Lid"), "{:?}", s.visibility.hidden_bodies);
assert!(s.execute("body.rename", &json!({"body": "Lid (K:9)", "name": "Other"})).is_err());
run(&mut s, "edit.undo", json!({}));
let back = world(&s);
assert!(back.iter().any(|n| n == "KB") && back.iter().any(|n| n == "KB (K:2)"), "{back:?}");
}
#[test]
fn body_rename_preserves_plastic_rules_and_current_visibility_through_history() {
let mut s = Session::default();