forked from clojure-emacs/cider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cider-doc.el
302 lines (254 loc) · 10.4 KB
/
cider-doc.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
;;; cider-doc.el --- Documentation viewer -*- lexical-binding: t -*-
;; Copyright © 2014 Jeff Valk
;; Author: Jeff Valk <[email protected]>
;; 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:
;; Mode for formatting and presenting documentation
;;; Code:
(require 'cider-util)
(require 'org-table)
(require 'button)
(require 'dash)
(require 'easymenu)
;; Variables
(defgroup cider-doc nil
"Formatting/fontifying documentation viewer."
:prefix "cider-doc-"
:group 'cider)
(defcustom cider-doc-fill-column fill-column
"Fill column for docstrings in doc buffer."
:type 'list
:group 'cider-doc
:package-version '(cider . "0.7.0"))
;; Faces
(defface cider-doc-emphasis-face
'((t (:inherit default :underline t)))
"Face for emphasized text"
:group 'cider-doc
:package-version '(cider . "0.7.0"))
(defface cider-doc-strong-face
'((t (:inherit default :underline t :weight bold)))
"Face for strongly emphasized text"
:group 'cider-doc
:package-version '(cider . "0.7.0"))
(defface cider-doc-literal-face
'((t (:inherit font-lock-string-face)))
"Face for literal text"
:group 'cider-doc
:package-version '(cider . "0.7.0"))
(defface cider-doc-table-border-face
'((t (:inherit shadow)))
"Face for table borders"
:group 'cider-doc
:package-version '(cider . "0.7.0"))
;; Colors & Theme Support
(defvar cider-doc-code-background-color
(cider-scale-background-color)
"Background color for code blocks.")
(defadvice enable-theme (after cider-doc-adapt-to-theme activate)
"When theme is changed, update `cider-doc-code-background-color'."
(setq cider-doc-code-background-color (cider-scale-background-color)))
;; Mode & key bindings
(defvar cider-doc-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "q" 'cider-popup-buffer-quit-function)
(define-key map "j" 'cider-doc-javadoc)
(easy-menu-define cider-doc-mode-menu map
"Menu for CIDER's doc mode"
'("Doc"
["Go to Javadoc" cider-doc-javadoc]))
map))
(define-derived-mode cider-doc-mode fundamental-mode "Doc"
"Major mode for displaying CIDER documentation
\\{cider-doc-mode-map}"
(setq buffer-read-only t)
(setq-local truncate-lines t)
(setq-local electric-indent-chars nil)
(setq-local cider-doc-symbol nil)
(setq-local cider-doc-javadoc-url nil))
;; Interactive functions
(defun cider-doc-javadoc ()
"Open the Javadoc for the current class, if available."
(interactive)
(if cider-doc-javadoc-url
(browse-url cider-doc-javadoc-url)
(message "No Javadoc available for %s" cider-doc-symbol)))
;; Font Lock and Formatting
(defun cider-doc-fontify-code-blocks (buffer mode)
"Font lock BUFFER code blocks using MODE and remove markdown characters.
This processes the triple backtick GFM markdown extension. An overlay is used
to shade the background. Blocks are marked to be ignored by other fonification
and line wrap."
(with-current-buffer buffer
(save-excursion
(while (search-forward-regexp "```\n" nil t)
(replace-match "")
(let ((beg (point))
(bg `(:background ,cider-doc-code-background-color)))
(when (search-forward-regexp "```\n" nil t)
(replace-match "")
(cider-font-lock-region-as mode beg (point))
(overlay-put (make-overlay beg (point)) 'face bg)
(put-text-property beg (point) 'block 'code)))))))
(defun cider-doc-fontify-literals (buffer)
"Font lock BUFFER literal text and remove backtick markdown characters.
Preformatted code text blocks are ignored."
(with-current-buffer buffer
(save-excursion
(while (search-forward "`" nil t)
(if (eq (get-text-property (point) 'block) 'code)
(forward-char)
(progn
(replace-match "")
(let ((beg (point)))
(when (search-forward "`" (line-end-position) t)
(replace-match "")
(put-text-property beg (point) 'face 'cider-doc-literal-face)))))))))
(defun cider-doc-fontify-emphasis (buffer)
"Font lock BUFFER emphasized text and remove markdown characters.
One '*' represents emphasis, multiple '**'s represent strong emphasis.
Preformatted code text blocks are ignored."
(with-current-buffer buffer
(save-excursion
(while (search-forward-regexp "\\(*+\\)\\(\\w\\)" nil t)
(if (eq (get-text-property (point) 'block) 'code)
(forward-char)
(progn
(replace-match "\\2")
(let ((beg (1- (point)))
(face (if (> (length (match-string 1)) 1)
'cider-doc-strong-face
'cider-doc-emphasis-face)))
(when (search-forward-regexp "\\(\\w\\)\\*+" (line-end-position) t)
(replace-match "\\1")
(put-text-property beg (point) 'face face)))))))))
(defun cider-doc-format-tables (buffer)
"Align BUFFER tables and dim borders.
This processes the GFM table markdown extension using `org-table'.
Tables are marked to be ignored by line wrap."
(with-current-buffer buffer
(save-excursion
(let ((border 'cider-doc-table-border-face))
(org-table-map-tables
(lambda ()
(org-table-align)
(goto-char (org-table-begin))
(while (search-forward-regexp "[+|-]" (org-table-end) t)
(put-text-property (match-beginning 0) (match-end 0) 'face border))
(put-text-property (org-table-begin) (org-table-end) 'block 'table)))))))
(defun cider-doc-wrap-text (buffer)
"For text in BUFFER not propertized as 'block', apply line wrap."
(with-current-buffer buffer
(save-excursion
(while (not (eobp))
(unless (get-text-property (point) 'block)
(fill-region (point) (line-end-position)))
(forward-line)))))
;; Rendering
(defun cider-doc-render-java-doc (buffer text)
"Emit into BUFFER formatted doc TEXT for a Java class or member."
(with-current-buffer buffer
(let ((beg (point)))
(insert text)
(save-excursion
(goto-char beg)
(cider-doc-fontify-code-blocks buffer 'java-mode) ; left alone hereafter
(cider-doc-fontify-literals buffer)
(cider-doc-fontify-emphasis buffer)
(cider-doc-format-tables buffer) ; may contain literals, emphasis
(cider-doc-wrap-text buffer))))) ; ignores code, table blocks
(defun cider-doc-render-info (buffer info)
"Emit into BUFFER formatted INFO for the Clojure or Java symbol."
(let* ((ns (cadr (assoc "ns" info)))
(name (cadr (assoc "name" info)))
(added (cadr (assoc "added" info)))
(depr (cadr (assoc "deprecated" info)))
(macro (cadr (assoc "macro" info)))
(special (cadr (assoc "special-form" info)))
(forms (cadr (assoc "forms-str" info)))
(args (cadr (assoc "arglists-str" info)))
(doc (cadr (assoc "doc" info)))
(url (cadr (assoc "url" info)))
(class (cadr (assoc "class" info)))
(member (cadr (assoc "member" info)))
(javadoc (cadr (assoc "javadoc" info)))
(super (cadr (assoc "super" info)))
(ifaces (cadr (assoc "interfaces" info)))
(clj-name (if ns (concat ns "/" name) name))
(java-name (if member (concat class "/" member) class)))
(with-current-buffer buffer
(cl-flet ((emit (text &optional face)
(insert (if face
(propertize text 'font-lock-face face)
text))
(newline)))
(emit (if class java-name clj-name) 'font-lock-function-name-face)
(when super
(emit (concat " Extends: " (cider-font-lock-as 'java-mode super))))
(when ifaces
(emit (concat "Implements: " (cider-font-lock-as 'java-mode (car ifaces))))
(dolist (iface (cdr ifaces))
(emit (concat " "(cider-font-lock-as 'java-mode iface)))))
(when (or super ifaces)
(newline))
(when (or forms args)
(emit (cider-font-lock-as-clojure (or forms args))))
(when (or special macro)
(emit (if special "Special Form" "Macro") 'font-lock-comment-face))
(when added
(emit (concat "Added in " added) 'font-lock-comment-face))
(when depr
(emit (concat "Deprecated in " depr) 'font-lock-comment-face))
(when doc
(if class
(cider-doc-render-java-doc (current-buffer) doc)
(emit (concat " " doc))))
(when url
(newline)
(insert " Please see ")
(insert-text-button url
'url url
'follow-link t
'action (lambda (x)
(browse-url (button-get x 'url))))
(newline))
(when javadoc
(newline)
(newline)
(insert "For additional documentation, see the ")
(insert-text-button "Javadoc"
'url javadoc
'follow-link t
'action (lambda (x)
(browse-url (button-get x 'url))))
(insert ".")
(newline))
(let ((beg (point-min))
(end (point-max)))
(dolist (x info)
(put-text-property beg end (car x) (cadr x)))))
(current-buffer))))
(defun cider-doc-render (buffer symbol info)
"Emit into BUFFER formatted documentation for SYMBOL's INFO."
(with-current-buffer buffer
(let ((javadoc (cadr (assoc "javadoc" info)))
(inhibit-read-only t))
(cider-doc-mode)
(setq-local cider-doc-symbol symbol)
(setq-local cider-doc-javadoc-url javadoc)
(remove-overlays)
(cider-doc-render-info buffer info)
(goto-char (point-min))
(current-buffer))))
(provide 'cider-doc)
;;; cider-doc.el ends here