Skip to content

Commit

Permalink
Add print function for Vars.
Browse files Browse the repository at this point in the history
  • Loading branch information
greglook committed Jun 23, 2014
1 parent a20728a commit b58d40a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/puget/printer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@



;; DISPATCH MULTIMETHOD
;;;;; CANONIZING MULTIMETHOD ;;;;;

(defn- canonize-dispatch
[value]
Expand All @@ -112,6 +112,9 @@
#'canonize-dispatch)



;;;;; PRIMITIVE VALUES ;;;;;

(defmacro ^:private canonize-element
"Defines a canonization of a primitive value type by mapping it to an element
in the color scheme."
Expand All @@ -130,6 +133,9 @@
(canonize-element clojure.lang.Symbol :symbol)



;;;;; COLLECTIONS ;;;;;

(defmethod canonize clojure.lang.ISeq
[s]
(let [elements (if (symbol? (first s))
Expand Down Expand Up @@ -198,6 +204,25 @@
(prefer-method canonize clojure.lang.IRecord clojure.lang.IPersistentMap)



;;;;; CLOJURE-SPECIFIC TYPES ;;;;;

; TODO: regex


(defmethod canonize clojure.lang.Var
[v]
(when *strict-mode*
(throw (IllegalArgumentException.
(str "No canonical representation for " (class v) ": " v))))
[:span
(color-doc :delimiter "#'")
(color-doc :symbol (subs (str v) 2))])



;;;;; OTHER TYPES ;;;;;

(defmethod canonize :tagged-value
[tv]
(let [tag (data/edn-tag tv)
Expand All @@ -221,7 +246,7 @@



;; PRINT FUNCTIONS
;;;;; PRINT FUNCTIONS ;;;;;

(def ^:private default-opts
"Default Puget printing options."
Expand Down
10 changes: 10 additions & 0 deletions test/puget/printer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
"#puget.printer_test.TestRecord{:bar \\y :foo \\x}\n")))))


(deftest clojure-types
(testing "vars"
(let [v #'TaggedValue]
(binding [*strict-mode* true]
(is (thrown? IllegalArgumentException (pprint v))
"should not print non-EDN representation"))
(is (= (pprint-str v)
"#'puget.data/TaggedValue")))))


(deftest canonical-tagged-value
(let [tval (reify TaggedValue
(edn-tag [this] 'foo)
Expand Down

0 comments on commit b58d40a

Please sign in to comment.