-
-
Notifications
You must be signed in to change notification settings - Fork 54
[inspect] Add support for printing compact qualified keywords #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
{:author "Oleksandr Yakushev" | ||
:added "0.24"} | ||
(:refer-clojure :exclude [print print-str]) | ||
(:require [clojure.string :as str]) | ||
(:import | ||
(clojure.core Eduction) | ||
(clojure.lang AFunction Compiler IDeref IPending IPersistentMap MultiFn | ||
|
@@ -16,8 +17,7 @@ | |
(java.io Writer) | ||
(java.util List Map Map$Entry) | ||
(mx.cider.orchard TruncatingStringWriter | ||
TruncatingStringWriter$TotalLimitExceeded)) | ||
(:require [clojure.string :as str])) | ||
TruncatingStringWriter$TotalLimitExceeded))) | ||
|
||
(defmulti print | ||
(fn [x _] | ||
|
@@ -28,8 +28,8 @@ | |
(instance? String x) :string | ||
(instance? Double x) :double | ||
(instance? Number x) :scalar | ||
(instance? Keyword x) :scalar | ||
(instance? Symbol x) :scalar | ||
(instance? Keyword x) :keyword | ||
(instance? IRecord x) :record | ||
(instance? Map x) :map | ||
(instance? IPersistentVector x) :vector | ||
|
@@ -52,6 +52,13 @@ | |
"When displaying collection diffs, whether to hide matching values." | ||
false) | ||
|
||
(def ^:dynamic *pov-ns* | ||
"The \"point-of-view namespace\" for the printer. When bound to a namespace | ||
object, use this namespace data to shorten qualified keywords: | ||
- print `::foo` instead of `:pov.ns/foo` | ||
- print `::alias/foo` instead of `:ns.aliases.in.pov.ns/foo`" | ||
nil) | ||
|
||
(defn- print-coll-item | ||
"Print an item in the context of a collection. When printing a map, don't print | ||
`[]` characters around map entries." | ||
|
@@ -114,6 +121,22 @@ | |
(defmethod print :scalar [^Object x, ^Writer w] | ||
(.write w (.toString x))) | ||
|
||
(defmethod print :keyword [^Keyword kw, ^Writer w] | ||
(if-some [kw-ns (and *pov-ns* (namespace kw))] | ||
(if (= kw-ns (name (ns-name *pov-ns*))) | ||
(do (.write w "::") | ||
(.write w (name kw))) | ||
(if-some [matched-alias (some (fn [[alias ns]] | ||
(when (= kw-ns (name (ns-name ns))) | ||
alias)) | ||
(ns-aliases *pov-ns*))] | ||
Comment on lines
+129
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This stuff is O(N) from the number of namespace aliases in the POV-ns, so it banks on the assumption that people don't alias hundreds of namespaces. I think it is good enough for now, can improve later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think that's fine. Perhaps you can leave this in the code as a comment for future reference. |
||
(do (.write w "::") | ||
(.write w (name matched-alias)) | ||
(.write w "/") | ||
(.write w (name kw))) | ||
(.write w (.toString kw)))) | ||
(.write w (.toString kw)))) | ||
|
||
(defmethod print :double [x, ^Writer w] | ||
(cond (= Double/POSITIVE_INFINITY x) (.write w "##Inf") | ||
(= Double/NEGATIVE_INFINITY x) (.write w "##-Inf") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mention in the docstring that pov stands for "point-of-view", so people don't wonder about this?