🔧 Make small improvements from PR comments

This commit is contained in:
Andrés Moya
2025-09-29 11:44:27 +02:00
committed by Andrés Moya
parent 4c35571336
commit 9ad8d3fd08
8 changed files with 55 additions and 65 deletions

View File

@@ -376,19 +376,19 @@
[:type [:= :set-token]] [:type [:= :set-token]]
[:set-id ::sm/uuid] [:set-id ::sm/uuid]
[:token-id ::sm/uuid] [:token-id ::sm/uuid]
[:token [:maybe ctob/schema:token-attrs]]]] [:attrs [:maybe ctob/schema:token-attrs]]]]
[:set-token-set [:set-token-set
[:map {:title "SetTokenSetChange"} [:map {:title "SetTokenSetChange"}
[:type [:= :set-token-set]] [:type [:= :set-token-set]]
[:id ::sm/uuid] [:id ::sm/uuid]
[:token-set [:maybe ctob/schema:token-set-attrs]]]] [:attrs [:maybe ctob/schema:token-set-attrs]]]]
[:set-token-theme [:set-token-theme
[:map {:title "SetTokenThemeChange"} [:map {:title "SetTokenThemeChange"}
[:type [:= :set-token-theme]] [:type [:= :set-token-theme]]
[:id ::sm/uuid] [:id ::sm/uuid]
[:theme [:maybe ctob/schema:token-theme-attrs]]]] [:attrs [:maybe ctob/schema:token-theme-attrs]]]]
[:set-active-token-themes [:set-active-token-themes
[:map {:title "SetActiveTokenThemes"} [:map {:title "SetActiveTokenThemes"}
@@ -976,54 +976,54 @@
(assoc data :tokens-lib tokens-lib)) (assoc data :tokens-lib tokens-lib))
(defmethod process-change :set-token (defmethod process-change :set-token
[data {:keys [set-id token-id token]}] [data {:keys [set-id token-id attrs]}]
(update data :tokens-lib (update data :tokens-lib
(fn [lib] (fn [lib]
(let [lib' (ctob/ensure-tokens-lib lib)] (let [lib' (ctob/ensure-tokens-lib lib)]
(cond (cond
(not token) (not attrs)
(ctob/delete-token lib' set-id token-id) (ctob/delete-token lib' set-id token-id)
(not (ctob/get-token lib' set-id token-id)) (not (ctob/get-token lib' set-id token-id))
(ctob/add-token lib' set-id (ctob/make-token token)) (ctob/add-token lib' set-id (ctob/make-token attrs))
:else :else
(ctob/update-token lib' set-id token-id (ctob/update-token lib' set-id token-id
(fn [prev-token] (fn [prev-token]
(ctob/make-token (merge prev-token token))))))))) (ctob/make-token (merge prev-token attrs)))))))))
(defmethod process-change :set-token-set (defmethod process-change :set-token-set
[data {:keys [id token-set]}] [data {:keys [id attrs]}]
(update data :tokens-lib (update data :tokens-lib
(fn [lib] (fn [lib]
(let [lib' (ctob/ensure-tokens-lib lib)] (let [lib' (ctob/ensure-tokens-lib lib)]
(cond (cond
(not token-set) (not attrs)
(ctob/delete-set lib' id) (ctob/delete-set lib' id)
(not (ctob/get-set lib' id)) (not (ctob/get-set lib' id))
(ctob/add-set lib' (ctob/make-token-set token-set)) (ctob/add-set lib' (ctob/make-token-set attrs))
:else :else
(ctob/update-set lib' id (fn [_] (ctob/make-token-set token-set)))))))) (ctob/update-set lib' id (fn [_] (ctob/make-token-set attrs))))))))
(defmethod process-change :set-token-theme (defmethod process-change :set-token-theme
[data {:keys [id theme]}] [data {:keys [id attrs]}]
(update data :tokens-lib (update data :tokens-lib
(fn [lib] (fn [lib]
(let [lib' (ctob/ensure-tokens-lib lib)] (let [lib' (ctob/ensure-tokens-lib lib)]
(cond (cond
(not theme) (not attrs)
(ctob/delete-theme lib' id) (ctob/delete-theme lib' id)
(not (ctob/get-theme lib' id)) (not (ctob/get-theme lib' id))
(ctob/add-theme lib' (ctob/make-token-theme theme)) (ctob/add-theme lib' (ctob/make-token-theme attrs))
:else :else
(ctob/update-theme lib' (ctob/update-theme lib'
id id
(fn [prev-token-theme] (fn [prev-token-theme]
(ctob/make-token-theme (merge prev-token-theme theme))))))))) (ctob/make-token-theme (merge prev-token-theme attrs)))))))))
(defmethod process-change :set-active-token-themes (defmethod process-change :set-active-token-themes
[data {:keys [theme-paths]}] [data {:keys [theme-paths]}]

View File

@@ -950,11 +950,11 @@
(update :redo-changes conj {:type :set-token (update :redo-changes conj {:type :set-token
:set-id set-id :set-id set-id
:token-id token-id :token-id token-id
:token token}) :attrs (datafy token)})
(update :undo-changes conj {:type :set-token (update :undo-changes conj {:type :set-token
:set-id set-id :set-id set-id
:token-id token-id :token-id token-id
:token prev-token}) :attrs (datafy prev-token)})
(apply-changes-local)))) (apply-changes-local))))
(defn set-token-set (defn set-token-set
@@ -966,10 +966,10 @@
(-> changes (-> changes
(update :redo-changes conj {:type :set-token-set (update :redo-changes conj {:type :set-token-set
:id id :id id
:token-set (datafy token-set)}) :attrs (datafy token-set)})
(update :undo-changes conj {:type :set-token-set (update :undo-changes conj {:type :set-token-set
:id id :id id
:token-set (datafy prev-token-set)}) :attrs (datafy prev-token-set)})
(apply-changes-local)))) (apply-changes-local))))
(defn rename-token-set (defn rename-token-set
@@ -981,10 +981,10 @@
(-> changes (-> changes
(update :redo-changes conj {:type :set-token-set (update :redo-changes conj {:type :set-token-set
:id id :id id
:token-set (datafy (ctob/rename prev-token-set new-name))}) :attrs (datafy (ctob/rename prev-token-set new-name))})
(update :undo-changes conj {:type :set-token-set (update :undo-changes conj {:type :set-token-set
:id id :id id
:token-set (datafy prev-token-set)}) :attrs (datafy prev-token-set)})
(apply-changes-local)))) (apply-changes-local))))
(defn set-token-theme [changes id theme] (defn set-token-theme [changes id theme]
@@ -995,10 +995,10 @@
(-> changes (-> changes
(update :redo-changes conj {:type :set-token-theme (update :redo-changes conj {:type :set-token-theme
:id id :id id
:theme theme}) :attrs (datafy theme)})
(update :undo-changes conj {:type :set-token-theme (update :undo-changes conj {:type :set-token-theme
:id id :id id
:theme prev-theme}) :attrs (datafy prev-theme)})
(apply-changes-local)))) (apply-changes-local))))
(defn set-active-token-themes (defn set-active-token-themes

View File

@@ -165,8 +165,7 @@
(delete-token- [_ id] "delete a token from the list") (delete-token- [_ id] "delete a token from the list")
(get-token- [_ id] "get a token by its id") (get-token- [_ id] "get a token by its id")
(get-token-by-name- [_ name] "get a token by its name") (get-token-by-name- [_ name] "get a token by its name")
(get-tokens-seq- [_] "return an ordered sequence of all tokens in the set") (get-tokens- [_] "return a map of tokens in the set, indexed by token-name"))
(get-tokens-map- [_] "return a map of tokens in the set, indexed by token-name"))
;; TODO: this structure is temporary. It's needed to be able to migrate TokensLib ;; TODO: this structure is temporary. It's needed to be able to migrate TokensLib
;; from 1.2 to 1.3 when TokenSet datatype was changed to a deftype. This should ;; from 1.2 to 1.3 when TokenSet datatype was changed to a deftype. This should
@@ -275,10 +274,7 @@
(assert (string? name) "expected string for `name`") (assert (string? name) "expected string for `name`")
(get tokens name)) (get tokens name))
(get-tokens-seq- [_] (get-tokens- [_]
(vals tokens))
(get-tokens-map- [_]
tokens)) tokens))
(defmethod pp/simple-dispatch TokenSet [^TokenSet obj] (defmethod pp/simple-dispatch TokenSet [^TokenSet obj]
@@ -909,8 +905,7 @@ Will return a value that matches this schema:
`:partial` Mixed active state of nested sets") `:partial` Mixed active state of nested sets")
(get-tokens-in-active-sets [_] "set of set names that are active in the the active themes") (get-tokens-in-active-sets [_] "set of set names that are active in the the active themes")
(get-all-tokens [_] "all tokens in the lib") (get-all-tokens [_] "all tokens in the lib")
(get-tokens-seq [_ set-id] "return an ordered sequence of all tokens in a set") (get-tokens [_ set-id] "return a map of tokens in the set, indexed by token-name")
(get-tokens-map [_ set-id] "return a map of tokens in the set, indexed by token-name")
(validate [_])) (validate [_]))
(declare parse-multi-set-dtcg-json) (declare parse-multi-set-dtcg-json)
@@ -1296,7 +1291,7 @@ Will return a value that matches this schema:
active-set-names (filter theme-set-names all-set-names) active-set-names (filter theme-set-names all-set-names)
tokens (reduce (fn [tokens set-name] tokens (reduce (fn [tokens set-name]
(let [set (get-set-by-name this set-name)] (let [set (get-set-by-name this set-name)]
(merge tokens (get-tokens-map- set)))) (merge tokens (get-tokens- set))))
(d/ordered-map) (d/ordered-map)
active-set-names)] active-set-names)]
tokens)) tokens))
@@ -1304,19 +1299,14 @@ Will return a value that matches this schema:
(get-all-tokens [this] (get-all-tokens [this]
(reduce (reduce
(fn [tokens' set] (fn [tokens' set]
(into tokens' (map (fn [x] [(:name x) x]) (get-tokens-seq- set)))) (into tokens' (map (fn [x] [(:name x) x]) (vals (get-tokens- set)))))
{} {}
(get-sets this))) (get-sets this)))
(get-tokens-seq [this set-id] (get-tokens [this set-id]
(some-> this (some-> this
(get-set set-id) (get-set set-id)
(get-tokens-seq-))) (get-tokens-)))
(get-tokens-map [this set-id]
(some-> this
(get-set set-id)
(get-tokens-map-)))
(validate [_] (validate [_]
(and (valid-token-sets? sets) (and (valid-token-sets? sets)
@@ -1769,7 +1759,7 @@ Will return a value that matches this schema:
sets (->> (get-sets tokens-lib) sets (->> (get-sets tokens-lib)
(map (fn [token-set] (map (fn [token-set]
(let [name (get-name token-set) (let [name (get-name token-set)
tokens (get-tokens-map- token-set)] tokens (get-tokens- token-set)]
[(str name ".json") (tokens-tree tokens :update-token-fn token->dtcg-token)]))) [(str name ".json") (tokens-tree tokens :update-token-fn token->dtcg-token)])))
(into {}))] (into {}))]
(-> sets (-> sets
@@ -1789,7 +1779,7 @@ Will return a value that matches this schema:
(filter (partial instance? TokenSet)) (filter (partial instance? TokenSet))
(map (fn [set] (map (fn [set]
[(get-name set) [(get-name set)
(tokens-tree (get-tokens-map- set) :update-token-fn token->dtcg-token)]))) (tokens-tree (get-tokens- set) :update-token-fn token->dtcg-token)])))
ordered-set-names ordered-set-names
(mapv first name-set-tuples) (mapv first name-set-tuples)

View File

@@ -83,11 +83,11 @@
(t/is (= (ctob/get-name token-set1) "test-token-set-1")) (t/is (= (ctob/get-name token-set1) "test-token-set-1"))
(t/is (= (ctob/get-description token-set1) "")) (t/is (= (ctob/get-description token-set1) ""))
(t/is (some? (ctob/get-modified-at token-set1))) (t/is (some? (ctob/get-modified-at token-set1)))
(t/is (empty? (ctob/get-tokens-map- token-set1))) (t/is (empty? (ctob/get-tokens- token-set1)))
(t/is (= (ctob/get-name token-set2) "test-token-set-2")) (t/is (= (ctob/get-name token-set2) "test-token-set-2"))
(t/is (= (ctob/get-description token-set2) "test description")) (t/is (= (ctob/get-description token-set2) "test description"))
(t/is (= (ctob/get-modified-at token-set2) now)) (t/is (= (ctob/get-modified-at token-set2) now))
(t/is (empty? (ctob/get-tokens-map- token-set2))))) (t/is (empty? (ctob/get-tokens- token-set2)))))
(t/deftest make-invalid-token-set (t/deftest make-invalid-token-set
(let [params {:name 777 :description 999}] (let [params {:name 777 :description 999}]
@@ -197,7 +197,7 @@
:type :boolean :type :boolean
:value true)}))) :value true)})))
expected (-> tokens-lib expected (-> tokens-lib
(ctob/get-tokens-map (thi/id :test-token-set)) (ctob/get-tokens (thi/id :test-token-set))
(ctob/tokens-tree))] (ctob/tokens-tree))]
(t/is (= (get-in expected ["foo" "bar" "baz" :name]) "foo.bar.baz")) (t/is (= (get-in expected ["foo" "bar" "baz" :name]) "foo.bar.baz"))
(t/is (= (get-in expected ["foo" "bar" "bam" :name]) "foo.bar.bam")) (t/is (= (get-in expected ["foo" "bar" "bam" :name]) "foo.bar.bam"))
@@ -328,7 +328,7 @@
(t/is (some? token-set-copy)) (t/is (some? token-set-copy))
(t/is (= (ctob/get-name token-set-copy) "test-token-set-copy")) (t/is (= (ctob/get-name token-set-copy) "test-token-set-copy"))
(t/is (= (count (ctob/get-tokens-map- token-set-copy)) 1)) (t/is (= (count (ctob/get-tokens- token-set-copy)) 1))
(t/is (= (:name token) "test-token")))) (t/is (= (:name token) "test-token"))))
(t/deftest duplicate-token-set-twice (t/deftest duplicate-token-set-twice
@@ -348,7 +348,7 @@
(t/is (some? token-set-copy)) (t/is (some? token-set-copy))
(t/is (= (ctob/get-name token-set-copy) "test-token-set-copy-2")) (t/is (= (ctob/get-name token-set-copy) "test-token-set-copy-2"))
(t/is (= (count (ctob/get-tokens-map- token-set-copy)) 1)) (t/is (= (count (ctob/get-tokens- token-set-copy)) 1))
(t/is (= (:name token) "test-token")))) (t/is (= (:name token) "test-token"))))
(t/deftest duplicate-empty-token-set (t/deftest duplicate-empty-token-set
@@ -357,11 +357,11 @@
:name "test-token-set"))) :name "test-token-set")))
token-set-copy (ctob/duplicate-set (thi/id :test-token-set) tokens-lib {:suffix "copy"}) token-set-copy (ctob/duplicate-set (thi/id :test-token-set) tokens-lib {:suffix "copy"})
tokens (ctob/get-tokens-map- token-set-copy)] tokens (ctob/get-tokens- token-set-copy)]
(t/is (some? token-set-copy)) (t/is (some? token-set-copy))
(t/is (= (ctob/get-name token-set-copy) "test-token-set-copy")) (t/is (= (ctob/get-name token-set-copy) "test-token-set-copy"))
(t/is (= (count (ctob/get-tokens-map- token-set-copy)) 0)) (t/is (= (count (ctob/get-tokens- token-set-copy)) 0))
(t/is (= (count tokens) 0)))) (t/is (= (count tokens) 0))))
(t/deftest duplicate-not-existing-token-set (t/deftest duplicate-not-existing-token-set
@@ -390,7 +390,7 @@
(thi/id :token))] (thi/id :token))]
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))
(t/is (= (count (ctob/get-tokens-map tokens-lib' (thi/id :test-token-set))) 1)) (t/is (= (count (ctob/get-tokens tokens-lib' (thi/id :test-token-set))) 1))
(t/is (= (:name token') "test-token")) (t/is (= (:name token') "test-token"))
(t/is (ct/is-after? (ctob/get-modified-at token-set') (ctob/get-modified-at token-set))))) (t/is (ct/is-after? (ctob/get-modified-at token-set') (ctob/get-modified-at token-set)))))
@@ -432,7 +432,7 @@
token' (ctob/get-token tokens-lib' token' (ctob/get-token tokens-lib'
(thi/id :test-token-set) (thi/id :test-token-set)
(thi/id :test-token-1)) (thi/id :test-token-1))
tokens' (ctob/get-tokens-map tokens-lib' tokens' (ctob/get-tokens tokens-lib'
(thi/id :test-token-set))] (thi/id :test-token-set))]
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))
@@ -473,7 +473,7 @@
token' (ctob/get-token tokens-lib' token' (ctob/get-token tokens-lib'
(thi/id :test-token-set) (thi/id :test-token-set)
(thi/id :test-token-1)) (thi/id :test-token-1))
tokens' (ctob/get-tokens-map tokens-lib' tokens' (ctob/get-tokens tokens-lib'
(thi/id :test-token-set))] (thi/id :test-token-set))]
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))
@@ -504,7 +504,7 @@
token' (ctob/get-token tokens-lib' token' (ctob/get-token tokens-lib'
(thi/id :test-token-set) (thi/id :test-token-set)
(thi/id :test-token)) (thi/id :test-token))
tokens' (ctob/get-tokens-map tokens-lib' tokens' (ctob/get-tokens tokens-lib'
(thi/id :test-token-set))] (thi/id :test-token-set))]
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))
@@ -914,7 +914,7 @@
:type :boolean :type :boolean
:value true))) :value true)))
tokens-list (ctob/get-tokens-seq tokens-lib (thi/id :test-token-set))] tokens-list (vals (ctob/get-tokens tokens-lib (thi/id :test-token-set)))]
(t/is (= (count tokens-list) 5)) (t/is (= (count tokens-list) 5))
(t/is (= (:name (nth tokens-list 0)) "token1")) (t/is (= (:name (nth tokens-list 0)) "token1"))
@@ -1042,7 +1042,7 @@
token' (ctob/get-token tokens-lib' token' (ctob/get-token tokens-lib'
(thi/id :test-token-set) (thi/id :test-token-set)
(thi/id :test-token-2)) (thi/id :test-token-2))
tokens' (ctob/get-tokens-map tokens-lib' tokens' (ctob/get-tokens tokens-lib'
(thi/id :test-token-set))] (thi/id :test-token-set))]
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))
@@ -1075,7 +1075,7 @@
token' (ctob/get-token tokens-lib' token' (ctob/get-token tokens-lib'
(thi/id :test-token-set) (thi/id :test-token-set)
(thi/id :test-token-2)) (thi/id :test-token-2))
tokens' (ctob/get-tokens-map tokens-lib' tokens' (ctob/get-tokens tokens-lib'
(thi/id :test-token-set))] (thi/id :test-token-set))]
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))

View File

@@ -410,7 +410,7 @@
(when-let [token (ctob/get-token tokens-lib (when-let [token (ctob/get-token tokens-lib
(ctob/get-id token-set) (ctob/get-id token-set)
token-id)] token-id)]
(let [tokens (ctob/get-tokens-seq tokens-lib (ctob/get-id token-set)) (let [tokens (vals (ctob/get-tokens tokens-lib (ctob/get-id token-set)))
unames (map :name tokens) unames (map :name tokens)
suffix (tr "workspace.tokens.duplicate-suffix") suffix (tr "workspace.tokens.duplicate-suffix")
copy-name (cfh/generate-unique-name (:name token) unames :suffix suffix)] copy-name (cfh/generate-unique-name (:name token) unames :suffix suffix)]

View File

@@ -35,4 +35,4 @@
(when-let [set-id (get-selected-token-set-id state)] (when-let [set-id (get-selected-token-set-id state)]
(some-> (dsh/lookup-file-data state) (some-> (dsh/lookup-file-data state)
(get :tokens-lib) (get :tokens-lib)
(ctob/get-tokens-map set-id)))) (ctob/get-tokens set-id))))

View File

@@ -693,7 +693,7 @@
[sets] [sets]
(map (fn [s] (map (fn [s]
{:set (ctob/get-name s) {:set (ctob/get-name s)
:tokens (ctob/get-tokens-seq- s)}) ;; TODO: this function should be moved to common.logic and refactored :tokens (vals (ctob/get-tokens- s))}) ;; TODO: this function should be moved to common.logic and refactored
sets)) sets))
(defn- filter-active-sets (defn- filter-active-sets

View File

@@ -82,7 +82,7 @@
;; select the first one from the list of sets ;; select the first one from the list of sets
selected-token-set-tokens selected-token-set-tokens
(when selected-token-set-id (when selected-token-set-id
(some-> tokens-lib (ctob/get-tokens-map selected-token-set-id))) (some-> tokens-lib (ctob/get-tokens selected-token-set-id)))
tokens tokens
(mf/with-memo [active-tokens selected-token-set-tokens] (mf/with-memo [active-tokens selected-token-set-tokens]