diff --git a/cljfmt/src/cljfmt/core.cljc b/cljfmt/src/cljfmt/core.cljc index ae3b963..cc7319e 100644 --- a/cljfmt/src/cljfmt/core.cljc +++ b/cljfmt/src/cljfmt/core.cljc @@ -59,6 +59,12 @@ (defn- element? [zloc] (and zloc (not (z/whitespace-or-comment? zloc)))) +(defn- comment? [zloc] + (some-> zloc z/node n/comment?)) + +(defn- line-break? [zloc] + (or (z/linebreak? zloc) (comment? zloc))) + (defn- reader-macro? [zloc] (and zloc (= (n/tag (z/node zloc)) :reader-macro))) @@ -139,8 +145,27 @@ (and align-args (align-args zloc-pos))))) +(defn- sexpr-able? [zloc] + (try + (z/sexpr zloc) + true + (catch Exception _e + false))) + +(defn- multiline? [zloc] + (z/find (z/down zloc) z/right* line-break?)) + +(defn- align-candidate? [zloc] + (and (multiline? zloc) + (sexpr-able? zloc))) + +(defn- align-binding? [zloc align-bindings-args] + (and (binding? zloc align-bindings-args) + (align-candidate? zloc))) + (defn- align-map? [zloc] - (z/map? zloc)) + (and (z/map? zloc) + (align-candidate? zloc))) (defn insert-missing-whitespace [form] (transform form edit-all missing-whitespace? z/insert-space-right)) @@ -149,7 +174,7 @@ ([form] (align-bindings form default-align-bindings-args)) ([form align-bindings-args] - (transform form edit-all #(binding? % align-bindings-args) align-binding))) + (transform form edit-all #(align-binding? % align-bindings-args) align-binding))) (defn- align-maps [form] (transform form edit-all align-map? align-map)) @@ -157,15 +182,9 @@ (defn- space? [zloc] (= (z/tag zloc) :whitespace)) -(defn- comment? [zloc] - (some-> zloc z/node n/comment?)) - (defn- comma? [zloc] (some-> zloc z/node n/comma?)) -(defn- line-break? [zloc] - (or (z/linebreak? zloc) (comment? zloc))) - (defn- skip-whitespace [zloc] (z/skip z/next* space? zloc)) diff --git a/cljfmt/test/cljfmt/core_test.cljc b/cljfmt/test/cljfmt/core_test.cljc index 1b2acd4..c408d0b 100644 --- a/cljfmt/test/cljfmt/core_test.cljc +++ b/cljfmt/test/cljfmt/core_test.cljc @@ -1470,34 +1470,44 @@ " :longer 2}"] {:align-maps? true}))))) -(deftest test-align-associative-abnormal - (testing "abnormal test cases" - (testing "indentation off #1" +(deftest test-align-associative-abnormal-1 + (testing "cljs map values" (is (reformats-to? - ["{ :a 1" - " :longer 2}"] - ["{:a 1" - " :longer 2}"] - {:align-maps? true}))) - (testing "indentation off #2" - (is (reformats-to? - ["{ :a 1" - " :longer 2}"] - ["{:a 1" - " :longer 2}"] - {:align-maps? true}))) - (testing "indentation off #3" + ["{:indents {'thing.core/defthing [[:inner 0]]" + "'let [[:inner 0]]}" + "#?@(:cljs [:alias-map {}])}"] + ["{:indents {'thing.core/defthing [[:inner 0]]" + " 'let [[:inner 0]]}" + " #?@(:cljs [:alias-map {}])}"] + {:align-maps? true})))) + +(deftest test-align-associative-abnormal-2 + (testing "indentation off #1" + (is (reformats-to? + ["{ :a 1" + " :longer 2}"] + ["{:a 1" + " :longer 2}"] + {:align-maps? true}))) + (testing "indentation off #2" + (is (reformats-to? + ["{ :a 1" + " :longer 2}"] + ["{:a 1" + " :longer 2}"] + {:align-maps? true}))) + (testing "indentation off #3" + (is (reformats-to? + ["{:a 1" + " :longer 2}"] + ["{:a 1" + " :longer 2}"] + {:align-maps? true}))) + (testing "future effort?" + (testing "multi-value line" (is (reformats-to? - ["{:a 1" - " :longer 2}"] - ["{:a 1" - " :longer 2}"] - {:align-maps? true}))) - (testing "future effort?" - (testing "multi-value line" - (is (reformats-to? - ["{:a 1 :b 2" - " :longer 2}"] - ["{:a 1 :b 2" - " :longer 2}"] - {:align-maps? true})))))) + ["{:a 1 :b 2" + " :longer 2}"] + ["{:a 1 :b 2" + " :longer 2}"] + {:align-maps? true})))))