diff --git a/cljfmt/src/cljfmt/core.cljc b/cljfmt/src/cljfmt/core.cljc index 1e9f0e87..ee4eacfd 100644 --- a/cljfmt/src/cljfmt/core.cljc +++ b/cljfmt/src/cljfmt/core.cljc @@ -339,11 +339,21 @@ (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 [max-depth (transduce + (map (fn [[spec-type :as spec]] + (case spec-type + :inner (let [[_inner depth] spec] + depth) + 0))) + max + 0 + specs) + key-type* (cond + (qualified-symbol? key) 0 + (simple-symbol? key) 1 + (pattern? key) 2)] + [(- max-depth) key-type* (str key)])) (defn- custom-indent [zloc indents context] (if (empty? indents) diff --git a/cljfmt/test/cljfmt/core_test.cljc b/cljfmt/test/cljfmt/core_test.cljc index 7721d1e7..3f063f81 100644 --- a/cljfmt/test/cljfmt/core_test.cljc +++ b/cljfmt/test/cljfmt/core_test.cljc @@ -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?