|
3 | 3 | [clojure |
4 | 4 | [pprint :refer [pprint]] |
5 | 5 | [string :as str]] |
6 | | - [refactor-nrepl.core :as core :refer [prefix-form?]]) |
| 6 | + [refactor-nrepl |
| 7 | + [core :as core :refer [prefix-form?]] |
| 8 | + [util :as util :refer [replace-last]]]) |
7 | 9 |
|
8 | 10 | (:import java.util.regex.Pattern)) |
9 | 11 |
|
|
52 | 54 | (and (sequential? form) |
53 | 55 | (= (first form) type))) |
54 | 56 |
|
| 57 | +(defn pprint-meta |
| 58 | + "Given some metadata m, print the shorthand metadata first, and the |
| 59 | + longhand metadata second, trying to convert to shorthand notation if |
| 60 | + possible |
| 61 | +
|
| 62 | + If newlines is true, it prints a newline after each piece of |
| 63 | + longhand metadata" |
| 64 | + [m & {:keys [newlines] |
| 65 | + :or {newlines false}}] |
| 66 | + (let [short? #(= (str %) "true") |
| 67 | + shorthand (sort (filter (fn [[k v]] (short? v)) m)) |
| 68 | + longhand (remove (fn [[k v]] (short? v)) m)] |
| 69 | + (doseq [[k v] shorthand] |
| 70 | + (print "^" (str k) "")) |
| 71 | + (when-not (empty? longhand) |
| 72 | + (printf "^{") |
| 73 | + (doseq [[k v] longhand] |
| 74 | + (print k) |
| 75 | + (if newlines |
| 76 | + (print (with-out-str (pprint v))) |
| 77 | + (print (replace-last (with-out-str (pprint v)) #"\s" "")))) |
| 78 | + (if newlines |
| 79 | + (println "}") |
| 80 | + (print "}"))))) |
| 81 | + |
55 | 82 | (defn- pprint-gen-class-form |
56 | | - [[_ & elems]] |
| 83 | + "Prints the gen class form and :methods metadata (if any)." |
| 84 | + [[_ & elems] metadata] |
57 | 85 | (if (empty? elems) |
58 | 86 | (println "(:gen-class)") |
59 | 87 | (println "(:gen-class")) |
60 | 88 | (dorun |
61 | 89 | (map-indexed |
62 | 90 | (fn [idx [key val]] |
63 | | - (if (= idx (dec (count (partition 2 elems)))) |
64 | | - (printf "%s %s)\n" key val) |
65 | | - (println key val))) |
| 91 | + (if (= key :methods) |
| 92 | + (do |
| 93 | + (print key "[") |
| 94 | + (doseq [method val] ;val are all the methods |
| 95 | + (pprint-meta (filter (fn [[k v]] |
| 96 | + (contains? metadata k)) |
| 97 | + (meta method))) |
| 98 | + (print method) |
| 99 | + (when-not (= method (last val)) |
| 100 | + (println))) |
| 101 | + (print "]")) |
| 102 | + (print key val)) |
| 103 | + (when (= idx (dec (count (partition 2 elems)))) |
| 104 | + (print ")")) |
| 105 | + (println)) |
66 | 106 | (partition 2 elems)))) |
67 | 107 |
|
68 | 108 | (defn- pprint-import-form |
|
76 | 116 | (println import))) |
77 | 117 | imports))) |
78 | 118 |
|
79 | | -(defn pprint-meta |
80 | | - [m] |
81 | | - (if-let [shorthand-meta-coll (::core/shorthand-meta-coll m)] |
82 | | - (doseq [shorthand-meta shorthand-meta-coll] |
83 | | - (print (str "^" shorthand-meta " "))) |
84 | | - (printf (.replaceAll (str "^" (into (sorted-map) m) "\n") |
85 | | - ", " "\n")))) |
86 | | - |
87 | 119 | (defn pprint-ns |
88 | 120 | [[_ name & more :as ns-form]] |
89 | 121 | (let [docstring? (when (string? (first more)) (first more)) |
90 | 122 | attrs? (when (map? (second more)) (second more)) |
91 | 123 | forms (cond (and docstring? attrs?) (nthrest more 2) |
92 | 124 | (not (or docstring? attrs?)) more |
93 | 125 | :else (rest more)) |
94 | | - ns-meta (meta ns-form)] |
| 126 | + ns-meta (:top-level-meta (meta ns-form))] |
95 | 127 | (-> (with-out-str |
96 | 128 | (printf "(ns ") |
97 | | - (when (seq ns-meta) (pprint-meta ns-meta)) |
| 129 | + (when (seq ns-meta) (pprint-meta ns-meta :newlines true)) |
98 | 130 | (print name) |
99 | 131 | (if (or docstring? attrs? forms) |
100 | 132 | (println) |
|
116 | 148 | (str/trim-newline |
117 | 149 | (with-out-str |
118 | 150 | (cond (form-is? form :require) (pprint-require-form form) |
119 | | - (form-is? form :gen-class) (pprint-gen-class-form form) |
| 151 | + (form-is? form :gen-class) (pprint-gen-class-form form (:gc-methods-meta (meta ns-form))) |
120 | 152 | (form-is? form :import) (pprint-import-form form) |
121 | 153 | :else (pprint form))))) |
122 | 154 | (cond (form-is? form :require) (pprint-require-form form) |
123 | | - (form-is? form :gen-class) (pprint-gen-class-form form) |
| 155 | + (form-is? form :gen-class) (pprint-gen-class-form form (:gc-methods-meta (meta ns-form))) |
124 | 156 | (form-is? form :import) (pprint-import-form form) |
125 | 157 | :else (pprint form)))) |
126 | 158 | forms))) |
|
0 commit comments