diff --git a/cljfmt/src/cljfmt/core.cljc b/cljfmt/src/cljfmt/core.cljc index 1e9f0e8..db84570 100644 --- a/cljfmt/src/cljfmt/core.cljc +++ b/cljfmt/src/cljfmt/core.cljc @@ -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))) @@ -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*)) diff --git a/cljfmt/test/cljfmt/core_test.cljc b/cljfmt/test/cljfmt/core_test.cljc index 7721d1e..3310ca1 100644 --- a/cljfmt/test/cljfmt/core_test.cljc +++ b/cljfmt/test/cljfmt/core_test.cljc @@ -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}] @@ -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 @@ -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? @@ -1687,5 +1718,7 @@ "(~@foo" " bar)" "(#:foo{:bar 1}" - " baz)"] + " baz)" + "(~ @foo" + " bar)"] {:function-arguments-indentation :zprint})))))