forked from edani/ensime-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensime-search.el
430 lines (344 loc) · 13.6 KB
/
ensime-search.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
;;; ensime-search.el
;;
;;;; License
;;
;; Copyright (C) 2010 Aemon Cannon
;;
;; 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 2 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
(eval-when-compile
(require 'cl)
(require 'ensime-macros))
(require 'dash)
(defvar ensime-search-mode nil
"Enables the ensime-search minor mode.")
(defvar ensime-search-buffer-name "*ensime-search*"
"Buffer to use for ensime-search.")
(defvar ensime-search-target-buffer-name "*ensime-search-results*"
"Buffer name for target-buffer.")
(defvar ensime-search-target-buffer nil
"Buffer to which the ensime-search is applied to.")
(defvar ensime-search-target-window nil
"Window to which the ensime-search is applied to.")
(defvar ensime-search-originating-buffer nil
"Window from which the search was initiated.")
(defvar ensime-search-window-config nil
"Old window configuration.")
(defvar ensime-search-mode-string ""
"String in mode line for additional info.")
(defvar ensime-search-current-results '()
"The most recent ensime-search result list.")
(defvar ensime-search-current-selected-result '()
"The currently selected ensime-search result.")
(defvar ensime-search-text ""
"The active filter text.")
(defvar ensime-search-min-length 2
"The minimum length a search must be
before rpc call is placed..")
(defvar ensime-search-max-results 50
"The max number of results to return per rpc call.")
(defvar ensime-search-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-q" 'ensime-search-quit)
(define-key map "\C-q" 'ensime-search-quit)
(define-key map "\C-g" 'ensime-search-quit)
(define-key map (kbd "<up>") 'ensime-search-prev-match)
(define-key map "\C-p" 'ensime-search-prev-match)
(define-key map (kbd "<down>") 'ensime-search-next-match)
(define-key map "\C-n" 'ensime-search-next-match)
(define-key map "\C-i" 'ensime-search-insert-import-of-current-result)
(define-key map [(return)] 'ensime-search-choose-current-result)
map)
"Keymap used by ensime-search.")
(defvar ensime-search-target-buffer-map
(let ((map (make-sparse-keymap)))
(define-key map "q" 'ensime-search-quit)
(define-key map "\C-p" 'ensime-search-prev-match)
(define-key map (kbd "<up>") 'ensime-search-prev-match)
(define-key map "\C-n" 'ensime-search-next-match)
(define-key map (kbd "<down>") 'ensime-search-next-match)
(define-key map "\C-i" 'ensime-search-insert-import-of-current-result)
(define-key map [(return)] 'ensime-search-choose-current-result)
map)
"Keymap used by ensime-search.")
(defvar ensime-search-selection-overlay nil
"Overlay that highlights the currently selected search result.")
(defstruct ensime-search-result
"A ensime-search search result.
* summary
The full body of text presented in the results list,
may contain leading and trailing text, in addition to the match.
* match-file-name
The filename of the buffer containing the match
* summary-start
The offset at which summary begins in the results buffer.
* data
"
(summary nil)
(match-file-name nil)
(summary-start 0)
(data nil))
(defun ensime-search ()
"The main entrypoint for ensime-search-mode.
Initiate an incremental search of all live buffers."
(interactive)
(ensime-with-conn-interactive
conn
(if (and (string= (buffer-name) ensime-search-buffer-name)
(memq major-mode '(ensime-search-mode)))
(message "Already in ensime-search buffer")
(when buffer-file-name
(setq ensime-search-originating-buffer (current-buffer)))
(setq ensime-search-window-config (current-window-configuration))
(setq ensime-search-target-buffer
(switch-to-buffer-other-window
(get-buffer-create ensime-search-target-buffer-name)))
(setq ensime-search-target-window (selected-window))
(use-local-map ensime-search-target-buffer-map)
(setq ensime-buffer-connection conn)
(select-window (split-window (selected-window) (- (window-height) 4)))
(switch-to-buffer (get-buffer-create ensime-search-buffer-name))
(setq ensime-buffer-connection conn)
(erase-buffer)
(ensime-search-mode)
(ensime-search-update-target-buffer)
)))
(defun ensime-search-mode ()
"Major mode for incrementally seaching through all open buffers."
(interactive)
(setq major-mode 'ensime-search-mode
mode-name "ensime-search-mode")
(use-local-map ensime-search-mode-map)
(setq ensime-search-mode-string ""
ensime-search-mode-valid-string ""
mode-line-buffer-identification
'(25 . ("%b" ensime-search-mode-string ensime-search-valid-string)))
(ensime-search-update-modestring)
(make-local-variable 'after-change-functions)
(add-hook 'after-change-functions
'ensime-search-auto-update)
(make-local-variable 'ensime-search-kill-buffer)
(add-hook 'kill-buffer-hook 'ensime-search-kill-buffer nil t)
)
(defun ensime-search-quit ()
"Quit the ensime-search mode."
(interactive)
(kill-buffer ensime-search-buffer-name)
(set-window-configuration ensime-search-window-config))
(defun ensime-search-choose-current-result ()
"Jump to the target of the currently selected ensime-search-result."
(interactive)
(when (and (ensime-search-result-p ensime-search-current-selected-result)
(get-buffer ensime-search-buffer-name))
(switch-to-buffer ensime-search-buffer-name)
(let ((ensime-dispatching-connection ensime-buffer-connection))
(ensime-search-quit)
(let* ((r ensime-search-current-selected-result)
(item (ensime-search-result-data r)))
;; If the chosen item has a source location,
;; jump there..
(let ((pos (ensime-search-sym-pos item)))
(let* ((file-name (ensime-pos-file pos))
(line (ensime-pos-line pos))
(offset (ensime-pos-offset pos)))
(if (and file-name
(integerp (string-match
"\\.scala$\\|\\.java$"
file-name)))
(progn
(find-file file-name)
(cond
(offset (goto-char (ensime-internalize-offset offset)))
(line (ensime-goto-line line))))
;; Otherwise, open the inspector
(let ((decl-as (ensime-search-sym-decl-as item)))
(cond
((or (equal decl-as 'method)
(equal decl-as 'field))
(ensime-inspect-by-path
(ensime-search-sym-owner-name item)
(ensime-search-sym-local-name item)
))
(t (ensime-inspect-by-path
(ensime-search-sym-name item))))))))))))
(defun ensime-search-next-match ()
"Go to next match in the ensime-search target window."
(interactive)
(if (and ensime-search-current-results
ensime-search-current-selected-result)
(let* ((i (position ensime-search-current-selected-result
ensime-search-current-results))
(len (length ensime-search-current-results))
(next (if (< (+ i 1) len)
(nth (+ i 1) ensime-search-current-results)
(nth 0 ensime-search-current-results))))
(setq ensime-search-current-selected-result next)
(ensime-search-update-result-selection)
)))
(defun ensime-search-insert-import-of-current-result ()
"Insert an import statement for the currently selected type."
(interactive)
(when (and ensime-search-current-selected-result
ensime-search-originating-buffer)
(let* ((item (ensime-search-result-data
ensime-search-current-selected-result))
(qualified-name (ensime-search-sym-name item)))
(with-current-buffer ensime-search-originating-buffer
(insert (format "import %s\n" qualified-name))))))
(defun ensime-search-prev-match ()
"Go to previous match in the ensime-search target window."
(interactive)
(if (and ensime-search-current-results
ensime-search-current-selected-result)
(let* ((i (position ensime-search-current-selected-result
ensime-search-current-results))
(len (length ensime-search-current-results))
(next (if (> i 0)
(nth (- i 1) ensime-search-current-results)
(nth (- len 1) ensime-search-current-results))))
(setq ensime-search-current-selected-result next)
(ensime-search-update-result-selection)
)))
;;
;; Non-interactive functions below
;;
(defun ensime-search-update-result-selection ()
"Move cursor to current result selection in target buffer."
(when (and ensime-search-current-results
ensime-search-current-selected-result)
(with-current-buffer ensime-search-target-buffer
(when ensime-search-selection-overlay
(delete-overlay ensime-search-selection-overlay))
(let ((target-point (ensime-search-result-summary-start
ensime-search-current-selected-result)))
(goto-char target-point)
(setq ensime-search-selection-overlay
(ensime-make-overlay target-point (point-at-eol)
nil 'ensime-warnline-highlight))
(set-window-point (ensime-window-showing-buffer
ensime-search-target-buffer)
target-point)
)
)))
(defun ensime-search-auto-update (beg end lenold &optional force)
"Called from `after-update-functions' to update the display.
BEG, END and LENOLD are passed in from the hook.
An actual update is only done if the regexp has changed or if the
optional fourth argument FORCE is non-nil."
(let ((new-query (buffer-string)))
(when (not (equal new-query ensime-search-text))
(setq ensime-search-text new-query)
(if (>= (length new-query) ensime-search-min-length)
(ensime-rpc-async-public-symbol-search
(split-string new-query " ")
ensime-search-max-results
(lambda (info)
(when (buffer-live-p ensime-search-target-buffer)
(let ((results (ensime-search-make-results info)))
(setq ensime-search-current-results results)
(ensime-search-update-target-buffer)
(ensime-event-sig :search-buffer-populated)
))))
(with-current-buffer ensime-search-target-buffer
(setq ensime-search-current-results nil)
(ensime-search-update-target-buffer))
))
(force-mode-line-update)))
(defun ensime-search-assert-buffer-in-window ()
"Assert that `ensime-search-target-buffer' is displayed in
`ensime-search-target-window'."
(if (not (eq ensime-search-target-buffer
(window-buffer ensime-search-target-window)))
(set-window-buffer ensime-search-target-window
ensime-search-target-buffer)))
(defun ensime-search-update-modestring ()
"Update the variable `ensime-search-mode-string' displayed in the mode line."
(force-mode-line-update))
(defun ensime-search-kill-buffer ()
"When the ensime-search buffer is killed, kill the target buffer."
(remove-hook 'kill-buffer-hook 'ensime-search-kill-buffer)
(if (buffer-live-p ensime-search-target-buffer)
(kill-buffer ensime-search-target-buffer)))
(defun ensime-search-buffers-to-search ()
"Return the list of buffers that are suitable for searching."
(let ((all-buffers (buffer-list)))
(remove-if
(lambda (b)
(let ((b-name (buffer-name b)))
(or (null (buffer-file-name b))
(equal b-name ensime-search-target-buffer-name)
(equal b-name ensime-search-buffer-name)
(equal b-name "*Messages*"))))
all-buffers)))
(defun ensime-search-make-results (info)
"Map the results of the rpc call into search result
structures."
(let ((items info))
(mapcar
(lambda (item)
(make-ensime-search-result
:summary (ensime-search-sym-name item)
:match-file-name (-when-let (pos (ensime-search-sym-pos item))
(ensime-pos-file pos))
:data item))
items)))
(defun ensime-search-update-target-buffer ()
"This is where the magic happens. Update the result list."
(save-excursion
(set-buffer ensime-search-target-buffer)
(setq buffer-read-only nil)
(goto-char (point-min))
(erase-buffer)
(ensime-insert-with-face
(concat "Enter space-separated keywords. "
"C-n, C-p (or up/down) to navigate. "
"C-i to insert import statement. "
"RETURN to goto source location. C-q (or C-g) to quit.")
'font-lock-constant-face)
(insert "\n\n")
(when ensime-search-current-results
(setq ensime-search-current-selected-result
(first ensime-search-current-results)))
(dolist (r ensime-search-current-results)
;; Save this for later use, for next/prev actions
(setf (ensime-search-result-summary-start r) (point))
(let ((p (point))
(text (ensime-search-result-summary r)))
;; Insert the actual text, highlighting the matched substring
(insert (format "%s \n" text))
(ensime-search-highlight-matches text p))
;; Insert filename
(-when-let (f (ensime-search-result-match-file-name r))
(ensime-insert-with-face (format " %s" f)
'font-lock-comment-face))
(insert "\n\n")
)
(setq buffer-read-only t)
(ensime-search-update-result-selection)
))
(defun ensime-search-highlight-matches (text start-pt)
(let ((keywords (split-string ensime-search-text " ")))
(dolist (key keywords)
(let ((start (string-match key text))
(len (length key)))
(when (integerp start)
(add-text-properties
(+ start-pt start)
(+ start-pt start len)
'(comment nil face font-lock-keyword-face))))
)))
(provide 'ensime-search)
;; Local Variables:
;; End: