-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflycheck-hl-todo.el
167 lines (133 loc) · 5.61 KB
/
flycheck-hl-todo.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
;;; flycheck-hl-todo.el --- Display hl-todo keywords in flycheck -*- lexical-binding: t; -*-
;; Author: Álvaro González Sotillo <alvarogonzalezsotillo@gmail.com>
;; Homepage: https://github.com/alvarogonzalezsotillo/flycheck-hl-todo
;; Package-Requires: ((emacs "25.1") (hl-todo "1.9.0") (flycheck "0.14"))
;; Version: 1.0
;; Keywords: convenience
;; This file is not part of GNU Emacs.
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file 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 file 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 file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;
;; Quick start:
;;
;; Configure `hl-todo' and `flycheck'.
;; Invoke `flycheck-hl-todo-enable' and open flyckeck error list.
;;
(require 'flycheck)
(require 'hl-todo)
;;; Code:
;; PROMT: elisp function that receives a regex and returns a list of
;; line numbers where the regex matches the current buffer
(defun flycheck-hl-todo--occur-to-error (&optional buffer regex)
"Find lines in BUFFER where the given REGEX matches.
Return a list of (position text id)."
(let* ((buffer (or buffer (current-buffer)))
(regex (or regex (hl-todo--regexp)))
(occurrences '()))
(with-current-buffer buffer
;; REVIEW: hl-todo-occur use this sytax table, dont know the actual effect
(with-syntax-table hl-todo--syntax-table
(save-excursion
(goto-char (point-min))
(let ((case-fold-search nil)) ; Only exact case in search
(while (re-search-forward regex nil t)
(let* ((pos (point))
(id (thing-at-point 'word))
(bol (line-beginning-position))
(eol (line-end-position))
(line-at-point (buffer-substring bol eol))
(msg (substring line-at-point (string-match regex line-at-point))))
(push (list pos msg id) occurrences)))))))
occurrences))
(defun flycheck-hl-todo--start (checker callback)
"Start function of hl-todo checker.
CHECKER and CALLBACK are documented in `flycheck-define-generic-checker'."
(funcall
callback 'finished
(mapcar (lambda (pos-msg-id)
(let ((pos (nth 0 pos-msg-id))
(msg (nth 1 pos-msg-id))
(id (nth 2 pos-msg-id)))
(flycheck-error-new-at-pos pos 'info msg :id id :checker checker)))
(flycheck-hl-todo--occur-to-error))))
(defun flycheck-hl-todo--get-all-modes-of-checkers ()
"Computes all modes referenced by existing checkers."
(seq-uniq
(mapcan (lambda (checker)
(let* ((modes (flycheck-checker-get checker 'modes))
;; Ensure modes is a list
(modes (if (listp modes)
modes
(list modes))))
;; Copy the list, to do not modify original list of checker
(copy-sequence modes)))
flycheck-checkers)))
(defgroup flycheck-hl-todo-group nil
"Integration of hl-todo and flycheck."
:group 'convenience)
(defcustom flycheck-hl-todo-not-chained-checkers '()
"List of checkers to not be augmented with hl-todo."
:type '(repeat (symbol :tag "Flycheck checker")))
(defcustom flycheck-hl-todo-extra-modes '()
"List of additional major modes where hl-todo checker will be registered.
All the modes of the checkers of `flycheck-checkers' will also be registered."
:type '(repeat (symbol :tag "Mode")))
(defvar flycheck-hl-todo-enabled
t
"Buffer local variable to decide if the checker should be run." )
(make-variable-buffer-local 'flycheck-hl-todo-enabled)
(defun flycheck-hl-todo-enabled-p ()
"Decide if the checker should generate errors."
flycheck-hl-todo-enabled)
(defun flycheck-hl-todo-enable ()
"Enable flycheck-hl-todo in the current buffer."
(interactive)
(flycheck-hl-todo-setup)
(setq flycheck-hl-todo-enabled t)
;; Force flycheck update
(flycheck-buffer))
(defun flycheck-hl-todo-disable ()
"Disable flycheck-hl-todo in the current buffer."
(interactive)
(setq flycheck-hl-todo-enabled nil)
;; Force flycheck update
(flycheck-buffer))
;; Create hl-todo checker
(flycheck-define-generic-checker 'hl-todo
"Syntax checker for hl-todo."
:start #'flycheck-hl-todo--start
:predicate #'flycheck-hl-todo-enabled-p
:modes '(text-mode))
;;;###autoload
(defun flycheck-hl-todo-setup ()
"Install flycheck-hl-todo, and enable it in the current buffer."
(interactive)
;; Register hl-todo checker
;; Add it to the end of the checkers list, it will be invoked via chained checkers
(add-to-list 'flycheck-checkers 'hl-todo t)
;; Add all modes to hl-todo checker
(dolist (mode (flycheck-hl-todo--get-all-modes-of-checkers))
(flycheck-add-mode 'hl-todo mode))
(dolist (mode flycheck-hl-todo-extra-modes)
(flycheck-add-mode 'hl-todo mode))
;; Chain hl-todo checker to all existing checkers, except disabled modes, and self
(dolist (checker flycheck-checkers)
(unless (or
(eq checker 'hl-todo)
(member checker flycheck-hl-todo-not-chained-checkers))
(flycheck-add-next-checker checker 'hl-todo t))))
(provide 'flycheck-hl-todo)
;;; flycheck-hl-todo.el ends here