🐛 Keep a variant's name when its main is updated from a renamed copy

Updating a main from a copy with a name override renamed the component
and its main to the copy's name. A variant takes its name from its
set, though, and every member carries it, so a renamed copy of a
variant left the file failing validation (a variant main and component
with a name other than their set's). Updating the main of a variant
copy now pushes everything but the name, and the copy keeps its name as
an override. The component model and its update-main check expect the
same.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  20.618 |  0.59 |
| edit-through-a-chain-of-dependents |   68.931 |  40.714 |  0.59 |
| library-update-into-copies         |   83.083 |  49.993 |  0.60 |
| main-edit-among-many-shapes        |    4.837 |   2.112 |  0.44 |
| main-edit-into-10-copies           |    1.790 |   1.105 |  0.62 |
| main-edit-into-100-copies          |   15.415 |   9.427 |  0.61 |
| main-edit-into-1000-copies         |  153.086 | 101.540 |  0.66 |
| variant-switch-into-copies         |   32.052 |  21.918 |  0.68 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-08 10:53:03 +01:00
parent 4cf38cfad1
commit 0ca00b7286
5 changed files with 78 additions and 23 deletions

View File

@ -787,8 +787,11 @@
component (when-not (update-main-nesting-loop? libraries container shape-id)
(ctkl/get-component library (:component-id shape-inst)))
;; a variant takes its name from its set, so the copy keeps its own
keep-name? (ctk/is-variant? component)
changes (cond-> changes
(and component (contains? (:touched shape-inst) :name-group))
(and component (not keep-name?) (contains? (:touched shape-inst) :name-group))
(generate-rename-component (:component-id shape-inst) (:name shape-inst) library))]
(if component
@ -803,6 +806,7 @@
:libraries libraries
:root-inst shape-inst
:root-main root-main
:keep-name? keep-name?
;; An orientation the copy root inherits places it
;; and is not pushed into the main. Sync composes a
;; root with its parent's turn; a top copy root that
@ -857,29 +861,37 @@
its touched groups move there (the copy keeps a swap). A geometry that
sync kept for the copy (`:kept?`) is not the copy's, so the main keeps
its own."
[changes {:keys [container main-container root-inst root-main remote-synced inherited-turn kept?]} shape-inst shape-main]
(cond-> (-> changes
(update-attrs shape-main
(cond-> shape-inst
(some? inherited-turn) inherited-turn
kept? (with-main-geometry shape-main
(cond-> root-inst (some? inherited-turn) inherited-turn)
root-main))
root-main
(cond-> root-inst (some? inherited-turn) inherited-turn)
main-container false)
;; the copy keeps its swap, and the main takes the copy's overrides
(set-touched shape-inst container (when (ctk/swapped? shape-inst) #{:swap-group}))
;; A previous update is not a reset of the main's authored
;; overrides. Repeating it must retain the groups it pushed.
(set-touched shape-main main-container
(set/union (:touched shape-main) (:touched shape-inst))))
(ctsl/any-layout? shape-main)
(update-layout-children main-container shape-main shape-inst
#(ctf/get-shape-in-copy container % shape-inst) false)
[changes {:keys [container main-container root-inst root-main remote-synced inherited-turn kept? keep-name?]} shape-inst shape-main]
(let [;; the root of a variant keeps its name, and the copy its override of it
keep-name? (and keep-name? (= (:id shape-inst) (:id root-inst)))
inst-touched (cond-> (:touched shape-inst) keep-name? (disj :name-group))]
(cond-> (-> changes
(update-attrs shape-main
(cond-> shape-inst
keep-name? (assoc :name (:name shape-main))
(some? inherited-turn) inherited-turn
kept? (with-main-geometry shape-main
(cond-> root-inst (some? inherited-turn) inherited-turn)
root-main))
root-main
(cond-> root-inst (some? inherited-turn) inherited-turn)
main-container false)
;; the copy keeps its swap, and the main takes the copy's overrides
(set-touched shape-inst container
(not-empty (cond-> #{}
(ctk/swapped? shape-inst) (conj :swap-group)
(and keep-name? (ctk/touched-group? shape-inst :name-group))
(conj :name-group))))
;; A previous update is not a reset of the main's authored
;; overrides. Repeating it must retain the groups it pushed.
(set-touched shape-main main-container
(set/union (:touched shape-main) inst-touched)))
(ctsl/any-layout? shape-main)
(update-layout-children main-container shape-main shape-inst
#(ctf/get-shape-in-copy container % shape-inst) false)
:always
(change-remote-synced shape-inst container remote-synced)))
:always
(change-remote-synced shape-inst container remote-synced))))
(defn- update-main-recursive
"Push the copy shape `shape-inst` into `shape-main`, the main shape it

View File

@ -530,6 +530,11 @@
(not= (or (:content-scale source) 1) (or (:content-scale kept) 1)))
(mismatch id group (shadow/values source group) (shadow/values kept group))))
(and (= group :name-group) (= id root-id) (:variant-id target))
;; a variant main keeps its set's name, and the copy root its own
(when-not (= (:name source) (:name kept))
(mismatch id group (:name source) (:name kept)))
(= group :swap-group)
(when-not (= (select-keys source [:component-id :component-file])
(select-keys target [:component-id :component-file]))

View File

@ -477,10 +477,15 @@
(in-orientation geometry root main-root)
(in-orientation geometry (get before (:parent-id shape))
(get before (:parent-id ref)))))))
;; a variant main keeps its set's name, and the copy root its own
kept-name (when (and (= id root-id) (:variant-id ref)) (:name-group groups))
groups (cond-> groups kept-name (dissoc :name-group))
result (cond-> (dissoc result id)
(and (contains? groups :swap-group)
(not= (:component-id updated) (:component-id ref)))
(assoc id {:swap-group {}})
kept-name
(assoc-in [id :name-group] kept-name)
(and (:shape-ref ref) (seq groups))
(update (:id ref) merge
(cond-> (grid/remap-groups groups (grid/get-child-map after shape ref))

View File

@ -1800,6 +1800,13 @@
[:combine 13 0 0 0] [:variant-property 0 0 0 0] [:switch 13] [:make-variant 0]
[:switch 0] [:make-variant 2 0 0 0] [:make-variant 0] [:switch 16]]}))))
(t/deftest updating-a-variant-main-from-a-renamed-copy-keeps-the-variant-name
(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 [[:duplicate 0 0 0 0] [:edit 2 2 0 1] [:combine 0] [:switch 0]
[:update-main 0 0 0 0]]}))))
(t/deftest random-variant-compositions
(let [coverage (atom {})
failure (atom nil)]

View File

@ -10,6 +10,7 @@
[app.common.geom.point :as gpt]
[app.common.logic.duplicate :as cld]
[app.common.logic.instances :as cli]
[app.common.logic.libraries :as cll]
[app.common.logic.shapes :as cls]
[app.common.logic.variant-properties :as clvp]
[app.common.logic.variants :as clv]
@ -21,6 +22,7 @@
[app.common.test-helpers.shapes :as ths]
[app.common.test-helpers.variants :as thv]
[app.common.types.component :as ctk]
[app.common.types.container :as ctn]
[app.common.types.variant :as ctv]
[app.common.uuid :as uuid]
[clojure.test :as t]
@ -823,3 +825,27 @@
(cpn/merge-path-item (:path comp2') (:name comp2'))))
(t/is (some? (:variant-id comp1')))
(t/is (some? (:variant-id comp2')))))
(t/deftest test-update-main-from-a-renamed-copy-keeps-the-variant-name
;; a variant's name is its set's, so an update main from a renamed copy
;; leaves it and the copy keeps its name as an override
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
(thc/instantiate-component :c01 :copy01))
page (thf/current-page file)
file (thf/apply-changes file (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
#{(thi/id :copy01)}
#(assoc % :name "Label 0")
(:objects page)
{}))
container (ctn/make-container (thf/current-page file) :page)
file' (thf/apply-changes file (-> (pcb/empty-changes)
(pcb/with-container container)
(cll/generate-sync-shape-inverse {(:id file) file} container
(thi/id :copy01))))
copy01' (ths/get-shape file' :copy01)]
(t/is (= (:name (ths/get-shape file :m01)) (:name (ths/get-shape file' :m01))))
(t/is (= (:name (thc/get-component file :c01)) (:name (thc/get-component file' :c01))))
(t/is (= "Label 0" (:name copy01')))
(t/is (ctk/touched-group? copy01' :name-group))
(t/is (some? (thf/validate-file! file')))))