Skip to content

Commit

Permalink
catch non-sexprable zlocs
Browse files Browse the repository at this point in the history
  • Loading branch information
rsh-blip committed Mar 13, 2023
1 parent f078b9f commit 2b50ff2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
35 changes: 27 additions & 8 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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))
Expand All @@ -149,23 +174,17 @@
([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))

(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))

Expand Down
68 changes: 39 additions & 29 deletions cljfmt/test/cljfmt/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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})))))

0 comments on commit 2b50ff2

Please sign in to comment.