-
Notifications
You must be signed in to change notification settings - Fork 5
/
helm-flycheck.el
196 lines (162 loc) · 7.24 KB
/
helm-flycheck.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
;;; helm-flycheck.el --- Show flycheck errors with helm
;; Copyright (C) 2013-2016 Yasuyuki Oka <[email protected]>
;; Author: Yasuyuki Oka <[email protected]>
;; Version: 0.4
;; URL: https://github.com/yasuyk/helm-flycheck
;; Package-Requires: ((dash "2.12.1") (flycheck "28") (helm-core "1.9.8"))
;; Keywords: helm, flycheck
;; 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:
;; Installation:
;; Add the following to your Emacs init file:
;;
;; (require 'helm-flycheck) ;; Not necessary if using ELPA package
;; (eval-after-load 'flycheck
;; '(define-key flycheck-mode-map (kbd "C-c ! h") 'helm-flycheck))
;; That's all.
;;; Code:
(require 'dash)
(require 'flycheck)
(require 'helm)
(defvar helm-source-flycheck
'((name . "Flycheck")
(init . helm-flycheck-init)
(candidates . helm-flycheck-candidates)
(action-transformer helm-flycheck-action-transformer)
(multiline)
(action . (("Go to" . helm-flycheck-action-goto-error)))
(follow . 1)))
(defvar helm-flycheck-candidates nil)
(defconst helm-flycheck-status-message-no-errors
"There are no errors in the current buffer.")
(defconst helm-flycheck-status-message-syntax-checking
"Syntax checking now. Do action to reexecute `helm-flycheck'.")
(defconst helm-flycheck-status-message-checker-not-found
"A suitable syntax checker is not found. \
See Selection in flycheck manual, for more information.")
(defconst helm-flycheck-status-message-failed
"The syntax check failed. Inspect the *Messages* buffer for details.")
(defconst helm-flycheck-status-message-dubious
"The syntax check had a dubious result. \
Inspect the *Messages* buffer for details.")
(defun helm-flycheck-init ()
"Initialize `helm-source-flycheck'."
(setq helm-flycheck-candidates
(if (flycheck-has-current-errors-p)
(mapcar 'helm-flycheck-make-candidate
(sort flycheck-current-errors #'flycheck-error-<))
(list (helm-flycheck-status-message)))))
(defun helm-flycheck-status-message ()
"Return message about `flycheck' STATUS."
(cond ((equal flycheck-last-status-change 'finished)
helm-flycheck-status-message-no-errors)
((equal flycheck-last-status-change 'running)
helm-flycheck-status-message-syntax-checking)
((equal flycheck-last-status-change 'no-checker)
helm-flycheck-status-message-checker-not-found)
((equal flycheck-last-status-change 'errored)
helm-flycheck-status-message-failed)
((equal flycheck-last-status-change 'suspicious)
helm-flycheck-status-message-dubious)))
(defun helm-flycheck-make-candidate (error)
"Return a cons constructed from string of message and ERROR."
(cons (helm-flycheck-candidate-display-string error) error))
(defun helm-flycheck-candidate-display-string (error)
"Return a string of message constructed from ERROR."
(let ((face (-> error
flycheck-error-level
flycheck-error-level-error-list-face)))
(format "%5s %3s%8s %s"
(propertize (number-to-string (flycheck-error-line error)) 'font-lock-face 'flycheck-error-list-line-number)
(-if-let (column (flycheck-error-column error))
(propertize (number-to-string column) 'font-lock-face 'flycheck-error-list-column-number) "")
(propertize (symbol-name (flycheck-error-level error))
'font-lock-face face)
(or (flycheck-error-message error) ""))))
(defun helm-flycheck-action-transformer (actions candidate)
"Return modified ACTIONS if CANDIDATE is status message."
(if (stringp candidate)
(cond ((string= candidate helm-flycheck-status-message-no-errors) nil)
((string= candidate helm-flycheck-status-message-syntax-checking)
'(("Reexecute helm-flycheck" . helm-flycheck-action-reexecute)))
((string= candidate helm-flycheck-status-message-checker-not-found)
'(("Enter info of Syntax checker selection" .
helm-flycheck-action-selection-info)))
((or (string= candidate helm-flycheck-status-message-failed)
(string= candidate helm-flycheck-status-message-dubious))
'(("Switch to *Messages*" .
helm-flycheck-action-switch-to-messages-buffer))))
actions))
(defun helm-flycheck-action-goto-error (candidate)
"Visit error of CANDIDATE."
(let ((buffer (flycheck-error-buffer candidate))
(lineno (flycheck-error-line candidate))
error-pos)
(with-current-buffer buffer
(switch-to-buffer buffer)
(goto-char (point-min))
(forward-line (1- lineno))
(setq error-pos
(car
(->> (flycheck-overlays-in
(point)
(save-excursion (forward-line 1) (point)))
(-map #'overlay-start)
-uniq
(-sort #'<=))))
(goto-char error-pos)
(let ((recenter-redisplay nil))
(recenter)))))
(defun helm-flycheck-action-reexecute (candidate)
"Reexecute `helm-flycheck' without CANDIDATE."
(catch 'exit
(helm-run-after-exit 'helm-flycheck)))
(defun helm-flycheck-action-switch-to-messages-buffer (candidate)
"Switch to *Messages* buffer without CANDIDATE."
(switch-to-buffer "*Messages*"))
(defun helm-flycheck-action-selection-info (candidate)
"Enter info of flycheck syntax checker selection without CANDIDATE."
(info "(flycheck)Top > Usage > Selection"))
(defun helm-flycheck-preselect ()
"PreSelect nearest error from the current point."
(let* ((point (point))
(overlays-at-point (flycheck-overlays-at point))
candidates nearest-point)
(if overlays-at-point
(helm-flycheck-candidate-display-string
(car (flycheck-overlay-errors-at point)))
(setq candidates (->> (flycheck-overlays-in (point-min) (point-max))
(-map #'overlay-start)
-uniq))
(setq nearest-point (helm-flycheck-nearest-point point candidates))
(when nearest-point
(helm-flycheck-candidate-display-string
(car (flycheck-overlay-errors-at nearest-point)))))))
(defun helm-flycheck-nearest-point (current-point points)
"Return nearest point from CURRENT-POINT in POINTS."
(--tree-reduce-from
(if (< (abs (- current-point it)) (abs (- current-point acc)))
it acc) (car points) points))
;;;###autoload
(defun helm-flycheck ()
"Show flycheck errors with `helm'."
(interactive)
(unless flycheck-mode
(user-error "Flycheck mode not enabled"))
(helm :sources 'helm-source-flycheck
:buffer "*helm flycheck*"
:preselect (helm-flycheck-preselect)))
(provide 'helm-flycheck)
;; Local Variables:
;; coding: utf-8
;; End:
;;; helm-flycheck.el ends here