Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit 65acb2f

Browse files
authored
Add some docstrings to clojure.cljs and eval.clj namespaces (#93)
Add docstrings to some functions and make use of the new :doc key. Various misc formatting doc changes too.
1 parent d45e5f9 commit 65acb2f

File tree

2 files changed

+98
-23
lines changed

2 files changed

+98
-23
lines changed

lein-light-nrepl/src/lighttable/nrepl/eval.clj

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,18 @@
5353
(if (instance? clojure.lang.IObj thing)
5454
(meta thing)))
5555

56-
(defn clean-serialize [res & [opts]]
56+
(defn clean-serialize
57+
"Stringify the result `res` into a form appropiate to its type. Defaults to
58+
using function `pr-str` for most types.
59+
60+
Allowed options:
61+
62+
- `:print-length` - Restricts length of stringified results to it. Defaults to 1000.
63+
- `:allow-var?` - Whether to return a var itself or its stringified version.
64+
Defaults to nil.
65+
- `:result` - Whether to return an atom itself or its content. Defaults to nil.
66+
- `:verbatim` - Whether to return an string itself or its 'pr-str' version."
67+
[res & [opts]]
5768
(binding [*print-length* (or (:print-length opts) *print-length* 1000)]
5869
(cond
5970
(fn? res) 'fn
@@ -70,7 +81,8 @@
7081
(and (string? res) (:verbatim opts)) res
7182
:else (pr-str res))))
7283

73-
(defn truncate [v]
84+
(defn truncate
85+
[v]
7486
v)
7587

7688
(defn ->result [opts f]
@@ -118,7 +130,10 @@
118130
(str (reduce str "" (repeat (:start meta 0) "\n"))
119131
code)))
120132

121-
(defn watch [v meta]
133+
(defn watch
134+
"Stringify result-value `v` with pretty-print, send it back to Light Table,
135+
and then return the value itself to continue the evaling call."
136+
[v meta]
122137
(let [ppv (with-out-str (pprint v))
123138
data {:meta meta :result (subs ppv 0 (dec (count ppv)))}]
124139
(core/safe-respond-to (:obj meta) :editor.eval.clj.watch data))

src/lt/plugins/clojure.cljs

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@
6868
(run-local-server (clients/client! :nrepl.client))))))
6969

7070
(behavior ::on-eval.clj
71+
:desc "Clojure: Eval editor content"
7172
:triggers #{:eval}
7273
:reaction (fn [editor]
7374
(object/raise clj-lang :eval! {:origin editor
7475
:info (assoc (@editor :info)
7576
:print-length (object/raise-reduce editor :clojure.print-length+ nil)
7677
:code (watches/watched-range editor nil nil nil))})))
7778
(behavior ::on-eval.cljs
79+
:desc "Clojure: Eval editor content"
7880
:triggers #{:eval}
7981
:reaction (fn [editor]
8082
(object/raise clj-lang :eval! {:origin editor
@@ -87,6 +89,7 @@
8789
"(set! js/COMPILED js/COMPILED-temp)"))})))
8890

8991
(behavior ::on-eval.one
92+
:desc "Clojure: Eval a single form in editor"
9093
:triggers #{:eval.one}
9194
:reaction (fn [editor]
9295
(let [code (watches/watched-range editor nil nil nil)
@@ -103,12 +106,21 @@
103106
:info info}))))
104107

105108

106-
(defn fill-placeholders [editor exp]
109+
(defn fill-placeholders
110+
"Replace editor-selection-flags, that is `__SELECTION*__`, inside `exp` with the currently
111+
selected code in `editor`."
112+
[editor exp]
107113
(-> exp
108114
(string/replace "__SELECTION*__" (pr-str (ed/selection editor)))
109115
(string/replace "__SELECTION__" (ed/selection editor))))
110116

111117
(behavior ::on-eval.custom
118+
:desc "Clojure: Eval a form that has been wrapped by custom code"
119+
:doc "Example:
120+
121+
```
122+
(eval (time form)) ;; instead of (eval form)
123+
```"
112124
:triggers #{:eval.custom}
113125
:reaction (fn [editor exp opts]
114126
(let [code (fill-placeholders editor exp)
@@ -168,14 +180,16 @@
168180

169181
(defn lighttable-ui-project?
170182
"Determine if path is part of a project that evals to LightTable's process
171-
e.g. LightTable plugin or LightTable itself"
183+
(i.e., LightTable plugin or LightTable itself)."
172184
[path]
173185
(or (files/walk-up-find path "plugin.edn")
174186
(files/walk-up-find path "plugin.json")
175187
(when-let [project-file (files/walk-up-find path "project.clj")]
176188
(= 'lighttable (second (reader/read-string (:content (files/open-sync project-file))))))))
177189

178190
(behavior ::eval!
191+
:desc "Clojure: Send event information for evaluation."
192+
:doc "This event information goes to the appropriate nREPL or LightTable-UI client."
179193
:triggers #{:eval!}
180194
:reaction (fn [this event]
181195
(let [{:keys [info origin]} event
@@ -263,6 +277,7 @@
263277
(notifos/done-working)))
264278

265279
(behavior ::cljs-result
280+
:desc "Clojure: Receive a cljs result and dispatch it"
266281
:triggers #{:editor.eval.cljs.result}
267282
:reaction (fn [obj res]
268283
(notifos/done-working)
@@ -271,20 +286,24 @@
271286
(object/raise obj ev res))))
272287

273288
(behavior ::cljs-result.replace
289+
:desc "Clojure: Replace current selection with the result of an evaluation"
274290
:triggers #{:editor.eval.cljs.result.replace}
275291
:reaction (fn [obj res]
276292
(if-let [err (or (:stack res) (:ex res))]
277293
(notifos/set-msg! err {:class "error"})
278294
(ed/replace-selection obj (unescape-unicode (or (:result res) ""))))))
279295

280296
(behavior ::cljs-result.statusbar
297+
:desc "Clojure: Show the result of an evaluation on the statusbar"
281298
:triggers #{:editor.eval.cljs.result.statusbar}
282299
:reaction (fn [obj res]
283300
(if-let [err (or (:stack res) (:ex res))]
284301
(notifos/set-msg! err {:class "error"})
285302
(notifos/set-msg! (unescape-unicode (or (:result res) "")) {:class "result"}))))
286303

287304
(behavior ::cljs-result.inline
305+
:desc "Clojure: Show the resulting evaluation on inline widget"
306+
:doc "The resulting evaluation is dispatched either as an exception or an inline-result."
288307
:triggers #{:editor.eval.cljs.result.inline}
289308
:reaction (fn [obj res]
290309
(let [meta (:meta res)
@@ -295,6 +314,8 @@
295314
(object/raise obj :editor.result (unescape-unicode (or (:result res) "")) loc)))))
296315

297316
(behavior ::cljs-result.inline-at-cursor
317+
:desc "Clojure: Show the resulting evaluation inline at cursor location"
318+
:doc "This is similar to `::cljs-result.inline`, but puts the result as the cursor location."
298319
:triggers #{:editor.eval.cljs.result.inline-at-cursor}
299320
:reaction (fn [obj res]
300321
(let [meta (:meta res)
@@ -316,6 +337,8 @@
316337
:meta meta})))))
317338

318339
(behavior ::clj-result
340+
:desc "Clojure: Receive an eval! result and dispatch it"
341+
:doc "The dispatch is according to the appropiate result type. Defaults to `:inline`."
319342
:triggers #{:editor.eval.clj.result}
320343
:reaction (fn [obj res]
321344
(notifos/done-working)
@@ -324,6 +347,7 @@
324347
(object/raise obj ev res))))
325348

326349
(behavior ::clj-result.replace
350+
:desc "Clojure: Replace current selection with the result"
327351
:triggers #{:editor.eval.clj.result.replace}
328352
:reaction (fn [obj res]
329353
(doseq [result (-> res :results)
@@ -335,6 +359,7 @@
335359
(ed/replace-selection obj (:result result))))))
336360

337361
(behavior ::clj-result.statusbar
362+
:desc "Clojure: Show evaluation result on the statusbar"
338363
:triggers #{:editor.eval.clj.result.statusbar}
339364
:reaction (fn [obj res]
340365
(doseq [result (-> res :results)
@@ -346,6 +371,8 @@
346371
(notifos/set-msg! (:result result) {:class "result"})))))
347372

348373
(behavior ::clj-result.inline
374+
:desc "Clojure: Display eval result inline"
375+
:doc "Dispatches as an exception or an inline-result."
349376
:triggers #{:editor.eval.clj.result.inline}
350377
:reaction (fn [obj res]
351378
(doseq [result (-> res :results)
@@ -354,10 +381,11 @@
354381
:start-line (dec (:line meta))}]]
355382
(if (:stack result)
356383
(object/raise obj :editor.eval.clj.exception result :passed)
357-
(do
358-
(object/raise obj :editor.result (:result result) loc))))))
384+
(object/raise obj :editor.result (:result result) loc)))))
359385

360386
(behavior ::clj-result.inline-at-cursor
387+
:desc "Clojure: Display the eval result inline at cursor location"
388+
:doc "This is similar to `::clj-result.inline`, but puts the result at the cursor location."
361389
:triggers #{:editor.eval.clj.result.inline-at-cursor}
362390
:reaction (fn [obj res]
363391
(doseq [result (-> res :results)
@@ -366,8 +394,7 @@
366394
:start-line (-> res :meta :start)}]]
367395
(if (:stack result)
368396
(object/raise obj :editor.eval.clj.exception result :passed)
369-
(do
370-
(object/raise obj :editor.result (:result result) loc))))))
397+
(object/raise obj :editor.result (:result result) loc)))))
371398

372399
(behavior ::clj-result.return
373400
:triggers #{:editor.eval.clj.result.return}
@@ -382,6 +409,7 @@
382409
:meta meta})))))
383410

384411
(behavior ::clj-exception
412+
:desc "Clojure: Display stacktrace information and summary in statusbar"
385413
:triggers #{:editor.eval.clj.exception}
386414
:reaction (fn [obj res passed?]
387415
(when-not passed?
@@ -390,10 +418,13 @@
390418
loc {:line (dec (:end-line meta)) :ch (:end-column meta 0)
391419
:start-line (dec (:line meta 1))}]
392420
(notifos/set-msg! (:result res) {:class "error"})
393-
(object/raise obj :editor.exception (:stack res) loc))
394-
))
421+
(object/raise obj :editor.exception (:stack res) loc))))
395422

396423
(behavior ::cljs-exception
424+
:desc "Clojure: Display stacktrace information and summary in statusbar"
425+
;; Line below is too long to include, but will be useful after https://github.com/LightTable/LightTable/issues/2197
426+
:doc "Take the result of evaling a cljs form, which resulted in an exception,
427+
and displays a message in the status bar and an exception widget with the stacktrace."
397428
:triggers #{:editor.eval.cljs.exception}
398429
:reaction (fn [obj res passed?]
399430
(when-not passed?
@@ -413,8 +444,7 @@
413444
msg
414445
"Unknown error")]
415446
(notifos/set-msg! msg {:class "error"})
416-
(object/raise obj :editor.exception stack loc))
417-
))
447+
(object/raise obj :editor.exception stack loc))))
418448

419449
(behavior ::eval-print
420450
:triggers #{:editor.eval.clj.print}
@@ -424,8 +454,7 @@
424454
:line (when (object/has-tag? this :nrepl.client)
425455
"stdout")
426456
:id (:id str)
427-
:content (:out str)}
428-
))))
457+
:content (:out str)}))))
429458

430459
(behavior ::eval-print-err
431460
:triggers #{:editor.eval.clj.print.err}
@@ -517,18 +546,36 @@
517546
;; watches
518547
;;****************************************************
519548

549+
;; For more information on watches check
550+
;;
551+
;; Original anouncement: https://groups.google.com/forum/#!msg/light-table-discussion/lyFzPGI2XMs/ec8T1OUPvMsJ
552+
;;
553+
;; Blog posts: http://scattered-thoughts.net/blog/2014/01/27/were-not-even-trying/?utm_source=dlvr.it&utm_medium=twitter
554+
;; https://medium.com/@zindlerb/guide-to-light-table-watches-fad560f698d3#.oqwq991sx
555+
;;
556+
;; Rolex watches plugin: https://groups.google.com/forum/#!topic/light-table-discussion/NQWGC0vVHMY
557+
520558
(behavior ::cljs-watch-src
559+
:desc "Clojure: Wraps the watched source code"
560+
:doc "Wraps watched code to catch its result and send it back to LightTable,
561+
while continuing normal evaluation of an expression."
521562
:triggers #{:watch.src+}
522563
:reaction (fn [editor cur meta src]
523564
(let [meta (assoc meta :ev :editor.eval.cljs.watch)]
524565
(str "(js/lttools.watch " src " (clj->js " (pr-str meta) "))"))))
525566

526567
(behavior ::clj-watch-src
568+
:desc "Clojure: Wraps the watched source code"
569+
:doc "Wraps watched code to catch its result and send it back to LightTable,
570+
while continuing normal evaluation of an expression."
527571
:triggers #{:watch.src+}
528572
:reaction (fn [editor cur meta src]
529573
(str "(lighttable.nrepl.eval/watch " src " " (pr-str meta) ")")))
530574

531-
(defn fill-watch-placeholders [exp src meta watch]
575+
(defn fill-watch-placeholders
576+
"Replace editor-selection-flags (placeholders) for custom watches inside `exp`
577+
with the src-code to be watched."
578+
[exp src meta watch]
532579
(-> exp
533580
(string/replace "\n" " ")
534581
(string/replace "__SELECTION*__" (pr-str src))
@@ -537,12 +584,20 @@
537584
(string/replace #"__\|(.*)\|__" watch)))
538585

539586
(behavior ::cljs-watch-custom-src
587+
:desc "Clojure: Prepare expression for watching"
588+
:doc "The expression is prepared by filling its placeholders and wrapping its watcher-code
589+
with custom call to `:editor.eval.cljs.watch`."
540590
:triggers #{:watch.custom.src+}
541591
:reaction (fn [editor cur meta opts src]
542-
(let [watch (str "(js/lttools.raise " (:obj meta) " :editor.eval.cljs.watch {:meta " (pr-str (merge (dissoc opts :exp) meta)) " :result $1})")]
592+
(let [watch (str "(js/lttools.raise " (:obj meta)
593+
" :editor.eval.cljs.watch {:meta " (pr-str (merge (dissoc opts :exp) meta))
594+
" :result $1})")]
543595
(fill-watch-placeholders (:exp opts) src meta watch))))
544596

545597
(behavior ::clj-watch-custom-src
598+
:desc "Clojure: Prepare expression for watching"
599+
:doc "The exp is prepared by filling its placeholders and wrapping its watcher-code
600+
with custom call to `:editor.eval.clj.watch`"
546601
:triggers #{:watch.custom.src+}
547602
:reaction (fn [editor cur meta opts src]
548603
(let [wrapped (if (:verbatim opts)
@@ -578,6 +633,7 @@
578633
;;****************************************************
579634

580635
(behavior ::clj-doc
636+
:desc "Clojure: Request docstring for symbol at cursor from nREPL"
581637
:triggers #{:editor.doc}
582638
:reaction (fn [editor]
583639
(let [token (find-symbol-at-cursor editor)
@@ -593,8 +649,7 @@
593649
:info info
594650
:origin editor
595651
:create try-connect})
596-
command info :only editor)))
597-
))
652+
command info :only editor)))))
598653

599654
(behavior ::print-clj-doc
600655
:triggers #{:editor.clj.doc}
@@ -617,6 +672,7 @@
617672
(assoc token-left :loc loc)))))
618673

619674
(behavior ::cljs-doc
675+
:desc "Clojure: Request docstring for symbol at cursor from nREPL"
620676
:triggers #{:editor.doc}
621677
:reaction (fn [editor]
622678
(let [token (find-symbol-at-cursor editor)
@@ -643,16 +699,20 @@
643699
(object/raise editor :editor.doc.show! result)))))
644700

645701
(behavior ::clj-doc-search
702+
:desc "Clojure: Add trigger for Clojure in language documentation search"
703+
:doc "Links the 'Search language docs' input-text on the sidebar with a trigger to
704+
`:docs.clj.search` to retrieve all the documentation on a user-input."
646705
:triggers #{:types+}
647706
:reaction (fn [this cur]
648-
(conj cur {:label "clj" :trigger :docs.clj.search :file-types #{"Clojure"}})
649-
))
707+
(conj cur {:label "clj" :trigger :docs.clj.search :file-types #{"Clojure"}})))
650708

651709
(behavior ::cljs-doc-search
710+
:desc "Clojure: Add trigger for ClojureScript in language documentation search"
711+
:doc "Links the 'Search language docs' input-text on the sidebar with a trigger to
712+
`:docs.cljs.search` to retrieve all the documentation on a user-input."
652713
:triggers #{:types+}
653714
:reaction (fn [this cur]
654-
(conj cur {:label "cljs" :trigger :docs.cljs.search :file-types #{"ClojureScript"}})
655-
))
715+
(conj cur {:label "cljs" :trigger :docs.cljs.search :file-types #{"ClojureScript"}})))
656716

657717
;;****************************************************
658718
;; autocomplete

0 commit comments

Comments
 (0)