|
8 | 8 | {:author "Oleksandr Yakushev"
|
9 | 9 | :added "0.24"}
|
10 | 10 | (:refer-clojure :exclude [print print-str])
|
| 11 | + (:require [clojure.string :as str]) |
11 | 12 | (:import
|
12 | 13 | (clojure.core Eduction)
|
13 | 14 | (clojure.lang AFunction Compiler IDeref IPending IPersistentMap MultiFn
|
|
16 | 17 | (java.io Writer)
|
17 | 18 | (java.util List Map Map$Entry)
|
18 | 19 | (mx.cider.orchard TruncatingStringWriter
|
19 |
| - TruncatingStringWriter$TotalLimitExceeded)) |
20 |
| - (:require [clojure.string :as str])) |
| 20 | + TruncatingStringWriter$TotalLimitExceeded))) |
21 | 21 |
|
22 | 22 | (defmulti print
|
23 | 23 | (fn [x _]
|
|
28 | 28 | (instance? String x) :string
|
29 | 29 | (instance? Double x) :double
|
30 | 30 | (instance? Number x) :scalar
|
31 |
| - (instance? Keyword x) :scalar |
32 | 31 | (instance? Symbol x) :scalar
|
| 32 | + (instance? Keyword x) :keyword |
33 | 33 | (instance? IRecord x) :record
|
34 | 34 | (instance? Map x) :map
|
35 | 35 | (instance? IPersistentVector x) :vector
|
|
52 | 52 | "When displaying collection diffs, whether to hide matching values."
|
53 | 53 | false)
|
54 | 54 |
|
| 55 | +(def ^:dynamic *pov-ns* |
| 56 | + "The \"point-of-view namespace\" for the printer. When bound to a namespace |
| 57 | + object, use this namespace data to shorten qualified keywords: |
| 58 | + - print `::foo` instead of `:pov.ns/foo` |
| 59 | + - print `::alias/foo` instead of `:ns.aliases.in.pov.ns/foo`" |
| 60 | + nil) |
| 61 | + |
55 | 62 | (defn- print-coll-item
|
56 | 63 | "Print an item in the context of a collection. When printing a map, don't print
|
57 | 64 | `[]` characters around map entries."
|
|
114 | 121 | (defmethod print :scalar [^Object x, ^Writer w]
|
115 | 122 | (.write w (.toString x)))
|
116 | 123 |
|
| 124 | +(defmethod print :keyword [^Keyword kw, ^Writer w] |
| 125 | + (if-some [kw-ns (and *pov-ns* (namespace kw))] |
| 126 | + (if (= kw-ns (name (ns-name *pov-ns*))) |
| 127 | + (do (.write w "::") |
| 128 | + (.write w (name kw))) |
| 129 | + (if-some [matched-alias (some (fn [[alias ns]] |
| 130 | + (when (= kw-ns (name (ns-name ns))) |
| 131 | + alias)) |
| 132 | + (ns-aliases *pov-ns*))] |
| 133 | + (do (.write w "::") |
| 134 | + (.write w (name matched-alias)) |
| 135 | + (.write w "/") |
| 136 | + (.write w (name kw))) |
| 137 | + (.write w (.toString kw)))) |
| 138 | + (.write w (.toString kw)))) |
| 139 | + |
117 | 140 | (defmethod print :double [x, ^Writer w]
|
118 | 141 | (cond (= Double/POSITIVE_INFINITY x) (.write w "##Inf")
|
119 | 142 | (= Double/NEGATIVE_INFINITY x) (.write w "##-Inf")
|
|
0 commit comments