Skip to content

Commit 9c1975e

Browse files
katomusobbatsov
authored andcommitted
Refactor code used to insert sections into ClojureDocs buffer
1 parent 1fcc6d2 commit 9c1975e

File tree

1 file changed

+60
-33
lines changed

1 file changed

+60
-33
lines changed

cider-clojuredocs.el

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,42 +109,69 @@ opposite of what that option dictates."
109109
(goto-char (point-min))
110110
(current-buffer)))
111111

112+
(defun cider-clojuredocs--insert-overview (dict)
113+
"Insert \"Overview\" section based on data from DICT."
114+
(insert (format "= %s/%s\n"
115+
(nrepl-dict-get dict "ns")
116+
(nrepl-dict-get dict "name")))
117+
(newline)
118+
(when-let ((arglists (nrepl-dict-get dict "arglists")))
119+
(dolist (arglist arglists)
120+
(insert (format " [%s]\n" arglist)))
121+
(newline))
122+
(when-let* ((doc (nrepl-dict-get dict "doc"))
123+
;; As this is a literal docstring from the source code and
124+
;; there are two spaces at the beginning of lines in docstrings,
125+
;; we remove them to make it align nicely in ClojureDocs buffer.
126+
(doc (replace-regexp-in-string "\n " "\n" doc)))
127+
(insert doc "\n")
128+
(newline)))
129+
130+
(defun cider-clojuredocs--insert-see-also (dict)
131+
"Insert \"See Also\" section based on data from DICT."
132+
(insert "== See Also\n")
133+
(newline)
134+
(if-let ((see-alsos (nrepl-dict-get dict "see-alsos")))
135+
(dolist (see-also see-alsos)
136+
(insert "* ")
137+
(insert-text-button see-also
138+
'sym see-also
139+
'action (lambda (btn)
140+
(cider-clojuredocs-lookup (button-get btn 'sym)))
141+
'help-echo (format "Press Enter or middle click to jump to %s" see-also))
142+
(insert "\n"))
143+
(insert "Not available\n"))
144+
(newline))
145+
146+
(defun cider-clojuredocs--insert-examples (dict)
147+
"Insert \"Examples\" section based on data from DICT."
148+
(insert "== Examples\n")
149+
(newline)
150+
(if-let ((examples (nrepl-dict-get dict "examples")))
151+
(dolist (example examples)
152+
(insert (cider-font-lock-as-clojure example) "\n")
153+
(insert "-------------------------------------------------\n"))
154+
(insert "Not available\n"))
155+
(newline))
156+
157+
(defun cider-clojuredocs--insert-notes (dict)
158+
"Insert \"Notes\" section based on data from DICT."
159+
(insert "== Notes\n")
160+
(newline)
161+
(if-let ((notes (nrepl-dict-get dict "notes")))
162+
(dolist (note notes)
163+
(insert note "\n")
164+
(insert "-------------------------------------------------\n"))
165+
(insert "Not available\n"))
166+
(newline))
167+
112168
(defun cider-clojuredocs--content (dict)
113169
"Generate a nice string from DICT."
114170
(with-temp-buffer
115-
(insert "= " (nrepl-dict-get dict "ns") "/" (nrepl-dict-get dict "name") "\n\n")
116-
(let ((arglists (nrepl-dict-get dict "arglists")))
117-
(dolist (arglist arglists)
118-
(insert (format " [%s]\n" arglist)))
119-
(when-let* ((doc (nrepl-dict-get dict "doc"))
120-
;; As this is a literal docstring from the source code and
121-
;; there are two spaces at the beginning of lines in docstrings,
122-
;; we remove them to make it align nicely in ClojureDocs buffer.
123-
(doc (replace-regexp-in-string "\n " "\n" doc)))
124-
(insert "\n" doc "\n")))
125-
(insert "\n== See Also\n\n")
126-
(if-let ((see-alsos (nrepl-dict-get dict "see-alsos")))
127-
(dolist (see-also see-alsos)
128-
(insert "* ")
129-
(insert-text-button see-also
130-
'sym see-also
131-
'action (lambda (btn)
132-
(cider-clojuredocs-lookup (button-get btn 'sym)))
133-
'help-echo (format "Press Enter or middle click to jump to %s" see-also))
134-
(insert "\n"))
135-
(insert "Not available\n"))
136-
(insert "\n== Examples\n\n")
137-
(if-let ((examples (nrepl-dict-get dict "examples")))
138-
(dolist (example examples)
139-
(insert (cider-font-lock-as-clojure example))
140-
(insert "\n-------------------------------------------------\n"))
141-
(insert "Not available\n"))
142-
(insert "\n== Notes\n\n")
143-
(if-let ((notes (nrepl-dict-get dict "notes")))
144-
(dolist (note notes)
145-
(insert note)
146-
(insert "\n-------------------------------------------------\n"))
147-
(insert "Not available\n"))
171+
(cider-clojuredocs--insert-overview dict)
172+
(cider-clojuredocs--insert-see-also dict)
173+
(cider-clojuredocs--insert-examples dict)
174+
(cider-clojuredocs--insert-notes dict)
148175
(buffer-string)))
149176

150177
(defun cider-clojuredocs-lookup (sym)

0 commit comments

Comments
 (0)