Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested :inner specs #350

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,14 @@
(defn- make-indenter [[key opts] context]
(apply some-fn (map (partial indenter-fn key context) opts)))

(defn- indent-order [[key _]]
(cond
(and (symbol? key) (namespace key)) (str 0 key)
(symbol? key) (str 1 key)
(pattern? key) (str 2 key)))
(defn- indent-order [[key specs]]
(let [get-depth (fn [[type depth]] (if (= type :inner) depth 0))
max-depth (transduce (map get-depth) max 0 specs)
key-order (cond
(qualified-symbol? key) 0
(simple-symbol? key) 1
(pattern? key) 2)]
[(- max-depth) key-order (str key)]))

(defn- custom-indent [zloc indents context]
(if (empty? indents)
Expand Down
35 changes: 34 additions & 1 deletion cljfmt/test/cljfmt/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,40 @@
["(defrecord Foo [x]"
" Closeable"
" (close [_]"
" (prn x)))"])))
" (prn x)))"]))
(testing "nested rules like [:inner 1] (#349)"
(is (reformats-to?
["(ns my.namespace)"
""
"(defprotocol MyProtocol"
"MyClass"
"(with-x [this x]"
"\"with-x is a method\"))"
""
"(extend-protocol MyProtocol"
"MyClass"
"(with-x [this x]"
"(+ this x)))"
""
"(defn x [x]"
"(with-x x "
"1))"]
["(ns my.namespace)"
""
"(defprotocol MyProtocol"
" MyClass"
" (with-x [this x]"
" \"with-x is a method\"))"
""
"(extend-protocol MyProtocol"
" MyClass"
" (with-x [this x]"
" (+ this x)))"
""
"(defn x [x]"
" (with-x x"
" 1))"]
{:extra-indents '{my.namespace/with-x [[:block 0]]}}))))

(testing "data structure indentation"
(is (reformats-to?
Expand Down
Loading