-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
flycheck-languagetool.el
387 lines (348 loc) · 14.6 KB
/
flycheck-languagetool.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
;;; flycheck-languagetool.el --- Flycheck support for LanguageTool -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2024 Shen, Jen-Chieh; Peter Oliver
;; Created date 2021-04-02 23:22:44
;; Author: Shen, Jen-Chieh <[email protected]>
;; Peter Oliver <[email protected]>
;; URL: https://github.com/emacs-languagetool/flycheck-languagetool
;; Version: 0.3.0
;; Package-Requires: ((emacs "25.1") (flycheck "0.14"))
;; Keywords: convenience grammar check
;; This file is NOT part of GNU Emacs.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Flycheck support for LanguageTool.
;;
;;; Code:
(require 'flycheck)
(eval-when-compile (require 'subr-x))
(defgroup flycheck-languagetool nil
"Flycheck support for LanguageTool."
:prefix "flycheck-languagetool-"
:group 'flycheck
:link '(url-link :tag "Github" "https://github.com/emacs-languagetool/flycheck-languagetool"))
(defcustom flycheck-languagetool-active-modes
'(text-mode latex-mode org-mode markdown-mode message-mode)
"List of major mode that work with LanguageTool."
:type 'list
:group 'flycheck-languagetool)
(defcustom flycheck-languagetool-url nil
"The URL for the LanguageTool API we should connect to."
:type '(choice (const :tag "Auto" nil)
(string :tag "URL"))
:package-version '(flycheck-languagetool . "0.3.0")
:group 'flycheck-languagetool)
(defcustom flycheck-languagetool-server-command ()
"Custom command to start LanguageTool server.
If non-nil, this list of strings replaces the standard java cli command."
:type '(repeat string)
:group 'flycheck-languagetool)
(defcustom flycheck-languagetool-server-jar nil
"The path of languagetool-server.jar.
The server will be automatically started if specified. Set to
nil if you’re going to connect to a remote LanguageTool server,
or plan to start a local server some other way."
:type '(choice (const :tag "Off" nil)
(file :tag "Filename" :must-match t))
:package-version '(flycheck-languagetool . "0.3.0")
:link '(url-link :tag "LanguageTool embedded HTTP Server"
"https://dev.languagetool.org/http-server.html")
:group 'flycheck-languagetool)
(defcustom flycheck-languagetool-server-port 8081
"The port on which an automatically started LanguageTool server should listen."
:type 'integer
:package-version '(flycheck-languagetool . "0.3.0")
:link '(url-link :tag "LanguageTool embedded HTTP Server"
"https://dev.languagetool.org/http-server.html")
:group 'flycheck-languagetool)
(defcustom flycheck-languagetool-server-args ()
"Extra arguments to pass when starting the LanguageTool server."
:type '(repeat string)
:link '(url-link :tag "LanguageTool embedded HTTP Server"
"https://dev.languagetool.org/http-server.html")
:group 'flycheck-languagetool)
(defcustom flycheck-languagetool-language "en-US"
"The language code of the text to check."
:type '(string :tag "Language")
:safe #'stringp
:group 'flycheck-languagetool)
(make-variable-buffer-local 'flycheck-languagetool-language)
(defcustom flycheck-languagetool-check-params ()
"Extra parameters to pass with LanguageTool check requests."
:type '(alist :key-type string :value-type string)
:options '("level"
"enabledOnly"
"disabledCategories"
"enabledCategories"
"disabledRules"
"enabledRules"
"preferredVariants"
"motherTongue"
"dicts"
"apiKey"
"username")
:link '(url-link
:tag "LanguageTool API"
"https://languagetool.org/http-api/swagger-ui/#!/default/post_check")
:group 'flycheck-languagetool)
(defvar flycheck-languagetool--started-server nil
"Have we ever attempted to start the LanguageTool server?")
(defvar flycheck-languagetool--spelling-rules
'("HUNSPELL_RULE"
"HUNSPELL_RULE_AR"
"MORFOLOGIK_RULE_AST"
"MORFOLOGIK_RULE_BE_BY"
"MORFOLOGIK_RULE_BR_FR"
"MORFOLOGIK_RULE_CA_ES"
"MORFOLOGIK_RULE_DE_DE"
"MORFOLOGIK_RULE_EL_GR"
"MORFOLOGIK_RULE_EN"
"MORFOLOGIK_RULE_EN_AU"
"MORFOLOGIK_RULE_EN_CA"
"MORFOLOGIK_RULE_EN_GB"
"MORFOLOGIK_RULE_EN_NZ"
"MORFOLOGIK_RULE_EN_US"
"MORFOLOGIK_RULE_EN_ZA"
"MORFOLOGIK_RULE_ES"
"MORFOLOGIK_RULE_GA_IE"
"MORFOLOGIK_RULE_IT_IT"
"MORFOLOGIK_RULE_LT_LT"
"MORFOLOGIK_RULE_ML_IN"
"MORFOLOGIK_RULE_NL_NL"
"MORFOLOGIK_RULE_PL_PL"
"MORFOLOGIK_RULE_RO_RO"
"MORFOLOGIK_RULE_RU_RU"
"MORFOLOGIK_RULE_RU_RU_YO"
"MORFOLOGIK_RULE_SK_SK"
"MORFOLOGIK_RULE_SL_SI"
"MORFOLOGIK_RULE_SR_EKAVIAN"
"MORFOLOGIK_RULE_SR_JEKAVIAN"
"MORFOLOGIK_RULE_TL"
"MORFOLOGIK_RULE_UK_UA"
"SYMSPELL_RULE")
"LanguageTool rules for checking of spelling.
These rules will be disabled if Emacs’ `flyspell-mode' or
`jinx-mode' is active.")
;;
;; (@* "External" )
;;
(defvar url-http-end-of-headers)
(defvar url-request-method)
(defvar url-request-extra-headers)
(defvar url-request-data)
;;
;; (@* "Util" )
;;
(defun flycheck-languagetool--column-at-pos (&optional pt)
"Return column at PT."
(unless pt (setq pt (point)))
(save-excursion (goto-char pt) (current-column)))
;;
;; (@* "Core" )
;;
(defun flycheck-languagetool--check-all (results)
"Map RESULTS from LanguageTool to positions of errors in the buffer."
(let ((matches (cdr (assoc 'matches results)))
check-list)
(dolist (match matches)
(let* ((pt-beg (+ (point-min) (cdr (assoc 'offset match))))
(len (cdr (assoc 'length match)))
(pt-end (+ pt-beg len))
(ln (save-restriction
(widen)
(line-number-at-pos pt-beg)))
(type 'warning)
(id (cdr (assoc 'id (assoc 'rule match))))
(subid (cdr (assoc 'subId (assoc 'rule match))))
(desc (cdr (assoc 'message match)))
(col-start (flycheck-languagetool--column-at-pos pt-beg))
(col-end (flycheck-languagetool--column-at-pos pt-end)))
(push (list ln col-start type desc
:end-column col-end
:id (cons id subid))
check-list)))
check-list))
(defun flycheck-languagetool--read-results (status source-buffer callback)
"Callback for results from LanguageTool API.
STATUS is passed from `url-retrieve'.
SOURCE-BUFFER is the buffer currently being checked.
CALLBACK is passed from Flycheck."
(let ((err (plist-get status :error)))
(when err
(error
(funcall callback 'errored
(error-message-string
(append err
(list (progn
(goto-char (+ 1 url-http-end-of-headers))
(buffer-substring (point) (point-max))))))))))
(set-buffer-multibyte t)
(goto-char url-http-end-of-headers)
(let ((results (car (flycheck-parse-json
(buffer-substring (point) (point-max))))))
(kill-buffer)
(with-current-buffer source-buffer
(funcall
callback 'finished
(flycheck-increment-error-columns
(mapcar
(lambda (x)
(apply #'flycheck-error-new-at `(,@x :checker languagetool)))
(condition-case err
(flycheck-languagetool--check-all results)
(error (funcall callback 'errored (error-message-string err))))))))))
(defun flycheck-languagetool--start-server ()
"Start the LanguageTool server if we didn’t already."
(unless (process-live-p (get-process "languagetool-server"))
(let* ((cmd (or flycheck-languagetool-server-command
(list "java" "-cp" (expand-file-name flycheck-languagetool-server-jar)
"org.languagetool.server.HTTPServer"
"--port" (format "%s" flycheck-languagetool-server-port))))
(process
(apply #'start-process
"languagetool-server"
" *LanguageTool server*"
(append cmd flycheck-languagetool-server-args))))
(set-process-query-on-exit-flag process nil)
(while
(with-current-buffer (process-buffer process)
(goto-char (point-min))
(unless (re-search-forward " Server started$" nil t)
(accept-process-output process 1)
(process-live-p process)))))))
(defun flycheck-languagetool--start (_checker callback)
"Flycheck start function for _CHECKER `languagetool', invoking CALLBACK."
(when (or flycheck-languagetool-server-command
flycheck-languagetool-server-jar)
(unless flycheck-languagetool--started-server
(setq flycheck-languagetool--started-server t)
(flycheck-languagetool--start-server)))
(let* ((url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")))
(disabled-rules
(flatten-tree (list
(cdr (assoc "disabledRules"
flycheck-languagetool-check-params))
(when (or (bound-and-true-p flyspell-mode)
(bound-and-true-p jinx-mode))
flycheck-languagetool--spelling-rules))))
(other-params (assoc-delete-all "disabledRules"
flycheck-languagetool-check-params))
(url-request-data
(mapconcat
(lambda (param)
(concat (url-hexify-string (car param)) "="
(url-hexify-string (cdr param))))
(append other-params
`(("language" . ,flycheck-languagetool-language)
("text" . ,(buffer-substring-no-properties
(point-min) (point-max))))
(when disabled-rules
(list (cons "disabledRules"
(string-join disabled-rules ",")))))
"&")))
(url-retrieve
(concat (or flycheck-languagetool-url
(format "http://localhost:%s"
flycheck-languagetool-server-port))
"/v2/check")
#'flycheck-languagetool--read-results
(list (current-buffer) callback)
t)))
(defun flycheck-languagetool--error-explainer (err)
"Link to a detailed explanation of ERR on the LanguageTool website."
(let* ((error-id (flycheck-error-id err))
(id (car error-id))
(subid (cdr error-id))
(url (apply #'format
"https://community.languagetool.org/rule/show/%s?lang=%s"
(mapcar #'url-hexify-string
(list id flycheck-languagetool-language)))))
(when subid
(setq url (concat url
(format "&subId=%s" (url-hexify-string subid)))))
`(url . ,url)))
(defun flycheck-languagetool--enabled ()
"Can the Flycheck LanguageTool checker be enabled?"
(cond (flycheck-languagetool-url
(not (string= "" flycheck-languagetool-url)))
(flycheck-languagetool-server-command
(and (listp flycheck-languagetool-server-command)
(executable-find (car flycheck-languagetool-server-command))))
(flycheck-languagetool-server-jar
(and (not (string= "" flycheck-languagetool-server-jar))
(file-exists-p flycheck-languagetool-server-jar)
(executable-find "java")))))
(defun flycheck-languagetool--verify (_checker)
"Verify proper configuration of Flycheck _CHECKER `languagetool'."
(list
(flycheck-verification-result-new
;; We could improve this test by also checking that we can
;; successfully make requests to the URL.
:label "LanguageTool API URL"
:message (if flycheck-languagetool-url
(if (not (string= "" flycheck-languagetool-url))
flycheck-languagetool-url "Blank")
"Not configured")
:face (if flycheck-languagetool-url
(if (not (string= "" flycheck-languagetool-url))
'success '(bold error))
'(bold warning)))
(flycheck-verification-result-new
:label "LanguageTool server command"
:message
(if flycheck-languagetool-server-command
(format (if (and (executable-find
(car flycheck-languagetool-server-command)))
"Found at %s" "Configured as %s but missing")
(car flycheck-languagetool-server-command))
"Not configured")
:face (if flycheck-languagetool-server-command
(if (and (listp flycheck-languagetool-server-command)
(executable-find
(car flycheck-languagetool-server-command)))
'success '(bold error))
'(bold warning)))
(flycheck-verification-result-new
:label "LanguageTool server JAR"
:message
(if flycheck-languagetool-server-jar
(format (if (and (not (string= "" flycheck-languagetool-server-jar))
(file-exists-p flycheck-languagetool-server-jar))
"Found at %s" "Missing from %s")
flycheck-languagetool-server-jar)
"Not configured")
:face (if flycheck-languagetool-server-jar
(if (and (not (string= "" flycheck-languagetool-server-jar))
(file-exists-p flycheck-languagetool-server-jar))
'success '(bold error))
'(bold warning)))
(flycheck-verification-result-new
:label "Java executable"
:message (or (executable-find "java") "Not found")
:face (if (executable-find "java") 'success '(bold warning)))))
(flycheck-define-generic-checker 'languagetool
"LanguageTool flycheck definition."
:start #'flycheck-languagetool--start
:enabled #'flycheck-languagetool--enabled
:verify #'flycheck-languagetool--verify
:error-explainer #'flycheck-languagetool--error-explainer
:modes flycheck-languagetool-active-modes
:next-checkers '(proselint))
;;;###autoload
(defun flycheck-languagetool-setup ()
"Setup flycheck-package."
(interactive)
(add-to-list 'flycheck-checkers 'languagetool))
(provide 'flycheck-languagetool)
;;; flycheck-languagetool.el ends here