Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
24 changes: 20 additions & 4 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,25 @@
(or (symbol-matches-key? (fully-qualified-symbol possible-sym context) key)
(symbol-matches-key? (remove-namespace possible-sym) key))))

(defn- form-matches-thread-macro? [zloc context]
(let [possible-sym (form-symbol zloc)
fully-qualified-sym (fully-qualified-symbol possible-sym context)
sym-without-namespace (remove-namespace possible-sym)]
(some #(or (symbol-matches-key? fully-qualified-sym %)
(symbol-matches-key? sym-without-namespace %))
#{'-> 'as-> 'some-> 'cond->})))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing it this way:

(defn- form-symbol-matches? [zloc pred context]
  (let [possible-sym (form-symbol zloc)]
    (or (pred (fully-qualified-symbol possible-sym context))
        (pred (remove-namespace possible-sym)))))

(defn- form-matches-key? [zloc key context]
  (form-symbol-matches? zloc #(symbol-matches-key? % key) context))

(defn- form-matches-thread-macro? [zloc context]
  (form-symbol-matches? zloc #{'-> 'as-> 'some-> 'cond->} context))


(defn- first-argument? [zloc]
(= (z/right (z/leftmost zloc)) zloc))

(defn- in-thread-macro? [zloc context]
(and (form-matches-thread-macro? zloc context) (not (first-argument? zloc))))

(defn- inner-indent [zloc key depth idx context]
(let [top (nth (iterate z/up zloc) depth)]
(let [top (nth (iterate z/up zloc) depth)
adjusted-idx (cond-> idx (in-thread-macro? (z/up top) context) (some-> dec))]
(when (and (form-matches-key? top key context)
(or (nil? idx) (index-matches-top-argument? zloc depth idx)))
(or (nil? idx) (index-matches-top-argument? zloc depth adjusted-idx)))
(let [zup (z/up zloc)]
(+ (margin zup) (indent-width zup))))))

Expand All @@ -302,9 +317,10 @@

(defn- block-indent [zloc key idx context]
(when (form-matches-key? zloc key context)
(let [zloc-after-idx (some-> zloc (nth-form (inc idx)))]
(let [adjusted-idx (cond-> idx (in-thread-macro? (z/up zloc) context) (some-> dec (max 0)))
zloc-after-idx (some-> zloc (nth-form (inc adjusted-idx)))]
(if (and (or (nil? zloc-after-idx) (first-form-in-line? zloc-after-idx))
(> (index-of zloc) idx))
(> (index-of zloc) adjusted-idx))
(inner-indent zloc key 0 nil context)
(list-indent zloc context)))))

Expand Down
34 changes: 33 additions & 1 deletion cljfmt/test/cljfmt/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,39 @@
["#:clj {:a :b"
":c :d}"]
["#:clj {:a :b"
" :c :d}"]))))
" :c :d}"])))

(testing "thread first"
(is (reformats-to?
["(-> v"
"(cond->"
"a b"
"c d))"]
["(-> v"
" (cond->"
" a b"
" c d))"]))
(is (reformats-to?
["(cond-> v"
"a b"
"c d)"]
["(cond-> v"
" a b"
" c d)"]))
(is (reformats-to?
["(-> v"
"(cond-> a b"
"c d))"]
["(-> v"
" (cond-> a b"
" c d))"]))
(is (reformats-to?
["(-> (cond-> a"
"odd? inc)"
"inc)"]
["(-> (cond-> a"
" odd? inc)"
" inc)"]))))

(deftest test-remove-multiple-non-indenting-spaces
(let [opts {:remove-multiple-non-indenting-spaces? true}]
Expand Down