forked from Sasanidas/Apprentice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apprentice-help.el
282 lines (239 loc) · 11.2 KB
/
apprentice-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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
;;; apprentice-help.el --- Functionality for Elixir documentation lookup -*- lexical-binding: t -*-
;; Copyright © 2014-2017 Samuel Tonini
;; Copyright © 2022 Fermin MF
;; Author: Samuel Tonini <tonini.samuel@gmail.com
;; Maintainer: Fermin MF <fmfs@posteo.net>
;; 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 'ansi-color)
;; (require 'apprentice-project)
;; (require 'apprentice-scope)
;; (require 'apprentice-iex)
;; (defgroup apprentice-help nil
;; "Functionality for Elixir documentation lookup."
;; :prefix "apprentice-help-"
;; :group 'apprentice)
;; (defcustom apprentice-help-buffer-name "*apprentice help*"
;; "Name of the Elixir help buffer."
;; :type 'string
;; :group 'apprentice-help)
;; (defvar apprentice-help-search-history '()
;; "Storage for the search history.")
;; (defvar apprentice-help-current-search-text '()
;; "Stores the current search.")
;; (defvar apprentice-help-filter-output nil)
;; (defface apprentice-help-key-face
;; '((t (:inherit font-lock-variable-name-face :bold t :foreground "red")))
;; "Face for the letter keys in the summary."
;; :group 'apprentice-help)
;; (defun apprentice-help-lookup-doc (search)
;; "Lookup Elixir documentation for SEARCH."
;; (setq apprentice-help-current-search-text search)
;; (setq apprentice-help-filter-output nil)
;; (if (not (or (null search)
;; (string= "" search)))
;; (apprentice-help-display-doc )
;; (message "Not implemented")
;; (message "No documentation for [%s] found." search)))
;; (defun apprentice-help--comint-last-output (process)
;; (let ((proc process)
;; (inhibit-read-only t)
;; output)
;; (with-current-buffer (process-buffer proc)
;; (save-excursion
;; (let ((pmark
;; (progn (goto-char (process-mark proc))
;; (forward-line 0)
;; (point-marker))))
;; (copy-region-as-kill comint-last-input-end pmark)
;; (setf output
;; (buffer-substring-no-properties comint-last-input-end pmark))
;; (goto-char (process-mark proc)))))
;; ;; Output message and put back prompt
;; output))
;; (defun apprentice-help--comint-restart-buffer (proc prev)
;; (let ((inhibit-read-only t))
;; (with-current-buffer (process-buffer proc)
;; (kill-region (point-min) (point-max))
;; (insert prev))))
;; (defun apprentice-help--iex-get-all-modules ()
;; (let* ((proc (apprentice-iex-process))
;; (prev
;; (with-current-buffer (process-buffer proc)
;; (buffer-string)))
;; output)
;; (apprentice-iex--send-command
;; proc ":erlang.loaded() |> Enum.sort() |> inspect(limit: :infinity)")
;; (sit-for 0.2)
;; (setf output (apprentice-help--comint-last-output proc))
;; (with-current-buffer (process-buffer proc)
;; (comint-delete-output))
;; output))
;; (defun apprentice-help-no-doc-available-p (string)
;; "Return non-nil if STRING contain 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)
;; (or (null string) (string= "" string))))
;; (defun apprentice-help-store-search-in-history ()
;; "Store the last `apprentice-help-current-search-text' in `apprentice-help-search-history'."
;; (unless (memq 'apprentice-help-current-search-text apprentice-help-search-history)
;; (add-to-list 'apprentice-help-search-history apprentice-help-current-search-text)))
;; (defun apprentice-help-display-doc (content)
;; "Initialize the `apprentice-help-buffer-name' and insert CONTENT."
;; (let ((default-directory (apprentice-project-root-or-default-dir))
;; (buffer (get-buffer-create apprentice-help-buffer-name)))
;; (cond
;; ((apprentice-help-no-doc-available-p content)
;; (message (format "No documentation for [%s] found." apprentice-help-current-search-text)))
;; (t
;; (apprentice-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))
;; (apprentice-help-minor-mode)))
;; (pop-to-buffer buffer)))))
;; (defun apprentice-help--search-at-point ()
;; "Search through `apprentice-help' with the expression under the cursor"
;; (let* ((expr (apprentice-scope-expression)))
;; (apprentice-help-lookup-doc (apprentice-help--prepare-search-expr expr))))
;; (defun apprentice-help--search-marked-region (begin end)
;; "Run `apprentice-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)))
;; (apprentice-help-lookup-doc (apprentice-help--prepare-search-expr expr))))
;; (defun apprentice-help--prepare-search-expr (expr)
;; (let* ((module (apprentice-scope-extract-module expr))
;; (module (apprentice-scope-alias-full-path module))
;; (module (if module module ""))
;; (function (apprentice-scope-extract-function expr))
;; (function (if function function ""))
;; (expr (cond
;; ((and (not (s-blank? module))
;; (not (s-blank? function)))
;; (format "%s.%s" module function))
;; ((not (s-blank? module))
;; module)
;; (t
;; expr))))
;; expr))
;; (defun apprentice-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 (cl-remove-duplicates modules :test #'equal)))
;; modules))
;; (defun apprentice-help-minor-mode-key-binding-summary ()
;; (interactive)
;; (message
;; (concat "[" (propertize "q" 'face 'apprentice-help-key-face)
;; "]-quit ["
;; (propertize "e" 'face 'apprentice-help-key-face)
;; "]-search-at-point ["
;; (propertize "m" 'face 'apprentice-help-key-face)
;; "]-search-module ["
;; (propertize "s" 'face 'apprentice-help-key-face)
;; "]-search ["
;; (propertize "h" 'face 'apprentice-help-key-face)
;; "]-history ["
;; (propertize "?" 'face 'apprentice-help-key-face)
;; "]-keys")))
;; (defun apprentice-help-filter-output (_process output)
;; (setq apprentice-help-filter-output (cons output apprentice-help-filter-output))
;; (if (apprentice-server-contains-end-marker-p output)
;; (let ((string (apprentice-server-prepare-filter-output apprentice-help-filter-output)))
;; (apprentice-help-display-doc string)
;; (setq apprentice-help-current-search-text nil)
;; (setq apprentice-help-filter-output nil))))
;; (defun apprentice-help-modules-filter (_process output)
;; (with-local-quit
;; (setq apprentice-help-filter-output (cons output apprentice-help-filter-output))
;; (if (apprentice-server-contains-end-marker-p output)
;; (let* ((output (apprentice-server-prepare-filter-output apprentice-help-filter-output))
;; (modules (apprentice-help--elixir-modules-to-list output))
;; (search (completing-read
;; "Elixir help: "
;; modules
;; nil
;; nil
;; nil))
;; (module (apprentice-scope-extract-module search))
;; (function (apprentice-scope-extract-function search))
;; (search (cond
;; ((and module function)
;; search)
;; ((and module
;; (not (string-match-p "[\/0-9]+$" module)))
;; (concat module "."))
;; (t
;; search))))
;; (apprentice-help-lookup-doc (apprentice-utils-remove-dot-at-the-end search))))))
;; ;; ;; Public functions
;; (defun apprentice-help-search-at-point ()
;; "Search through `apprentice-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 `apprentice-help'."
;; (interactive)
;; (if mark-active
;; (apprentice-help--search-marked-region (region-beginning) (region-end))
;; (apprentice-help--search-at-point)))
;; (defun apprentice-help-module ()
;; "Load Elixir documentation for the module of the most recent SEARCH.
;; This is helpful to jump from the documentation of, say, the String.split/1
;; function to the documetation of the String module."
;; (interactive)
;; (let* ((current-search (car apprentice-help-search-history))
;; (module (apprentice-scope-extract-module current-search))
;; (module (apprentice-scope-alias-full-path module)))
;; (if module
;; (apprentice-help-lookup-doc (apprentice-help--prepare-search-expr module))
;; (message "No module found"))))
;; (defvar apprentice-help-minor-mode-map
;; (let ((map (make-sparse-keymap)))
;; (define-key map (kbd "q") #'quit-window)
;; (define-key map (kbd "e") #'apprentice-help-search-at-point)
;; (define-key map (kbd "m") #'apprentice-help-module)
;; (define-key map (kbd "s") #'apprentice-help)
;; (define-key map (kbd "h") #'apprentice-help-history)
;; (define-key map (kbd "?") #'apprentice-help-minor-mode-key-binding-summary)
;; map)
;; "Keymap for `apprentice-help-minor-mode'.")
;; (define-minor-mode apprentice-help-minor-mode
;; "Minor mode for displaying elixir help."
;; :group 'apprentice-help
;; :keymap apprentice-help-minor-mode-map
;; (cond (apprentice-help-minor-mode
;; (setq buffer-read-only t))
;; (t
;; (setq buffer-read-only nil))))
;; (defun apprentice-help ()
;; "Load Elixir documentation for SEARCH."
;; (interactive)
;; (setq apprentice-help-filter-output nil)
;; (apprentice-server-info "{ :type, :modules }" #'apprentice-help-modules-filter))
;; (defun apprentice-help-history (search)
;; "Load Elixir from the documentation history for SEARCH."
;; (interactive
;; (list
;; (completing-read "Elixir help history: " apprentice-help-search-history nil nil "")))
;; (apprentice-help-lookup-doc search))
(provide 'apprentice-help)
;;; apprentice-help.el ends here