🐛 Advance a pasted copy to its component's main by its content refs

Pasting a copy held in a swapped head inside a copy inside a main left
an invalid file (`:ref-not-in-near-main`). The clipboard advanced each
shape by the number of copy levels it leaves, but the swap above makes
its references reach the component's main one step sooner, so its nested
copies overshot past that main. Users reach it by copying such a copy
and pasting it on the page (F591).

Every copy the clipboard advances now refers each shape to its
counterpart in the main of its component: the first of its content
references there, which a swapped head below follows by its slot. It
keeps the overrides of the references it skips. The level count stays
only as the fallback when a shape finds no counterpart. This generalizes
the case of a swapped reference among those it skips (F590).

Found by a fresh gate round (seed 20261117, variant property).

Test (swap-group-test, fails before the fix):
`paste-out-of-a-swapped-head-reaches-the-nested-copies-of-its-component`.

Benchmark (bench-components, alone):

|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |  21.742 |  22.873 |   0.62 |
| edit-through-a-chain-of-dependents |    68.931 |  46.657 |  47.664 |   0.68 |
|         library-update-into-copies |    83.083 |  52.163 |  53.121 |   0.63 |
|        main-edit-among-many-shapes |     4.837 |    2.34 |   2.519 |   0.48 |
|           main-edit-into-10-copies |      1.79 |   1.103 |   1.136 |   0.62 |
|          main-edit-into-100-copies |    15.415 |  10.677 |  11.348 |   0.69 |
|         main-edit-into-1000-copies |   153.086 | 108.821 | 113.548 |   0.71 |
|         variant-switch-into-copies |    32.052 |   21.95 |  22.607 |   0.68 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-18 00:12:59 +01:00
parent 51fa5e5a00
commit f1f5ed862a
2 changed files with 78 additions and 26 deletions

View File

@ -469,34 +469,43 @@
(assoc % :touched touched)
(dissoc % :touched)))))
objects)))
;; a copy whose references skip a swapped head shows that head's
;; component: each shape refers to its counterpart in the main of the
;; component, the first of its content references there, and keeps
;; the overrides of the references it skips
;; a copy out of its copy levels shows its component: each shape refers
;; to its counterpart in the main of the component, the first of its
;; content references there, and keeps the overrides of the references
;; it skips. Counting levels instead would miss when a swapped head, its
;; own or one around it, makes its references skip another number.
content-reheaded
(fn [objects shape level-delta]
(let [chain (->> (ctf/get-ref-chain-until-target-ref container libraries shape nil)
rest
(take level-delta))]
(when (some ctk/swapped? chain)
(when-let [[file-data component] (ctf/head-component file libraries shape :include-deleted? true)]
(let [main-ids (into #{} (map :id) (ctf/get-component-shapes file-data component))
unchanged-ids (cli/swapped-descendant-ids objects (:id shape))]
(reduce
(fn [objects child]
(let [chain (rest (ctf/get-ref-chain-until-target-ref container libraries child nil))
[skipped [target]] (split-with #(not (contains? main-ids (:id %))) chain)]
(if (some? target)
(let [touched (-> (into (or (:touched child) #{}) (mapcat :touched) skipped)
(disj :swap-group))]
(update objects (:id child)
#(cond-> (assoc % :shape-ref (:id target))
(seq touched) (assoc :touched touched)
(empty? touched) (dissoc :touched))))
objects)))
objects
(->> (cfh/get-children-with-self objects (:id shape))
(remove #(contains? unchanged-ids (:id %))))))))))
(when-let [[file-data component] (ctf/head-component file libraries shape :include-deleted? true)]
(let [main-ids (into #{} (map :id) (ctf/get-component-shapes file-data component))
unchanged-ids (cli/swapped-descendant-ids objects (:id shape))
;; a swapped head below follows its slot, not its own component
positional (fn [child]
(->> (ctf/find-ref-shape file container libraries child
:include-deleted? true :with-context? true)
(iterate #(when (:shape-ref %)
(ctf/find-ref-shape file (:container (meta %)) libraries %
:include-deleted? true :with-context? true)))
(take-while some?)
(take 16)))]
(reduce
(fn [objects child]
(let [chain (if (and (ctk/swapped? child) (not= (:id child) (:id shape)))
(positional child)
(rest (ctf/get-ref-chain-until-target-ref container libraries child nil)))
[skipped [target]] (split-with #(not (contains? main-ids (:id %))) chain)]
(if (some? target)
(let [touched (-> (into (or (:touched child) #{})
(cli/inherited-touched child (into #{} (mapcat :touched) skipped)))
(cond-> (= (:id child) (:id shape)) (disj :swap-group)))]
(update objects (:id child)
#(cond-> (assoc % :shape-ref (:id target))
(seq touched) (assoc :touched touched)
(empty? touched) (dissoc :touched))))
(advance-shape level-delta objects child))))
objects
(->> (cfh/get-children-with-self objects (:id shape))
(remove #(contains? unchanged-ids (:id %))))))))
detach-copy (fn [objects shape]
(reduce #(update %1 %2 ctk/detach-shape)
objects

View File

@ -416,6 +416,49 @@
(t/is (not (ctk/swapped? head)))
(t/is (= (thi/id :badge-dot) (:shape-ref dot)))))
(t/deftest paste-out-of-a-swapped-head-reaches-the-nested-copies-of-its-component
;; {:dot-main} [:name Frame1] # [Component :dot]
;; :dot-heart [:name Frame1] @--> [Component :heart] :heart-main
;; {:badge-main} [:name Frame1] # [Component :badge]
;; :badge-dot [:name Frame1] @--> [Component :dot] :dot-main
;; {:page-main} [:name Frame1] # [Component :page]
;; :inner-copy [:name Frame1] @--> [Component :card] :card-main
;; :inner-star [:name Frame1] @--> [Component :star] :card-star
;; :outer-copy [:name Frame1] #--> [Component :page] :page-main
;; :outer-inner [:name Frame1] @--> [Component :card] :inner-copy
;; :outer-star [:name Frame1] @--> [Component :badge] :inner-star (swapped)
;; <dot> @--> [Component :dot] :badge-dot
;; <heart> @--> [Component :heart] (dot-heart, via badge-dot)
;; the dot pasted out of its copy leaves three copy levels, but the swap makes
;; its references reach the main of :dot in two: its heart refers to the
;; heart of that main, not further up to the main of :heart
(let [file (-> (setup)
(tho/add-frame :dot-main)
(thc/instantiate-component :heart :dot-heart :parent-label :dot-main)
(thc/make-component :dot :dot-main)
(tho/add-frame :badge-main)
(thc/instantiate-component :dot :badge-dot :parent-label :badge-main)
(thc/make-component :badge :badge-main)
(tho/add-frame :page-main)
(thc/instantiate-component :card :inner-copy
:parent-label :page-main
:children-labels [:inner-star])
(thc/make-component :page :page-main)
(thc/instantiate-component :page :outer-copy
:children-labels [:outer-inner :outer-star])
(tho/swap-component-in-shape :outer-star :badge)
(thf/validate-file!))
dot (first-child file :outer-star)
heart (ths/get-shape-by-id file (first (:shapes dot)))
page (thf/current-page file)
objects (cld/advance-copied-objects file page {(:id file) file}
(select-keys (:objects page)
(cfh/get-children-ids-with-self (:objects page) (:id dot)))
[(:id dot)])]
(t/is (= (thi/id :dot-main) (:shape-ref (get objects (:id dot)))))
(t/is (= (thi/id :dot-heart) (:shape-ref (get objects (:id heart)))))))
(t/deftest duplicate-keeps-the-swap
(let [file (-> (setup)
(tho/duplicate-shape :copy)