Просмотр исходного кода

Improve backtick autopairing (#6496)

* If you type a backtick and then some letters and then another backtick, the last backtick should close the first one and not spawn another autopair
Viktor Moros 3 лет назад
Родитель
Сommit
f5824322b3
2 измененных файлов с 10 добавлено и 4 удалено
  1. 8 2
      src/main/frontend/handler/editor.cljs
  2. 2 2
      src/main/frontend/util/text.cljs

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

@@ -2720,9 +2720,15 @@
         (do (util/stop e)
             (autopair input-id "(" format nil))
 
+        ;; If you type `xyz`, the last backtick should close the first and not add another autopair
+        ;; If you type several backticks in a row, each one should autopair to accommodate multiline code (```)        
         (contains? (set (keys autopair-map)) key)
-        (do (util/stop e)
-            (autopair input-id key format nil))
+        (let [curr (get-current-input-char input)
+                  prev (util/nth-safe value (dec pos))]
+            (util/stop e) 
+            (if (and (= key "`") (= "`" curr) (not= "`" prev))
+              (cursor/move-cursor-forward input)
+              (autopair input-id key format nil)))
 
         (and hashtag? (or (zero? pos) (re-matches #"\s" (get value (dec pos)))))
         (do

+ 2 - 2
src/main/frontend/util/text.cljs

@@ -66,7 +66,7 @@
       result)))
 
 (defn surround-by?
-  "`pos` must be surrounded by `before` and `and` in string `value`, e.g. ((|))"
+  "`pos` must be surrounded by `before` and `end` in string `value`, e.g. ((|))"
   [value pos before end]
   (let [start-pos (if (= :start before) 0 (- pos (count before)))
         end-pos (if (= :end end) (count value) (+ pos (count end)))]
@@ -97,7 +97,7 @@
        acc))))
 
 (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 `end` 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.