/
sideline-flycheck.el
129 lines (106 loc) · 4.47 KB
/
sideline-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
;;; sideline-flycheck.el --- Show flycheck errors with sideline -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2024 Shen, Jen-Chieh
;; Created date 2022-06-14 17:10:48
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/emacs-sideline/sideline-flycheck
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1") (sideline "0.1.1") (flycheck "0.14") (ht "2.4"))
;; Keywords: convenience flycheck
;; 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:
;;
;; This package allows display flycheck errors with sideline.
;;
;; 1) Add sideline-flycheck to sideline backends list,
;;
;; (setq sideline-backends-right '(sideline-flycheck))
;;
;; 2) Then enable sideline-mode in the target buffer,
;;
;; M-x sideline-mode
;;
;;; Code:
(require 'cl-lib)
(require 'sideline)
(require 'flycheck)
(require 'ht)
(defgroup sideline-flycheck nil
"Show flycheck errors with sideline."
:prefix "sideline-flycheck-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/emacs-sideline/sideline-flycheck"))
(defcustom sideline-flycheck-inhibit-functions nil
"Functions to inhibit display of sideline flycheck."
:type 'hook
:group 'sideline-flycheck)
(defcustom sideline-flycheck-show-checker-name nil
"If non-nil, show the checker's name at the back."
:type 'boolean
:group 'sideline-flycheck)
(defcustom sideline-flycheck-max-lines 1
"Maximum number of lines to show."
:type 'integer
:group 'sideline-flycheck)
(defvar-local sideline-flycheck--old-display-function nil
"The former value of `flycheck-display-errors-function'.")
(defvar-local sideline-flycheck--callback nil
"Callback to display errors with sideline.")
(defvar-local sideline-flycheck--errors (ht-create)
"Store error messages as key.")
;;;###autoload
(defun sideline-flycheck (command)
"Backend for sideline.
Argument COMMAND is required in sideline backend."
(cl-case command
(`candidates (cons :async
(lambda (callback &rest _)
(setq sideline-flycheck--callback callback))))))
(defun sideline-flycheck--show (errors)
"Display ERRORS, using sideline.el library."
(when (and errors
(not (run-hook-with-args-until-success 'sideline-flycheck-inhibit-functions))
sideline-flycheck--callback)
(let (msgs)
(dolist (err errors)
(let* ((level (flycheck-error-level err))
(face (if (eq level 'info) 'success level))
(msg (flycheck-error-message err))
(lines (split-string msg "\n"))
(lines (butlast lines (- (length lines) sideline-flycheck-max-lines)))
(msg (mapconcat #'identity lines "\n"))
(checker (flycheck-error-checker err)))
(when sideline-flycheck-show-checker-name
(setq msg (format "%s (%s)" msg checker)))
(add-face-text-property 0 (length msg) face nil msg)
(unless (ht-contains-p sideline-flycheck--errors msg)
(ht-set sideline-flycheck--errors msg nil) ; doesn't care about value
(push msg msgs))))
(funcall sideline-flycheck--callback msgs))))
(defun sideline-flycheck--reset ()
"After sideline is reset."
(ht-clear sideline-flycheck--errors))
;;;###autoload
(defun sideline-flycheck-setup ()
"Setup for `flycheck-mode'."
(cond
(flycheck-mode
(setq sideline-flycheck--old-display-function flycheck-display-errors-function)
(setq-local flycheck-display-errors-function #'sideline-flycheck--show)
(add-hook 'sideline-reset-hook #'sideline-flycheck--reset nil t))
(t
(setq-local flycheck-display-errors-function sideline-flycheck--old-display-function)
(setq sideline-flycheck--old-display-function nil)
(remove-hook 'sideline-reset-hook #'sideline-flycheck--reset t)
(sideline-render)))) ; update sideline once
(provide 'sideline-flycheck)
;;; sideline-flycheck.el ends here