Fix wrapped-by? utility function

Improve readability of test failures, add more unit tests
pull/6256/head
Phoenix Eliot 2022-08-04 18:17:09 -04:00 committed by Andelf
parent d0ff784ebe
commit bb199c4de5
3 changed files with 34 additions and 7 deletions

View File

@ -1576,7 +1576,7 @@
[input before end]
(when input
(let [value (gobj/get input "value")
pos (dec (cursor/pos input))]
pos (cursor/pos input)]
(when (>= pos 0)
(text-util/wrapped-by? value pos before end)))))
@ -1856,8 +1856,7 @@
(and
(not= :property-search (state/get-editor-action))
(let [{:keys [line start-pos]} (text-util/get-current-line-by-pos (.-value input) (dec pos))]
(text-util/wrapped-by? line (dec (- pos start-pos)) "" gp-property/colons)))
(text-util/wrapped-by? line (- pos start-pos) "" gp-property/colons)))
(do
(state/set-editor-action-data! {:pos (cursor/get-caret-pos input)})
(state/set-editor-action! :property-search))

View File

@ -89,7 +89,7 @@
"Get all indexes of `value` in the string `s`."
[s value {:keys [before?] :or {before? true}}]
(if (= value "")
(if before? [0] [(dec (count s))])
(if before? [0] [(count s)]) ;; Hack: this prevents unnecessary work in wrapped-by?
(loop [acc []
i 0]
(if-let [i (string/index-of s value i)]
@ -99,10 +99,12 @@
(defn wrapped-by?
"`pos` must be wrapped by `before` and `and` in string `value`, e.g. ((a|b))"
[value pos before end]
;; Increment 'before' matches by (length of before string - 0.5) to make them be just before the cursor position they precede.
;; Increment 'after' matches by 0.5 to make them be just after the cursor position they follow.
(let [before-matches (->> (get-string-all-indexes value before {:before? true})
(map (fn [i] [i :before])))
(map (fn [i] [(+ i (- (count before) 0.5)) :before])))
end-matches (->> (get-string-all-indexes value end {:before? false})
(map (fn [i] [i :end])))
(map (fn [i] [(+ i 0.5) :end])))
indexes (sort-by first (concat before-matches end-matches [[pos :between]]))
ks (map second indexes)
q [:before :between :end]]

View File

@ -30,4 +30,30 @@
[0 4 8]
(text-util/get-string-all-indexes "a.c a.c ab" "a." {})
[0 4]))
[0 4]
(text-util/get-string-all-indexes "abc" "" { :before? true })
[0]
(text-util/get-string-all-indexes "abc" "" { :before? false })
[3]
))
(deftest test-wrapped-by
[]
(are [x y] (= x y)
'(false false true false false)
(map #(text-util/wrapped-by? "[[]]" % "[[" "]]") (take 5 (range)))
'(false false true true true true false false)
(map #(text-util/wrapped-by? "[[abc]]" % "[[" "]]") (take 8 (range)))
'(false false false false false false true true false false false false true true false false)
(map #(text-util/wrapped-by? "012 [[6]] [[2]]" % "[[" "]]") (take 16 (range)))
'(true true true true true false false false false false false false)
(map #(text-util/wrapped-by? "prop::value" % "" "::") (take 12 (range)))
'(false false false false false false true true true true true true)
(map #(text-util/wrapped-by? "prop::value" % "::" "") (take 12 (range)))
))