forked from clojure-emacs/cider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cider-browse-spec.el
361 lines (316 loc) · 15.6 KB
/
cider-browse-spec.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
;;; cider-browse-spec.el --- CIDER spec browser -*- lexical-binding: t; -*-
;; Copyright © 2017-2021 Juan Monetta, Bozhidar Batsov and CIDER contributors
;; Author: Juan Monetta <jpmonettas@gmail.com>
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; M-x cider-browse-spec
;;
;; Display a spec description you can browse.
;; Pressing <enter> over a sub spec will take you to the description of that sub spec.
;; Pressing ^ takes you to the list of all specs.
;; M-x cider-browse-spec-all
;;
;; Explore clojure.spec registry by browsing a list of all specs.
;; Pressing <enter> over a spec display the spec description you can browse.
;;; Code:
(require 'cider-client)
(require 'cider-popup)
(require 'cider-util)
(require 'cl-lib)
(require 'nrepl-dict)
(require 'seq)
(require 'subr-x)
(require 'help-mode)
;; The buffer names used by the spec browser
(defconst cider-browse-spec-buffer "*cider-spec-browser*")
(defconst cider-browse-spec-example-buffer "*cider-spec-example*")
;; Mode Definition
(defvar cider-browse-spec-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map (make-composed-keymap button-buffer-map
cider-popup-buffer-mode-map))
(define-key map (kbd "RET") #'cider-browse-spec--browse-at)
(define-key map "n" #'forward-button)
(define-key map "p" #'backward-button)
map)
"Keymap for `cider-browse-spec-mode'.")
(define-derived-mode cider-browse-spec-mode special-mode "Specs"
"Major mode for browsing Clojure specs.
\\{cider-browse-spec-mode-map}"
(setq-local electric-indent-chars nil)
(setq-local sesman-system 'CIDER)
(when cider-special-mode-truncate-lines
(setq-local truncate-lines t)))
(defvar cider-browse-spec--current-spec nil)
(defvar cider-browse-spec-view-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map help-mode-map)
(define-key map (kbd "RET") #'cider-browse-spec--browse-at)
(define-key map "^" #'cider-browse-spec-all)
(define-key map "e" #'cider-browse-spec--print-curr-spec-example)
(define-key map "n" #'forward-button)
(define-key map "p" #'backward-button)
map)
"Keymap for `cider-browse-spec-view-mode'.")
(define-derived-mode cider-browse-spec-view-mode help-mode "Spec"
"Major mode for displaying CIDER spec.
\\{cider-browse-spec-view-mode-map}"
(setq-local cider-browse-spec--current-spec nil)
(setq-local electric-indent-chars nil)
(setq-local sesman-system 'CIDER)
(when cider-special-mode-truncate-lines
(setq-local truncate-lines t)))
(defvar cider-browse-spec-example-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map cider-popup-buffer-mode-map)
(define-key map "^" #'cider-browse-spec-all)
(define-key map "e" #'cider-browse-spec--print-curr-spec-example)
(define-key map "g" #'revert-buffer)
map)
"Keymap for `cider-browse-spec-example-mode'.")
(define-derived-mode cider-browse-spec-example-mode special-mode "Example"
"Major mode for Clojure spec examples.
\\{cider-browse-spec-example-mode-map}"
(setq-local electric-indent-chars nil)
(setq-local revert-buffer-function #'cider-browse-spec--example-revert-buffer-function)
(setq-local sesman-system 'CIDER)
(when cider-special-mode-truncate-lines
(setq-local truncate-lines t)))
;; Non interactive functions
(define-button-type 'cider-browse-spec--spec
'action #'cider-browse-spec--browse-at
'face nil
'follow-link t
'help-echo "View spec")
(defun cider-browse-spec--draw-list-buffer (buffer title specs)
"Reset contents of BUFFER.
Display TITLE at the top and SPECS are indented underneath."
(with-current-buffer buffer
(cider-browse-spec-mode)
(let ((inhibit-read-only t))
(erase-buffer)
(goto-char (point-max))
(insert (cider-propertize title 'emph) "\n")
(dolist (spec-name specs)
(insert (propertize " " 'spec-name spec-name))
(thread-first (cider-font-lock-as-clojure spec-name)
(insert-text-button 'type 'cider-browse-spec--spec)
(button-put 'spec-name spec-name))
(insert (propertize "\n" 'spec-name spec-name)))
(goto-char (point-min)))))
(defun cider--qualified-keyword-p (str)
"Return non nil if STR is a namespaced keyword."
(string-match-p "^:.+/.+$" str))
(defun cider--spec-fn-p (value fn-name)
"Return non nil if VALUE is clojure.spec.[alpha]/FN-NAME."
(string-match-p (concat "^\\(clojure.spec\\|clojure.spec.alpha\\)/" fn-name "$") value))
(defun cider-browse-spec--pprint (form)
"Given a spec FORM builds a multi line string with a pretty render of that FORM."
(cond ((stringp form)
(if (cider--qualified-keyword-p form)
(with-temp-buffer
(thread-first form
(insert-text-button 'type 'cider-browse-spec--spec)
(button-put 'spec-name form))
(buffer-string))
;; to make it easier to read replace all clojure.spec ns with s/
;; and remove all clojure.core ns
(thread-last form
(replace-regexp-in-string "^\\(clojure.spec\\|clojure.spec.alpha\\)/" "s/")
(replace-regexp-in-string "^\\(clojure.core\\)/" ""))))
((and (listp form) (stringp (cl-first form)))
(let ((form-tag (cl-first form)))
(cond
;; prettier fns #()
((string-equal form-tag "clojure.core/fn")
(if (equal (cl-second form) '("%"))
(format "#%s" (cl-reduce #'concat (mapcar #'cider-browse-spec--pprint (cl-rest (cl-rest form)))))
(format "(fn [%%] %s)" (cl-reduce #'concat (mapcar #'cider-browse-spec--pprint (cl-rest (cl-rest form)))))))
;; prettier (s/and )
((cider--spec-fn-p form-tag "and")
(format "(s/and\n%s)" (string-join (thread-last (cl-rest form)
(mapcar #'cider-browse-spec--pprint)
(mapcar (lambda (x) (format "%s" x))))
"\n")))
;; prettier (s/or )
((cider--spec-fn-p form-tag "or")
(let ((name-spec-pair (seq-partition (cl-rest form) 2)))
(format "(s/or\n%s)" (string-join
(thread-last name-spec-pair
(mapcar (lambda (s) (format "%s %s" (cl-first s) (cider-browse-spec--pprint (cl-second s))))))
"\n"))))
;; prettier (s/merge )
((cider--spec-fn-p form-tag "merge")
(format "(s/merge\n%s)" (string-join (thread-last (cl-rest form)
(mapcar #'cider-browse-spec--pprint)
(mapcar (lambda (x) (format "%s" x))))
"\n")))
;; prettier (s/keys )
((cider--spec-fn-p form-tag "keys")
(let ((keys-args (seq-partition (cl-rest form) 2)))
(format "(s/keys%s)" (thread-last
keys-args
(mapcar (lambda (s)
(let ((key-type (cl-first s))
(specs-vec (cl-second s)))
(concat "\n" key-type
" ["
(string-join (thread-last specs-vec
(mapcar #'cider-browse-spec--pprint)
(mapcar (lambda (x) (format "%s" x))))
"\n")
"]"))))
(cl-reduce #'concat)))))
;; prettier (s/multi-spec)
((cider--spec-fn-p form-tag "multi-spec")
(let ((multi-method (cl-second form))
(retag (cl-third form))
(sub-specs (cl-rest (cl-rest (cl-rest form)))))
(format "(s/multi-spec %s %s\n%s)"
multi-method
retag
(string-join
(thread-last sub-specs
(mapcar (lambda (s)
(concat "\n\n" (cl-first s) " " (cider-browse-spec--pprint (cl-second s))))))
"\n"))))
;; prettier (s/cat )
((cider--spec-fn-p form-tag "cat")
(let ((name-spec-pairs (seq-partition (cl-rest form) 2)))
(format "(s/cat %s)"
(thread-last name-spec-pairs
(mapcar (lambda (s)
(concat "\n" (cl-first s) " " (cider-browse-spec--pprint (cl-second s)))))
(cl-reduce #'concat)))))
;; prettier (s/alt )
((cider--spec-fn-p form-tag "alt")
(let ((name-spec-pairs (seq-partition (cl-rest form) 2)))
(format "(s/alt %s)"
(thread-last name-spec-pairs
(mapcar (lambda (s)
(concat "\n" (cl-first s) " " (cider-browse-spec--pprint (cl-second s)))))
(cl-reduce #'concat)))))
;; prettier (s/fspec )
((cider--spec-fn-p form-tag "fspec")
(thread-last (seq-partition (cl-rest form) 2)
(cl-remove-if (lambda (s) (and (stringp (cl-second s))
(string-empty-p (cl-second s)))))
(mapcar (lambda (s)
(format "\n%-11s: %s" (pcase (cl-first s)
(":args" "arguments")
(":ret" "returns")
(":fn" "invariants"))
(cider-browse-spec--pprint (cl-second s)))))
(cl-reduce #'concat)
(format "%s")))
;; every other with no special management
(t (format "(%s %s)"
(cider-browse-spec--pprint form-tag)
(string-join (mapcar #'cider-browse-spec--pprint (cl-rest form)) " "))))))
(t (format "%s" form))))
(defun cider-browse-spec--pprint-indented (spec-form)
"Indent (pretty-print) and font-lock SPEC-FORM.
Return the result as a string."
(with-temp-buffer
(clojure-mode)
(insert (cider-browse-spec--pprint spec-form))
(indent-region (point-min) (point-max))
(font-lock-ensure)
(buffer-string)))
(defun cider-browse-spec--draw-spec-buffer (buffer spec spec-form)
"Reset contents of BUFFER and draws everything needed to browse the SPEC-FORM.
Display SPEC as a title and uses `cider-browse-spec--pprint' to display
a more user friendly representation of SPEC-FORM."
(with-current-buffer buffer
(let ((inhibit-read-only t))
(cider--help-setup-xref (list #'cider-browse-spec spec) nil buffer)
(goto-char (point-max))
(insert (cider-font-lock-as-clojure spec) "\n\n")
(insert (cider-browse-spec--pprint-indented spec-form))
(cider--make-back-forward-xrefs)
(current-buffer))))
(defun cider-browse-spec--browse (spec)
"Browse SPEC."
(cider-ensure-connected)
(cider-ensure-op-supported "spec-form")
;; Expand auto-resolved keywords
(when-let* ((val (and (string-match-p "^::.+" spec)
(nrepl-dict-get (cider-sync-tooling-eval spec (cider-current-ns)) "value"))))
(setq spec val))
(with-current-buffer (cider-popup-buffer cider-browse-spec-buffer 'select #'cider-browse-spec-view-mode 'ancillary)
(setq-local cider-browse-spec--current-spec spec)
(cider-browse-spec--draw-spec-buffer (current-buffer)
spec
(cider-sync-request:spec-form spec))
(goto-char (point-min))
(current-buffer)))
(defun cider-browse-spec--browse-at (&optional pos)
"View the definition of a spec.
Optional argument POS is the position of a spec, defaulting to point. POS
may also be a button, so this function can be used a the button's `action'
property."
(interactive)
(let ((pos (or pos (point))))
(when-let* ((spec (button-get pos 'spec-name)))
(cider-browse-spec--browse spec))))
;; Interactive Functions
(defun cider-browse-spec--print-curr-spec-example ()
"Generate and print an example of the current spec."
(interactive)
(cider-ensure-connected)
(cider-ensure-op-supported "spec-example")
(if-let* ((spec cider-browse-spec--current-spec))
(if-let* ((example (cider-sync-request:spec-example spec)))
(with-current-buffer (cider-popup-buffer cider-browse-spec-example-buffer 'select #'cider-browse-spec-example-mode 'ancillary)
(setq-local cider-browse-spec--current-spec spec)
(let ((inhibit-read-only t))
(insert "Example of " (cider-font-lock-as-clojure spec))
(insert "\n\n")
(insert (cider-font-lock-as-clojure example))
(goto-char (point-min))))
(error (format "No example for spec %s" spec)))
(error "No current spec")))
(defun cider-browse-spec--example-revert-buffer-function (&rest _)
"`revert-buffer' function for `cider-browse-spec-example-mode'.
Generates a new example for the current spec."
(cider-browse-spec--print-curr-spec-example))
;;;###autoload
(defun cider-browse-spec (spec)
"Browse SPEC definition."
(interactive (list (completing-read "Browse spec: "
(cider-sync-request:spec-list)
nil nil
(cider-symbol-at-point))))
(cider-browse-spec--browse spec))
(defun cider-browse-spec-regex (regex)
"Open the list of specs that matches REGEX in a popup buffer.
Displays all specs when REGEX is nil."
(cider-ensure-connected)
(cider-ensure-op-supported "spec-list")
(let ((filter-regex (or regex "")))
(with-current-buffer (cider-popup-buffer cider-browse-spec-buffer 'select nil 'ancillary)
(let ((specs (cider-sync-request:spec-list filter-regex)))
(cider-browse-spec--draw-list-buffer (current-buffer)
(if (string-empty-p filter-regex)
"All specs in registry"
(format "All specs matching regex `%s' in registry" filter-regex))
specs)))))
;;;###autoload
(defun cider-browse-spec-all (&optional arg)
"Open list of specs in a popup buffer.
With a prefix argument ARG, prompts for a regexp to filter specs.
No filter applied if the regexp is the empty string."
(interactive "P")
(cider-browse-spec-regex (if arg (read-string "Filter regex: ") "")))
(provide 'cider-browse-spec)
;;; cider-browse-spec.el ends here