From f5824322b3dc99b7229370c0853766bf5259f616 Mon Sep 17 00:00:00 2001 From: Viktor Moros Date: Fri, 26 Aug 2022 21:32:46 -0700 Subject: [PATCH] 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 --- src/main/frontend/handler/editor.cljs | 10 ++++++++-- src/main/frontend/util/text.cljs | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index fc12bbed5..fa8285acc 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/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 diff --git a/src/main/frontend/util/text.cljs b/src/main/frontend/util/text.cljs index 85331780b..fea8a1a60 100644 --- a/src/main/frontend/util/text.cljs +++ b/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.