This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
ensime-mode.el
444 lines (362 loc) · 15.9 KB
/
ensime-mode.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
;;; ensime-mode.el --- ensime mode
(eval-when-compile
(require 'cl)
(require 'ensime-macros))
(defvar ensime-source-buffer-saved-hook nil
"Hook called whenever an ensime source buffer is saved.")
(defvar ensime-source-buffer-loaded-hook nil
"Hook called whenever an ensime source buffer is loaded.")
(defvar ensime-mode-map
(let ((map (make-sparse-keymap)))
(let ((prefix-map (make-sparse-keymap)))
(define-key prefix-map (kbd "C-v i") 'ensime-inspect-type-at-point)
(define-key prefix-map (kbd "C-v 5 i")
'ensime-inspect-type-at-point-other-frame)
(define-key prefix-map (kbd "C-v p") 'ensime-inspect-package-at-point)
(define-key prefix-map (kbd "C-v o") 'ensime-inspect-project-package)
(define-key prefix-map (kbd "C-v r") 'ensime-show-uses-of-symbol-at-point)
(define-key prefix-map (kbd "C-v s") 'ensime-sbt-switch)
(define-key prefix-map (kbd "C-v z") 'ensime-inf-switch)
(define-key prefix-map (kbd "C-v f") 'ensime-format-source)
(define-key prefix-map (kbd "C-v u") 'ensime-undo-peek)
(define-key prefix-map (kbd "C-v v") 'ensime-search)
(define-key prefix-map (kbd "C-v d") 'ensime-show-doc-for-symbol-at-point)
(define-key prefix-map (kbd "C-v D") 'ensime-project-docs)
(define-key prefix-map (kbd "C-v t") 'ensime-print-type-at-point)
(define-key prefix-map (kbd "C-v e") 'ensime-print-errors-at-point)
(define-key prefix-map (kbd "C-v .") 'ensime-expand-selection-command)
(define-key prefix-map (kbd "C-v C-r") 'ensime-inf-eval-region)
(define-key prefix-map (kbd "C-v b") 'ensime-inf-eval-buffer)
(define-key prefix-map (kbd "C-v l") 'ensime-inf-load-file)
(define-key prefix-map (kbd "C-c c") 'ensime-typecheck-current-file)
(define-key prefix-map (kbd "C-c a") 'ensime-typecheck-all)
(define-key prefix-map (kbd "C-c r") 'ensime-reload-open-files)
(define-key prefix-map (kbd "C-c e") 'ensime-show-all-errors-and-warnings)
(define-key prefix-map (kbd "C-t t") 'ensime-goto-test)
(define-key prefix-map (kbd "C-t i") 'ensime-goto-impl)
(define-key prefix-map (kbd "C-d d") 'ensime-db-start)
(define-key prefix-map (kbd "C-d b") 'ensime-db-set-break)
(define-key prefix-map (kbd "C-d u") 'ensime-db-clear-break)
(define-key prefix-map (kbd "C-d s") 'ensime-db-step)
(define-key prefix-map (kbd "C-d o") 'ensime-db-step-out)
(define-key prefix-map (kbd "C-d n") 'ensime-db-next)
(define-key prefix-map (kbd "C-d r") 'ensime-db-run)
(define-key prefix-map (kbd "C-d c") 'ensime-db-continue)
(define-key prefix-map (kbd "C-d q") 'ensime-db-quit)
(define-key prefix-map (kbd "C-d i") 'ensime-db-inspect-value-at-point)
(define-key prefix-map (kbd "C-d t") 'ensime-db-backtrace)
(define-key prefix-map (kbd "C-d a") 'ensime-db-clear-all-breaks)
(define-key prefix-map (kbd "C-b s") 'ensime-sbt-switch)
(define-key prefix-map (kbd "C-b S") 'ensime-stacktrace-switch)
(define-key prefix-map (kbd "C-b c") 'ensime-sbt-do-compile)
(define-key prefix-map (kbd "C-b n") 'ensime-sbt-do-clean)
(define-key prefix-map (kbd "C-b o") 'ensime-sbt-do-test-only)
(define-key prefix-map (kbd "C-b p") 'ensime-sbt-do-package)
(define-key prefix-map (kbd "C-b r") 'ensime-sbt-do-run)
(define-key prefix-map (kbd "C-b T") 'ensime-sbt-do-test)
(define-key prefix-map (kbd "C-b t") 'ensime-sbt-do-test-quick)
(define-key prefix-map (kbd "C-d u") 'ensime-db-clear-break)
(define-key prefix-map (kbd "C-d s") 'ensime-db-step)
(define-key prefix-map (kbd "C-d n") 'ensime-db-next)
(define-key prefix-map (kbd "C-d r") 'ensime-db-run)
(define-key prefix-map (kbd "C-d c") 'ensime-db-continue)
(define-key prefix-map (kbd "C-d q") 'ensime-db-quit)
(define-key prefix-map (kbd "C-d l") 'ensime-db-list-locals)
(define-key prefix-map (kbd "C-r r") 'ensime-refactor-rename)
(define-key prefix-map (kbd "C-r o") 'ensime-refactor-organize-imports)
(define-key prefix-map (kbd "C-r l") 'ensime-refactor-extract-local)
(define-key prefix-map (kbd "C-r m") 'ensime-refactor-extract-method)
(define-key prefix-map (kbd "C-r i") 'ensime-refactor-inline-local)
(define-key prefix-map (kbd "C-r t") 'ensime-import-type-at-point)
(define-key map ensime-mode-key-prefix prefix-map)
;; Prefix-less shortcuts bindings...
(define-key map (kbd "M-.") 'ensime-edit-definition)
(define-key map (kbd "M-,") 'ensime-pop-find-definition-stack)
(define-key map (kbd "M-n") 'ensime-forward-note)
(define-key map (kbd "M-p") 'ensime-backward-note)
(define-key map [C-down-mouse-1] 'ignore)
(define-key map [C-up-mouse-1] 'ignore)
(define-key map [C-down-mouse-3] 'ignore)
(define-key map [C-up-mouse-3] 'ignore)
(define-key map [C-mouse-1] 'ensime-control-mouse-1-single-click)
(define-key map [C-mouse-3] 'ensime-control-mouse-3-single-click)
)
map)
"Keymap for ENSIME mode."
)
;;;;; ensime-mode
(defun ensime-scala-mode-hook ()
"Conveniance hook function that just starts ensime-mode."
(ensime-mode 1))
(defun ensime-run-after-save-hooks ()
"Things to run whenever a source buffer is saved."
(when (and (ensime-connected-p) (ensime-analyzer-ready))
(condition-case err-info
(run-hooks 'ensime-source-buffer-saved-hook)
(error
(message
"Error running ensime-source-buffer-saved-hook: %s"
err-info)))))
(defun ensime-run-find-file-hooks ()
"Things to run whenever a source buffer is opened."
(when (and (ensime-connected-p) (ensime-analyzer-ready))
(condition-case err-info
(run-hooks 'ensime-source-buffer-loaded-hook)
(error
(message
"Error running ensime-source-buffer-loaded-hook: %s"
err-info)))))
(defun ensime-save-buffer-no-hooks ()
"Just save the buffer per usual, don't type-check!"
(let ((after-save-hook nil)
(before-save-hook nil))
(save-buffer)))
(defun ensime-delete-buffer-and-file ()
"Kill the current buffer and delete the corresponding file!"
(interactive)
(ensime-assert-buffer-saved-interactive
(let ((f buffer-file-name))
(ensime-rpc-remove-file f)
(delete-file f)
(kill-buffer nil)
)))
(easy-menu-define ensime-mode-menu ensime-mode-map
"Menu for ENSIME mode"
'("ENSIME"
("Test")
("Source"
["Format source" ensime-format-source]
["Find all references" ensime-show-uses-of-symbol-at-point]
["Inspect type" ensime-inspect-type-at-point]
["Inspect type in another frame" ensime-inspect-type-at-point-other-frame]
["Inspect enclosing package" ensime-inspect-package-at-point]
["Inspect project package" ensime-inspect-project-package]
["Undo source change" ensime-undo-peek])
("Typecheck"
["Typecheck file" ensime-typecheck-current-file]
["Typecheck project" ensime-typecheck-all]
["Reload typechecker" ensime-reload-open-files]
["Show all errors and warnings" ensime-show-all-errors-and-warnings])
("Refactor"
["Organize imports" ensime-refactor-organize-imports]
["Import type at point" ensime-import-type-at-point]
["Rename" ensime-refactor-rename]
["Extract local val" ensime-refactor-extract-local]
["Extract method" ensime-refactor-extract-method]
["Inline local val" ensime-refactor-inline-local])
("Navigation"
["Lookup definition" ensime-edit-definition]
["Lookup definition in other window" ensime-edit-definition-other-window]
["Lookup definition in other frame" ensime-edit-definition-other-frame]
["Go to test class" ensime-goto-test]
["Go to implementation class" ensime-goto-impl]
["Pop definition stack" ensime-pop-find-definition-stack]
["Backward compilation note" ensime-backward-note]
["Forward compilation note" ensime-forward-note]
["Expand selection" ensime-expand-selection-command]
["Search" ensime-search])
("Documentation"
["Browse documentation of symbol" ensime-show-doc-for-symbol-at-point]
["Browse all documentation" ensime-project-docs])
("SBT"
["Start or switch to" ensime-sbt-switch]
["Compile" ensime-sbt-do-compile]
["Clean" ensime-sbt-do-clean]
["Test" ensime-sbt-do-test]
["Test Quick" ensime-sbt-do-test-quick]
["Test current class" ensime-sbt-do-test-only]
["Run" ensime-sbt-do-run]
["Package" ensime-sbt-do-package])
("Debugger"
["Start" ensime-db-start]
["Set break point" ensime-db-set-break]
["Clear breakpoint" ensime-db-clear-break]
["Clear all breakpoints" ensime-db-clear-all-breaks]
["Step" ensime-db-step]
["Next" ensime-db-next]
["Run" ensime-db-run]
["Continue" ensime-db-continue]
["Quit" ensime-db-quit]
["Show Backtrace" ensime-db-backtrace]
["Inspect value at point" ensime-db-inspect-value-at-point]
)
"---"
["Go to SBT console" ensime-sbt-switch]
["Go to stacktrace buffer" ensime-stacktrace-switch]
["Go to Scala REPL" ensime-inf-switch]
["Shutdown ENSIME server" ensime-shutdown]
))
(define-minor-mode ensime-mode
"ENSIME: The ENhanced Scala Interaction Mode for Emacs (minor-mode).
\\{ensime-mode-map}"
nil
nil
ensime-mode-map
(if ensime-mode
(progn
(pcase ensime-completion-style
(`company
(ensime-company-enable))
(`auto-complete
(ensime-ac-enable))
(_ t))
;; Always add the cap function. Some users may prefer it,
;; and it may be useful in other completion modes (helm?).
(add-hook 'completion-at-point-functions
'ensime-completion-at-point-function nil t)
(easy-menu-add ensime-mode-menu ensime-mode-map)
(add-hook 'after-save-hook 'ensime-run-after-save-hooks nil t)
(add-hook 'find-file-hook 'ensime-run-find-file-hooks nil t)
(add-hook 'ensime-source-buffer-saved-hook
'ensime-typecheck-current-file)
(add-hook 'ensime-source-buffer-saved-hook
'ensime-sem-high-refresh-hook t)
(add-hook 'ensime-source-buffer-loaded-hook
'ensime-sem-high-refresh-hook t)
(add-hook 'ensime-source-buffer-loaded-hook
'ensime-typecheck-current-file)
(add-hook 'after-change-functions
'ensime-after-change-function nil t)
(ensime-idle-typecheck-set-timer)
(when ensime-tooltip-hints
(add-hook 'tooltip-functions 'ensime-tooltip-handler)
(make-local-variable 'track-mouse)
(setq track-mouse t)
(make-local-variable 'tooltip-delay)
(setq tooltip-delay 1.0)
(define-key ensime-mode-map [mouse-movement] 'ensime-mouse-motion))
(ensime-refresh-all-note-overlays))
(progn
(ensime-ac-disable)
(remove-hook 'after-save-hook 'ensime-run-after-save-hooks t)
(remove-hook 'find-file-hook 'ensime-run-find-file-hooks t)
(remove-hook 'ensime-source-buffer-saved-hook
'ensime-typecheck-current-file)
(remove-hook 'ensime-source-buffer-saved-hook
'ensime-sem-high-refresh-hook)
(remove-hook 'ensime-source-buffer-loaded-hook
'ensime-sem-high-refresh-hook)
(remove-hook 'ensime-source-buffer-loaded-hook
'ensime-typecheck-current-file)
(remove-hook 'after-change-functions
'ensime-after-change-function t)
(remove-hook 'tooltip-functions 'ensime-tooltip-handler)
(make-local-variable 'track-mouse)
(setq track-mouse nil))))
;;;;;; Mouse handlers
(defun ensime-control-mouse-1-single-click (event)
"Command handler for control+clicks of mouse button 1.
If control is held, jump to definition of symbol under
point."
(interactive "e")
(mouse-set-point event)
(ensime-edit-definition))
(defun ensime-control-mouse-3-single-click (event)
"Command handler for double clicks of mouse button 1.
If the user clicks on a package declaration or import,
inspect that package. Otherwise, try to inspect the type
of the thing at point."
(interactive "e")
(ensime-inspect-type-at-point))
(defun ensime-mouse-motion (event)
"Command handler for mouse movement events in `ensime-mode-map'."
(interactive "e")
(tooltip-hide)
(when (car (mouse-pixel-position))
(setq tooltip-last-mouse-motion-event (copy-sequence event))
(tooltip-start-delayed-tip)))
;;;;;; Tooltips
(defun ensime-tooltip-show-message (msg)
"Display tooltip, respecting ensime tooltip options."
(if ensime-graphical-tooltips
(tooltip-show msg tooltip-use-echo-area)
(message msg)))
(defun ensime-tooltip-handler (event)
"Hook function to display a help tooltip. If an error
or warning overlay exists at point, show the description
of that error or warning. Otherwise try to inspect the
type of the expression under the cursor."
(when (and (eventp event)
ensime-mode
(ensime-connected-p)
(ensime-analyzer-ready)
(posn-point (event-end event)))
(let* ((point (posn-point (event-end event)))
(external-pos (ensime-externalize-offset point))
(ident (tooltip-identifier-from-point point))
(note-overlays (ensime-overlays-at point))
(sem-high-overlays (ensime-sem-high-sym-types-at-point point))
(val-at-pt (ensime-db-tooltip point)))
(cond
;; If debugger is active and we can get the value of the symbol
;; at the point, show it in the tooltip.
(val-at-pt (ensime-tooltip-show-message val-at-pt) t)
;; If error or warning overlays exist,
;; show that message..
(note-overlays (progn
(ensime-tooltip-show-message
(overlay-get (car note-overlays) 'help-echo))
t))
;; Show implicit conversions if present
((or (find 'implicitConversion sem-high-overlays)
(find 'implicitParams sem-high-overlays))
(ensime-tooltip-show-message
(mapconcat 'identity (ensime-implicit-notes-at point) "\n")))
;; Otherwise show a type hint..
((and ident ensime-tooltip-type-hints)
(progn
(ensime-eval-async
`(swank:type-at-point ,buffer-file-name ,external-pos)
#'(lambda (type)
(when type
(let ((msg (ensime-type-full-name-with-args type)))
(ensime-tooltip-show-message msg)
))))
t
)))
)))
;;;;;; Modeline
;; Setup the custom ensime modeline handler
(add-to-list 'minor-mode-alist
'(ensime-mode (:eval (ensime-modeline-string))))
(defun ensime-modeline-string ()
"Return the string to display in the modeline.
\"ENSIME\" only appears if we aren't connected. If connected, include
connection-name, and possibly some state
information."
(when ensime-mode
(condition-case err
(let ((conn (ensime-connection-or-nil)))
(cond ((and ensime-mode (not conn))
(cond
((ensime-owning-server-process-for-source-file buffer-file-name)
" [ENSIME: (Starting)]")
(t " [ENSIME: (Disconnected)]")))
((and ensime-mode (ensime-connected-p conn))
(concat " ["
(or (plist-get (ensime-config conn) :name)
"ENSIME: (Connected)")
(let ((status (ensime-modeline-state-string conn))
(unready (not (ensime-analyzer-ready conn))))
(cond (status (concat " (" status ")"))
(unready " (Analyzing)")
(t "")))
(concat (format " : %s/%s"
(ensime-num-errors conn)
(ensime-num-warnings conn)))
"]"))
(ensime-mode " [ENSIME: (Dead Connection)]")
))
(error (progn
" [ENSIME: wtf]"
)))))
(defun ensime-modeline-state-string (conn)
"Return a string possibly describing CONN's state."
(cond ((not (eq (process-status conn) 'open))
(format "%s" (process-status conn)))
((let ((pending (length (ensime-rex-continuations conn))))
(cond ((zerop pending) nil)
(t (format "%s" pending)))))))
(provide 'ensime-mode)
;; Local Variables:
;; End: