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 22, 2024
1 parent 8d8e26d commit 606380b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
14 changes: 13 additions & 1 deletion cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,20 @@
(defn- reader-conditional? [zloc]
(and (reader-macro? zloc) (#{"?" "?@"} (-> zloc z/down token-value str))))

(defn- find-next-keyword [zloc]
(z/find zloc z/right #(n/keyword-node? (z/node %))))

(defn- first-symbol-in-reader-conditional [zloc]
(when (reader-conditional? zloc)
(when-let [key-loc (-> zloc z/down z/right z/down find-next-keyword)]
(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)
(first-symbol-in-reader-conditional 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 606380b

Please sign in to comment.