2020-05-01 11:01:11 +02:00
|
|
|
|
;;; flycheck-eglot --- Hacky eglot support in flycheck -*- lexical-binding: t; -*-
|
2021-01-19 18:51:36 +01:00
|
|
|
|
;;; Commentary:
|
|
|
|
|
;; This file sets up flycheck so that, when eglot receives a publishDiagnostics method
|
|
|
|
|
;; from the server, then eglot calls a report function that creates diagnostics for
|
|
|
|
|
;; flycheck.
|
|
|
|
|
;;
|
|
|
|
|
;; It works by creating an eglot-specific callback function, and using this as
|
|
|
|
|
;; the REPORT-FN argument of `eglot-flymake-backend', which internally registers
|
|
|
|
|
;; that lambda as the function to use whenever there is a publishDiagnostics method.
|
|
|
|
|
;; Calling `+lsp--flycheck-eglot-init' "too late" is not a problem, since if there
|
|
|
|
|
;; are any unreported/missed diagnostics, eglot ensures that the
|
|
|
|
|
;; REPORT-FN function is called immediately.
|
|
|
|
|
;;
|
|
|
|
|
;; Note: as long as joaotavora/eglot#596 isn't fixed/dealt with, this checker cannot
|
|
|
|
|
;; work. Please check the issue on github for more context
|
2020-05-01 11:01:11 +02:00
|
|
|
|
;;; Code:
|
2020-08-22 15:31:38 -04:00
|
|
|
|
(defun +lsp--flycheck-eglot-init (checker callback)
|
2021-01-19 18:51:36 +01:00
|
|
|
|
"CHECKER is the checker (eglot).
|
2020-05-01 11:01:11 +02:00
|
|
|
|
CALLBACK is the function that we need to call when we are done, on all the errors."
|
|
|
|
|
(cl-labels
|
|
|
|
|
((flymake-diag->flycheck-err
|
|
|
|
|
(diag)
|
|
|
|
|
(with-current-buffer (flymake--diag-buffer diag)
|
|
|
|
|
(flycheck-error-new-at-pos
|
|
|
|
|
(flymake--diag-beg diag)
|
|
|
|
|
(pcase (flymake--diag-type diag)
|
|
|
|
|
('eglot-note 'info)
|
|
|
|
|
('eglot-warning 'warning)
|
|
|
|
|
('eglot-error 'error)
|
|
|
|
|
(_ (error "Unknown diagnostic type, %S" diag)))
|
|
|
|
|
(flymake--diag-text diag)
|
|
|
|
|
:end-pos (flymake--diag-end diag)
|
|
|
|
|
:checker checker
|
|
|
|
|
:buffer (current-buffer)
|
|
|
|
|
:filename (buffer-file-name)))))
|
|
|
|
|
;; NOTE: Setting up eglot to automatically create flycheck errors for the buffer.
|
2021-01-19 18:51:36 +01:00
|
|
|
|
;; Internally, this sets the lambda as the callback to be used by eglot
|
|
|
|
|
;; when it receives a publishDiagnostics method from the server
|
2020-05-18 19:43:20 +02:00
|
|
|
|
(eglot-flymake-backend
|
|
|
|
|
(lambda (flymake-diags &rest _)
|
|
|
|
|
(funcall callback
|
|
|
|
|
'finished
|
2021-01-19 18:51:36 +01:00
|
|
|
|
(mapcar #'flymake-diag->flycheck-err flymake-diags))))))
|
2020-05-01 11:01:11 +02:00
|
|
|
|
|
2020-08-22 15:31:38 -04:00
|
|
|
|
(defun +lsp--flycheck-eglot-available-p ()
|
2020-05-01 11:01:11 +02:00
|
|
|
|
(bound-and-true-p eglot--managed-mode))
|
|
|
|
|
|
|
|
|
|
(flycheck-define-generic-checker 'eglot
|
|
|
|
|
"Report `eglot' diagnostics using `flycheck'."
|
2020-08-22 15:31:38 -04:00
|
|
|
|
:start #'+lsp--flycheck-eglot-init
|
|
|
|
|
:predicate #'+lsp--flycheck-eglot-available-p
|
2020-05-01 11:01:11 +02:00
|
|
|
|
:modes '(prog-mode text-mode))
|
|
|
|
|
|
|
|
|
|
(push 'eglot flycheck-checkers)
|
|
|
|
|
|
2021-01-19 18:51:36 +01:00
|
|
|
|
(add-hook! 'eglot-managed-mode-hook
|
2020-08-22 15:31:38 -04:00
|
|
|
|
(defun +lsp-eglot-prefer-flycheck-h ()
|
|
|
|
|
(when eglot--managed-mode
|
|
|
|
|
(when-let ((current-checker (flycheck-get-checker-for-buffer)))
|
|
|
|
|
(unless (equal current-checker 'eglot)
|
|
|
|
|
(flycheck-add-next-checker 'eglot current-checker)))
|
|
|
|
|
(flycheck-add-mode 'eglot major-mode)
|
|
|
|
|
(flycheck-mode 1)
|
|
|
|
|
(flymake-mode -1))))
|
2020-05-01 11:01:11 +02:00
|
|
|
|
|
|
|
|
|
;;; flycheck-eglot.el ends here
|