Skip to content

Commit

Permalink
Merge pull request #345 from frenchy64/ws-resolved-ambiguity
Browse files Browse the repository at this point in the history
Don't rewrite '~ @foo' to '~@foo'
  • Loading branch information
weavejester authored Aug 22, 2024
2 parents c832761 + 7734fe2 commit fe8ae0e
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 @@ -704,7 +704,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 @@ -1588,7 +1615,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 @@ -1647,7 +1676,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 @@ -1707,5 +1738,7 @@
"(~@foo"
" bar)"
"(#:foo{:bar 1}"
" baz)"]
" baz)"
"(~ @foo"
" bar)"]
{:function-arguments-indentation :zprint})))))

0 comments on commit fe8ae0e

Please sign in to comment.