Skip to content

Commit e9d8cfc

Browse files
committed
Include :return-type in response from jdoc-data
1 parent 8a557ec commit e9d8cfc

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Returns all the structured data instead of printing the description.
9797
;; :methods [{:signature "valueOf(int i)"
9898
;; :description "Returns the string representation..."
9999
;; :static? true
100+
;; :return-type "String"
100101
;; :clojure-call "^[int] String/valueOf"}
101102
;; ...]}
102103

@@ -108,6 +109,7 @@ Returns all the structured data instead of printing the description.
108109
;; :selected-method [{:signature "valueOf(char[] data)"
109110
;; :description "Returns the string representation..."
110111
;; :static? true
112+
;; :return-type "String"
111113
;; :clojure-call "^[char/1] String/valueOf"
112114
;; :method-description-html "..."
113115
;; :method-description-md "..."}]}

src/clojure/java/doc/impl.clj

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@
188188
(str/ends-with? raw-type "...") (str/replace raw-type #"[.]{3}$" "/1")
189189
:else raw-type)))
190190

191+
(defn- clojure-return-type
192+
"Extract return type from modifier text and convert to Clojure type hint syntax: static char[] -> char/1"
193+
[modifier-text]
194+
(when modifier-text
195+
(-> modifier-text
196+
str/trim
197+
(str/replace #"^(?:public|private|protected)\s+" "")
198+
(str/replace #"^static\s+" "")
199+
(str/replace #"^final\s+" "")
200+
(str/replace #"^default\s+" "")
201+
(str/replace #"^abstract\s+" "")
202+
compress-array-syntax
203+
str/trim)))
204+
191205
(defn- clojure-call-syntax
192206
"javadoc signature to clojure param-tag syntax: valueOf(char[] data) -> ^[char/1] String/valueOf"
193207
[class-part method-signature is-static?]
@@ -222,12 +236,13 @@
222236
(let [desc-div ^org.jsoup.nodes.Element (.nextElementSibling method-div)
223237
signature (.text (.select method-div "code"))
224238
modifier-div ^org.jsoup.nodes.Element (.previousElementSibling method-div)
225-
modifier-html (when modifier-div (.html modifier-div))
226-
is-static? (and modifier-html (str/includes? modifier-html "static"))]
239+
modifier-text (when modifier-div (.text modifier-div))
240+
is-static? (and modifier-text (str/includes? modifier-text "static"))]
227241
{:signature signature
228242
:description (.text (.select desc-div ".block"))
229-
:static? is-static?
230-
:clojure-call (clojure-call-syntax class-part signature is-static?)})))
243+
:static? is-static?
244+
:return-type (clojure-return-type modifier-text)
245+
:clojure-call (clojure-call-syntax class-part signature is-static?)})))
231246
class-html (when class-desc-section (.outerHtml ^org.jsoup.nodes.Element class-desc-section))
232247
result {:classname class-name
233248
:class-description-html class-html

test/clojure/java/doc/impl_test.clj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,42 @@
130130
(is (= "int[]" (#'sut/expand-array-syntax "int[]")))
131131
(is (= "String[][]" (#'sut/expand-array-syntax "String[][]")))))
132132

133+
(deftest clojure-return-type-test
134+
135+
(testing "simple types unchanged"
136+
(is (= "String" (#'sut/clojure-return-type "String")))
137+
(is (= "int" (#'sut/clojure-return-type "int")))
138+
(is (= "void" (#'sut/clojure-return-type "void"))))
139+
140+
(testing "modifiers stripped"
141+
(is (= "String" (#'sut/clojure-return-type "static String")))
142+
(is (= "int" (#'sut/clojure-return-type "static int")))
143+
(is (= "String" (#'sut/clojure-return-type "public static String")))
144+
(is (= "String" (#'sut/clojure-return-type "static final String"))))
145+
146+
(testing "array types converted to clojure syntax"
147+
(is (= "char/1" (#'sut/clojure-return-type "char[]")))
148+
(is (= "char/1" (#'sut/clojure-return-type "static char[]")))
149+
(is (= "String/1" (#'sut/clojure-return-type "String[]")))
150+
(is (= "String/2" (#'sut/clojure-return-type "String[][]"))))
151+
152+
(testing "generic types stripped"
153+
(is (= "List" (#'sut/clojure-return-type "List<String>")))
154+
(is (= "List" (#'sut/clojure-return-type "static List<E>")))
155+
(is (= "Map" (#'sut/clojure-return-type "Map<K,V>"))))
156+
157+
(testing "type parameter declarations stripped"
158+
(is (= "List" (#'sut/clojure-return-type "<E> List<E>")))
159+
(is (= "T/1" (#'sut/clojure-return-type "<T> T[]")))
160+
(is (= "T" (#'sut/clojure-return-type "<T> T"))))
161+
162+
(testing "complex type parameter declarations with nested generics"
163+
(is (= "T" (#'sut/clojure-return-type "<T extends Comparable<T>> T")))
164+
(is (= "T" (#'sut/clojure-return-type "<T extends Object & Comparable<? super T>> T"))))
165+
166+
(testing "nil input"
167+
(is (nil? (#'sut/clojure-return-type nil)))))
168+
133169
(deftest compress-array-syntax-test
134170

135171
(testing "primitive types unchanged"

0 commit comments

Comments
 (0)