Skip to content

Commit

Permalink
Improve char handling, add auto-indent for text areas
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaaad committed Mar 25, 2021
1 parent f870e0a commit 61c157b
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/vlaaad/reveal/fx.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@
(let [range-start (.getRangeStart change)
range-end (.getRangeEnd change)
control-text (.getControlText change)
inputs-char (and (pos? range-start)
(= \\ (.charAt control-text (dec range-start))))
look-back (fn [start pred]
(loop [matches 0]
(let [i (- start matches)]
(cond
(neg? i) matches
(pred (.charAt control-text i)) (recur (inc matches))
:else matches))))
inputs-char (odd? (look-back (dec range-start) #{\\}))
move-right? (fn [char]
(and (not (.isDeleted change))
(< range-end (.length control-text))
Expand Down Expand Up @@ -84,6 +90,27 @@
"}" (when (move-right? \}) (move-right!))
"(" (.setText change "()")
")" (when (move-right? \)) (move-right!))
"\n" (let [spaces
(loop [i (dec range-start)
stack 0]
(cond
(neg? stack) (inc (look-back i (complement #{\newline})))
(neg? i) 0
:else (let [ch (.charAt control-text i)]
(recur
(dec i)
(cond
(and (#{\[ \{ \(} ch) (even? (look-back (dec i) #{\\})))
(dec stack)

(and (#{\] \} \)} ch) (even? (look-back (dec i) #{\\})))
(inc stack)

:else
stack)))))]
(doto change
(.setText (str \newline (apply str (repeat spaces \space))))
(.selectRange (+ spaces range-start 1) (+ spaces range-start 1))))
nil)))))

(def code-text-formatter-filter
Expand Down

0 comments on commit 61c157b

Please sign in to comment.