Skip to content

Commit 4930267

Browse files
[print] Add special printing rules for records and allow meta :type overrides
1 parent 2333f4b commit 4930267

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
## master (unreleased)
44

5+
* [#314](https://github.com/clojure-emacs/orchard/pull/314): Print: add special printing rules for records and allow meta :type overrides.
6+
57
## 0.34.0 (2025-04-18)
68

7-
* [#335](https://github.com/clojure-emacs/orchard/pull/335) Add `orchard.pp` and pretty view mode.
9+
* [#335](https://github.com/clojure-emacs/orchard/pull/335): Add `orchard.pp` and pretty view mode.
810

911
## 0.33.0 (2025-04-08)
1012

src/orchard/print.clj

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
(:import
1212
(clojure.core Eduction)
1313
(clojure.lang AFunction Compiler IDeref IPending IPersistentMap
14-
IPersistentSet IPersistentVector Keyword Symbol TaggedLiteral
15-
Var)
14+
IPersistentSet IPersistentVector IRecord Keyword Symbol
15+
TaggedLiteral Var)
1616
(java.util List Map Map$Entry)
1717
(mx.cider.orchard TruncatingStringWriter
1818
TruncatingStringWriter$TotalLimitExceeded))
@@ -23,10 +23,13 @@
2323
(fn [x _]
2424
(cond
2525
(nil? x) nil
26+
;; Allow meta :type override regular types.
27+
(:type (meta x)) (type x)
2628
(instance? String x) :string
2729
(instance? Number x) :scalar
2830
(instance? Keyword x) :scalar
2931
(instance? Symbol x) :scalar
32+
(instance? IRecord x) :record
3033
(instance? Map x) :map
3134
(instance? IPersistentVector x) :vector
3235
(instance? List x) :list
@@ -118,14 +121,22 @@
118121
(defmethod print :set [x w]
119122
(print-coll w x " " "#{" "}"))
120123

121-
(defmethod print :map [^Map x, w]
124+
(defn- print-map [^Map x, w]
122125
(if (.isEmpty x)
123126
(print-method x w)
124127
(let [;; If the map is a Clojure map, don't take the entrySet but iterate
125128
;; directly as the order might be important.
126129
coll (if (instance? IPersistentMap x) x (.entrySet ^Map x))]
127130
(print-coll w coll ", " "{" "}" true))))
128131

132+
(defmethod print :map [^Map x, w]
133+
(print-map x w))
134+
135+
(defmethod print :record [x, ^TruncatingStringWriter w]
136+
(.write w "#")
137+
(.write w (.getSimpleName (class x)))
138+
(print-map x w))
139+
129140
(defmethod print :array [x, ^TruncatingStringWriter w]
130141
(let [ct (.getName (or (.getComponentType (class x)) Object))
131142
as-seq (seq x)]

test/orchard/inspect_test.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,13 +960,13 @@
960960
" " [:value ":flags" pos?] " = " [:value "#{:public}" pos?]
961961
[:newline]
962962
" " [:value ":members" pos?] " = "
963-
[:value #=(str "{clone [{:name clone, :return-type java.lang.Object, :declaring-class java.lang.Object, "
963+
[:value #=(str "{clone [#Method{:name clone, :return-type java.lang.Object, :declaring-class java.lang.Object, "
964964
":parameter-types [], :exception-types [java.lang.CloneNotSupportedException], ...}], equals "
965-
"[{:name equals, :return-type boolean, :declaring-class java.lang.Object, :parameter-types "
966-
"[java.lang.Object], :exception-types [], ...}], finalize [{:name finalize, :return-type void, "
965+
"[#Method{:name equals, :return-type boolean, :declaring-class java.lang.Object, :parameter-types "
966+
"[java.lang.Object], :exception-types [], ...}], finalize [#Method{:name finalize, :return-type void, "
967967
":declaring-class java.lang.Object, :parameter-types [], :exception-types [java.lang.Throwable], "
968-
"...}], getClass [{:name getClass, :return-type java.lang.Class, :declaring-class java.lang.Object, "
969-
":parameter-types [], :exception-types [], ...}], hashCode [{:name hashCode, :return-type int, "
968+
"...}], getClass [#Method{:name getClass, :return-type java.lang.Class, :declaring-class java.lang.Object, "
969+
":parameter-types [], :exception-types [], ...}], hashCode [#Method{:name hashCode, :return-type int, "
970970
":declaring-class java.lang.Object, :parameter-types [], :exception-types [], ...}], ...}")
971971
pos?]
972972
[:newline]

test/orchard/print_test.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"(1 1 1 1 1)" (java.util.ArrayList. ^java.util.Collection (repeat 5 1))
7878
"{:a 1, :b 2}" (let [^java.util.Map x {:a 1 :b 2}]
7979
(java.util.HashMap. x))
80-
"{:a 1, :b 2, :c 3, :d 4}" (->TestRecord 1 2 3 4)
80+
"#TestRecord{:a 1, :b 2, :c 3, :d 4}" (->TestRecord 1 2 3 4)
8181
"long[] {1, 2, 3, 4}" (long-array [1 2 3 4])
8282
"long[] {}" (long-array [])
8383
"java.lang.Long[] {0, 1, 2, 3, 4}" (into-array Long (range 5))
@@ -144,6 +144,12 @@
144144
(equiv [t o] (.equals t o))
145145
(seq [_] (seq [1 2 3])))))))
146146

147+
(defmethod orchard.print/print ::custom-rec [_ w] (sut/print 'hello w))
148+
149+
(deftest print-custom-print-method
150+
(is (= "hello"
151+
(sut/print-str (with-meta (->TestRecord 1 2 3 4) {:type ::custom-rec})))))
152+
147153
(deftest pprint-no-limits
148154
(are [result form] (match? result (sut/pprint-str form))
149155
"1" 1

0 commit comments

Comments
 (0)