Skip to content

Commit a686a77

Browse files
committed
Add a defcustom to control default diagnostic severity.
Previously, there was a Flycheck specific defcustom, which was used to select a default error level for Flycheck. However, there was nothing for Flymake. Additionally, there were numerous other places in lsp-mode which query the severity attribute of the diagnostic, such as for statistics, modeline, headerline, etc. All of these were being handled inconsistently, either not at all (causing an error when the attribute was not sent by the server), ignoring it when it was missing (causing statistics to be inaccurate) or assuming a default of error, which might have been different than the Flycheck specific configuration, therefore causing an inconsistency in the modeline statistics vs what Flycheck would report. This change creates a common defcustom which is then used anywhere the diagnostic severity is needed, but was not provided by the server. This should create consistent statistics between the modeline and the back-end checkers. Additionally, the mapping between this defcustom and the checker specific values happens in the diagnostic package. Since this defcustom is used outside of the lsp-diagnostic package, it resides in the lsp-mode package and renamed more generally. Additionally, the Flycheck specific defcustom has been obsoleted.
1 parent 5a20661 commit a686a77

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

lsp-diagnostics.el

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,9 @@
5050
(define-obsolete-variable-alias 'lsp-flycheck-default-level
5151
'lsp-diagnostics-flycheck-default-level "lsp-mode 7.0.1")
5252

53-
(defcustom lsp-diagnostics-flycheck-default-level 'error
54-
"Error level to use when the server does not report back a diagnostic level."
55-
:type '(choice
56-
(const error)
57-
(const warning)
58-
(const info))
59-
:group 'lsp-diagnostics)
53+
;;;###autoload
54+
(define-obsolete-variable-alias 'lsp-diagnostics-flycheck-default-level
55+
'lsp-diagnostics-default-severity "lsp-mode 9.0.1")
6056

6157
(defcustom lsp-diagnostics-attributes
6258
`((unnecessary :foreground "gray")
@@ -131,18 +127,19 @@ g. `error', `warning') and list of LSP TAGS."
131127

132128
(defun lsp-diagnostics--flycheck-calculate-level (severity tags)
133129
"Calculate flycheck level by SEVERITY and TAGS."
134-
(let ((level (pcase severity
135-
(1 'error)
136-
(2 'warning)
137-
(3 'info)
138-
(4 'info)
139-
(_ lsp-flycheck-default-level)))
140-
;; materialize only first tag.
141-
(tags (seq-map (lambda (tag)
142-
(cond
143-
((= tag lsp/diagnostic-tag-unnecessary) 'unnecessary)
144-
((= tag lsp/diagnostic-tag-deprecated) 'deprecated)))
145-
tags)))
130+
(let* ((severity (or severity
131+
(lsp-diagnostics-severity->numeric
132+
lsp-diagnostics-default-severity)))
133+
(level (cond ((= severity lsp/diagnostic-severity-error) 'error)
134+
((= severity lsp/diagnostic-severity-warning) 'warning)
135+
((= severity lsp/diagnostic-severity-information) 'info)
136+
((= severity lsp/diagnostic-severity-hint) 'info)))
137+
;; materialize only first tag.
138+
(tags (seq-map (lambda (tag)
139+
(cond
140+
((= tag lsp/diagnostic-tag-unnecessary) 'unnecessary)
141+
((= tag lsp/diagnostic-tag-deprecated) 'deprecated)))
142+
tags)))
146143
(if tags
147144
(lsp-diagnostics--flycheck-level level tags)
148145
level)))
@@ -305,14 +302,14 @@ See https://github.com/emacs-lsp/lsp-mode."
305302
(goto-char (point-min))
306303
(setq start (line-beginning-position (1+ start-line))
307304
end (line-end-position (1+ end-line))))))
308-
(flymake-make-diagnostic (current-buffer)
309-
start
310-
end
311-
(cl-case severity?
312-
(1 :error)
313-
(2 :warning)
314-
(t :note))
315-
message))))
305+
(let* ((severity (or severity?
306+
(lsp-diagnostics-severity->numeric
307+
lsp-diagnostics-default-severity)))
308+
(type (cond ((= severity lsp/diagnostic-severity-error) :error)
309+
((= severity lsp/diagnostic-severity-warning) :warning)
310+
((= severity lsp/diagnostic-severity-information) :note)
311+
((= severity lsp/diagnostic-severity-hint) :note))))
312+
(flymake-make-diagnostic (current-buffer) start end type message)))))
316313
;; This :region keyword forces flymake to delete old diagnostics in
317314
;; case the buffer hasn't changed since the last call to the report
318315
;; function. See https://github.com/joaotavora/eglot/issues/159

lsp-headerline.el

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,11 @@ PATH is the current folder to be checked."
306306
(let ((range-severity 10))
307307
(mapc (-lambda ((&Diagnostic :range (&Range :start) :severity?))
308308
(when (lsp-point-in-range? start range)
309-
(setq range-severity (min range-severity severity?))))
309+
(setq range-severity
310+
(min range-severity
311+
(or severity?
312+
(lsp-diagnostics-severity->numeric
313+
lsp-diagnostics-default-severity))))))
310314
(lsp--get-buffer-diagnostics))
311315
range-severity))
312316

lsp-mode.el

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,15 @@ diagnostics have changed."
634634
:type 'hook
635635
:group 'lsp-mode)
636636

637+
(defcustom lsp-diagnostics-default-severity 'error
638+
"Error level to use when the server does not report one."
639+
:type '(choice (const :tag "Error" error)
640+
(const :tag "Warning" warning)
641+
(const :tag "Information" info)
642+
(const :tag "Hint" hint))
643+
:group 'lsp-mode
644+
:package-version '(lsp-mode . "9.0.1"))
645+
637646
(define-obsolete-variable-alias 'lsp-workspace-folders-changed-hook
638647
'lsp-workspace-folders-changed-functions "lsp-mode 6.3")
639648

@@ -2277,6 +2286,14 @@ Common usecase are:
22772286
result)))
22782287
(ht)))
22792288

2289+
(defun lsp-diagnostics-severity->numeric (severity)
2290+
"Determine numeric severity from symbolic SEVERITY."
2291+
(pcase severity
2292+
('error lsp/diagnostic-severity-error)
2293+
('warning lsp/diagnostic-severity-warning)
2294+
('info lsp/diagnostic-severity-information)
2295+
('hint lsp/diagnostic-severity-hint)))
2296+
22802297
(defun lsp-diagnostics-stats-for (path)
22812298
"Get diagnostics statistics for PATH.
22822299
The result format is vector [_ errors warnings infos hints] or nil."
@@ -2296,11 +2313,15 @@ The result format is vector [_ errors warnings infos hints] or nil."
22962313
(let ((path (lsp--fix-path-casing (lsp--uri-to-path uri)))
22972314
(new-stats (make-vector 5 0)))
22982315
(mapc (-lambda ((&Diagnostic :severity?))
2299-
(cl-incf (aref new-stats (or severity? 1))))
2316+
(cl-incf (aref new-stats (or severity?
2317+
(lsp-diagnostics-severity->numeric
2318+
lsp-diagnostics-default-severity)))))
23002319
diagnostics)
23012320
(when-let ((old-diags (gethash path (lsp--workspace-diagnostics workspace))))
23022321
(mapc (-lambda ((&Diagnostic :severity?))
2303-
(cl-decf (aref new-stats (or severity? 1))))
2322+
(cl-decf (aref new-stats (or severity?
2323+
(lsp-diagnostics-severity->numeric
2324+
lsp-diagnostics-default-severity)))))
23042325
old-diags))
23052326
(lsp-diagnostics--update-path path new-stats)
23062327
(while (not (string= path (setf path (file-name-directory

lsp-modeline.el

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ The `:global' workspace is global one.")
229229
(mapc (lambda (buf-diags)
230230
(mapc (lambda (diag)
231231
(-let [(&Diagnostic? :severity?) diag]
232-
(when severity?
233-
(cl-incf (aref stats severity?)))))
232+
(cl-incf (aref stats
233+
(or severity?
234+
(lsp-diagnostics-severity->numeric
235+
lsp-diagnostics-default-severity))))))
234236
buf-diags))
235237
diagnostics)
236238
(while (< i lsp/diagnostic-severity-max)

0 commit comments

Comments
 (0)