Skip to content

Commit ec472e2

Browse files
committed
Created :thread rule and handled idx and :even/:odd
1 parent b400730 commit ec472e2

File tree

3 files changed

+81
-48
lines changed

3 files changed

+81
-48
lines changed

cljfmt/resources/cljfmt/indents/clojure.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
{alt! [[:block 0]]
1+
{-> [[:thread] [:block 0]]
2+
alt! [[:block 0]]
23
alt!! [[:block 0]]
34
are [[:block 2]]
4-
as-> [[:block 2]]
5+
as-> [[:thread] [:block 2]]
56
binding [[:block 1]]
67
bound-fn [[:inner 0]]
78
case [[:block 1]]
89
catch [[:block 2]]
910
comment [[:block 0]]
1011
cond [[:block 0]]
1112
condp [[:block 2]]
12-
cond-> [[:block 1]]
13+
cond-> [[:thread :odd] [:block 1]]
1314
cond->> [[:block 1]]
1415
def [[:inner 0]]
1516
defmacro [[:inner 0]]
@@ -50,6 +51,7 @@
5051
ns [[:block 1]]
5152
proxy [[:block 2] [:inner 1]]
5253
reify [[:inner 0] [:inner 1]]
54+
some-> [[:thread] [:block 0]]
5355
struct-map [[:block 1]]
5456
testing [[:block 1]]
5557
thread [[:block 0]]

cljfmt/src/cljfmt/core.cljc

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,28 +275,39 @@
275275
(symbol? key) (= key sym)
276276
(pattern? key) (re-find key (str sym)))))
277277

278-
(defn form-matches-key? [zloc key context]
278+
(defn- form-symbol-matches? [zloc pred context]
279279
(let [possible-sym (form-symbol zloc)]
280-
(or (symbol-matches-key? (fully-qualified-symbol possible-sym context) key)
281-
(symbol-matches-key? (remove-namespace possible-sym) key))))
280+
(or (pred (fully-qualified-symbol possible-sym context))
281+
(pred (remove-namespace possible-sym)))))
282+
283+
(defn- form-matches-key? [zloc key context]
284+
(form-symbol-matches? zloc #(symbol-matches-key? % key) context))
282285

283286
(defn- form-matches-thread-macro? [zloc context]
284-
(let [possible-sym (form-symbol zloc)
285-
fully-qualified-sym (fully-qualified-symbol possible-sym context)
286-
sym-without-namespace (remove-namespace possible-sym)]
287-
(some #(or (symbol-matches-key? fully-qualified-sym %)
288-
(symbol-matches-key? sym-without-namespace %))
289-
#{'-> 'as-> 'some-> 'cond->})))
287+
(form-symbol-matches? zloc (:threads context) context))
290288

291-
(defn- first-argument? [zloc]
292-
(= (z/right (z/leftmost zloc)) zloc))
289+
(defn- get-zipper-position [zloc]
290+
(loop [idx 0
291+
loop-zloc (z/leftmost zloc)]
292+
(cond
293+
(= zloc loop-zloc) idx
294+
(= loop-zloc (z/rightmost zloc)) nil
295+
:else (recur (inc idx) (z/right loop-zloc)))))
293296

294297
(defn- in-thread-macro? [zloc context]
295-
(and (form-matches-thread-macro? zloc context) (not (first-argument? zloc))))
298+
(when (= zloc (z/root zloc))
299+
(when-some [idx (form-matches-thread-macro? zloc context)]
300+
(let [position (cond-> (get-zipper-position (z/up zloc))
301+
(in-thread-macro? (z/up (z/up zloc)) context) inc)]
302+
(cond
303+
(< position 2) false
304+
(= idx :odd) (odd? position)
305+
(= idx :even) (even? position)
306+
:else (<= idx position))))))
296307

297308
(defn- inner-indent [zloc key depth idx context]
298309
(let [top (nth (iterate z/up zloc) depth)
299-
adjusted-idx (cond-> idx (in-thread-macro? (z/up top) context) (some-> dec))]
310+
adjusted-idx (cond-> idx (in-thread-macro? zloc context) (some-> dec))]
300311
(when (and (form-matches-key? top key context)
301312
(or (nil? idx) (index-matches-top-argument? zloc depth adjusted-idx)))
302313
(let [zup (z/up zloc)]
@@ -317,7 +328,7 @@
317328

318329
(defn- block-indent [zloc key idx context]
319330
(when (form-matches-key? zloc key context)
320-
(let [adjusted-idx (cond-> idx (in-thread-macro? (z/up zloc) context) (some-> dec (max 0)))
331+
(let [adjusted-idx (cond-> idx (in-thread-macro? zloc context) (some-> dec (max 0)))
321332
zloc-after-idx (some-> zloc (nth-form (inc adjusted-idx)))]
322333
(if (and (or (nil? zloc-after-idx) (first-form-in-line? zloc-after-idx))
323334
(> (index-of zloc) adjusted-idx))
@@ -352,6 +363,9 @@
352363
(defmethod indenter-fn :block [sym context [_ idx]]
353364
(fn [zloc] (block-indent zloc sym idx context)))
354365

366+
(defmethod indenter-fn :thread [_ _ _]
367+
(constantly nil))
368+
355369
(defn- make-indenter [[key opts] context]
356370
(apply some-fn (map (partial indenter-fn key context) opts)))
357371

@@ -388,6 +402,13 @@
388402
(defn- find-namespace [zloc]
389403
(some-> zloc root z/down (z/find z/right ns-form?) z/down z/next z/sexpr))
390404

405+
(defn- get-thread-indents [indents]
406+
(->> indents
407+
(keep (fn [[k v]]
408+
(when-first [[_ idx] (filter (fn [[rule]] (= rule :thread)) v)]
409+
[k (or idx 2)])))
410+
(into {})))
411+
391412
(defn indent
392413
([form]
393414
(indent form default-indents {}))
@@ -400,7 +421,8 @@
400421
sorted-indents (sort-by indent-order indents)
401422
context (merge (select-keys opts [:function-arguments-indentation])
402423
{:alias-map alias-map
403-
:ns-name ns-name})]
424+
:ns-name ns-name
425+
:threads (get-thread-indents indents)})]
404426
(transform form edit-all should-indent?
405427
#(indent-line % sorted-indents context)))))
406428

cljfmt/test/cljfmt/core_test.cljc

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -692,39 +692,48 @@
692692
["#:clj {:a :b"
693693
":c :d}"]
694694
["#:clj {:a :b"
695-
" :c :d}"])))
695+
" :c :d}"])))
696696

697697
(testing "thread first"
698698
(is (reformats-to?
699-
["(-> v"
700-
"(cond->"
701-
"a b"
702-
"c d))"]
703-
["(-> v"
704-
" (cond->"
705-
" a b"
706-
" c d))"]))
707-
(is (reformats-to?
708-
["(cond-> v"
709-
"a b"
710-
"c d)"]
711-
["(cond-> v"
712-
" a b"
713-
" c d)"]))
714-
(is (reformats-to?
715-
["(-> v"
716-
"(cond-> a b"
717-
"c d))"]
718-
["(-> v"
719-
" (cond-> a b"
720-
" c d))"]))
721-
(is (reformats-to?
722-
["(-> (cond-> a"
723-
"odd? inc)"
724-
"inc)"]
725-
["(-> (cond-> a"
726-
" odd? inc)"
727-
" inc)"]))))
699+
["(-> v"
700+
"(cond-> a b"
701+
"c d))"]
702+
["(-> v"
703+
" (cond-> a b"
704+
" c d))"]))
705+
(is (reformats-to?
706+
["(cond-> v"
707+
"a b"
708+
"c d)"]
709+
["(cond-> v"
710+
" a b"
711+
" c d)"]))
712+
(is (reformats-to?
713+
["(-> v"
714+
"(cond-> a b"
715+
"c d))"]
716+
["(-> v"
717+
" (cond-> a b"
718+
" c d))"]))
719+
(is (reformats-to?
720+
["(-> (cond-> a"
721+
"odd? inc)"
722+
"inc)"]
723+
["(-> (cond-> a"
724+
" odd? inc)"
725+
" inc)"]))
726+
(is (reformats-to?
727+
["(cond-> a"
728+
"(cond-> 1
729+
odd? inc)
730+
(cond-> a b
731+
c d))"]
732+
["(cond-> a"
733+
" (cond-> 1"
734+
" odd? inc)"
735+
" (cond-> a b"
736+
" c d))"]))))
728737

729738
(deftest test-remove-multiple-non-indenting-spaces
730739
(let [opts {:remove-multiple-non-indenting-spaces? true}]

0 commit comments

Comments
 (0)