Skip to content

Commit d750bc1

Browse files
authored
Merge pull request #348 from camsaul/fix-reader-conditional-symbols
Fix indentation for symbols in reader conditionals
2 parents fe8ae0e + 606380b commit d750bc1

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

cljfmt/src/cljfmt/core.cljc

+13-1
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,20 @@
259259
(defn- reader-conditional? [zloc]
260260
(and (reader-macro? zloc) (#{"?" "?@"} (-> zloc z/down token-value str))))
261261

262+
(defn- find-next-keyword [zloc]
263+
(z/find zloc z/right #(n/keyword-node? (z/node %))))
264+
265+
(defn- first-symbol-in-reader-conditional [zloc]
266+
(when (reader-conditional? zloc)
267+
(when-let [key-loc (-> zloc z/down z/right z/down find-next-keyword)]
268+
(when-let [value-loc (-> key-loc z/next skip-meta)]
269+
(when (token? value-loc)
270+
(z/sexpr value-loc))))))
271+
262272
(defn- form-symbol [zloc]
263-
(-> zloc z/leftmost token-value))
273+
(let [zloc (z/leftmost zloc)]
274+
(or (token-value zloc)
275+
(first-symbol-in-reader-conditional zloc))))
264276

265277
(defn- index-matches-top-argument? [zloc depth idx]
266278
(and (> depth 0)

cljfmt/test/cljfmt/core_test.cljc

+41-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,47 @@
697697
":cljs bar)"]
698698
["#?@(:clj foo"
699699
" :cljs bar)"])
700-
"splicing syntax"))
700+
"splicing syntax")
701+
(testing "symbols using reader conditionals should indent correctly"
702+
(let [opts {:indents '{defprotocol [[:block 1] [:inner 1]]
703+
potemkin/defprotocol+ [[:block 1] [:inner 1]]}}]
704+
(testing "standard syntax"
705+
(is (reformats-to?
706+
["(#?(:clj potemkin/defprotocol+ :cljs defprotocol) MyProtocol"
707+
" \"This is a docstring for my protocol.\""
708+
" (method [this x]"
709+
" \"This is a docstring for a protocol method.\")"
710+
")"]
711+
["(#?(:clj potemkin/defprotocol+ :cljs defprotocol) MyProtocol"
712+
" \"This is a docstring for my protocol.\""
713+
" (method [this x]"
714+
" \"This is a docstring for a protocol method.\"))"]
715+
opts)
716+
":clj and :cljs"))
717+
(is (reformats-to?
718+
["(#?(:clj potemkin/defprotocol+) MyProtocol"
719+
" \"This is a docstring for my protocol.\""
720+
" (method [this x]"
721+
" \"This is a docstring for a protocol method.\")"
722+
")"]
723+
["(#?(:clj potemkin/defprotocol+) MyProtocol"
724+
" \"This is a docstring for my protocol.\""
725+
" (method [this x]"
726+
" \"This is a docstring for a protocol method.\"))"]
727+
opts)
728+
"only :clj")
729+
(is (reformats-to?
730+
["(#?(:cljs ^:wow defprotocol) MyProtocol"
731+
" \"This is a docstring for my protocol.\""
732+
" (method [this x]"
733+
" \"This is a docstring for a protocol method.\")"
734+
")"]
735+
["(#?(:cljs ^:wow defprotocol) MyProtocol"
736+
" \"This is a docstring for my protocol.\""
737+
" (method [this x]"
738+
" \"This is a docstring for a protocol method.\"))"]
739+
opts)
740+
"only :cljs; skip metadata in front of symbol"))))
701741

702742
(testing "namespaced maps"
703743
(is (reformats-to?

0 commit comments

Comments
 (0)