Skip to content

Commit

Permalink
Support pretty printing namespace declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfaulhaber committed Jan 7, 2012
1 parent 69d31ab commit 4822d47
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
80 changes: 80 additions & 0 deletions src/slam/hound/prettify.clj
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,80 @@
(ns slam.hound.prettify
"Format a namespace declaration using pretty print with custom dispatch."
(:use [clojure.pprint :only [code-dispatch pprint with-pprint-dispatch cl-format
pprint-logical-block pprint-newline formatter-out
write-out]]))

(defn- pprint-ns-reference
"Pretty print a single reference (import, use, etc.) from a namespace decl"
[reference]
(if (sequential? reference)
(let [[start end] (brackets reference)
[keyw & args] reference]
(pprint-logical-block :prefix start :suffix end
((formatter-out "~w~:i") keyw)
(loop [args args]
(when (seq args)
((formatter-out " "))
(let [arg (first args)]
(if (sequential? arg)
(let [[start end] (brackets arg)]
(pprint-logical-block :prefix start :suffix end
(if (and (= (count arg) 3) (keyword? (second arg)))
(let [[ns kw lis] arg]
((formatter-out "~w ~w ") ns kw)
(if (sequential? lis)
((formatter-out (if (vector? lis)
"~<[~;~@{~w~^ ~:_~}~;]~:>"
"~<(~;~@{~w~^ ~:_~}~;)~:>"))
lis)
(write-out lis)))
(apply (formatter-out "~w ~:i~@{~w~^ ~:_~}") arg)))
(when (next args)
((formatter-out "~_"))))
(do
(write-out arg)
(when (next args)
((formatter-out "~:_"))))))
(recur (next args))))))
(write-out reference)))

(defn- pprint-ns
"The pretty print dispatch chunk for the ns macro"
[alis]
(if (next alis)
(let [[ns-sym ns-name & stuff] alis
[doc-str stuff] (if (string? (first stuff))
[(first stuff) (next stuff)]
[nil stuff])
[attr-map references] (if (map? (first stuff))
[(first stuff) (next stuff)]
[nil stuff])]
(pprint-logical-block :prefix "(" :suffix ")"
((formatter-out "~w ~1I~@_~w") ns-sym ns-name)
(when (or doc-str attr-map (seq references))
((formatter-out "~@:_")))
(when doc-str
(cl-format true "\"~a\"~:[~;~:@_~]" doc-str (or attr-map (seq references))))
(when attr-map
((formatter-out "~w~:[~;~:@_~]") attr-map (seq references)))
(loop [references references]
(pprint-ns-reference (first references))
(when-let [references (next references)]
(pprint-newline :linear)
(recur references)))))
(write-out alis)))

(defn augmented-dispatch
"A wrapper for code-dispatch that supports better ns printing"
[form]
(if (and (seq? form) (= 'ns (first form)))
(pprint-ns form)
(code-dispatch form)))

(defn prettify
"Pretty print the ns-form to a string"
[ns-form]
(with-out-str
(with-pprint-dispatch augmented-dispatch
(pprint ns-form))))

8 changes: 1 addition & 7 deletions src/slam/hound/stitch.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns slam.hound.stitch (ns slam.hound.stitch
(:use [clojure.pprint :only [code-dispatch pprint with-pprint-dispatch]])) (:use [slam.hound.prettify :only [prettify]]))


(def ns-clauses [:use :require :import]) (def ns-clauses [:use :require :import])


Expand Down Expand Up @@ -46,12 +46,6 @@
:when (seq (clause-type ns-map))] :when (seq (clause-type ns-map))]
(cons clause-type (clause-type ns-map))))) (cons clause-type (clause-type ns-map)))))


(defn prettify [ns-form]
(.replace (with-out-str
(with-pprint-dispatch code-dispatch
(pprint ns-form)))
"(ns\n " "(ns"))

(defn stitch-up [ns-map] (defn stitch-up [ns-map]
(-> ns-map (-> ns-map
collapse-clauses collapse-clauses
Expand Down

0 comments on commit 4822d47

Please sign in to comment.