From 25611707ed72d96394ec5bfb34875076e3279ed6 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 21 Feb 2023 03:16:54 -0500 Subject: [PATCH] refactor(lsp): switch to flycheck-eglot package We no longer need to maintain our own juryrigged version of this functionality. --- modules/tools/lsp/+eglot.el | 9 +-- modules/tools/lsp/autoload/flycheck-eglot.el | 76 -------------------- modules/tools/lsp/packages.el | 4 +- 3 files changed, 8 insertions(+), 81 deletions(-) delete mode 100644 modules/tools/lsp/autoload/flycheck-eglot.el diff --git a/modules/tools/lsp/+eglot.el b/modules/tools/lsp/+eglot.el index 48dc8c5eebd..6bf5d39c704 100644 --- a/modules/tools/lsp/+eglot.el +++ b/modules/tools/lsp/+eglot.el @@ -25,10 +25,6 @@ (add-to-list 'doom-debug-variables '(eglot-events-buffer-size . 0)) - (when (modulep! :checkers syntax) - (after! flycheck - (load! "autoload/flycheck-eglot"))) - (defadvice! +lsp--defer-server-shutdown-a (fn &optional server) "Defer server shutdown for a few seconds. This gives the user a chance to open other project files before the server is @@ -55,3 +51,8 @@ server getting expensively restarted when reverting buffers." :when (modulep! :completion vertico) :init (map! :map eglot-mode-map [remap xref-find-apropos] #'consult-eglot-symbols)) + + +(use-package! flycheck-eglot + :when (modulep! :checkers syntax) + :hook (eglot-managed-mode . flycheck-eglot-mode)) diff --git a/modules/tools/lsp/autoload/flycheck-eglot.el b/modules/tools/lsp/autoload/flycheck-eglot.el deleted file mode 100644 index c0568f59eb4..00000000000 --- a/modules/tools/lsp/autoload/flycheck-eglot.el +++ /dev/null @@ -1,76 +0,0 @@ -;;; flycheck-eglot --- Hacky eglot support in flycheck -*- lexical-binding: t; -*- -;;; Commentary: -;; This file sets up flycheck so that, when eglot receives a publishDiagnostics method -;; from the server, flycheck updates the reports. -;; -;; Thanks to: -;; - joaotavora for adding a handle to plug flycheck, and -;; - purcell for finding out the initial stub and the current implementation -;; -;; It works by creating a bridge function which can be used as the argument of -;; `eglot-flymake-backend', which both consumes diagnostics and queue a call to -;; 'flycheck-buffer' -;; -;;; Code: - -(defvar-local +lsp--flycheck-eglot--current-errors nil) - -(defun +lsp--flycheck-eglot-init (checker callback) - "CHECKER is the checker (eglot). -CALLBACK is the function that we need to call when we are done, on all the errors." - (eglot-flymake-backend #'+lsp--flycheck-eglot--on-diagnostics) - (funcall callback 'finished +lsp--flycheck-eglot--current-errors)) - -(defun +lsp--flycheck-eglot--on-diagnostics (diags &rest _) - (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 'eglot - :buffer (current-buffer) - :filename (buffer-file-name))))) - (setq +lsp--flycheck-eglot--current-errors - (mapcar #'flymake-diag->flycheck-err diags)) - ;; Call Flycheck to update the diagnostics annotations - (flycheck-buffer-deferred))) - -(defun +lsp--flycheck-eglot-available-p () - (bound-and-true-p eglot--managed-mode)) - -(flycheck-define-generic-checker 'eglot - "Report `eglot' diagnostics using `flycheck'." - :start #'+lsp--flycheck-eglot-init - :predicate #'+lsp--flycheck-eglot-available-p - :modes '(prog-mode text-mode)) - -(push 'eglot flycheck-checkers) - -(add-hook! 'eglot-managed-mode-hook - (defun +lsp-eglot-prefer-flycheck-h () - (when eglot--managed-mode - (flymake-mode -1) - (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) - ;; Call flycheck on initilization to make sure to display initial - ;; errors - (flycheck-buffer-deferred)))) - -(after! flymake - (when (and - (not (fboundp 'flymake--diag-buffer)) - (fboundp 'flymake--diag-locus)) - (defalias 'flymake--diag-buffer 'flymake--diag-locus))) - -;;; flycheck-eglot.el ends here diff --git a/modules/tools/lsp/packages.el b/modules/tools/lsp/packages.el index 5b74540f183..d7cef71e057 100644 --- a/modules/tools/lsp/packages.el +++ b/modules/tools/lsp/packages.el @@ -5,7 +5,9 @@ (progn (package! eglot :pin "e501275e06952889056268dabe08ccd0dbaf23e5") (when (modulep! :completion vertico) - (package! consult-eglot :pin "6504ccd2b7555452c61cc24a45965f7e2a37c44d"))) + (package! consult-eglot :pin "6504ccd2b7555452c61cc24a45965f7e2a37c44d")) + (when (modulep! :checkers syntax) + (package! flycheck-eglot :pin "9abab011071c93250a894cf2bfeaf30c8e3367a7"))) (package! lsp-mode :pin "a655f3600e040f872408da0e9c1b9fe65ca0aad9") (package! lsp-ui :pin "295d8984da06a745b0a36c56e28ce915bc389adb") (when (modulep! :completion ivy)