-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort_requires.clj
executable file
·28 lines (23 loc) · 1.09 KB
/
sort_requires.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env bb
(require '[babashka.pods :as pods])
(pods/load-pod "pod-babashka-parcera")
(require '[pod.babashka.parcera :as parcera])
(def parsed (parcera/ast (slurp *in*)))
;; we're not using postwalk since it doesn't preserve metadata
(defn transform-node [node]
(if (seq? node)
(if (and (= :list (first node))
(= '(:keyword ":require") (second node)))
(let [children (nnext node)
first-whitespace (some #(when (= :whitespace (first %)) %) children)
children (remove #(= :whitespace (first %)) children)
loc (meta (first children))
start (-> loc :parcera.core/start :column)
whitespace (str (apply str "\n" (repeat start " ")))
children (sort-by (comp str parcera/code) children)
children (butlast (interleave children (repeat (list :whitespace whitespace))))]
(cons :list (cons '(:keyword ":require") (cons first-whitespace children))))
(cons (first node) (map transform-node (rest node))))
node))
(def processed (transform-node parsed))
(println (parcera/code processed))