-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sideline-flymake.el
124 lines (102 loc) · 4.15 KB
/
sideline-flymake.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
;;; sideline-flymake.el --- Show flymake errors with sideline -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2025 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <[email protected]>
;; Maintainer: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/emacs-sideline/sideline-flymake
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1") (sideline "0.1.0"))
;; Keywords: convenience flymake
;; 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 flymake errors with sideline.
;;
;; 1) Add sideline-flymake to sideline backends list,
;;
;; (setq sideline-backends-right '(sideline-flymake))
;;
;; 2) Then enable sideline-mode in the target buffer,
;;
;; M-x sideline-mode
;;
;;; Code:
(require 'cl-lib)
(require 'flymake)
(require 'subr-x)
(require 'sideline)
(defgroup sideline-flymake nil
"Show flymake errors with sideline."
:prefix "sideline-flymake-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/emacs-sideline/sideline-flymake"))
(defcustom sideline-flymake-display-mode 'point
"Method type to when sideline will display flymake's errors."
:type '(choice (const line)
(const point))
:group 'sideline-flymake)
(defcustom sideline-flymake-show-backend-name nil
"If non-nil, show the checker's name at the back."
:type 'boolean
:group 'sideline-flymake)
(defcustom sideline-flymake-max-lines 1
"Maximum number of lines to show."
:type 'integer
:group 'sideline-flymake)
(defface sideline-flymake-error
`((t :inherit error))
"Indicate error operation."
:group 'sideline-flymake)
(defface sideline-flymake-warning
`((t :inherit warning))
"Indicate warning operation."
:group 'sideline-flymake)
(defface sideline-flymake-success
`((t :inherit success))
"Indicate successful operation."
:group 'sideline-flymake)
;;;###autoload
(defun sideline-flymake (command)
"Backend for sideline.
Argument COMMAND is required in sideline backend."
(cl-case command
(`candidates (cons :async #'sideline-flymake--show-errors))))
(defun sideline-flymake--get-errors ()
"Return flymake errors."
(cl-case sideline-flymake-display-mode
(`point (flymake-diagnostics (point)))
(`line (flymake-diagnostics (line-beginning-position) (line-end-position)))
(t (user-error "Invalid value of `sideline-flymake-display-mode': %s"
sideline-flymake-display-mode))))
(defun sideline-flymake--show-errors (callback &rest _)
"Execute CALLBACK to display with sideline."
(when flymake-mode
(when-let ((errors (sideline-flymake--get-errors)))
(dolist (err errors)
(let* ((text (flymake-diagnostic-text err))
(lines (split-string text "\n"))
(lines (butlast lines (- (length lines) sideline-flymake-max-lines)))
(text (mapconcat #'identity lines "\n"))
(type (flymake-diagnostic-type err))
(backend (flymake-diagnostic-backend err))
(face (cl-case type
(`eglot-error 'sideline-flymake-error)
(`eglot-warning 'sideline-flymake-warning)
(:error 'sideline-flymake-error)
(:warning 'sideline-flymake-warning)
(t 'sideline-flymake-success))))
(when sideline-flymake-show-backend-name
(setq text (format "%s (%s)" text backend)))
(add-face-text-property 0 (length text) face nil text)
(funcall callback (list text)))))))
(provide 'sideline-flymake)
;;; sideline-flymake.el ends here