forked from manateelazycat/lsp-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-bridge-diagnostic.el
384 lines (320 loc) · 15.9 KB
/
lsp-bridge-diagnostic.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
;;; lsp-bridge-diagnostic.el --- Diagnostic for lsp-bridge -*- lexical-binding: t; no-byte-compile: t; -*-*-
;; Filename: lsp-bridge-diagnostic.el
;; Description: Diagnostic for lsp-bridge
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2022, Andy Stewart, all rights reserved.
;; Created: 2022-12-14 19:55:28
;; Version: 0.1
;; Last-Updated: 2022-12-14 19:55:28
;; By: Andy Stewart
;; URL: https://www.github.org/manateelazycat/lsp-bridge-diagnostic
;; Keywords:
;; Compatibility: GNU Emacs 28.2
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Diagnostic for lsp-bridge
;;
;;; Installation:
;;
;; Put lsp-bridge-diagnostic.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'lsp-bridge-diagnostic)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET lsp-bridge-diagnostic RET
;;
;;; Change log:
;;
;; 2022/12/14
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'acm)
(require 'acm-frame)
;;; Code:
(defcustom lsp-bridge-diagnostic-fetch-idle 0.5
"The idle seconds to fetch diagnostics."
:type 'float
:group 'lsp-bridge)
(defcustom lsp-bridge-diagnostic-enable-overlays t
"If non-nil, rendering overlays in lsp-bridge-diagnostic byself.
Set to nil if flymake/flycheck frontend is used instead."
:type 'boolean
:group 'lsp-bridge)
(defcustom lsp-bridge-diagnostic-update-hook '()
"The hook for diagnostics updates."
:type 'hook
:group 'lsp-bridge)
(defface lsp-bridge-diagnostics-error-face
'((t (:underline (:style wave :color "Red1"))))
"Face error diagnostic."
:group 'lsp-bridge)
(defface lsp-bridge-diagnostics-warning-face
'((t (:underline (:style wave :color "DarkOrange"))))
"Face warning diagnostic."
:group 'lsp-bridge)
(defface lsp-bridge-diagnostics-info-face
'((t (:underline (:style wave :color "ForestGreen"))))
"Face info diagnostic."
:group 'lsp-bridge)
(defface lsp-bridge-diagnostics-hint-face
'((t (:underline (:style wave :color "grey"))))
"Face hint diagnostic."
:group 'lsp-bridge)
(defcustom lsp-bridge-diagnostic-buffer " *lsp-bridge-diagnostic*"
"Buffer for display diagnostic information."
:type 'string
:group 'lsp-bridge)
(defcustom lsp-bridge-diagnostic-tooltip-border-width 20
"The border width of lsp-bridge diagnostic tooltip, in pixels."
:type 'integer
:group 'lsp-bridge)
(defcustom lsp-bridge-diagnostic-display-errors-delay 0.25
"The delay in seconds before displaying errors."
:type 'integer
:group 'lsp-bridge)
(defcustom lsp-bridge-enable-hover-diagnostic nil
"Whether to popup diagnostic message when hover diagnostic place."
:type 'boolean
:group 'lsp-bridge)
(defcustom lsp-bridge-diagnostic-max-number 50
"The maximum number of diagnostic will be render.
Too many diagnostic render will cause do long time Emacs GC."
:type 'integer
:group 'lsp-bridge)
(defcustom lsp-bridge-diagnostic-hide-severities nil
"The rule to hide diagnostic that return from LSP server.
1 mean error
2 mean warning
3 mean info
4 mean hint
You can set this value with `(2 3 4) if you just need render error diagnostic."
:type 'list
:group 'lsp-bridge)
(defvar-local lsp-bridge-diagnostic-records nil)
(defvar-local lsp-bridge-diagnostic-count nil)
(defvar-local lsp-bridge-diagnostic-overlays '())
(defvar lsp-bridge-diagnostic-frame nil)
(defvar lsp-bridge-diagnostic-display-error-at-point-timer nil)
(autoload 'lsp-bridge--with-file-buffer "lsp-bridge")
(defun lsp-bridge-diagnostic-hide-tooltip ()
(acm-frame-hide-frame lsp-bridge-diagnostic-frame))
(defun lsp-bridge-diagnostic-hide-overlays ()
(when lsp-bridge-diagnostic-overlays
(dolist (diagnostic-overlay lsp-bridge-diagnostic-overlays)
(delete-overlay diagnostic-overlay)))
(setq-local lsp-bridge-diagnostic-overlays nil))
(defun lsp-bridge-diagnostic--render (filepath filehost diagnostics diagnostic-count)
(lsp-bridge--with-file-buffer filepath filehost
(setq-local lsp-bridge-diagnostic-count diagnostic-count)
(setq-local lsp-bridge-diagnostic-records diagnostics)
(run-hooks 'lsp-bridge-diagnostic-update-hook)
(when lsp-bridge-diagnostic-enable-overlays
(lsp-bridge-diagnostic-hide-overlays)
(let ((diagnostic-index 0)
(diagnostic-number (length diagnostics)))
(dolist (diagnostic diagnostics)
(let ((severity (plist-get diagnostic :severity)))
(unless (member severity lsp-bridge-diagnostic-hide-severities)
(let* ((diagnostic-start (acm-backend-lsp-position-to-point (plist-get (plist-get diagnostic :range) :start)))
(diagnostic-end (acm-backend-lsp-position-to-point (plist-get (plist-get diagnostic :range) :end)))
(overlay (if (eq diagnostic-start diagnostic-end)
;; Adjust diagnostic end position if start and end is same position.
(make-overlay diagnostic-start (1+ diagnostic-start))
(make-overlay diagnostic-start diagnostic-end)))
(message (plist-get diagnostic :message))
(overlay-face (cl-case severity
(1 'lsp-bridge-diagnostics-error-face)
(2 'lsp-bridge-diagnostics-warning-face)
(3 'lsp-bridge-diagnostics-info-face)
(4 'lsp-bridge-diagnostics-hint-face))))
(overlay-put overlay 'color (plist-get (face-attribute overlay-face :underline) :color))
(overlay-put overlay 'face overlay-face)
(overlay-put overlay 'message message)
(overlay-put overlay
'display-message
(if (> diagnostic-number 1)
(format "[%s:%s] %s" (1+ diagnostic-index) diagnostic-number message)
message))
(push overlay lsp-bridge-diagnostic-overlays))))
(setq diagnostic-index (1+ diagnostic-index))))
(setq-local lsp-bridge-diagnostic-overlays (reverse lsp-bridge-diagnostic-overlays)))))
(defvar lsp-bridge-diagnostic-frame nil)
(defun lsp-bridge-diagnostic-insert-colored-string (color text)
"Insert a colored string at point in the current buffer."
(let ((colored-text (propertize text 'face `(:foreground ,color))))
(insert colored-text)))
(defun lsp-bridge-diagnostic-show-tooltip (diagnostic-overlay &optional goto-beginning)
(let* ((diagnostic-display-message (overlay-get diagnostic-overlay 'display-message))
(diagnostic-message (overlay-get diagnostic-overlay 'message))
(foreground-color (plist-get (face-attribute (overlay-get diagnostic-overlay 'face) :underline) :color)))
;; Weather goto beginning of diagnostic.
(when goto-beginning
(goto-char (overlay-start diagnostic-overlay)))
;; Try scroll window when current command is diagnostic jump commands and code content exceeds screen.
(when (and (> (point-max) (window-end))
(member (format "%s" this-command) '("lsp-bridge-diagnostic-jump-next", "lsp-bridge-diagnostic-jump-prev")))
;; NOTE:
;; We need use redisplay function to make sure diagnostic show in visible area.
;; `redisplay' causes a slight flicker on the screen but currently there is no better solution.
(redisplay t)
;; Adjusting the cursor position when it is too close to the edge of the window.
(let* ((window-start-line (save-excursion
(goto-char (window-start))
(current-line)))
(window-end-line (save-excursion
(goto-char (window-end))
(current-line)))
(adjust-line-number 5))
(ignore-errors
(cond ((< (abs (- (current-line) window-start-line)) adjust-line-number)
(scroll-down-line adjust-line-number))
((< (abs (- (current-line) window-end-line)) adjust-line-number)
(scroll-up-line adjust-line-number))))))
;; Show diagnostic tooltip.
(with-current-buffer (get-buffer-create lsp-bridge-diagnostic-buffer)
(erase-buffer)
(lsp-bridge-diagnostic-insert-colored-string (overlay-get diagnostic-overlay 'color) diagnostic-display-message)
(setq-local lsp-bridge-diagnostic-message diagnostic-message))
(cond
((acm-frame-can-display-p)
(acm-frame-new lsp-bridge-diagnostic-frame
lsp-bridge-diagnostic-buffer
"lsp bridge diagnostic frame")
(set-face-background (acm-frame-border-face)
foreground-color
lsp-bridge-diagnostic-frame))
(t (message diagnostic-message)))))
(defun lsp-bridge-diagnostic-maybe-display-error-at-point ()
"Display error message at point with a delay, unless already displayed."
(acm-cancel-timer lsp-bridge-diagnostic-display-error-at-point-timer)
(when-let ((ol (lsp-bridge-diagnostic-overlay-at-point)))
(setq lsp-bridge-diagnostic-display-error-at-point-timer
(run-at-time lsp-bridge-diagnostic-display-errors-delay nil
'lsp-bridge-diagnostic-show-tooltip ol))))
(defun lsp-bridge-in-diagnostic-overlay-area-p (overlay)
(and
lsp-bridge-diagnostic-frame
(not (frame-visible-p lsp-bridge-diagnostic-frame))
(>= (point) (overlay-start overlay))
(<= (point) (overlay-end overlay))))
(defun lsp-bridge-diagnostic-jump-next ()
(interactive)
(if (zerop (length lsp-bridge-diagnostic-overlays))
(message "[LSP-Bridge] No diagnostics.")
(if-let ((diagnostic-overlay (cl-find-if
(lambda (overlay)
(or (< (point) (overlay-start overlay))
;; Show diagnostic information around cursor if diagnostic frame is not visiable.
(lsp-bridge-in-diagnostic-overlay-area-p overlay)))
lsp-bridge-diagnostic-overlays)))
(lsp-bridge-diagnostic-show-tooltip diagnostic-overlay t)
(message "[LSP-Bridge] Reach last diagnostic."))))
(defun lsp-bridge-diagnostic-jump-prev ()
(interactive)
(if (zerop (length lsp-bridge-diagnostic-overlays))
(message "[LSP-Bridge] No diagnostics.")
(if-let ((diagnostic-overlay (cl-find-if
(lambda (overlay)
(or (> (point) (overlay-end overlay))
;; Show diagnostic information around cursor if diagnostic frame is not visiable.
(lsp-bridge-in-diagnostic-overlay-area-p overlay)))
(reverse lsp-bridge-diagnostic-overlays))))
(lsp-bridge-diagnostic-show-tooltip diagnostic-overlay t)
(message "[LSP-Bridge] Reach first diagnostic."))))
(defun lsp-bridge-diagnostic-overlay-at-point ()
(cl-dolist (overlay lsp-bridge-diagnostic-overlays)
(when (and (>= (point) (overlay-start overlay))
(< (point) (overlay-end overlay)))
(cl-return overlay)
)))
(defun lsp-bridge-diagnostic-copy ()
(interactive)
(if (zerop (length lsp-bridge-diagnostic-overlays))
(message "[LSP-Bridge] No diagnostics.")
(when-let ((overlay (lsp-bridge-diagnostic-overlay-at-point)))
(let ((diagnostic-message (overlay-get overlay 'message)))
(kill-new diagnostic-message)
(message "Copy diagnostics: '%s'" diagnostic-message)))))
(defun lsp-bridge-diagnostic-list ()
(interactive)
(lsp-bridge-call-file-api "list_diagnostics"))
(defun lsp-bridge-diagnostic--list (diagnostics)
(let ((filepath acm-backend-lsp-filepath)
(current-buffer (current-buffer))
(diagnostic-counter 0))
(with-temp-buffer
(insert (concat "\n" "\033[95m" filepath "\033[0m" "\n"))
(dolist (diagnostic diagnostics)
(let* ((range (plist-get diagnostic :range))
(message (plist-get diagnostic :message))
(start (plist-get range :start))
(end (plist-get range :end))
(start-line (1+ (plist-get start :line)))
(start-column (plist-get start :character))
(end-line (1+ (plist-get end :line)))
(end-column (plist-get end :character))
(line-content (with-current-buffer current-buffer
(save-excursion
(goto-line start-line)
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))))
(content-end-column (if (eq start-line end-line) end-column (string-width line-content))))
(insert (concat "\033[93m" (format "%s %s" (1+ diagnostic-counter) message) "\033[0m" "\n"))
;; `start' point and `end' point will same if the diagnostic message is for a location rather than region.
;; Then we need adjust end-column to highlight diagnostic location.
(when (equal start-column end-column)
(setq end-column (1+ end-column)))
(insert (format "%s:%s:%s\n\n"
start-line
start-column
(concat (substring line-content 0 start-column)
"\033[94m"
(substring line-content start-column content-end-column)
(let ((line-difference (- end-line start-line)))
(unless (eq line-difference 0)
(format "... (+%d line%s)" line-difference (when (> line-difference 1) "s"))))
"\033[0m"
(substring line-content content-end-column))))
(setq diagnostic-counter (1+ diagnostic-counter))))
(lsp-bridge-ref-popup (buffer-string) diagnostic-counter))))
(provide 'lsp-bridge-diagnostic)
;;; lsp-bridge-diagnostic.el ends here