🐛 End the swap of a nested swapped shape a kept swap points at its component

A switch that keeps a copy's swapped child in place of the new copy's
child makes it a plain copy of its new position when both have the same
component, and points its descendants at the position's descendants. A
swapped shape among those descendants kept its swap though its new
reference had its own component, as a plain copy of it does: a copy
switched to a variant whose nested board its main had swapped to that
same component stayed marked as swapped, while the variant's other
copies were not. Such a descendant now drops its swap as well.

Test: switching-a-copy-takes-the-swap-of-a-nested-board-from-its-main
(the variant program of the common suite's seed 1794464684740).

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):
|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |   21.57 |  22.621 |   0.61 |
| edit-through-a-chain-of-dependents |    68.931 |   43.16 |  45.931 |   0.63 |
|         library-update-into-copies |    83.083 |  53.172 |  58.967 |   0.64 |
|        main-edit-among-many-shapes |     4.837 |   2.439 |    2.93 |    0.5 |
|           main-edit-into-10-copies |      1.79 |    1.12 |    1.22 |   0.63 |
|          main-edit-into-100-copies |    15.415 |  10.261 |  11.179 |   0.67 |
|         main-edit-into-1000-copies |   153.086 | 102.918 | 110.051 |   0.67 |
|         variant-switch-into-copies |    32.052 |  23.136 |  24.466 |   0.72 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-12 10:38:18 +01:00
parent 4fba88d567
commit e766345cd0
2 changed files with 25 additions and 3 deletions

View File

@ -476,7 +476,9 @@
`position` is that shape, found with context, or nil when it cannot be
reached. The head refers to it, and is swapped while its component differs
from the one of `position`; otherwise it is a plain copy of `position`, and
its descendants refer to the descendants of `position`."
its descendants refer to the descendants of `position`. A swapped
descendant that comes to refer to a shape of its own component is a plain
copy of it."
[file libraries objects head ref-id position]
(let [swapped? (or (nil? position)
(not= (:component-id position) (:component-id head))
@ -486,7 +488,12 @@
(keep #(:shape-ref (get objects %)))
(cfh/get-children-ids objects (:id head))))
ref-map (when (seq main-ids)
(descendant-refs file libraries position main-ids))]
(descendant-refs file libraries position main-ids))
;; the shapes of the subtree of `position`
position-objects (:objects (:container (meta position)))
same-component? (fn [shape ref]
(and (= (:component-id ref) (:component-id shape))
(= (:component-file ref) (:component-file shape))))]
(fn [shape]
(if (= (:id shape) (:id head))
(if swapped?
@ -494,7 +501,14 @@
(update :touched (fnil conj #{}) :swap-group))
(-> (assoc shape :shape-ref ref-id)
(ctk/remove-swap-group)))
(d/update-when shape :shape-ref #(get ref-map % %))))))
(let [shape-ref (get ref-map (:shape-ref shape))]
(cond-> shape
(some? shape-ref)
(assoc :shape-ref shape-ref)
(and (some? shape-ref) (ctk/swapped? shape)
(same-component? shape (get position-objects shape-ref)))
(ctk/remove-swap-group)))))))
(defn ref-outside-near-main?
"Whether `shape` refers to a main shape that is not a child of the main

View File

@ -2394,6 +2394,14 @@
:ops [[:delete] [:restore] [:combine 21] [:swap 24] [:switch 27] [:rotate 14] [:switch 11]
[:combine 11] [:switch 6] [:switch 5] [:edit 16] [:edit 0 0 0 1] [:update-main 24]]}))))
(t/deftest switching-a-copy-takes-the-swap-of-a-nested-board-from-its-main
(t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false
:layout-item-margins false :grid-square false :rich-variants true
:layout-children false :grid-empty false :layout-copies true :text true}]
:ops [[:delete 0 0 0 0] [:combine 6 0 0 0] [:add-variant 0] [:switch 1] [:add-variant 9 0 0 0]
[:combine 5] [:switch 0] [:variant-property 0 0 0 0] [:switch 1] [:combine 0] [:switch 4]
[:combine 0] [:switch 0]]}))))
(t/deftest random-shape-structure-compositions
(let [coverage (atom {})
failure (atom nil)]