forked from clojure-emacs/cider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cider-browse-ns.el
550 lines (470 loc) · 21.1 KB
/
cider-browse-ns.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
;;; cider-browse-ns.el --- CIDER namespace browser -*- lexical-binding: t; -*-
;; Copyright © 2014-2022 John Andrews, Bozhidar Batsov and CIDER contributors
;; Author: John Andrews <john.m.andrews@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-ns
;;
;; Display a list of all vars in a namespace.
;; Pressing <enter> will take you to the cider-doc buffer for that var.
;; Pressing ^ will take you to a list of all namespaces (akin to `dired-mode').
;; M-x cider-browse-ns-all
;;
;; Explore Clojure namespaces by browsing a list of all namespaces.
;; Pressing <enter> expands into a list of that namespace's vars as if by
;; executing the command (cider-browse-ns "my.ns").
;;; Code:
(require 'cider-client)
(require 'cider-popup)
(require 'cider-util)
(require 'nrepl-dict)
(require 'subr-x)
(require 'easymenu)
(require 'button)
(require 'cl-lib)
(require 'thingatpt)
(defgroup cider-browse-ns nil
"Display contents of namespaces for CIDER."
:prefix "cider-browse-ns-"
:group 'cider)
(defface cider-browse-ns-extra-info-face
'((t (:inherit shadow)))
"Face for displaying extra info of namespace vars."
:package-version '(cider . "1.4.0"))
(defcustom cider-browse-ns-default-filters nil
"List of default hide filters to apply to browse-ns buffer.
Available options include `private', `test', `macro', `function', and
`var'."
:type 'list
:package-version '(cider . "1.4.0"))
(defconst cider-browse-ns-buffer "*cider-ns-browser*")
(defvar-local cider-browse-ns-current-ns nil)
(defvar-local cider-browse-ns-filters nil)
(defvar-local cider-browse-ns-show-all nil)
(defvar-local cider-browse-ns-group-by nil)
(defvar-local cider-browse-ns-items nil)
(defvar-local cider-browse-ns-title nil)
(defvar-local cider-browse-ns-group-by nil)
;; Mode Definition
(defvar cider-browse-ns-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map cider-popup-buffer-mode-map)
(define-key map "d" #'cider-browse-ns-doc-at-point)
(define-key map "s" #'cider-browse-ns-find-at-point)
(define-key map (kbd "RET") #'cider-browse-ns-operate-at-point)
(define-key map "^" #'cider-browse-ns-all)
(define-key map "n" #'next-line)
(define-key map "p" #'previous-line)
(define-key map "a" #'cider-browse-ns-toggle-all)
(define-key map (kbd "h p") #'cider-browse-ns-toggle-hide-private)
(define-key map (kbd "h t") #'cider-browse-ns-toggle-hide-test)
(define-key map (kbd "h m") #'cider-browse-ns-toggle-hide-macro)
(define-key map (kbd "h f") #'cider-browse-ns-toggle-hide-function)
(define-key map (kbd "h v") #'cider-browse-ns-toggle-hide-var)
(define-key map (kbd "g t") #'cider-browse-ns-group-by-type)
(define-key map (kbd "g v") #'cider-browse-ns-group-by-visibility)
(easy-menu-define cider-browse-ns-mode-menu map
"Menu for CIDER's namespace browser"
'("Namespace Browser"
["Show doc" cider-browse-ns-doc-at-point]
["Go to definition" cider-browse-ns-find-at-point]
"--"
["Browse all namespaces" cider-browse-ns-all]))
map))
(defvar cider-browse-ns-mouse-map
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] #'cider-browse-ns-handle-mouse)
map))
(define-derived-mode cider-browse-ns-mode special-mode "browse-ns"
"Major mode for browsing Clojure namespaces.
\\{cider-browse-ns-mode-map}"
(setq-local electric-indent-chars nil)
(setq-local sesman-system 'CIDER)
(when cider-special-mode-truncate-lines
(setq-local truncate-lines t))
(setq-local cider-browse-ns-current-ns nil))
(defun cider-browse-ns--text-face (var-meta)
"Return font-lock-face for a var.
VAR-META contains the metadata information used to decide a face.
Presence of \"arglists\" and \"macro\" indicates a macro form.
Only \"arglists\" indicates a function. Otherwise, its a variable.
If the NAMESPACE is not loaded in the REPL, assume TEXT is a fn."
(cond
((not var-meta) 'font-lock-function-name-face)
((and (nrepl-dict-contains var-meta "arglists")
(string= (nrepl-dict-get var-meta "macro") "true"))
'font-lock-keyword-face)
((nrepl-dict-contains var-meta "arglists") 'font-lock-function-name-face)
(t 'font-lock-variable-name-face)))
(defun cider-browse-ns--properties (var var-meta)
"Decorate VAR with a clickable keymap and a face.
VAR-META is used to decide a font-lock face."
(let ((face (cider-browse-ns--text-face var-meta)))
(propertize var
'font-lock-face face
'mouse-face 'highlight
'keymap cider-browse-ns-mouse-map)))
(defun cider-browse-ns--ns-list (buffer title nss)
"List the namespaces NSS in BUFFER.
Buffer is rendered with TITLE at the top and lists ITEMS filtered according
to user settings."
(let ((dict (nrepl-dict)))
(dolist (ns nss)
(nrepl-dict-put dict ns (nrepl-dict "ns" "true")))
(cider-browse-ns--list buffer title dict nil)))
(defun cider-browse-ns--list (buffer title items ns)
"Initialize rendering of browse-ns BUFFER.
Initialize the buffer's TITLE, namespace NS, and the nrepl-dict ITEMS to be
displayed."
(with-current-buffer buffer
(cider-browse-ns-mode)
(setq-local cider-browse-ns-items items)
(setq-local cider-browse-ns-title title)
(setq-local cider-browse-ns-filters cider-browse-ns-default-filters)
(setq-local cider-browse-ns-current-ns ns))
(cider-browse-ns--render-buffer))
(defun cider-browse-ns--meta-macro-p (var-meta)
"Return non-nil if VAR-META is the metadata of a macro."
(and (nrepl-dict-contains var-meta "arglists")
(string= (nrepl-dict-get var-meta "macro") "true")))
(defun cider-browse-ns--meta-test-p (var-meta)
"Return non-nil if VAR-META is the metadata of a test."
(nrepl-dict-contains var-meta "test"))
(defun cider-browse-ns--meta-function-p (var-meta)
"Return non-nil if VAR-META is the metadata of a function."
(and (nrepl-dict-contains var-meta "arglists")
(not (cider-browse-ns--meta-macro-p var-meta))))
(defun cider-browse-ns--meta-private-p (var-meta)
"Return non-nil if VAR-META indicates a private element."
(string= (nrepl-dict-get var-meta "private") "true"))
(defun cider-browse-ns--meta-var-p (var-meta)
"Return non-nil if VAR-META indicates a var."
(not (or (cider-browse-ns--meta-test-p var-meta)
(cider-browse-ns--meta-macro-p var-meta)
(cider-browse-ns--meta-function-p var-meta))))
(defun cider-browse-ns--item-filter (_ var-meta)
"Return non-nil if item containing VAR-META should be listed in buffer."
(let ((function-filter-p (memq 'function cider-browse-ns-filters))
(var-filter-p (memq 'var cider-browse-ns-filters))
(private-filter-p (memq 'private cider-browse-ns-filters))
(test-filter-p (memq 'test cider-browse-ns-filters))
(macro-filter-p (memq 'macro cider-browse-ns-filters)))
;; check if item should be displayed
(let* ((macro-p (cider-browse-ns--meta-macro-p var-meta))
(function-p (cider-browse-ns--meta-function-p var-meta))
(private-p (cider-browse-ns--meta-private-p var-meta))
(test-p (cider-browse-ns--meta-test-p var-meta))
(var-p (cider-browse-ns--meta-var-p var-meta)))
(or cider-browse-ns-show-all
(not (or (and macro-p macro-filter-p)
(and function-p function-filter-p)
(and test-p test-filter-p)
(and var-p var-filter-p)
(and private-p private-filter-p)))))))
(defun cider-browse-ns--propertized-item (key items)
"Return propertized line of item KEY in nrepl-dict ITEMS."
(let* ((var-meta (nrepl-dict-get items key))
(face (cider-browse-ns--text-face (nrepl-dict-get items key)))
(private-p (string= (nrepl-dict-get var-meta "private") "true"))
(test-p (nrepl-dict-contains var-meta "test"))
(ns-p (nrepl-dict-contains var-meta "ns")))
(concat
(propertize key
'font-lock-face face
'ns ns-p)
" "
(cond
(test-p (propertize "(test) " 'face 'cider-browse-ns-extra-info-face))
(private-p (propertize "(-) " 'face 'cider-browse-ns-extra-info-face))
(t "")))))
(defun cider-browse-ns--display-list (keys items max-length &optional label)
"Render the items of KEYS as condained in the nrepl-dict ITEMS.
Pad the row to be MAX-LENGTH+1. If LABEL is non-nil, add a header to the
list of items."
(when keys
(when label
(insert " " label ":\n"))
(dolist (key keys)
(let* ((doc (nrepl-dict-get-in items (list key "doc")))
(doc (when doc (read doc)))
(first-doc-line (cider-browse-ns--first-doc-line doc))
(item-line (cider-browse-ns--propertized-item key items)))
(insert " ")
(insert item-line)
(when cider-browse-ns-current-ns
(insert (make-string (+ (- max-length (string-width item-line)) 1) ?·))
(insert " " (propertize first-doc-line 'font-lock-face 'font-lock-doc-face)))
(insert "\n")))
(insert "\n")))
(defun cider-browse-ns--column-width (items)
"Determine the display width of displayed ITEMS."
(let* ((propertized-lines
(seq-map (lambda (key)
(cider-browse-ns--propertized-item key items))
(nrepl-dict-keys items))))
(if propertized-lines
(apply #'max (seq-map (lambda (entry) (string-width entry))
propertized-lines))
0)))
(defun cider-browse-ns--render-items (items)
"Render the nrepl-dict ITEMS to the browse-ns buffer."
(let* ((max-length (cider-browse-ns--column-width items)))
(cl-labels
((keys-from-pred
(pred items)
(nrepl-dict-keys (nrepl-dict-filter (lambda (_ var-meta)
(funcall pred var-meta))
items))))
(cond
((eql cider-browse-ns-group-by 'type)
(let* ((func-keys (keys-from-pred #'cider-browse-ns--meta-function-p items))
(macro-keys (keys-from-pred #'cider-browse-ns--meta-macro-p items))
(var-keys (keys-from-pred #'cider-browse-ns--meta-var-p items))
(test-keys (keys-from-pred #'cider-browse-ns--meta-test-p items)))
(cider-browse-ns--display-list func-keys items max-length "Functions")
(cider-browse-ns--display-list macro-keys items max-length "Macros")
(cider-browse-ns--display-list var-keys items max-length "Vars")
(cider-browse-ns--display-list test-keys items max-length "Tests")))
((eql cider-browse-ns-group-by 'visibility)
(let* ((public-keys
(keys-from-pred
(lambda (var-meta)
(not (cider-browse-ns--meta-private-p var-meta)))
items))
(private-keys (keys-from-pred #'cider-browse-ns--meta-private-p items)))
(cider-browse-ns--display-list public-keys items max-length "Public")
(cider-browse-ns--display-list private-keys items max-length "Private")))
(t
(cider-browse-ns--display-list
(nrepl-dict-keys items) items max-length))))))
(defun cider-browse-ns--filter (flag)
"Toggle the filter indicated by FLAG and re-render the buffer."
(setq cider-browse-ns-filters
(if (memq flag cider-browse-ns-filters)
(remq flag cider-browse-ns-filters)
(cons flag cider-browse-ns-filters)))
(cider-browse-ns--render-buffer))
(defun cider-browse-ns--button-filter (button)
"Handle filter action for BUTTON."
(let ((flag (button-get button 'filter)))
(cider-browse-ns--filter flag)))
(defun cider-browse-ns--group (flag)
"Set the group-by option to FLAG and re-renderthe buffer."
(setq cider-browse-ns-group-by
(if (eql flag cider-browse-ns-group-by) nil flag))
(cider-browse-ns--render-buffer))
(defun cider-browse-ns--button-group (button)
"Handle grouping action for BUTTON."
(let ((flag (button-get button 'group-by)))
(cider-browse-ns--group flag)))
(defun cider-browse-ns--toggle-all (_button)
"Toggle the display-all visibility setting."
(setq cider-browse-ns-show-all (not cider-browse-ns-show-all))
(cider-browse-ns--render-buffer))
(defun cider-browse-ns--render-header (&optional filtered-items-ct)
"Render the section at the top of the buffer displaying visibility controls.
If FILTERED-ITEMS-CT is non-nil, then display a message of how many items
are being filtered."
;; Display Show line
(insert " Show: ")
(insert-text-button "All"
'follow-link t
'action #'cider-browse-ns--toggle-all
;; 'help-echo (cider-stacktrace-tooltip)
'face (if cider-browse-ns-show-all
'cider-stacktrace-filter-active-face
nil))
(insert "\n")
;; Display Filters
(let ((filters '(("Private" private)
("Test" test)
("Macro" macro)
("Function" function)
("Var" var))))
(insert " Hide: ")
(dolist (filter filters)
(seq-let (title key) filter
(let ((is-active (memq key cider-browse-ns-filters)))
(insert-text-button title
'filter key
'follow-link t
'action #'cider-browse-ns--button-filter
;; 'help-echo (cider-stacktrace-tooltip)
'face (if (and is-active (not cider-browse-ns-show-all))
'cider-stacktrace-filter-active-face
nil))
(insert " "))))
(when filtered-items-ct
(insert (format "(%d items filtered)" filtered-items-ct))))
(insert "\n")
;; Groupings
(insert " Group-by: ")
(let ((groupings '(("Type" type)
("Visibility" visibility))))
(dolist (grouping groupings)
(seq-let (title key) grouping
(let ((is-active (eql key cider-browse-ns-group-by)))
(insert-text-button title
'group-by key
'follow-link t
'action #'cider-browse-ns--button-group
;; 'help-echo ()
'face (if is-active
'cider-stacktrace-filter-active-face
nil)))
(insert " "))))
(insert "\n\n"))
(defun cider-browse-ns--render-buffer (&optional buffer)
"Render the sections of the browse-ns buffer.
Render occurs in BUFFER if non-nil. This function is the main entrypoint
for redisplaying the buffer when filters change."
(with-current-buffer (or buffer (current-buffer))
(let* ((inhibit-read-only t)
(point (point))
(filtered-items (nrepl-dict-filter #'cider-browse-ns--item-filter
cider-browse-ns-items))
(filtered-item-ct (- (length (nrepl-dict-keys cider-browse-ns-items))
(length (nrepl-dict-keys filtered-items)))))
(erase-buffer)
(insert (propertize (cider-propertize cider-browse-ns-title 'ns) 'ns t) "\n")
(when cider-browse-ns-current-ns
(cider-browse-ns--render-header filtered-item-ct))
(cider-browse-ns--render-items filtered-items)
(goto-char point))))
(defun cider-browse-ns--first-doc-line (doc)
"Return the first line of the given DOC string.
If the first line of the DOC string contains multiple sentences, only
the first sentence is returned. If the DOC string is nil, a Not documented
string is returned."
(if doc
(let* ((split-newline (split-string doc "\n"))
(first-line (car split-newline)))
(cond
((string-match "\\. " first-line) (substring first-line 0 (match-end 0)))
((= 1 (length split-newline)) first-line)
(t (concat first-line "..."))))
"Not documented."))
(defun cider-browse-ns--combined-vars-with-meta (namespace)
"Return the combined public and private vars in NAMESPACE.
Private vars have the additional metadata \"private\": \"true\" in their
var-meta map."
(let ((items (cider-sync-request:ns-vars-with-meta namespace))
(private-items (cider-sync-request:private-ns-vars-with-meta namespace)))
(when private-items
(dolist (key (nrepl-dict-keys private-items))
(let ((var-meta (nrepl-dict-put (nrepl-dict-get private-items key)
"private" "true")))
(setq items (nrepl-dict-put items key var-meta)))))
items))
;; Interactive Functions
;;;###autoload
(defun cider-browse-ns (namespace)
"List all NAMESPACE's vars in BUFFER."
(interactive (list (completing-read "Browse namespace: " (cider-sync-request:ns-list))))
(with-current-buffer (cider-popup-buffer cider-browse-ns-buffer 'select nil 'ancillary)
(cider-browse-ns--list (current-buffer)
namespace
(cider-browse-ns--combined-vars-with-meta namespace)
namespace)))
;;;###autoload
(defun cider-browse-ns-all ()
"List all loaded namespaces in BUFFER."
(interactive)
(with-current-buffer (cider-popup-buffer cider-browse-ns-buffer 'select nil 'ancillary)
(let ((names (cider-sync-request:ns-list)))
(cider-browse-ns--ns-list
(current-buffer)
"All loaded namespaces"
(mapcar (lambda (name)
(cider-browse-ns--properties name nil))
names)))))
(defun cider-browse-ns--thing-at-point ()
"Get the thing at point.
Return a list of the type ('ns or 'var) and the value."
(let ((ns-p (get-text-property (point) 'ns))
(line (car (split-string (string-trim (thing-at-point 'line)) " "))))
(if (or ns-p (string-match "\\." line))
`(ns ,line)
`(var ,(format "%s/%s"
(or (get-text-property (point) 'cider-browse-ns-current-ns)
cider-browse-ns-current-ns)
line)))))
(defun cider-browse-ns-toggle-all ()
"Toggle showing all of the items in the browse-ns buffer."
(interactive)
(cider-browse-ns--toggle-all nil))
(defun cider-browse-ns-toggle-hide-private ()
"Toggle visibility of private items displayed in browse-ns buffer."
(interactive)
(cider-browse-ns--filter 'private))
(defun cider-browse-ns-toggle-hide-test ()
"Toggle visibility of test items displayed in browse-ns buffer."
(interactive)
(cider-browse-ns--filter 'test))
(defun cider-browse-ns-toggle-hide-macro ()
"Toggle visibility of macro items displayed in browse-ns buffer."
(interactive)
(cider-browse-ns--filter 'macro))
(defun cider-browse-ns-toggle-hide-function ()
"Toggle visibility of function items displayed in browse-ns buffer."
(interactive)
(cider-browse-ns--filter 'function))
(defun cider-browse-ns-toggle-hide-var ()
"Toggle visibility of var items displayed in browse-ns buffer."
(interactive)
(cider-browse-ns--filter 'var))
(defun cider-browse-ns-group-by-type ()
"Toggle visibility of var items displayed in browse-ns buffer."
(interactive)
(cider-browse-ns--group 'type))
(defun cider-browse-ns-group-by-visibility ()
"Toggle visibility of var items displayed in browse-ns buffer."
(interactive)
(cider-browse-ns--group 'visibility))
(declare-function cider-doc-lookup "cider-doc")
(defun cider-browse-ns-doc-at-point ()
"Show the documentation for the thing at current point."
(interactive)
(let* ((thing (cider-browse-ns--thing-at-point))
(value (cadr thing)))
;; value is either some ns or a var
(cider-doc-lookup value)))
(defun cider-browse-ns-operate-at-point ()
"Expand browser according to thing at current point.
If the thing at point is a ns it will be browsed,
and if the thing at point is some var - its documentation will
be displayed."
(interactive)
(let* ((thing (cider-browse-ns--thing-at-point))
(type (car thing))
(value (cadr thing)))
(if (eq type 'ns)
(cider-browse-ns value)
(cider-doc-lookup value))))
(declare-function cider-find-ns "cider-find")
(declare-function cider-find-var "cider-find")
(defun cider-browse-ns-find-at-point ()
"Find the definition of the thing at point."
(interactive)
(let* ((thing (cider-browse-ns--thing-at-point))
(type (car thing))
(value (cadr thing)))
(if (eq type 'ns)
(cider-find-ns nil value)
(cider-find-var current-prefix-arg value))))
(defun cider-browse-ns-handle-mouse (_event)
"Handle mouse click EVENT."
(interactive "e")
(cider-browse-ns-operate-at-point))
(provide 'cider-browse-ns)
;;; cider-browse-ns.el ends here