]> git.eshelyaron.com Git - emacs.git/commitdiff
Support multiple servers out-of-box for same mode
authorJoão Távora <joaotavora@gmail.com>
Sat, 22 May 2021 10:49:47 +0000 (11:49 +0100)
committerJoão Távora <joaotavora@gmail.com>
Sat, 22 May 2021 20:01:59 +0000 (21:01 +0100)
Also per https://github.com/joaotavora/eglot/issues/537.

* eglot.el (eglot-alternatives): new helper.
(eglot-server-programs): Use it.  Use clangd and pylsp.

* NEWS.md: Mention feature.

* README.md (Connecting to a server): Mention pylsp and clangd.

GitHub-reference: fix https://github.com/joaotavora/eglot/issues/688

lisp/progmodes/eglot.el

index 13d9952494aea7bc854b792de539e90ddfa80ed4..a739419e9dc2771a9b1b7adf7c7984be707424f6 100644 (file)
   :prefix "eglot-"
   :group 'applications)
 
-(defvar eglot-server-programs '((rust-mode . (eglot-rls "rls"))
-                                (python-mode . ("pyls"))
+(defun eglot-alternatives (alternatives)
+  "Compute server-choosing function for `eglot-server-programs'.
+Each element of ALTERNATIVES is a string PROGRAM or a list of
+strings (PROGRAM ARGS...) where program names an LSP server
+program to start with ARGS.  Returns a function of one
+argument."
+  (lambda (&optional interactive)
+    (let* ((listified (cl-loop for a in alternatives
+                               collect (if (listp a) a (list a))))
+           (available (cl-remove-if-not #'executable-find listified :key #'car)))
+      (cond ((and interactive (cdr available))
+             (let ((chosen (completing-read
+                            "[eglot] More than one server executable available:"
+                            (mapcar #'car available)
+                            nil t nil nil (car (car available)))))
+               (assoc chosen available #'equal)))
+            ((car available))
+            (t
+             (car listified))))))
+
+(defvar eglot-server-programs `((rust-mode . (eglot-rls "rls"))
+                                (python-mode
+                                 . ,(eglot-alternatives '("pyls" "pylsp")))
                                 ((js-mode typescript-mode)
                                  . ("typescript-language-server" "--stdio"))
                                 (sh-mode . ("bash-language-server" "start"))
                                 ((php-mode phps-mode)
                                  . ("php" "vendor/felixfbecker/\
 language-server/bin/php-language-server.php"))
-                                ((c++-mode c-mode) . ("ccls"))
+                                ((c++-mode c-mode) . ,(eglot-alternatives
+                                                       '("clangd" "ccls")))
                                 (((caml-mode :language-id "ocaml")
                                   (tuareg-mode :language-id "ocaml") reason-mode)
                                  . ("ocamllsp"))