Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 16 additions & 5 deletions src/fipp/clojure.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]}))

Expand Down
29 changes: 29 additions & 0 deletions test/fipp/clojure_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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