Skip to content

Commit

Permalink
Close #901: Easier initializationOptions in eglot-server-programs
Browse files Browse the repository at this point in the history
Per #845.

* NEWS.md: Update.

* eglot.el (eglot-server-programs): Document new syntax.
(eglot-initialization-options): Can use initializationOptions from
server's saved initargs.
(eglot--connect): Allow a plist to be appended to a server
contact.
  • Loading branch information
joaotavora committed Mar 29, 2022
1 parent cf5e4f8 commit 83a61f6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# (upcoming)

##### Easier to use LSP initialize.initializationOptions ([#901][github#901], [#845][github#845])
In `eglot-server-programs` a plist may be appended to the usual list
of strings passed as command line arguments. The value of its
`:initializationOptions` key constructs the corresponding LSP JSON
object. This may be easier than creating a `defclass` for a specific
server and specializing `eglot-initialization-options` to that class.

##### Support on-type-formatting ([#899][github#899])

##### Provide basic workspace-folders support ([#893][github#893])
Expand Down Expand Up @@ -356,5 +363,7 @@ and now said bunch of references-->
[github#803]: https://github.com/joaotavora/eglot/issues/803
[github#810]: https://github.com/joaotavora/eglot/issues/810
[github#813]: https://github.com/joaotavora/eglot/issues/813
[github#845]: https://github.com/joaotavora/eglot/issues/845
[github#893]: https://github.com/joaotavora/eglot/issues/893
[github#899]: https://github.com/joaotavora/eglot/issues/899
[github#901]: https://github.com/joaotavora/eglot/issues/901
51 changes: 33 additions & 18 deletions eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,24 @@ CONTACT can be:
PROGRAM is called with ARGS and is expected to serve LSP requests
over the standard input/output channels.
* A list (PROGRAM [ARGS...] :initializationOptions OPTIONS),
whereupon PROGRAM is called with ARGS as in the first option,
and the LSP \"initializationOptions\" JSON object is
constructed from OPTIONS. If OPTIONS is a unary function, it
is called with the server instance and should return a JSON
object.
* A list (HOST PORT [TCP-ARGS...]) where HOST is a string and
PORT is a positive integer for connecting to a server via TCP.
Remaining ARGS are passed to `open-network-stream' for
upgrading the connection with encryption or other capabilities.
* A list (PROGRAM [ARGS...] :autoport [MOREARGS...]), whereupon a
combination of the two previous options is used. First, an
attempt is made to find an available server port, then PROGRAM
is launched with ARGS; the `:autoport' keyword substituted for
that number; and MOREARGS. Eglot then attempts to establish a
TCP connection to that port number on the localhost.
combination of previous options is used. First, an attempt is
made to find an available server port, then PROGRAM is launched
with ARGS; the `:autoport' keyword substituted for that number;
and MOREARGS. Eglot then attempts to establish a TCP
connection to that port number on the localhost.
* A cons (CLASS-NAME . INITARGS) where CLASS-NAME is a symbol
designating a subclass of `eglot-lsp-server', for representing
Expand Down Expand Up @@ -627,7 +634,11 @@ treated as in `eglot-dbind'."

(cl-defgeneric eglot-initialization-options (server)
"JSON object to send under `initializationOptions'."
(:method (_s) eglot--{})) ; blank default
(:method (s)
(let ((probe (plist-get (eglot--saved-initargs s) :initializationOptions)))
(cond ((functionp probe) (funcall probe s))
(probe)
(t eglot--{})))))

(cl-defgeneric eglot-register-capability (server method id &rest params)
"Ask SERVER to register capability METHOD marked with ID."
Expand Down Expand Up @@ -1116,18 +1127,22 @@ This docstring appeases checkdoc, that's all."
(setq autostart-inferior-process inferior)
connection))))
((stringp (car contact))
`(:process
,(lambda ()
(let ((default-directory default-directory))
(make-process
:name readable-name
:command (setq server-info (eglot--cmd contact))
:connection-type 'pipe
:coding 'utf-8-emacs-unix
:noquery t
:stderr (get-buffer-create
(format "*%s stderr*" readable-name))
:file-handler t)))))))
(let* ((probe (cl-position-if #'keywordp contact))
(more-initargs (and probe (cl-subseq contact probe)))
(contact (cl-subseq contact 0 probe)))
`(:process
,(lambda ()
(let ((default-directory default-directory))
(make-process
:name readable-name
:command (setq server-info (eglot--cmd contact))
:connection-type 'pipe
:coding 'utf-8-emacs-unix
:noquery t
:stderr (get-buffer-create
(format "*%s stderr*" readable-name))
:file-handler t)))
,@more-initargs)))))
(spread (lambda (fn) (lambda (server method params)
(let ((eglot--cached-server server))
(apply fn server method (append params nil))))))
Expand Down

0 comments on commit 83a61f6

Please sign in to comment.