Pārlūkot izejas kodu

Fix wrapped-by? utility function

Improve readability of test failures, add more unit tests
Phoenix Eliot 3 gadi atpakaļ
vecāks
revīzija
bb199c4de5

+ 2 - 3
src/main/frontend/handler/editor.cljs

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

+ 5 - 3
src/main/frontend/util/text.cljs

@@ -89,7 +89,7 @@
   "Get all indexes of `value` in the string `s`."
   "Get all indexes of `value` in the string `s`."
   [s value {:keys [before?] :or {before? true}}]
   [s value {:keys [before?] :or {before? true}}]
   (if (= value "")
   (if (= value "")
-    (if before? [0] [(dec (count s))])
+    (if before? [0] [(count s)]) ;; Hack: this prevents unnecessary work in wrapped-by?
     (loop [acc []
     (loop [acc []
           i 0]
           i 0]
      (if-let [i (string/index-of s value i)]
      (if-let [i (string/index-of s value i)]
@@ -99,10 +99,12 @@
 (defn wrapped-by?
 (defn wrapped-by?
   "`pos` must be wrapped by `before` and `and` in string `value`, e.g. ((a|b))"
   "`pos` must be wrapped by `before` and `and` in string `value`, e.g. ((a|b))"
   [value pos before end]
   [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})
   (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})
         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]]))
         indexes (sort-by first (concat before-matches end-matches [[pos :between]]))
         ks (map second indexes)
         ks (map second indexes)
         q [:before :between :end]]
         q [:before :between :end]]

+ 27 - 1
src/test/frontend/util/text_test.cljs

@@ -30,4 +30,30 @@
     [0 4 8]
     [0 4 8]
 
 
     (text-util/get-string-all-indexes "a.c a.c ab" "a." {})
     (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)))
+    ))