forked from edani/ensime-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensime-notes.el
244 lines (206 loc) · 7.55 KB
/
ensime-notes.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
;;; ensime-notes.el --- Compiler Notes (Error/Warning overlays)
(eval-when-compile
(require 'cl)
(require 'ensime-macros))
;; Note: This might better be a connection-local variable, but
;; afraid that might lead to hanging overlays..
(defvar ensime-note-overlays '()
"The overlay structures created to highlight notes.")
(defun ensime-all-notes ()
(append (ensime-scala-compiler-notes (ensime-connection))
(ensime-java-compiler-notes (ensime-connection))))
(defun ensime-add-notes (lang result)
(let ((is-full (plist-get result :is-full))
(notes (plist-get result :notes)))
(cond
((equal lang 'scala)
(setf (ensime-scala-compiler-notes (ensime-connection))
(append
(ensime-scala-compiler-notes (ensime-connection))
notes)))
((equal lang 'java)
(setf (ensime-java-compiler-notes (ensime-connection))
(append
(ensime-java-compiler-notes (ensime-connection))
notes))))
(ensime-make-note-overlays notes)
(ensime-update-note-counts)
))
(defun ensime-clear-notes (lang)
(cond
((equal lang 'scala)
(setf (ensime-scala-compiler-notes (ensime-connection)) nil))
((equal lang 'java)
(setf (ensime-java-compiler-notes (ensime-connection)) nil)))
(ensime-clear-note-overlays lang)
(ensime-update-note-counts))
(defun ensime-make-overlay-at (file line b e msg visuals)
"Create an overlay highlighting the given line in
any buffer visiting the given file."
(let ((beg b)
(end e))
(assert (or (integerp line)
(and (integerp beg) (integerp end))))
(when-let (buf (find-buffer-visiting file))
(with-current-buffer buf
(if (and (integerp beg) (integerp end))
(progn
(setq beg (ensime-internalize-offset beg))
(setq end (ensime-internalize-offset end)))
;; If line provided, use line to define region
(save-excursion
(goto-line line)
(setq beg (point-at-bol))
(setq end (point-at-eol)))))
(ensime-make-overlay beg end msg visuals nil buf))
))
(defun ensime-make-note-overlays (notes)
(dolist (note notes)
(destructuring-bind
(&key severity msg beg end line col file &allow-other-keys) note
;; No empty note overlays!
(when (eq beg end)
(setq end (+ end 1)))
(let ((lang
(cond
((ensime-java-file-p file) 'java)
((ensime-scala-file-p file) 'scala)
(t 'scala)))
(visuals
(cond
((equal severity 'error)
(list :face 'ensime-errline-highlight
:char "!"
:bitmap 'exclamation-mark
:fringe 'ensime-compile-errline))
(t
(list :face 'ensime-warnline-highlight
:char "?"
:bitmap 'question-mark
:fringe 'ensime-compile-warnline)))))
(when-let (ov (ensime-make-overlay-at file line beg end msg visuals))
(overlay-put ov 'lang lang)
(push ov ensime-note-overlays))
))))
(defun ensime-update-note-counts ()
(let ((notes (ensime-all-notes))
(num-err 0)
(num-warn 0)
(conn (ensime-connection)))
(dolist (note notes)
(let ((severity (plist-get note :severity)))
(cond
((equal severity 'error)
(incf num-err))
((equal severity 'warn)
(incf num-warn))
(t))))
(setf (ensime-num-errors conn) num-err)
(setf (ensime-num-warnings conn) num-warn)))
(defun ensime-refresh-all-note-overlays ()
(let ((notes (when (ensime-connected-p)
(append
(ensime-java-compiler-notes (ensime-connection))
(ensime-scala-compiler-notes (ensime-connection)))
)))
(ensime-clear-note-overlays)
(ensime-make-note-overlays notes)
))
(defface ensime-errline-highlight
'((t (:inherit flymake-errline)))
"Face used for marking the specific region of an error, if available."
:group 'ensime-ui)
(defface ensime-warnline-highlight
'((t (:inherit flymake-warnline)))
"Face used for marking the specific region of an warning, if available."
:group 'ensime-ui)
(defun ensime-make-overlay (beg end tooltip-text visuals &optional mouse-face buf)
"Allocate a ensime overlay in range BEG and END."
(let ((ov (make-overlay beg end buf t t)))
(overlay-put ov 'face (plist-get visuals :face))
(overlay-put ov 'mouse-face mouse-face)
(overlay-put ov 'help-echo tooltip-text)
(overlay-put ov 'ensime-overlay t)
(overlay-put ov 'priority 100)
(let ((char (plist-get visuals :char)))
(when char
(overlay-put ov 'before-string
(propertize char
'display
(list 'left-fringe
(plist-get visuals :bitmap)
(plist-get visuals :fringe))))))
ov))
(defun ensime-overlays-at (point)
"Return list of overlays of type 'ensime-overlay at point."
(let ((ovs (overlays-at point)))
(remove-if-not
(lambda (ov) (overlay-get ov 'ensime-overlay))
ovs)
))
(defun ensime-clear-note-overlays (&optional lang)
"Delete note overlays language. If lang is nil, delete all
overlays."
(let ((revised '()))
(dolist (ov ensime-note-overlays)
(if (or (null lang)
(equal lang (overlay-get ov 'lang)))
(delete-overlay ov)
(setq revised (cons ov revised))))
(setq ensime-note-overlays revised)))
(defun ensime-next-note-in-current-buffer (notes forward)
(let ((best-note nil)
(best-dist most-positive-fixnum)
(external-offset (ensime-externalize-offset (point)))
(max-external-offset (ensime-externalize-offset (point-max))))
(dolist (note notes)
(if (and (ensime-files-equal-p (ensime-note-file note)
buffer-file-name)
(/= (ensime-note-beg note) external-offset))
(let ((dist (cond
(forward
(if (< (ensime-note-beg note) external-offset)
(+ (ensime-note-beg note)
(- max-external-offset external-offset))
(- (ensime-note-beg note) external-offset)))
(t (if (> (ensime-note-beg note) external-offset)
(+ external-offset (- max-external-offset
(ensime-note-beg note)))
(- external-offset (ensime-note-beg note)))))))
(when (< dist best-dist)
(setq best-dist dist)
(setq best-note note))
)))
best-note))
(defun ensime-goto-next-note (forward)
"Helper to move point to next note. Go forward if forward is non-nil."
(let* ((conn (ensime-connection))
(notes (append (ensime-java-compiler-notes conn)
(ensime-scala-compiler-notes conn)))
(next-note (ensime-next-note-in-current-buffer notes forward)))
(if next-note
(progn
(goto-char (ensime-internalize-offset (ensime-note-beg next-note)))
(message (ensime-note-message next-note)))
(message (concat
"No more compilation issues in this buffer. "
"Use ensime-typecheck-all [C-c C-c a] to find"
" all issues, project-wide.")))))
(defun ensime-forward-note ()
"Goto the next compilation note in this buffer"
(interactive)
(ensime-goto-next-note t))
(defun ensime-backward-note ()
"Goto the prev compilation note in this buffer"
(interactive)
(ensime-goto-next-note nil))
(defun ensime-errors-at (point)
(delq nil (mapcar (lambda (x) (overlay-get x 'help-echo)) (ensime-overlays-at point))))
(defun ensime-print-errors-at-point ()
(interactive)
(let ((msgs (apply 'concat (ensime-errors-at (point)))))
(when msgs
(message msgs))))
(provide 'ensime-notes)
;; Local Variables:
;; End: