diff --git a/CHANGELOG.md b/CHANGELOG.md index 16601cdd..d14ae3b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bugs fixed * [#719](https://github.com/clojure-emacs/cider-nrepl/issues/719): `middleware.test`: gracefully handle exceptions thrown within fixtures. +* [#722](https://github.com/clojure-emacs/cider-nrepl/issues/722): `middleware.format`: print otherwise non-serializable objects as strings. ## 0.27.2 (2021-10-03) diff --git a/src/cider/nrepl/middleware/format.clj b/src/cider/nrepl/middleware/format.clj index c44db5b5..900b46da 100644 --- a/src/cider/nrepl/middleware/format.clj +++ b/src/cider/nrepl/middleware/format.clj @@ -40,7 +40,10 @@ (let [reader (readers/string-push-back-reader s) sentinel (Object.)] (loop [forms []] - (let [form (edn/read {:eof sentinel} reader)] + (let [form (edn/read {:eof sentinel + :default (fn [_tag value] + (pr-str value))} + reader)] (if (= sentinel form) forms (recur (conj forms form))))))) diff --git a/test/clj/cider/nrepl/middleware/format_test.clj b/test/clj/cider/nrepl/middleware/format_test.clj index f2ae694f..057279a9 100644 --- a/test/clj/cider/nrepl/middleware/format_test.clj +++ b/test/clj/cider/nrepl/middleware/format_test.clj @@ -113,6 +113,19 @@ (is (= formatted-edn-sample formatted-edn)) (is (= #{"done"} status)))) + ;; See: https://github.com/clojure-emacs/cider-nrepl/issues/722 + (testing "Objects of classes without an associated data-reader function are converted to strings via `pr-str`" + (let [{:keys [formatted-edn status] + :as response} (session/message {:op "format-edn" + :edn (pr-str [1 2 (Object.) 3 4])}) + [a b ^String c d e] (read-string formatted-edn)] + (testing (pr-str response) + (is (= [1 2 3 4] + [a b d e])) + (is (string? c)) + (is (.contains c "java.lang.Object@")) + (is (= #{"done"} status))))) + (testing "format-edn works for multiple forms" (let [{:keys [formatted-edn status]} (session/message {:op "format-edn" :edn ugly-edn-forms-sample})]