Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## master (unreleased)

- [#359](https://github.com/clojure-emacs/orchard/pull/359): Print: protect printing from broken eductions.
- [#360](https://github.com/clojure-emacs/orchard/pull/360): Pretty-print: print short record names by default.

## 0.37.0 (2025-09-19)

Expand Down
4 changes: 3 additions & 1 deletion src/orchard/pp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
prefix."
[coll]
(if (record? coll)
[(str "#" (.getName (class coll)) "{") coll]
[(str "#" (if print/*short-record-names*
(.getSimpleName (class coll))
(.getName (class coll))) "{") coll]
;; If all keys in the map share a namespace and *print-
;; namespace-maps* is true, print the map using map namespace
;; syntax (e.g. #:a{:b 1} instead of {:a/b 1}). If the map is
Expand Down
8 changes: 7 additions & 1 deletion src/orchard/print.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
- print `::alias/foo` instead of `:ns.aliases.in.pov.ns/foo`"
nil)

(def ^:dynamic *short-record-names*
"When true, only simple record classnames will be displayed instead of FQNs."
true)

(defn- print-coll-item
"Print an item in the context of a collection. When printing a map, don't print
`[]` characters around map entries."
Expand Down Expand Up @@ -171,7 +175,9 @@

(defmethod print :record [x, ^Writer w]
(.write w "#")
(.write w (.getSimpleName (class x)))
(.write w (if *short-record-names*
(.getSimpleName (class x))
(.getName (class x))))
(print-map x w))

(defmethod print :array [x, ^Writer w]
Expand Down
16 changes: 15 additions & 1 deletion test/orchard/pp_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
"(1 1 1 1 1)" (java.util.ArrayList. ^java.util.Collection (repeat 5 1))
"{:a 1, :b 2}" (let [^java.util.Map x {:a 1 :b 2}]
(java.util.HashMap. x))
"#orchard.print_test.TestRecord{:a 1, :b 2, :c 3, :d 4}" (orchard.print-test/->TestRecord 1 2 3 4)
"#TestRecord{:a 1, :b 2, :c 3, :d 4}" (orchard.print-test/->TestRecord 1 2 3 4)
"[1 2 3 4]" (long-array [1 2 3 4])
"[]" (long-array [])
"[0 1 2 3 4]" (into-array Long (range 5))
Expand Down Expand Up @@ -281,3 +281,17 @@
(testing "writer won't go much over total-length"
(is (= 2003 (count (binding [print/*max-total-length* 2000]
(print/print-str orchard.print-test/infinite-map)))))))

(deftest short-record-names-test
(testing "*short-record-names* controls how records are printed"
(is (= "#TestRecord{:a (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
:b (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
:c 3,
:d 4}"
(sut/pprint-str (orchard.print-test/->TestRecord (range 15) (range 15) 3 4))))
(binding [print/*short-record-names* false]
(is (= "#orchard.print_test.TestRecord{:a (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
:b (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
:c 3,
:d 4}"
(sut/pprint-str (orchard.print-test/->TestRecord (range 15) (range 15) 3 4)))))))
7 changes: 7 additions & 0 deletions test/orchard/print_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,10 @@
(reduce f (range 10)))
(coll-reduce [_ f init]
(reduce f init (range 10)))))))))

(deftest short-record-names-test
(testing "*short-record-names* controls how records are printed"
(is (= "#TestRecord{:a 1, :b 2, :c 3, :d 4}" (sut/print-str (->TestRecord 1 2 3 4))))
(binding [sut/*short-record-names* false]
(is (= "#orchard.print_test.TestRecord{:a 1, :b 2, :c 3, :d 4}"
(sut/print-str (->TestRecord 1 2 3 4)))))))