Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support pretty printing namespace declarations.

  • Loading branch information...
commit 4822d474a73683d5333ef92719703335e10a2df0 1 parent 69d31ab
Tom Faulhaber authored
Showing with 81 additions and 7 deletions.
  1. +80 −0 src/slam/hound/prettify.clj
  2. +1 −7 src/slam/hound/stitch.clj
80 src/slam/hound/prettify.clj
View
@@ -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 src/slam/hound/stitch.clj
View
@@ -1,5 +1,5 @@
(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])
@@ -46,12 +46,6 @@
:when (seq (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]
(-> ns-map
collapse-clauses
Please sign in to comment.
Something went wrong with that request. Please try again.