Skip to content

Commit 06ab9d0

Browse files
mgmarlowjeffbowman
authored andcommitted
Add crafted-ide-eglot-auto-ensure-all, requiring user opt-in
Rather than relying on with-eval-after-load, make the auto configuration of eglot-ensure opt-in. This clarifies the loading behavior necessary for eglot-ensure. Additionally, only register eglot-ensure hooks for major modes that have an accompanying LSP binary available on the system. This avoids noisy error messages from trying to initialize Eglot when no LSP server exists.
1 parent 3848e81 commit 06ab9d0

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

modules/crafted-ide-config.el

+22-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,28 @@ manually with something like this:
4040
(message "adding eglot to %s" hook-name)
4141
(add-hook (intern hook-name) #'eglot-ensure))))))))
4242

43-
;; add eglot to existing programming modes when eglot is loaded.
44-
(with-eval-after-load "eglot"
45-
(crafted-ide--add-eglot-hooks eglot-server-programs))
43+
(defun crafted-ide--lsp-bin-exists-p (mode-def)
44+
"Return non-nil if LSP binary of MODE-DEF is found via `executable-find'."
45+
(let ((lsp-program (cdr mode-def)))
46+
;; `lsp-program' is either a list of strings or a function object
47+
;; calling `eglot-alternatives'.
48+
(if (functionp lsp-program)
49+
(condition-case nil
50+
(car (funcall lsp-program))
51+
;; When an error occurs it's because Eglot checked for a
52+
;; binary and didn't find one among alternatives.
53+
(error nil))
54+
(executable-find (car lsp-program)))))
55+
56+
(defun crafted-ide-eglot-auto-ensure-all ()
57+
"Add `eglot-ensure' to major modes that offer LSP support.
58+
59+
Major modes are only selected if the major mode's associated LSP
60+
binary is detected on the system."
61+
(when (require 'eglot nil :noerror)
62+
(crafted-ide--add-eglot-hooks (seq-filter
63+
#'crafted-ide--lsp-bin-exists-p
64+
eglot-server-programs))))
4665

4766
;; Shutdown server when last managed buffer is killed
4867
(customize-set-variable 'eglot-autoshutdown t)

0 commit comments

Comments
 (0)