Skip to content

Commit

Permalink
Fix indentation for symbols in reader conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Aug 20, 2024
1 parent 8d8e26d commit ce787be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
24 changes: 23 additions & 1 deletion cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,30 @@
(defn- reader-conditional? [zloc]
(and (reader-macro? zloc) (#{"?" "?@"} (-> zloc z/down token-value str))))

(defn- reader-conditional-form-symbol
"Given a zipper pointing to a reader conditional form like
#?(:clj potemkin.core/defprotocol+ :cljs defprotocol)
Return the first symbol inside it e.g.
potemkin.core/defprotocol+"
[zloc]
(when (reader-conditional? zloc)
;; find the first keyword e.g. `:clj`
(when-let [key-loc (z/find (-> zloc z/down z/right z/down)
z/right
#(n/keyword-node? (z/node %)))]
;; now move to the next non-whitespace/non-comment/non-metadata node after
;; the keyword node e.g. `potemkin.core/defprotocol+`.
(when-let [value-loc (-> key-loc z/next skip-meta)]
(when (token? value-loc)
(z/sexpr value-loc))))))

(defn- form-symbol [zloc]
(-> zloc z/leftmost token-value))
(let [zloc (z/leftmost zloc)]
(or (token-value zloc)
(reader-conditional-form-symbol zloc))))

(defn- index-matches-top-argument? [zloc depth idx]
(and (> depth 0)
Expand Down
42 changes: 41 additions & 1 deletion cljfmt/test/cljfmt/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,47 @@
":cljs bar)"]
["#?@(:clj foo"
" :cljs bar)"])
"splicing syntax"))
"splicing syntax")
(testing "symbols using reader conditionals should indent correctly"
(let [opts {:indents '{defprotocol [[:block 1] [:inner 1]]
potemkin/defprotocol+ [[:block 1] [:inner 1]]}}]
(testing "standard syntax"
(is (reformats-to?
["(#?(:clj potemkin/defprotocol+ :cljs defprotocol) MyProtocol"
" \"This is a docstring for my protocol.\""
" (method [this x]"
" \"This is a docstring for a protocol method.\")"
")"]
["(#?(:clj potemkin/defprotocol+ :cljs defprotocol) MyProtocol"
" \"This is a docstring for my protocol.\""
" (method [this x]"
" \"This is a docstring for a protocol method.\"))"]
opts)
":clj and :cljs"))
(is (reformats-to?
["(#?(:clj potemkin/defprotocol+) MyProtocol"
" \"This is a docstring for my protocol.\""
" (method [this x]"
" \"This is a docstring for a protocol method.\")"
")"]
["(#?(:clj potemkin/defprotocol+) MyProtocol"
" \"This is a docstring for my protocol.\""
" (method [this x]"
" \"This is a docstring for a protocol method.\"))"]
opts)
"only :clj")
(is (reformats-to?
["(#?(:cljs ^:wow defprotocol) MyProtocol"
" \"This is a docstring for my protocol.\""
" (method [this x]"
" \"This is a docstring for a protocol method.\")"
")"]
["(#?(:cljs ^:wow defprotocol) MyProtocol"
" \"This is a docstring for my protocol.\""
" (method [this x]"
" \"This is a docstring for a protocol method.\"))"]
opts)
"only :cljs; skip metadata in front of symbol"))))

(testing "namespaced maps"
(is (reformats-to?
Expand Down

0 comments on commit ce787be

Please sign in to comment.