|
141 | 141 | :method-description-md (html-to-md method-html))) |
142 | 142 | method))) |
143 | 143 |
|
| 144 | +(defn- get-constructor-detail [^org.jsoup.nodes.Document doc constructor] |
| 145 | + (let [param-types (extract-params (:signature constructor)) |
| 146 | + detail-section (find-method-section doc "<init>" param-types)] |
| 147 | + (if detail-section |
| 148 | + (let [html (.outerHtml ^org.jsoup.nodes.Element detail-section)] |
| 149 | + (assoc constructor |
| 150 | + :constructor-description-html html |
| 151 | + :constructor-description-md (html-to-md html))) |
| 152 | + constructor))) |
| 153 | + |
144 | 154 | (defn- expand-array-syntax |
145 | 155 | "expands array syntax for matching javadoc format: String/2 -> String[][]" |
146 | 156 | [type-str] |
|
233 | 243 | (str "^[" (str/join " " clojure-types) "] " class-part separator method-name)) |
234 | 244 | (str class-part separator method-name)))) |
235 | 245 |
|
| 246 | +(defn- clojure-constructor-call-syntax [class-part constructor-signature] |
| 247 | + (let [param-types (extract-params constructor-signature) |
| 248 | + prefix (when param-types (str "^[" (str/join " " (mapv compress-array-syntax param-types)) "] "))] |
| 249 | + (str prefix class-part "/.new"))) |
| 250 | + |
236 | 251 | (defn parse-javadoc |
237 | 252 | "Parse the javadoc HTML for a class or method into a data structure: |
238 | 253 | {:classname 'java.lang.String' |
|
263 | 278 | :static? is-static? |
264 | 279 | :return-type (clojure-return-type modifier-text) |
265 | 280 | :clojure-call (clojure-call-syntax class-part signature is-static?)}))) |
| 281 | + constructor-rows (.select ^org.jsoup.nodes.Document doc "div.col-constructor-name") |
| 282 | + all-constructors (vec (for [^org.jsoup.nodes.Element ctor-div constructor-rows] |
| 283 | + (let [desc-div ^org.jsoup.nodes.Element (.nextElementSibling ctor-div) |
| 284 | + signature (.text (.select ctor-div "code"))] |
| 285 | + {:signature signature |
| 286 | + :description (.text (.select desc-div ".block")) |
| 287 | + :clojure-call (clojure-constructor-call-syntax class-part signature)}))) |
266 | 288 | class-html (when class-desc-section (.outerHtml ^org.jsoup.nodes.Element class-desc-section)) |
267 | 289 | result {:classname class-name |
268 | 290 | :class-description-html class-html |
269 | 291 | :class-description-md (when class-html (html-to-md class-html)) |
270 | | - :methods all-methods}] |
271 | | - (if method-part |
272 | | - (let [filtered (filter-methods all-methods method-part param-tags) |
273 | | - declaring-class (when (empty? filtered) |
274 | | - (find-declaring-class class-name method-part param-tags))] |
275 | | - (assoc result :selected-method |
276 | | - (cond |
277 | | - (seq filtered) (mapv #(get-method-detail doc %) filtered) |
278 | | - declaring-class (:selected-method (parse-javadoc (str declaring-class "/." method-part) param-tags)) |
279 | | - :else []))) |
280 | | - result))) |
| 292 | + :methods all-methods |
| 293 | + :constructors all-constructors}] |
| 294 | + (cond |
| 295 | + (nil? method-part) result |
| 296 | + (= method-part "new") (let [filtered (if param-tags |
| 297 | + (filterv #(params-match? (extract-params (:signature %)) param-tags) all-constructors) |
| 298 | + all-constructors)] |
| 299 | + (assoc result :selected-constructor (mapv #(get-constructor-detail doc %) filtered))) |
| 300 | + :else (let [filtered (filter-methods all-methods method-part param-tags) |
| 301 | + declaring-class (when (empty? filtered) (find-declaring-class class-name method-part param-tags))] |
| 302 | + (assoc result :selected-method |
| 303 | + (cond |
| 304 | + (seq filtered) (mapv #(get-method-detail doc %) filtered) |
| 305 | + declaring-class (:selected-method (parse-javadoc (str declaring-class "/." method-part) param-tags)) |
| 306 | + :else [])))))) |
281 | 307 |
|
282 | | - (defn print-javadoc [{:keys [classname class-description-md selected-method]}] |
| 308 | + (defn print-javadoc [{:keys [classname class-description-md selected-method selected-constructor]}] |
283 | 309 | (let [condense-lines (fn [s] (str/replace s #"\n{3,}" "\n\n"))] |
284 | 310 | (cond |
| 311 | + selected-constructor (doseq [{:keys [constructor-description-md]} selected-constructor] |
| 312 | + (if constructor-description-md |
| 313 | + (println (condense-lines constructor-description-md)) |
| 314 | + (println "No javadoc description available for this constructor."))) |
285 | 315 | selected-method (doseq [{:keys [method-description-md]} selected-method] |
286 | 316 | (if method-description-md |
287 | 317 | (println (condense-lines method-description-md)) |
288 | 318 | (println "No javadoc description available for this method."))) |
289 | 319 | class-description-md (println (condense-lines class-description-md)) |
290 | 320 | :else (println (str "No javadoc description available for class: " classname))))) |
291 | 321 |
|
292 | | -(defn print-signatures [{:keys [classname methods selected-method]}] |
293 | | - (let [methods-to-print (or selected-method methods)] |
294 | | - (if (seq methods-to-print) |
295 | | - (doseq [{:keys [clojure-call]} methods-to-print] |
| 322 | +(defn print-signatures [{:keys [classname methods constructors selected-method selected-constructor]}] |
| 323 | + (let [items-to-print (or selected-method selected-constructor (concat constructors methods))] |
| 324 | + (if (seq items-to-print) |
| 325 | + (doseq [{:keys [clojure-call]} items-to-print] |
296 | 326 | (println clojure-call)) |
297 | 327 | (println (str "No method signatures available for: " classname))))) |
0 commit comments