-
-
Notifications
You must be signed in to change notification settings - Fork 890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A couple minor fixes for Flymake diagnostics #4435
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,13 +50,9 @@ | |
(define-obsolete-variable-alias 'lsp-flycheck-default-level | ||
'lsp-diagnostics-flycheck-default-level "lsp-mode 7.0.1") | ||
|
||
(defcustom lsp-diagnostics-flycheck-default-level 'error | ||
"Error level to use when the server does not report back a diagnostic level." | ||
:type '(choice | ||
(const error) | ||
(const warning) | ||
(const info)) | ||
:group 'lsp-diagnostics) | ||
;;;###autoload | ||
(define-obsolete-variable-alias 'lsp-diagnostics-flycheck-default-level | ||
'lsp-diagnostics-default-severity "lsp-mode 9.0.1") | ||
|
||
(defcustom lsp-diagnostics-attributes | ||
`((unnecessary :foreground "gray") | ||
|
@@ -131,18 +127,19 @@ g. `error', `warning') and list of LSP TAGS." | |
|
||
(defun lsp-diagnostics--flycheck-calculate-level (severity tags) | ||
"Calculate flycheck level by SEVERITY and TAGS." | ||
(let ((level (pcase severity | ||
(1 'error) | ||
(2 'warning) | ||
(3 'info) | ||
(4 'info) | ||
(_ lsp-flycheck-default-level))) | ||
;; materialize only first tag. | ||
(tags (seq-map (lambda (tag) | ||
(cond | ||
((= tag lsp/diagnostic-tag-unnecessary) 'unnecessary) | ||
((= tag lsp/diagnostic-tag-deprecated) 'deprecated))) | ||
tags))) | ||
(let* ((severity (or severity | ||
(lsp-diagnostics-severity->numeric | ||
lsp-diagnostics-default-severity))) | ||
(level (cond ((= severity lsp/diagnostic-severity-error) 'error) | ||
((= severity lsp/diagnostic-severity-warning) 'warning) | ||
((= severity lsp/diagnostic-severity-information) 'info) | ||
((= severity lsp/diagnostic-severity-hint) 'info))) | ||
;; materialize only first tag. | ||
(tags (seq-map (lambda (tag) | ||
(cond | ||
((= tag lsp/diagnostic-tag-unnecessary) 'unnecessary) | ||
((= tag lsp/diagnostic-tag-deprecated) 'deprecated))) | ||
tags))) | ||
(if tags | ||
(lsp-diagnostics--flycheck-level level tags) | ||
level))) | ||
|
@@ -298,21 +295,21 @@ See https://github.com/emacs-lsp/lsp-mode." | |
(when (= start end) | ||
(if-let ((region (flymake-diag-region (current-buffer) | ||
(1+ start-line) | ||
character))) | ||
(1+ character)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate why +1? 🤔 Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note that this logic is only executed when the LSP server doesn't support an end position (i.e., the start and end position are the same), so
|
||
(setq start (car region) | ||
end (cdr region)) | ||
(lsp-save-restriction-and-excursion | ||
(goto-char (point-min)) | ||
(setq start (line-beginning-position (1+ start-line)) | ||
end (line-end-position (1+ end-line)))))) | ||
(flymake-make-diagnostic (current-buffer) | ||
start | ||
end | ||
(cl-case severity? | ||
(1 :error) | ||
(2 :warning) | ||
(t :note)) | ||
message)))) | ||
(let* ((severity (or severity? | ||
(lsp-diagnostics-severity->numeric | ||
lsp-diagnostics-default-severity))) | ||
(type (cond ((= severity lsp/diagnostic-severity-error) :error) | ||
((= severity lsp/diagnostic-severity-warning) :warning) | ||
((= severity lsp/diagnostic-severity-information) :note) | ||
((= severity lsp/diagnostic-severity-hint) :note)))) | ||
(flymake-make-diagnostic (current-buffer) start end type message))))) | ||
;; This :region keyword forces flymake to delete old diagnostics in | ||
;; case the buffer hasn't changed since the last call to the report | ||
;; function. See https://github.com/joaotavora/eglot/issues/159 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -634,6 +634,15 @@ diagnostics have changed." | |
:type 'hook | ||
:group 'lsp-mode) | ||
|
||
(defcustom lsp-diagnostics-default-severity 'error | ||
"Error level to use when the server does not report one." | ||
:type '(choice (const :tag "Error" error) | ||
(const :tag "Warning" warning) | ||
(const :tag "Information" info) | ||
(const :tag "Hint" hint)) | ||
:group 'lsp-mode | ||
:package-version '(lsp-mode . "9.0.1")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we keep this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @jcs090218, the reason I put it in lsp-mode.el is because there are multiple users, not just lsp-diagnostics.el. lsp-modeline, lsp-headerline, lsp-mode and lsp-diagnostics all need this information, so it seemed more general and likely belongs in lsp-mode.el. It answers the general question of what the user wants to do in the absence of LSP severity information. lsp-diagnostics.el is just one such user of this information. I placed it in lsp-mode.el near a couple other diagnostic-related user options (i.e., lsp-after-diagnostics-hook and lsp-diagnostics-updated-hook) to be consistent there. It appears that lsp-diagnostics.el is strictly the diagnostics provider (i.e., Flymake/Flycheck) interfacing, but other LSP diagnostics related information is kept in lsp-mode.el. |
||
|
||
(define-obsolete-variable-alias 'lsp-workspace-folders-changed-hook | ||
'lsp-workspace-folders-changed-functions "lsp-mode 6.3") | ||
|
||
|
@@ -2277,6 +2286,14 @@ Common usecase are: | |
result))) | ||
(ht))) | ||
|
||
(defun lsp-diagnostics-severity->numeric (severity) | ||
"Determine numeric severity from symbolic SEVERITY." | ||
(pcase severity | ||
('error lsp/diagnostic-severity-error) | ||
('warning lsp/diagnostic-severity-warning) | ||
('info lsp/diagnostic-severity-information) | ||
('hint lsp/diagnostic-severity-hint))) | ||
|
||
(defun lsp-diagnostics-stats-for (path) | ||
"Get diagnostics statistics for PATH. | ||
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." | |
(let ((path (lsp--fix-path-casing (lsp--uri-to-path uri))) | ||
(new-stats (make-vector 5 0))) | ||
(mapc (-lambda ((&Diagnostic :severity?)) | ||
(cl-incf (aref new-stats (or severity? 1)))) | ||
(cl-incf (aref new-stats (or severity? | ||
(lsp-diagnostics-severity->numeric | ||
lsp-diagnostics-default-severity))))) | ||
diagnostics) | ||
(when-let ((old-diags (gethash path (lsp--workspace-diagnostics workspace)))) | ||
(mapc (-lambda ((&Diagnostic :severity?)) | ||
(cl-decf (aref new-stats (or severity? 1)))) | ||
(cl-decf (aref new-stats (or severity? | ||
(lsp-diagnostics-severity->numeric | ||
lsp-diagnostics-default-severity))))) | ||
old-diags)) | ||
(lsp-diagnostics--update-path path new-stats) | ||
(while (not (string= path (setf path (file-name-directory | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
pcase
is probably cleaner.And the one below, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that at first, but was having trouble with
pcase
not evaluating thelsp/diagnostic
variables. If you have an example of how to transform this, I'd appreciate it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this isn't any cleaner. 🤔
It's okay to leave it as it is.