From b6816ffff22abd1695539b3a5acea765f0625c03 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 9 Nov 2026 10:12:40 +0100 Subject: [PATCH] :bug: Drop a switched swap that holds a copy of a component it would be in A switch kept a swap whose subtree holds a copy of a nested copy's component of the new variant, the copy the swap goes into. The move of the kept swap was refused and it stayed at the page root. A switch now keeps a swap only when it holds no copy of a component of the shapes it would be inside: the ancestors of its place in the new variant, and those of the copy. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 19.637 | 20.776 | 0.56 | | edit-through-a-chain-of-dependents | 68.931 | 40.27 | 42.936 | 0.58 | | library-update-into-copies | 83.083 | 52.234 | 53.028 | 0.63 | | main-edit-among-many-shapes | 4.837 | 2.105 | 2.231 | 0.44 | | main-edit-into-10-copies | 1.79 | 1.079 | 1.11 | 0.6 | | main-edit-into-100-copies | 15.415 | 10.333 | 10.617 | 0.67 | | main-edit-into-1000-copies | 153.086 | 97.413 | 103.833 | 0.64 | | variant-switch-into-copies | 32.052 | 22.451 | 23.0 | 0.7 | Common suite: 2379 tests, 1 failure, the known insufficient-random-coverage of random-stale-library-compositions (detach attempted once in 20 programs). AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/variants.cljc | 36 ++++++++++--------- .../logic/composable_gen_test.cljc | 9 +++++ .../logic/variants_switch_test.cljc | 25 +++++++++++++ 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 4b7f3c0aae..22316a0dbc 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -339,14 +339,12 @@ (some ctk/swapped?))) (defn- nests-in-switch? - "Whether the swapped shape `swapped` of `objects` holds a copy of the - component `component-id` a switch of the copy head `head` goes to, or of - a component the head is inside, so keeping it would nest a component in - itself." - [objects swapped head component-id] + "Whether the swapped shape `swapped` of `objects` holds a copy of a + component of `holders`, the shapes a switch would place it inside, so + keeping it would nest a component in itself." + [objects swapped holders] (cfh/components-nesting-loop? (cfh/get-children-with-self objects (:id swapped)) - (cons {:component-id component-id} - (cfh/get-parents objects (:id head))))) + holders)) (defn- find-shape-ref-child-of "Get the shape referenced by the shape-ref of the near main of the shape, @@ -531,8 +529,8 @@ (not swapped-in-chain?) (or (not swapped?) (and (ctk/instance-head? new) - (not (nests-in-switch? page-objects old original-shape - (:component-id new-shape)))))) + (not (nests-in-switch? page-objects old + (cfh/get-parents objects (:id new))))))) {:old old :old-ref old-ref :new new :swapped? swapped?}))) orig-touched) @@ -696,18 +694,24 @@ component (ctkl/get-component ldata component-id) main (ctf/get-component-root ldata component) main-objects (:objects (ctf/get-component-container ldata component)) - ;; the paths of the new main where it holds a copy - new-paths (into #{} (comp (filter ctk/instance-head?) (map :shape-path)) + main-ids (into #{} (map :id) (cfh/get-children-with-self main-objects (:id main))) + ;; the copies the new main holds, by path + new-copies (into {} (comp (filter ctk/instance-head?) (map (juxt :shape-path identity))) (add-unique-path (reverse (cfh/get-children-with-self main-objects (:id main))) - main-objects (:id main)))] + main-objects (:id main))) + ;; the shapes a swap that takes the place of `place` is inside: the + ;; ancestors of `place` in the new main, and those of the head + holders (fn [place] + (concat (filter #(contains? main-ids (:id %)) (cfh/get-parents main-objects (:id place))) + (cfh/get-parents objects (:id shape))))] (into #{} (comp (filter ctk/swapped?) (remove #(child-of-swapped? % objects (:id shape))) - (remove #(nests-in-switch? objects % shape component-id)) (filter (fn [old] - (let [old-ref (find-shape-ref-child-of container libraries old (:id base-ref))] - (and (some? old-ref) - (contains? new-paths (get ref-paths (:id old-ref))))))) + (let [old-ref (find-shape-ref-child-of container libraries old (:id base-ref)) + place (when old-ref (get new-copies (get ref-paths (:id old-ref))))] + (and (some? place) + (not (nests-in-switch? objects old (holders place))))))) (map :id)) (rest (cfh/get-children-with-self objects (:id shape)))))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 1ccbe53e5e..ef93e20bea 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1936,6 +1936,15 @@ [:transform 12 0 0 18] [:add-variant 8] [:switch 0] [:add-variant 11 0 0 0] [:resize 25 0 0 0] [:style 22 9 1]]})))) +(t/deftest switching-a-copy-drops-a-swap-holding-a-copy-it-would-be-inside + (t/is (map? (cg/run {:world [3 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 [[:nest 18 0 0 0] [:swap 0 0 0 0] [:switch 0 0 0 0] [:add-variant 0 0 0 0] + [:combine 17 0 0 0] [:make-variant 2 0 0 0] [:make-variant 6 0 0 0] + [:swap 12 0 0 0] [:make-variant 4] [:switch 9] [:switch 16 0 0 0] + [:combine 7] [:switch 11]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 2a46ea93e7..598763316b 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -3638,6 +3638,31 @@ (t/is (= (:id (ths/get-shape file :m02-icon)) (:shape-ref icon'))) (t/is (nil? (ths/get-shape file' :copy-icon-c02))))) +(t/deftest test-switch-drops-a-swap-that-holds-a-copy-it-would-be-inside + (let [;; ==== Setup: inside the copy's :mid, its :inner is swapped for a + ;; :holder, which holds a copy of :mid + file (-> (thf/sample-file :file1) + (tho/add-nested-component :inner :inner-root :inner-child :mid :mid-root :mid-inner) + (tho/add-frame :holder-root) + (thc/instantiate-component :mid :holder-mid :parent-label :holder-root) + (thc/make-component :holder :holder-root) + (thv/add-variant-with-copy :v01 :c01 :m01 :c02 :m02 :m01-mid :m02-mid :mid) + (thc/instantiate-component :c01 :copy01 :children-labels [:copy-mid])) + _ (thi/set-id! :copy-mid-inner (first (:shapes (ths/get-shape file :copy-mid)))) + file (thc/component-swap file :copy-mid-inner :holder :copy-holder) + + ;; ==== Action + file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 + :keep-touched? true}) + mid' (ths/get-shape-by-id file' (-> (ths/get-shape file' :copy02) :shapes first)) + inner' (ths/get-shape-by-id file' (-> mid' :shapes first)) + m02-mid (ths/get-shape file :m02-mid)] + + ;; ==== Check: a copy of :mid cannot hold the :holder, which holds another + (t/is (= 1 (count (:shapes mid')))) + (t/is (= (first (:shapes m02-mid)) (:shape-ref inner'))) + (t/is (nil? (ths/get-shape file' :copy-holder))))) + (defn- resize-width [shape width] (gsh/transform-shape shape (ctm/change-size shape width nil)))