Skip to content

Commit

Permalink
Don't rewrite ~ @foo to ~@foo
Browse files Browse the repository at this point in the history
The problem is ~ @foo is being reformatted to ~@foo, a different
expression. We fix this by not treating spaces between ~ and @
as "surrounding spaces".
  • Loading branch information
frenchy64 committed Aug 13, 2024
1 parent 8d8e26d commit 7734fe2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
20 changes: 15 additions & 5 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
(defn- transform [form zf & args]
(z/root (apply zf (z/of-node form) args)))

(defn- surrounding? [zloc p?]
(and (p? zloc) (or (nil? (z/left* zloc))
(nil? (z/skip z/right* p? zloc)))))

(defn root? [zloc]
(nil? (z/up* zloc)))

Expand All @@ -49,9 +45,23 @@
(defn- clojure-whitespace? [zloc]
(z/whitespace? zloc))

(defn- unquote? [zloc]
(and zloc (= (n/tag (z/node zloc)) :unquote)))

(defn- deref? [zloc]
(and zloc (= (n/tag (z/node zloc)) :deref)))

(defn- unquote-deref? [zloc]
(and (deref? zloc)
(unquote? (z/up* zloc))))

(defn- surrounding-whitespace? [zloc]
(and (not (top? zloc))
(surrounding? zloc clojure-whitespace?)))
(clojure-whitespace? zloc)
(or (and (nil? (z/left* zloc))
;; don't convert ~ @ to ~@
(not (unquote-deref? (z/right* zloc))))
(nil? (z/skip z/right* clojure-whitespace? zloc)))))

(defn remove-surrounding-whitespace [form]
(transform form edit-all surrounding-whitespace? z/remove*))
Expand Down
41 changes: 37 additions & 4 deletions cljfmt/test/cljfmt/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,34 @@
["#:clj {:a :b"
":c :d}"]
["#:clj {:a :b"
" :c :d}"]))))
" :c :d}"])))
(testing "~ @ is not ~@"
(is (reformats-to?
["~ @foo"]
["~ @foo"]))
(is (reformats-to?
["~(deref foo)"]
["~(deref foo)"]))
(is (reformats-to?
["~(clojure.core/deref foo)"]
["~(clojure.core/deref foo)"]))
(is (reformats-to?
["~ @foo"]
["~ @foo"]))
(is (reformats-to?
["~ @foo"]
["~ @foo"]))
(is (reformats-to?
["~\n@foo"]
["~"
" @foo"]))
(is (reformats-to?
["~;;comment\n@foo"]
["~;;comment"
" @foo"]))
(is (reformats-to?
["~#_a@foo"]
["~#_a @foo"]))))

(deftest test-remove-multiple-non-indenting-spaces
(let [opts {:remove-multiple-non-indenting-spaces? true}]
Expand Down Expand Up @@ -1568,7 +1595,9 @@
"(~@foo"
"bar)"
"(#:foo{:bar 1}"
"baz)"]]
"baz)"
"(~ @foo"
"bar)"]]
(testing ":cursive style uses 2 spaces unless starting with a collection"
(is (reformats-to?
input
Expand Down Expand Up @@ -1627,7 +1656,9 @@
"(~@foo"
" bar)"
"(#:foo{:bar 1}"
" baz)"]
" baz)"
"(~ @foo"
" bar)"]
{:function-arguments-indentation :cursive})))
(testing ":zprint uses 2 spaces if starting with a symbol, keyword, or list"
(is (reformats-to?
Expand Down Expand Up @@ -1687,5 +1718,7 @@
"(~@foo"
" bar)"
"(#:foo{:bar 1}"
" baz)"]
" baz)"
"(~ @foo"
" bar)"]
{:function-arguments-indentation :zprint})))))

0 comments on commit 7734fe2

Please sign in to comment.