Skip to content

Commit 05a8e8b

Browse files
puredangerrichhickey
authored andcommitted
pprint maps with namespace map literal syntax when *print-namespace-maps*
Signed-off-by: Rich Hickey <richhickey@gmail.com>
1 parent 313da55 commit 05a8e8b

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

src/clj/clojure/core_print.clj

+16-17
Original file line numberDiff line numberDiff line change
@@ -233,26 +233,25 @@
233233
(defn- lift-ns
234234
"Returns [lifted-ns lifted-map] or nil if m can't be lifted."
235235
[m]
236-
(loop [ns nil
237-
[[k v :as entry] & entries] (seq m)
238-
lm (empty m)]
239-
(if entry
240-
(when (or (keyword? k) (symbol? k))
241-
(if ns
242-
(when (= ns (namespace k))
243-
(recur ns entries (assoc lm (strip-ns k) v)))
244-
(when-let [new-ns (namespace k)]
245-
(recur new-ns entries (assoc lm (strip-ns k) v)))))
246-
[ns lm])))
236+
(when *print-namespace-maps*
237+
(loop [ns nil
238+
[[k v :as entry] & entries] (seq m)
239+
lm (empty m)]
240+
(if entry
241+
(when (or (keyword? k) (symbol? k))
242+
(if ns
243+
(when (= ns (namespace k))
244+
(recur ns entries (assoc lm (strip-ns k) v)))
245+
(when-let [new-ns (namespace k)]
246+
(recur new-ns entries (assoc lm (strip-ns k) v)))))
247+
[ns lm]))))
247248

248249
(defmethod print-method clojure.lang.IPersistentMap [m, ^Writer w]
249250
(print-meta m w)
250-
(if *print-namespace-maps*
251-
(let [[ns lift-map] (lift-ns m)]
252-
(if ns
253-
(print-prefix-map (str "#:" ns) lift-map pr-on w)
254-
(print-map m pr-on w)))
255-
(print-map m pr-on w)))
251+
(let [[ns lift-map] (lift-ns m)]
252+
(if ns
253+
(print-prefix-map (str "#:" ns) lift-map pr-on w)
254+
(print-map m pr-on w))))
256255

257256
(defmethod print-dup java.util.Map [m, ^Writer w]
258257
(print-ctor m #(print-map (seq %1) print-dup %2) w))

src/clj/clojure/pprint/dispatch.clj

+17-13
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,23 @@
9292

9393
;;; (def pprint-map (formatter-out "~<{~;~@{~<~w~^ ~_~w~:>~^, ~_~}~;}~:>"))
9494
(defn- pprint-map [amap]
95-
(pprint-logical-block :prefix "{" :suffix "}"
96-
(print-length-loop [aseq (seq amap)]
97-
(when aseq
98-
(pprint-logical-block
99-
(write-out (ffirst aseq))
100-
(.write ^java.io.Writer *out* " ")
101-
(pprint-newline :linear)
102-
(set! *current-length* 0) ; always print both parts of the [k v] pair
103-
(write-out (fnext (first aseq))))
104-
(when (next aseq)
105-
(.write ^java.io.Writer *out* ", ")
106-
(pprint-newline :linear)
107-
(recur (next aseq)))))))
95+
(let [[ns lift-map] (when (not (record? amap))
96+
(#'clojure.core/lift-ns amap))
97+
amap (or lift-map amap)
98+
prefix (if ns (str "#:" ns "{") "{")]
99+
(pprint-logical-block :prefix prefix :suffix "}"
100+
(print-length-loop [aseq (seq amap)]
101+
(when aseq
102+
(pprint-logical-block
103+
(write-out (ffirst aseq))
104+
(.write ^java.io.Writer *out* " ")
105+
(pprint-newline :linear)
106+
(set! *current-length* 0) ; always print both parts of the [k v] pair
107+
(write-out (fnext (first aseq))))
108+
(when (next aseq)
109+
(.write ^java.io.Writer *out* ", ")
110+
(pprint-newline :linear)
111+
(recur (next aseq))))))))
108112

109113
(def ^{:private true} pprint-set (formatter-out "~<#{~;~@{~w~^ ~:_~}~;}~:>"))
110114

0 commit comments

Comments
 (0)