From c2b0dc8120ffdf61771bfc4fa6206a42c934cd66 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 11 Mar 2026 18:36:34 +0100 Subject: [PATCH] :bug: Fix comments and warnings --- common/src/app/common/data/macros.cljc | 7 +++ common/src/app/common/types/token.cljc | 35 +++++++------ .../test/common_tests/types/token_test.cljc | 50 ++++++++++--------- 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/common/src/app/common/data/macros.cljc b/common/src/app/common/data/macros.cljc index dc23426a95..2fdc9bc963 100644 --- a/common/src/app/common/data/macros.cljc +++ b/common/src/app/common/data/macros.cljc @@ -40,6 +40,13 @@ (list `c/get key))) keys))))) +(defmacro number + "Coerce number to number in a multiplatform way" + [o] + (if (:ns &env) + (with-meta o {:tag 'number}) + `(double ~o))) + (defmacro str [& params] `(str/concat ~@params)) diff --git a/common/src/app/common/types/token.cljc b/common/src/app/common/types/token.cljc index 4b4314d8db..55ecc842e7 100644 --- a/common/src/app/common/types/token.cljc +++ b/common/src/app/common/types/token.cljc @@ -7,6 +7,7 @@ (ns app.common.types.token (:require [app.common.data :as d] + [app.common.data.macros :as dm] [app.common.schema :as sm] [app.common.schema.generators :as sg] [app.common.time :as ct] @@ -607,8 +608,7 @@ "400" #{"book" "normal" "buch" "regular"}, "500" #{"kräftig" "medium" "kraeftig"}, "600" #{"demi-bold" "halbfett" "demibold" "demi bold" "semibold" "semi bold" "semi-bold"}, - "700" #{"dreivi - ertelfett" "bold"}, + "700" #{"dreiviertelfett" "bold"}, "800" #{"extrabold" "fett" "extra-bold" "ultrabold" "ultra-bold" "extra bold" "ultra bold"}, "900" #{"heavy" "black" "extrafett"}, "950" #{"extra-black" "extra black" "ultra-black" "ultra black"}}) @@ -642,7 +642,7 @@ ;;;;;; Combobox token parsing -(defn inside-ref? +(defn- inside-ref? "Returns true if `position` in `value` is inside an open reference block (i.e. after a `{` that has no matching `}` to its left). A reference block is considered open when the last `{` appears after the last `}`, @@ -683,7 +683,7 @@ sort last))) -(defn inside-closed-ref? +(defn- inside-closed-ref? "Returns true if `position` falls inside a complete (closed) reference block, i.e. there is a `{` to the left and a `}` to the right with no spaces between either delimiter and the position. @@ -691,15 +691,17 @@ [value position] (let [left (str/slice value 0 position) right (str/slice value position) + open-pos (d/nth-last-index-of left "{" 1) close-pos (d/nth-index-of right "}" 1) last-space-left (d/nth-last-index-of left " " 1) first-space-right (d/nth-index-of right " " 1)] + (boolean - (and open-pos - close-pos - (or (nil? last-space-left) (> open-pos last-space-left)) - (or (nil? first-space-right) (< close-pos first-space-right)))))) + (and (number? open-pos) + (number? close-pos) + (or (nil? last-space-left) (> (dm/number open-pos) (dm/number last-space-left))) + (or (nil? first-space-right) (< (dm/number close-pos) (dm/number first-space-right))))))) (defn- build-result "Builds the result map for `insert-ref` by replacing the substring of `value` @@ -727,13 +729,16 @@ :value — the resulting string after insertion :cursor — the index immediately after the inserted reference" [value position name] - (cond - (inside-ref? value position) + (if (inside-ref? value position) (if (inside-closed-ref? value position) - (let [open-pos (d/nth-last-index-of (str/slice value 0 position) "{" 1) - close-pos (+ position (d/nth-index-of (str/slice value position) "}" 1) 1)] + (let [open-pos (-> (str/slice value 0 position) + (d/nth-last-index-of "{" 1)) + close-pos (-> (str/slice value position) + (d/nth-index-of "}" 1)) + close-pos (if (number? close-pos) + (+ position close-pos 1) + position)] (build-result value open-pos close-pos name)) - (build-result value (start-ref-position value position) position name)) - :else - (build-result value position position name))) \ No newline at end of file + (build-result value (start-ref-position value position) position name)) + (build-result value position position name))) diff --git a/common/test/common_tests/types/token_test.cljc b/common/test/common_tests/types/token_test.cljc index 2cde8166cf..96e642690c 100644 --- a/common/test/common_tests/types/token_test.cljc +++ b/common/test/common_tests/types/token_test.cljc @@ -81,28 +81,32 @@ (t/is (= (cto/insert-ref "abc" 3 "") {:value "abc{}" :cursor 5})) (t/is (= (cto/insert-ref "{a} {b}" 4 "x") - {:value "{a} {x}{b}" :cursor 7})))) + {:value "{a} {x}{b}" :cursor 7})) + (t/is (= (cto/insert-ref "{a {b {c" 8 "x") + {:value "{a {b {x}" :cursor 9})) + (t/is (= (cto/insert-ref "{ { {" 5 "x") + {:value "{ { {x}" :cursor 7}))) -(t/deftest inside-ref - (t/is (= (cto/inside-ref? "" 1) false)) - (t/is (= (cto/inside-ref? "AAA " 4) false)) - (t/is (= (cto/inside-ref? "{abc" 0) false)) - (t/is (= (cto/inside-ref? "{abc}" 5) false)) - (t/is (= (cto/inside-ref? "{a}{b}" 6) false)) - (t/is (= (cto/inside-ref? "{a{b" 4) true)) - (t/is (= (cto/inside-ref? "abc{" 4) true)) - (t/is (= (cto/inside-ref? "abc}" 4) false)) - (t/is (= (cto/inside-ref? "{abc[}" 1) true)) - (t/is (= (cto/inside-ref? "abc {def}ghi" 5) true)) - (t/is (= (cto/inside-ref? "abc {def]ghi" 8) true))) + ;; inside-ref? coverage + (t/is (= (cto/insert-ref "AAA " 4 "x") + {:value "AAA {x}" :cursor 7})) + (t/is (= (cto/insert-ref "{abc}" 5 "x") + {:value "{abc}{x}" :cursor 8})) + (t/is (= (cto/insert-ref "{a}{b}" 6 "x") + {:value "{a}{b}{x}" :cursor 9})) + (t/is (= (cto/insert-ref "abc}" 4 "x") + {:value "abc}{x}" :cursor 7})) + (t/is (= (cto/insert-ref "{abc[}" 0 "x") + {:value "{x}{abc[}" :cursor 3})) + (t/is (= (cto/insert-ref "{abc[}" 1 "x") + {:value "{x}" :cursor 3})) -(t/deftest inside-closed-ref - (t/is (= (cto/inside-closed-ref? "" 1) false)) - (t/is (= (cto/inside-closed-ref? "{abc}" 1) true)) - (t/is (= (cto/inside-closed-ref? "abc {def}ghi" 5) true)) - (t/is (= (cto/inside-closed-ref? "abc {def}ghi" 8) true)) - (t/is (= (cto/inside-closed-ref? "abc {def}ghi" 10) false)) - (t/is (= (cto/inside-closed-ref? "{abc}" 0) false)) - (t/is (= (cto/inside-closed-ref? "{abc}" 5) false)) - (t/is (= (cto/inside-closed-ref? "{ab cd}" 3) false)) - (t/is (= (cto/inside-closed-ref? "{a}{bc}" 5) true))) \ No newline at end of file + ;; inside-closed-ref? coverage + (t/is (= (cto/insert-ref "{abc}" 1 "x") + {:value "{x}" :cursor 3})) + (t/is (= (cto/insert-ref "abc {def}ghi" 8 "x") + {:value "abc {x}ghi" :cursor 7})) + (t/is (= (cto/insert-ref "{ab cd}" 3 "x") + {:value "{x} cd}" :cursor 3})) + (t/is (= (cto/insert-ref "{a}{bc}" 5 "x") + {:value "{a}{x}" :cursor 6})))