diff --git a/src/fipp/clojure.cljc b/src/fipp/clojure.cljc index 60b67a0..fdeca60 100644 --- a/src/fipp/clojure.cljc +++ b/src/fipp/clojure.cljc @@ -126,10 +126,21 @@ ;;; Format deref, quote, unquote, var -(defn pretty-quote [p [macro arg]] - [:span (case (keyword (name macro)) - :deref "@", :quote "'", :unquote "~", :var "#'") - (visit p arg)]) +(defn- deref? [arg] + (and (seq? arg) + (= 2 (count arg)) + (= 'clojure.core/deref (first arg)))) + +(defn pretty-quote [p [macro arg :as all]] + (if-some [rm (when (= 2 (count all)) + (case macro + clojure.core/deref "@" + quote "'" + clojure.core/unquote (if (deref? arg) "~ " "~") + var "#'" + nil))] + [:span rm (visit p arg)] + (apply list-group (interpose " " (map #(visit p %) all))))) ;;; Format let, loop, and similar @@ -183,7 +194,7 @@ pretty-ns '[ns] pretty-let '[binding doseq dotimes for if-let if-some let let* loop loop* when-first when-let when-some with-local-vars with-open with-redefs] - pretty-quote '[deref quote unquote var] + pretty-quote '[clojure.core/deref quote clojure.core/unquote var] pretty-type '[deftype defrecord] pretty-reify '[reify]})) diff --git a/test/fipp/clojure_test.cljc b/test/fipp/clojure_test.cljc index db620ca..c4a9832 100644 --- a/test/fipp/clojure_test.cljc +++ b/test/fipp/clojure_test.cljc @@ -18,4 +18,33 @@ #?(:clj "[#'clojure.core/inc ^{:y 1} x]" :cljs "[#'cljs.core/inc ^{:y 1} x]"))))) +(deftest lossy-reader-unexpansion-test + (is (= (clean (with-out-str (pprint '(let [deref foo] (deref foo))))) + "(let [deref foo] (deref foo))")) + (is (= (clean (with-out-str (pprint '~(deref foo)))) + "~(deref foo)")) + (is (= (clean (with-out-str (pprint '(clojure.core/deref)))) + "(clojure.core/deref)")) + (is (= (clean (with-out-str (pprint '(clojure.core/deref 1 2 3)))) + "(clojure.core/deref 1 2 3)")) + (is (= (clean (with-out-str (pprint '(clojure.core/unquote)))) + "(clojure.core/unquote)")) + (is (= (clean (with-out-str (pprint '(clojure.core/unquote 1 2 3)))) + "(clojure.core/unquote 1 2 3)")) + (is (= (clean (with-out-str (pprint '(unquote (deref foo))))) + "(unquote (deref foo))"))) + +(deftest quote-deref-test + (testing "~(deref) is not ~@" + (is (= (clean (with-out-str (pprint '(clojure.core/unquote (clojure.core/deref foo))))) + "~ @foo")) + (is (= (clean (with-out-str (pprint '~(clojure.core/deref foo)))) + "~ @foo")) + (is (= (clean (with-out-str (pprint '~(clojure.core/deref foo)))) + "~ @foo")) + (is (= (clean (with-out-str (pprint '~ @foo))) + "~ @foo")) + (is (= (clean (with-out-str (pprint '~,@foo))) + "~ @foo")))) + ;;TODO lots more tests