Skip to content

Commit 9693561

Browse files
[pp] Print short record names by default
1 parent 2c2d53a commit 9693561

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## master (unreleased)
44

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

78
## 0.37.0 (2025-09-19)
89

src/orchard/pp.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@
123123
prefix."
124124
[coll]
125125
(if (record? coll)
126-
[(str "#" (.getName (class coll)) "{") coll]
126+
[(str "#" (if print/*short-record-names*
127+
(.getSimpleName (class coll))
128+
(.getName (class coll))) "{") coll]
127129
;; If all keys in the map share a namespace and *print-
128130
;; namespace-maps* is true, print the map using map namespace
129131
;; syntax (e.g. #:a{:b 1} instead of {:a/b 1}). If the map is

src/orchard/print.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
- print `::alias/foo` instead of `:ns.aliases.in.pov.ns/foo`"
6060
nil)
6161

62+
(def ^:dynamic *short-record-names*
63+
"When true, only simple record classnames will be displayed instead of FQNs."
64+
true)
65+
6266
(defn- print-coll-item
6367
"Print an item in the context of a collection. When printing a map, don't print
6468
`[]` characters around map entries."
@@ -171,7 +175,9 @@
171175

172176
(defmethod print :record [x, ^Writer w]
173177
(.write w "#")
174-
(.write w (.getSimpleName (class x)))
178+
(.write w (if *short-record-names*
179+
(.getSimpleName (class x))
180+
(.getName (class x))))
175181
(print-map x w))
176182

177183
(defmethod print :array [x, ^Writer w]

test/orchard/pp_test.clj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
"(1 1 1 1 1)" (java.util.ArrayList. ^java.util.Collection (repeat 5 1))
249249
"{:a 1, :b 2}" (let [^java.util.Map x {:a 1 :b 2}]
250250
(java.util.HashMap. x))
251-
"#orchard.print_test.TestRecord{:a 1, :b 2, :c 3, :d 4}" (orchard.print-test/->TestRecord 1 2 3 4)
251+
"#TestRecord{:a 1, :b 2, :c 3, :d 4}" (orchard.print-test/->TestRecord 1 2 3 4)
252252
"[1 2 3 4]" (long-array [1 2 3 4])
253253
"[]" (long-array [])
254254
"[0 1 2 3 4]" (into-array Long (range 5))
@@ -281,3 +281,17 @@
281281
(testing "writer won't go much over total-length"
282282
(is (= 2003 (count (binding [print/*max-total-length* 2000]
283283
(print/print-str orchard.print-test/infinite-map)))))))
284+
285+
(deftest short-record-names-test
286+
(testing "*short-record-names* controls how records are printed"
287+
(is (= "#TestRecord{:a (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
288+
:b (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
289+
:c 3,
290+
:d 4}"
291+
(sut/pprint-str (orchard.print-test/->TestRecord (range 15) (range 15) 3 4))))
292+
(binding [print/*short-record-names* false]
293+
(is (= "#orchard.print_test.TestRecord{:a (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
294+
:b (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14),
295+
:c 3,
296+
:d 4}"
297+
(sut/pprint-str (orchard.print-test/->TestRecord (range 15) (range 15) 3 4)))))))

test/orchard/print_test.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,10 @@
193193
(reduce f (range 10)))
194194
(coll-reduce [_ f init]
195195
(reduce f init (range 10)))))))))
196+
197+
(deftest short-record-names-test
198+
(testing "*short-record-names* controls how records are printed"
199+
(is (= "#TestRecord{:a 1, :b 2, :c 3, :d 4}" (sut/print-str (->TestRecord 1 2 3 4))))
200+
(binding [sut/*short-record-names* false]
201+
(is (= "#orchard.print_test.TestRecord{:a 1, :b 2, :c 3, :d 4}"
202+
(sut/print-str (->TestRecord 1 2 3 4)))))))

0 commit comments

Comments
 (0)