-
Notifications
You must be signed in to change notification settings - Fork 0
/
starhugger-grep-context.el
271 lines (239 loc) · 9.69 KB
/
starhugger-grep-context.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
;;; starhugger-grep-context.el --- Get project-wide context by dumb grepping -*- lexical-binding: t; -*-
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'compat)
(require 'project)
(require 'starhugger)
;;;; Helpers
(defun starhugger--project-root ()
(-some--> (project-current) (project-root it)))
(cl-defmacro starhugger--at-project-root (&rest body)
(declare (debug t) (indent defun))
`(dlet ((default-directory (or (starhugger--project-root) default-directory)))
,@body))
(defun starhugger--not-literal-regex (str &optional additional-negations)
"Return an ungrouped Rust regex that is the negation of literal STR.
ADDITIONAL-NEGATIONS: a string to put in [^...] to negate also
them, default to an empty string."
(-let* ((additional-negations (or additional-negations "")))
(-->
(-map
(lambda (idx)
(concat
(regexp-quote (substring str 0 idx))
(format "[^%c%s]" (elt str idx) additional-negations)))
(-iota (length str)))
(string-join it "|"))))
(defun starhugger--buffer-string-lines ()
"Doesn't `save-excursion'.
Credit: `process-lines-handling-status'."
(goto-char (point-min))
(let (lines)
(while (not (eobp))
(setq lines
(cons
(buffer-substring-no-properties
(line-beginning-position) (line-end-position))
lines))
(forward-line 1))
(nreverse lines)))
(eval-and-compile (defun starhugger--group-regexes (&rest regexes)
(--> (string-join regexes "|") (concat "(" it ")"))))
;;;; Dumb Grep/Regex-based project-wide code context
(defun starhugger-grep-context--top-level-regex ()
"Return a Rust regex for top-level definitions.
May still include script statements."
(eval-when-compile
(-->
`(,(concat
"^[a-zA-Z]" ; begin with an alphabetic character
(starhugger--group-regexes
(concat
"[^=}]*?"; some characters before "(", but they can't be some
"\\(" ; literal "("
".*?[^\"',;.]$" ; doesn't end with those, cause it looks more like a natural sentence
)
;; Function expression assignments
"[[:alnum:]_ \t]*=[ \t]*\\(.*?\\)[^{]*\\{$"))
;; C macros
"^#define "
;; Lisp
"^\\(([-a-z]+-)?def")
(apply #'starhugger--group-regexes it))))
(defun starhugger-grep-context--non-top-level-def-regex ()
"Return an Rust regex to catch non-top-level definitions."
(eval-when-compile
(concat
;; line start, then a series of spaces or identifiers
"^[ \t_a-zA-Z0-9]+"
;; (then a word delimiter,) then a defined keyword
(format "\\b(%s)"
(string-join '("def"
"define"
"defn"
"defun"
"fn"
"func"
"function")
"|"))
;; space, then a non-underscore character (public function)
" [^_]"
;; contains "("
"[^(]*\\(")))
(defun starhugger-grep-context--class-methods-regex ()
"Return an ungrouped ERE to catch class methods."
(eval-when-compile
;; C++-style class methods:
(concat
;; begin with spaces
"^[ \t]*?"
;; then some identifiers separated by spaces
"([ \t]+[_a-zA-Z]+[_a-zA-Z0-9]*)+"
;; then an argument list (determined by heuristics)
"\\([^-\"'()!+<>;]*?\\)"
;; (maybe return type then) a brace
".*?\\{")))
(defvar starhugger-grep-context--ignored-keywords
'["if" "while"
;;
"switch" "else" "elif" "when" "for" "catch" "return"]
"List of keywords to ignore when looking for context.
When they are at line beginning.")
(defvar starhugger-grep-context--max-lines 96)
(defun starhugger-grep-context--regex-args ()
`("-e"
,(starhugger-grep-context--top-level-regex)
"-e"
,(starhugger-grep-context--non-top-level-def-regex)
"-e"
,(starhugger-grep-context--class-methods-regex)))
(defcustom starhugger-grep-context-ripgrep-executable (executable-find "rg")
"Executable path for ripgrep (https://github.com/BurntSushi/ripgrep).
When nil, project context won't be used."
:group 'starhugger
:type 'string)
(defun starhugger-grep-context--command-args-globs (&optional ext)
`(,@(and ext (list "-g" (format "*.%s" ext)))
;; exclude tests, after the "positive" globs
"-g" "!test"
"-g" "!tests"
"-g" "!test/"
"-g" "!tests/"
"-g" "!tests[-_.]*"
"-g" "!test[-_.]*"
"-g" "!**[-_.]tests[-_.]*"
"-g" "!**[-_.]test[-_.]*"
;;
))
(cl-defun starhugger-grep-context--command-args (&key ext)
(when starhugger-grep-context-ripgrep-executable
(-let* ((cmd-args
`[,starhugger-grep-context-ripgrep-executable
,@(starhugger-grep-context--command-args-globs
(or ext (-some--> buffer-file-name (file-name-extension it))))
"--no-line-number"
"--no-filename"
"--max-columns=160"
,(and (not starhugger-debug) "--color=never")
,@(starhugger-grep-context--regex-args)]))
cmd-args)))
(defvar starhugger-grep-context--script-path nil)
(defun starhugger-grep-context--script-path ()
"Return the path of the script to execute for grepping the context.
Reason(s ) for using another non-Elisp script: The filtering and
sorting computation is a bit expensive to run so it will block
Emacs, while current Emacs Lisp's asynchronous capabilities are
not great: `async-start' doesn't have access to the environment
of the current Emacs session including packages and libraries."
(with-memoization starhugger-grep-context--script-path
(or (locate-library "starhugger-grep-context-process.py")
(error
"%s not found, please review your installation recipe"
"starhugger-grep-context-process.py"))))
(defun starhugger-grep-context--get-lines (callback)
"CALLBACK is called with the grepped lines as argument.
Or when an error occurs, with nil."
(starhugger--at-project-root
(-let* ((grep-args (starhugger-grep-context--command-args))
(compare-text
(concat
(-let* ((prj-root (starhugger--project-root)))
(cond
((and prj-root buffer-file-name)
(file-name-sans-extension
(file-relative-name buffer-file-name prj-root)))
(buffer-file-name
(file-name-base buffer-file-name))
(t
(buffer-name))))))
(script-json-arg
(json-serialize
`((command-arguments . ,grep-args)
(compare-text . ,compare-text)
(ignored-keywords . ,starhugger-grep-context--ignored-keywords)
(max-lines . ,starhugger-grep-context--max-lines))))
(buf (generate-new-buffer " starhugger-grep-context--get-lines"))
(script-path (starhugger-grep-context--script-path))
(wrapped-callback
(lambda (stn-proc event)
(if (zerop (process-exit-status stn-proc))
(progn
(with-current-buffer buf
(-let* ((lines (starhugger--buffer-string-lines)))
(funcall callback lines)))
(kill-buffer buf))
(progn
(message "%s %s
error: %s
%s"
(file-name-nondirectory script-path)
script-json-arg
event
(buffer-string))
(funcall callback nil)))))
(proc
(start-process "starhugger-grep-context--get-lines" buf script-path
script-json-arg)))
(set-process-sentinel proc wrapped-callback)
proc)))
(defvar-local starhugger-grep-context--prefix-comments--cache nil)
(defun starhugger-grep-context--prefix-comments (callback)
"CALLBACK is called with an annotated comment string as argument.
This must be called in the completing buffer."
(if starhugger-grep-context--prefix-comments--cache
(funcall callback starhugger-grep-context--prefix-comments--cache)
(-let* ((buf0 (current-buffer))
(cmt-beg comment-start)
(cmt-end comment-end)
(cmt-end* (and (< 0 (length cmt-end)) (concat " " cmt-end)))
(cmt-fn (lambda (str) (concat cmt-beg " " str cmt-end*))))
(starhugger-grep-context--get-lines
(lambda (lines)
(-let* ((commented-lines
(--> lines (-map cmt-fn it) (string-join it "\n")))
(note (funcall cmt-fn "Context in other files:"))
(context
(if (< 0 (length lines))
(concat note "\n" commented-lines "\n")
"")))
(with-current-buffer buf0
(setq starhugger-grep-context--prefix-comments--cache context)
(funcall callback context))))))))
;;; starhugger-grep-context.el ends here
(provide 'starhugger-grep-context)
;; Local Variables:
;; byte-compile-docstring-max-column: 200
;; End: