Skip to content

Commit d551a4d

Browse files
committed
CLJS-1118: cljs.repl/doc support for protocols
1 parent 8049469 commit d551a4d

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

src/clj/cljs/core.clj

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,14 +1234,20 @@
12341234
=> 17"
12351235
[psym & doc+methods]
12361236
(let [p (:name (cljs.analyzer/resolve-var (dissoc &env :locals) psym))
1237-
psym (vary-meta psym assoc :protocol-symbol true)
1237+
[doc methods] (if (core/string? (first doc+methods))
1238+
[(first doc+methods) (next doc+methods)]
1239+
[nil doc+methods])
1240+
psym (vary-meta psym assoc
1241+
:doc doc
1242+
:protocol-symbol true)
12381243
ns-name (-> &env :ns :name)
12391244
fqn (fn [n] (symbol (core/str ns-name "." n)))
12401245
prefix (protocol-prefix p)
1241-
methods (if (core/string? (first doc+methods)) (next doc+methods) doc+methods)
12421246
_ (core/doseq [[mname & arities] methods]
12431247
(when (some #{0} (map count (filter vector? arities)))
1244-
(throw (Exception. (core/str "Invalid protocol, " psym " defines method " mname " with arity 0")))))
1248+
(throw (Exception.
1249+
(core/str "Invalid protocol, " psym
1250+
" defines method " mname " with arity 0")))))
12451251
expand-sig (fn [fname slot sig]
12461252
`(~sig
12471253
(if (and ~(first sig) (. ~(first sig) ~(symbol (core/str "-" slot)))) ;; Property access needed here.
@@ -1257,18 +1263,26 @@
12571263
(into {}
12581264
(map
12591265
(fn [[fname & sigs]]
1260-
(let [sigs (take-while vector? sigs)]
1261-
[fname (vec sigs)]))
1266+
(let [doc (as-> (last sigs) doc
1267+
(when (core/string? doc) doc))
1268+
sigs (take-while vector? sigs)]
1269+
[(vary-meta fname assoc :doc doc)
1270+
(vec sigs)]))
12621271
methods)))
12631272
method (fn [[fname & sigs]]
1264-
(let [sigs (take-while vector? sigs)
1273+
(let [doc (as-> (last sigs) doc
1274+
(when (core/string? doc) doc))
1275+
sigs (take-while vector? sigs)
12651276
slot (symbol (core/str prefix (name fname)))
1266-
fname (vary-meta fname assoc :protocol p)]
1267-
`(defn ~fname ~@(map (fn [sig]
1268-
(expand-sig fname
1269-
(symbol (core/str slot "$arity$" (count sig)))
1270-
sig))
1271-
sigs))))]
1277+
fname (vary-meta fname assoc
1278+
:protocol p
1279+
:doc doc)]
1280+
`(defn ~fname
1281+
~@(map (fn [sig]
1282+
(expand-sig fname
1283+
(symbol (core/str slot "$arity$" (count sig)))
1284+
sig))
1285+
sigs))))]
12721286
`(do
12731287
(set! ~'*unchecked-if* true)
12741288
(def ~psym (js-obj))

src/clj/cljs/repl.clj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,20 @@ itself (not its value) is returned. The reader macro #'x expands to (var x)."}})
986986

987987
(ana-api/resolve &env name)
988988
`(cljs.repl/print-doc
989-
(quote ~(update-in
990-
(select-keys (ana-api/resolve &env name)
991-
[:ns :name :doc :forms :arglists :macro :url])
992-
[:name] clojure.core/name)))))))))
989+
(quote ~(let [var (ana-api/resolve &env name)
990+
m (select-keys var
991+
[:ns :name :doc :forms :arglists :macro :url])]
992+
(cond-> (update-in m [:name] clojure.core/name)
993+
(:protocol-symbol var)
994+
(assoc :protocol true
995+
:methods
996+
(->> (get-in var [:protocol-info :methods])
997+
(map (fn [[fname sigs]]
998+
[fname {:doc (:doc
999+
(ana-api/resolve &env
1000+
(symbol (str (:ns var)) (str fname))))
1001+
:arglists (seq sigs)}]))
1002+
(into {})))))))))))))
9931003

9941004
(defmacro find-doc
9951005
"Prints documentation for any var whose documentation or name

src/cljs/cljs/repl.cljs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
(defn print-doc [m]
1313
(println "-------------------------")
1414
(println (str (when-let [ns (:ns m)] (str ns "/")) (:name m)))
15+
(when (:protocol m)
16+
(println "Protocol"))
1517
(cond
1618
(:forms m) (doseq [f (:forms m)]
1719
(println " " f))
@@ -33,4 +35,11 @@
3335
(println "Macro"))
3436
(when (:repl-special-function m)
3537
(println "REPL Special Function"))
36-
(println " " (:doc m)))))
38+
(println " " (:doc m))
39+
(when (:protocol m)
40+
(doseq [[name {:keys [doc arglists]}] (:methods m)]
41+
(println)
42+
(println " " name)
43+
(println " " arglists)
44+
(when doc
45+
(println " " doc)))))))

0 commit comments

Comments
 (0)