-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathalchemist-help.el
267 lines (227 loc) · 10.8 KB
/
alchemist-help.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
;;; alchemist-help.el --- Functionality for Elixir documentation lookup -*- lexical-binding: t -*-
;; Copyright © 2014-2015 Samuel Tonini
;; Author: Samuel Tonini <[email protected]
;; This file is not part of GNU Emacs.
;; 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/>.
;;; Commentary:
;; Functionality for Elixir documentation lookup.
;;; Code:
(require 'dash)
(require 'alchemist-utils)
(require 'alchemist-project)
(require 'alchemist-server)
(require 'alchemist-scope)
(require 'alchemist-goto)
(defgroup alchemist-help nil
"Functionality for Elixir documentation lookup."
:prefix "alchemist-help-"
:group 'alchemist)
(defcustom alchemist-help-buffer-name "*alchemist help*"
"Name of the Elixir help buffer."
:type 'string
:group 'alchemist-help)
(defvar alchemist-help-search-history '()
"Storage for the search history.")
(defvar alchemist-help-current-search-text '()
"Stores the current search.")
(defvar alchemist-help-filter-output nil)
(defface alchemist-help-key-face
'((t (:inherit font-lock-variable-name-face :bold t :foreground "red")))
"Face for the letter keys in the summary."
:group 'alchemist-help)
(defun alchemist-help-lookup-doc (search)
"Lookup Elixir documentation for SEARCH."
(setq alchemist-help-current-search-text search)
(setq alchemist-help-filter-output nil)
(if (not (alchemist-utils--empty-string-p search))
(alchemist-server-complete-candidates
(alchemist-help--completion-server-arguments search)
#'alchemist-help-complete-filter-output)
(message "No documentation for [%s] found." search)))
(defun alchemist-help-no-doc-available-p (string)
"Return non-nil if STRING contains Elixir no documentation message."
(or (string-match-p "No documentation for" string)
(string-match-p "Could not load module" string)
(string-match-p "it does not have Elixir-style docs" string)
(alchemist-utils--empty-string-p string)))
(defun alchemist-help-store-search-in-history ()
"Store the last `alchemist-help-current-search-text' in `alchemist-help-search-history'."
(unless (memq 'alchemist-help-current-search-text alchemist-help-search-history)
(add-to-list 'alchemist-help-search-history alchemist-help-current-search-text)))
(defun alchemist-help-display-doc (content)
"Initialize the `alchemist-help-buffer-name' and insert CONTENT."
(let ((default-directory (alchemist-project-root-or-default-dir))
(buffer (get-buffer-create alchemist-help-buffer-name)))
(cond
((alchemist-help-no-doc-available-p content)
(message (format "No documentation for [%s] found."
alchemist-help-current-search-text)))
(t
(alchemist-help-store-search-in-history)
(with-current-buffer buffer
(let ((inhibit-read-only t))
(goto-char (point-min))
(erase-buffer)
(insert content)
(goto-char (point-min))
(ansi-color-apply-on-region (point-min) (point-max))
(alchemist-help-minor-mode)))
(pop-to-buffer buffer)))))
(defun alchemist-help--search-at-point ()
"Search through `alchemist-help' with the expression under the cursor"
(let* ((expr (alchemist-scope-expression)))
(alchemist-help-lookup-doc (alchemist-help--prepare-search-expr expr))))
(defun alchemist-help--search-marked-region (begin end)
"Run `alchemist-help' with the marked region.
Argument BEGIN where the mark starts.
Argument END where the mark ends."
(let ((expr (buffer-substring-no-properties begin end)))
(alchemist-help-lookup-doc (alchemist-help--prepare-search-expr expr))))
(defun alchemist-help--prepare-search-expr (expr)
(let* ((module (alchemist-scope-extract-module expr))
(module (alchemist-scope-alias-full-path module))
(module (if module module ""))
(function (alchemist-scope-extract-function expr))
(function (if function function ""))
(expr (cond
((and (not (alchemist-utils--empty-string-p module))
(not (alchemist-utils--empty-string-p function)))
(format "%s.%s" module function))
((not (alchemist-utils--empty-string-p module))
module)
(t
expr))))
expr))
(defun alchemist-help--elixir-modules-to-list (str)
(let* ((str (replace-regexp-in-string "^Elixir\\." "" str))
(modules (split-string str))
(modules (delete nil modules))
(modules (cl-sort modules 'string-lessp :key 'downcase))
(modules (-distinct modules)))
modules))
(defun alchemist-help-minor-mode-key-binding-summary ()
(interactive)
(message
(concat "[" (propertize "q" 'face 'alchemist-help-key-face)
"]-quit ["
(propertize "e" 'face 'alchemist-help-key-face)
"]-search-at-point ["
(propertize "s" 'face 'alchemist-help-key-face)
"]-search ["
(propertize "h" 'face 'alchemist-help-key-face)
"]-history ["
(propertize "?" 'face 'alchemist-help-key-face)
"]-keys")))
(defun alchemist-help--server-arguments (args)
(if (not (equal major-mode 'alchemist-iex-mode))
(let* ((modules (alchemist-utils--prepare-modules-for-elixir
(alchemist-scope-all-modules))))
(format "%s;%s" args modules))
(format "%s;[];" args)))
(defun alchemist-help--completion-server-arguments (args)
"Build informations about the current context."
(if (not (equal major-mode 'alchemist-iex-mode))
(let* ((modules (alchemist-utils--prepare-modules-for-elixir
(alchemist-scope-all-modules)))
(aliases (alchemist-utils--prepare-aliases-for-elixir
(alchemist-scope-aliases))))
(format "%s;%s;%s" args modules aliases))
(format "%s;[];[]" args)))
(defun alchemist-help-complete-filter-output (_process output)
(with-local-quit
(setq alchemist-help-filter-output (cons output alchemist-help-filter-output))
(if (alchemist-server-contains-end-marker-p output)
(let* ((string (alchemist-server-prepare-filter-output alchemist-help-filter-output))
(candidates (alchemist-complete--output-to-list
(alchemist--utils-clear-ansi-sequences string)))
(candidates (if (= (length candidates) 2)
nil
candidates)))
(setq alchemist-help-filter-output nil)
(if candidates
(let* ((search (alchemist-complete--completing-prompt alchemist-help-current-search-text candidates)))
(setq alchemist-help-current-search-text search)
(alchemist-server-help (alchemist-help--server-arguments search) #'alchemist-help-filter-output))
(alchemist-server-help (alchemist-help--server-arguments alchemist-help-current-search-text) #'alchemist-help-filter-output))))))
(defun alchemist-help-filter-output (_process output)
(setq alchemist-help-filter-output (cons output alchemist-help-filter-output))
(if (alchemist-server-contains-end-marker-p output)
(let ((string (alchemist-server-prepare-filter-output alchemist-help-filter-output)))
(alchemist-help-display-doc string)
(setq alchemist-help-current-search-text nil)
(setq alchemist-help-filter-output nil))))
(defun alchemist-help-modules-filter (_process output)
(with-local-quit
(setq alchemist-help-filter-output (cons output alchemist-help-filter-output))
(if (alchemist-server-contains-end-marker-p output)
(let* ((output (alchemist-server-prepare-filter-output alchemist-help-filter-output))
(modules (alchemist-help--elixir-modules-to-list output))
(search (completing-read
"Elixir help: "
modules
nil
nil
nil))
(module (alchemist-scope-extract-module search))
(function (alchemist-scope-extract-function search))
(search (cond
((and module function)
search)
((and module
(not (string-match-p "[\/0-9]+$" module)))
(concat module "."))
(t
search))))
(alchemist-help-lookup-doc (alchemist-utils--remove-dot-at-the-end search))))))
;; Public functions
(defun alchemist-help-search-at-point ()
"Search through `alchemist-help' with the expression under the cursor.
If the buffer local variable `mark-active' is non-nil,
the actively marked region will be used for passing to `alchemist-help'."
(interactive)
(if mark-active
(alchemist-help--search-marked-region (region-beginning) (region-end))
(alchemist-help--search-at-point)))
(defvar alchemist-help-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "q") #'quit-window)
(define-key map (kbd "e") #'alchemist-help-search-at-point)
(define-key map (kbd "s") #'alchemist-help)
(define-key map (kbd "h") #'alchemist-help-history)
(define-key map (kbd "M-.") #'alchemist-goto-definition-at-point)
(define-key map (kbd "?") #'alchemist-help-minor-mode-key-binding-summary)
map)
"Keymap for `alchemist-help-minor-mode'.")
(define-minor-mode alchemist-help-minor-mode
"Minor mode for displaying elixir help."
:group 'alchemist-help
:keymap alchemist-help-minor-mode-map
(cond (alchemist-help-minor-mode
(setq buffer-read-only t))
(t
(setq buffer-read-only nil))))
(defun alchemist-help ()
"Load Elixir documentation for SEARCH."
(interactive)
(setq alchemist-help-filter-output nil)
(alchemist-server-help-with-modules #'alchemist-help-modules-filter))
(defun alchemist-help-history (search)
"Load Elixir from the documentation history for SEARCH."
(interactive
(list
(completing-read "Elixir help history: " alchemist-help-search-history nil nil "")))
(alchemist-help-lookup-doc search))
;; Deprecated functions; these will get removed in v1.5.0
(defun alchemist-help-search-marked-region () (interactive)
(alchemist-utils-deprecated-message "alchemist-help-search-marked-region" "alchemist-help-search-at-point"))
(provide 'alchemist-help)
;;; alchemist-help.el ends here