Skip to content

Commit

Permalink
Fix nested :inner specs
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Aug 21, 2024
1 parent 8d8e26d commit 7ffee0e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
26 changes: 20 additions & 6 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,25 @@
(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 [spec-type :as spec]]]
(let [depth* (case spec-type
:inner (let [[_inner depth] spec]
(- 0 depth))
0)
key-type* (cond
(qualified-symbol? key) 0
(simple-symbol? key) 1
(pattern? key) 2)]
[depth* key-type* (str key)]))

(defn- sorted-indents [indents]
(->> indents
(mapcat (fn [[k specs]]
(for [spec specs]
[k spec])))
(sort-by indent-order)
(map (fn [[k spec]]
[k [spec]]))))

(defn- custom-indent [zloc indents context]
(if (empty? indents)
Expand Down Expand Up @@ -381,7 +395,7 @@
(indent form indents alias-map default-options))
([form indents alias-map opts]
(let [ns-name (find-namespace (z/of-node form))
sorted-indents (sort-by indent-order indents)
sorted-indents (sorted-indents indents)
context (merge (select-keys opts [:function-arguments-indentation])
{:alias-map alias-map
:ns-name ns-name})]
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

0 comments on commit 7ffee0e

Please sign in to comment.