Skip to content

Commit

Permalink
Merge pull request #348 from camsaul/fix-reader-conditional-symbols
Browse files Browse the repository at this point in the history
Fix indentation for symbols in reader conditionals
  • Loading branch information
weavejester authored Aug 25, 2024
2 parents fe8ae0e + 606380b commit d750bc1
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 @@ -259,8 +259,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 @@ -697,7 +697,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 d750bc1

Please sign in to comment.