-
Notifications
You must be signed in to change notification settings - Fork 56
/
flycheck-meghanada.el
156 lines (132 loc) · 5.62 KB
/
flycheck-meghanada.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
;;; flycheck-meghanada.el --- Flycheck support for meghanada -*- coding: utf-8; lexical-binding: t -*-
;; Copyright (C) 2017 Yutaka Matsubara
;; License: http://www.gnu.org/licenses/gpl.html
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; The `flycheck-meghanada' provides `flycheck-chcker' for java.
;;; Code:
(eval-when-compile
(require 'pcase))
(require 'flycheck)
(require 'meghanada)
(require 'cl-lib)
(defgroup flycheck-meghanada nil
"Meghanada mode's flycheck checker."
:group 'meghanada)
(defcustom flycheck-meghanada-enable-live-check t
"If true, check the buffer immediately after a new line or a short time."
:group 'flycheck-meghanada
:type 'boolean)
(defcustom flycheck-meghanada-java-encoding 'utf-8
"The default java compilation encoding."
:group 'flycheck-meghanada
:type 'symbol)
(defun flycheck-meghanada--build-error (diagnostic checker buffer)
(let* ((severity (intern (nth 2 diagnostic))))
(when (memq severity '(NOTE MANDATORY_WARNING WARNING ERROR FATAL OTHER))
(flycheck-error-new-at
(nth 0 diagnostic)
(nth 1 diagnostic)
(pcase severity
(`NOTE 'info)
(`WARNING 'warning)
(`MANDATORY_WARNING 'warning)
((or `ERROR `FATAL `OTHER) 'error))
(nth 3 diagnostic)
:checker checker
:buffer buffer
:filename (buffer-file-name buffer)))))
(defun flycheck-meghanada--build-errors (buffer result callback checker)
(mapc (lambda (r)
(let ((file (nth 0 r))
(diagnostics (nth 1 r)))
(with-current-buffer (find-file-noselect file)
(let* ((file-buf (current-buffer))
(errors (mapcar (lambda (d)
(flycheck-meghanada--build-error d checker file-buf))
diagnostics)))
(when (eq file-buf buffer)
(funcall callback 'finished (delq nil errors)))))))
result))
(defun flycheck-meghanada--decode-diagnostics (diagnostics)
(let (result result-errors file errors err msg)
(setq result '())
(dolist (buffer-errors diagnostics)
(setq file (car buffer-errors))
(setq errors (car (cdr buffer-errors)))
(setq result-errors '())
(dolist (err errors)
(setq msg (decode-coding-string
(encode-coding-string (car (last err)) flycheck-meghanada-java-encoding)
'utf-8))
(cl-pushnew (append (cl-subseq err 0 -1) (list msg)) result-errors))
(cl-pushnew (list file result-errors) result))
result))
(defun flycheck-meghanada--callback (result &rest args)
(let* ((callback (nth 0 args))
(checker (nth 1 args))
(buffer (nth 2 args))
(type (car result))
(diagnostics (car (cdr result))))
(pcase type
(`fatal (funcall callback 'errored '("Meghanada diagnostics fatal error")))
(`success (funcall callback 'finished nil))
(`error (flycheck-meghanada--build-errors buffer (if (eq flycheck-meghanada-java-encoding 'utf-8) diagnostics (flycheck-meghanada--decode-diagnostics diagnostics)) callback checker))
(_ (progn
(message "WARN not match type")
(funcall callback 'finished nil))))))
(defun flycheck-meghanada--start (checker callback)
(let ((buffer (current-buffer)))
(meghanada-diagnostics-async
(list #'flycheck-meghanada--callback callback checker buffer))))
(defun flycheck-meghanada-live--start (checker callback)
(let ((buffer (current-buffer)))
(meghanada-diagnostic-string-async
(list #'flycheck-meghanada--callback callback checker buffer))))
(defun flycheck-meghanada-after-hook ()
(let* ((errors flycheck-current-errors)
(current (current-buffer))
(new-error
(cl-remove-if-not
(lambda(e)
(let ((err-buf (flycheck-error-buffer e)))
(eq err-buf current))) errors)))
(setq flycheck-current-errors new-error)))
(flycheck-define-generic-checker 'meghanada
"A syntax checker for java, using meghanada-mode."
:start #'flycheck-meghanada--start
:modes '(java-mode)
:predicate (lambda ()
(and (meghanada-alive-p)
(flycheck-buffer-saved-p))))
(flycheck-define-generic-checker 'meghanada-live
"A syntax checker for java, using meghanada-mode."
:start #'flycheck-meghanada-live--start
:modes '(java-mode)
:predicate (lambda ()
(and (meghanada-alive-p)
(not (flycheck-buffer-empty-p))))
:verify (lambda (_)
(list
(flycheck-verification-result-new
:label "Meghanada server"
:message (if (meghanada-alive-p) "Running" "Not Running")
:face (if (meghanada-alive-p) 'success '(bold error))))))
;;;###autoload
(defun meghanada-flycheck-enable ()
"Enable flycheck for meghanada-mode."
(if flycheck-meghanada-enable-live-check
(add-to-list 'flycheck-checkers 'meghanada-live)
(add-to-list 'flycheck-checkers 'meghanada)))
(provide 'flycheck-meghanada)
;;; flycheck-meghanada.el ends here