[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 1.2 c2e05a3 030/101: Close #68: Implement asynchronous server con
From: |
Christian Johansson |
Subject: |
[elpa] 1.2 c2e05a3 030/101: Close #68: Implement asynchronous server connection |
Date: |
Thu, 29 Apr 2021 15:08:55 -0400 (EDT) |
tag: 1.2
commit c2e05a3eb987ab7606a79da1cdca93bf73500d7e
Author: João Távora <joaotavora@gmail.com>
Commit: João Távora <joaotavora@gmail.com>
Close #68: Implement asynchronous server connection
A new defcustom eglot-sync-connect controls this feature. If it is t,
eglot should behave like previously, waiting synchronously for a
connection to be established, with the exception that there is now a
non-nil timeout set to eglot-connect-timeout, which defaults to 30
seconds.
eglot-connect is now considerably more complicated as it replicates
most of the work that jsonrpc-request does vis-a-vis handling errors,
timeouts and user quits..
* eglot-tests.el
(eglot--call-with-dirs-and-files): Simplify cleanup logic.
(slow-sync-connection-wait)
(slow-sync-connection-intime, slow-async-connection)
(slow-sync-error): New tests.
* eglot.el (eglot-sync-connect): New defcustom.
(eglot-ensure, eglot): Simplify.
(eglot--connect): Honour eglot-sync-connect. Complicate
considerably.
(eglot-connect-timeout): New defcustom.
(Package-requires): Require jsonrpc 1.0.6
---
eglot-tests.el | 64 ++++++++++++++++++++++----
eglot.el | 138 +++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 145 insertions(+), 57 deletions(-)
diff --git a/eglot-tests.el b/eglot-tests.el
index 33d96a5..0598d9e 100644
--- a/eglot-tests.el
+++ b/eglot-tests.el
@@ -67,15 +67,10 @@
(setq cleanup-events-et-cetera-p t))
(unwind-protect
(let ((eglot-autoreconnect nil))
- (mapc (lambda (server) (eglot-shutdown server nil nil t))
+ (mapc (lambda (server)
+ (eglot-shutdown
+ server nil nil (not cleanup-events-et-cetera-p)))
(cl-remove-if-not #'jsonrpc-running-p new-servers)))
- (when cleanup-events-et-cetera-p
- (cl-loop for serv in new-servers
- do
- (kill-buffer (process-get (jsonrpc--process serv)
- 'jsonrpc-stderr))
- (kill-buffer (jsonrpc--events-buffer serv))
- (kill-buffer (process-buffer (jsonrpc--process serv)))))
(eglot--message
"Killing project buffers %s, deleting %s, killing server %s"
(mapconcat #'buffer-name new-buffers ", ")
@@ -456,6 +451,59 @@ Pass TIMEOUT to `eglot--with-timeout'."
(should (eq server (eglot--current-server)))))
(setq python-mode-hook saved-python-mode-hook)))))
+(ert-deftest slow-sync-connection-wait ()
+ "Connect with `eglot-sync-connect' set to t."
+ (skip-unless (executable-find "pyls"))
+ (eglot--with-dirs-and-files
+ '(("project" . (("something.py" . "import sys\nsys.exi"))))
+ (with-current-buffer
+ (eglot--find-file-noselect "project/something.py")
+ (let ((eglot-sync-connect t)
+ (eglot-server-programs
+ `((python-mode . ("sh" "-c" "sleep 1 && pyls")))))
+ (should (eglot--tests-connect 3))))))
+
+(ert-deftest slow-sync-connection-intime ()
+ "Connect synchronously with `eglot-sync-connect' set to 2."
+ (skip-unless (executable-find "pyls"))
+ (eglot--with-dirs-and-files
+ '(("project" . (("something.py" . "import sys\nsys.exi"))))
+ (with-current-buffer
+ (eglot--find-file-noselect "project/something.py")
+ (let ((eglot-sync-connect 2)
+ (eglot-server-programs
+ `((python-mode . ("sh" "-c" "sleep 1 && pyls")))))
+ (should (eglot--tests-connect 3))))))
+
+(ert-deftest slow-async-connection ()
+ "Connect asynchronously with `eglot-sync-connect' set to 2."
+ (skip-unless (executable-find "pyls"))
+ (eglot--with-dirs-and-files
+ '(("project" . (("something.py" . "import sys\nsys.exi"))))
+ (with-current-buffer
+ (eglot--find-file-noselect "project/something.py")
+ (let ((eglot-sync-connect 1)
+ (eglot-server-programs
+ `((python-mode . ("sh" "-c" "sleep 2 && pyls")))))
+ (should-not (apply #'eglot--connect (eglot--guess-contact)))
+ (eglot--with-timeout 3
+ (while (not (eglot--current-server))
+ (accept-process-output nil 0.2))
+ (should (eglot--current-server)))))))
+
+(ert-deftest slow-sync-timeout ()
+ "Failed attempt at connection synchronously."
+ (skip-unless (executable-find "pyls"))
+ (eglot--with-dirs-and-files
+ '(("project" . (("something.py" . "import sys\nsys.exi"))))
+ (with-current-buffer
+ (eglot--find-file-noselect "project/something.py")
+ (let ((eglot-sync-connect t)
+ (eglot-connect-timeout 1)
+ (eglot-server-programs
+ `((python-mode . ("sh" "-c" "sleep 2 && pyls")))))
+ (should-error (apply #'eglot--connect (eglot--guess-contact)))))))
+
(provide 'eglot-tests)
;;; eglot-tests.el ends here
diff --git a/eglot.el b/eglot.el
index 70a725c..ac529dc 100644
--- a/eglot.el
+++ b/eglot.el
@@ -7,7 +7,7 @@
;; Maintainer: João Távora <joaotavora@gmail.com>
;; URL: https://github.com/joaotavora/eglot
;; Keywords: convenience, languages
-;; Package-Requires: ((emacs "26.1") (jsonrpc "1.0.5"))
+;; Package-Requires: ((emacs "26.1") (jsonrpc "1.0.6"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -140,6 +140,19 @@ lasted more than that many seconds."
:type '(choice (boolean :tag "Whether to inhibit autoreconnection")
(integer :tag "Number of seconds")))
+(defcustom eglot-connect-timeout 30
+ "Number of seconds before timing out LSP connection attempts.
+If nil, never time out."
+ :type 'number)
+
+(defcustom eglot-sync-connect 3
+ "Control blocking of LSP connection attempts.
+If t, block for `eglot-connect-timeout' seconds. A positive
+integer number means block for that many seconds, and then wait
+for the connection in the background. nil has the same meaning
+as 0, i.e. don't block at all."
+ :type '(choice (boolean :tag "Whether to inhibit autoreconnection")
+ (integer :tag "Number of seconds")))
;;; API (WORK-IN-PROGRESS!)
;;;
@@ -259,9 +272,7 @@ running."
;; Now ask jsonrpc.el to shut down the server (which under normal
;; conditions should return immediately).
(jsonrpc-shutdown server (not preserve-buffers))
- (unless preserve-buffers
- (mapc #'kill-buffer
- `(,(jsonrpc-events-buffer server) ,(jsonrpc-stderr-buffer
server))))))
+ (unless preserve-buffers (kill-buffer (jsonrpc-events-buffer server)))))
(defun eglot--on-shutdown (server)
"Called by jsonrpc.el when SERVER is already dead."
@@ -399,15 +410,7 @@ INTERACTIVE is t if called interactively."
(y-or-n-p "[eglot] Live process found, reconnect instead? "))
(eglot-reconnect current-server interactive)
(when live-p (ignore-errors (eglot-shutdown current-server)))
- (let ((server (eglot--connect managed-major-mode
- project
- class
- contact)))
- (eglot--message "Connected! Process `%s' now \
-managing `%s' buffers in project `%s'."
- (jsonrpc-name server) managed-major-mode
- (eglot--project-nickname server))
- server))))
+ (eglot--connect managed-major-mode project class contact))))
(defun eglot-reconnect (server &optional interactive)
"Reconnect to SERVER.
@@ -432,12 +435,7 @@ INTERACTIVE is t if called interactively."
(remove-hook 'post-command-hook #'maybe-connect nil)
(eglot--with-live-buffer buffer
(unless eglot--managed-mode
- (let ((server (apply #'eglot--connect (eglot--guess-contact))))
- (eglot--message
- "Automatically started `%s' to manage `%s' buffers in project
`%s'"
- (jsonrpc-name server)
- major-mode
- (eglot--project-nickname server)))))))
+ (apply #'eglot--connect (eglot--guess-contact))))))
(when buffer-file-name
(add-hook 'post-command-hook #'maybe-connect 'append nil)))))
@@ -508,42 +506,84 @@ This docstring appeases checkdoc, that's all."
:request-dispatcher (funcall spread #'eglot-handle-request)
:on-shutdown #'eglot--on-shutdown
initargs))
- success)
+ (cancelled nil)
+ (tag (make-symbol "connected-catch-tag")))
(setf (eglot--saved-initargs server) initargs)
(setf (eglot--project server) project)
(setf (eglot--project-nickname server) nickname)
(setf (eglot--major-mode server) managed-major-mode)
(setf (eglot--inferior-process server) autostart-inferior-process)
- (push server (gethash project eglot--servers-by-project))
- (run-hook-with-args 'eglot-connect-hook server)
+ ;; Now start the handshake. To honour `eglot-sync-connect'
+ ;; maybe-sync-maybe-async semantics we use `jsonrpc-async-request'
+ ;; and mimic most of `jsonrpc-request'.
(unwind-protect
- (cl-destructuring-bind (&key capabilities)
- (jsonrpc-request
- server
- :initialize
- (list :processId (unless (eq (jsonrpc-process-type server)
'network)
- (emacs-pid))
- :rootPath (expand-file-name default-directory)
- :rootUri (eglot--path-to-uri default-directory)
- :initializationOptions (eglot-initialization-options server)
- :capabilities (eglot-client-capabilities server)))
- (setf (eglot--capabilities server) capabilities)
- (dolist (buffer (buffer-list))
- (with-current-buffer buffer
- (eglot--maybe-activate-editing-mode server)))
- (jsonrpc-notify server :initialized `(:__dummy__ t))
- (run-hook-with-args 'eglot-server-initialized-hook server)
- (setf (eglot--inhibit-autoreconnect server)
- (cond
- ((booleanp eglot-autoreconnect) (not eglot-autoreconnect))
- ((cl-plusp eglot-autoreconnect)
- (run-with-timer eglot-autoreconnect nil
- (lambda ()
- (setf (eglot--inhibit-autoreconnect server)
- (null eglot-autoreconnect)))))))
- (setq success server))
- (when (and (not success) (jsonrpc-running-p server))
- (eglot-shutdown server)))))
+ (condition-case _quit
+ (let ((retval
+ (catch tag
+ (jsonrpc-async-request
+ server
+ :initialize
+ (list :processId (unless (eq (jsonrpc-process-type
server)
+ 'network)
+ (emacs-pid))
+ :rootPath (expand-file-name default-directory)
+ :rootUri (eglot--path-to-uri default-directory)
+ :initializationOptions
(eglot-initialization-options
+ server)
+ :capabilities (eglot-client-capabilities server))
+ :success-fn
+ (jsonrpc-lambda (&key capabilities)
+ (unless cancelled
+ (push server
+ (gethash project eglot--servers-by-project))
+ (setf (eglot--capabilities server) capabilities)
+ (dolist (buffer (buffer-list))
+ (with-current-buffer buffer
+ (eglot--maybe-activate-editing-mode server)))
+ (jsonrpc-notify server :initialized `(:__dummy__ t))
+ (setf (eglot--inhibit-autoreconnect server)
+ (cond
+ ((booleanp eglot-autoreconnect)
+ (not eglot-autoreconnect))
+ ((cl-plusp eglot-autoreconnect)
+ (run-with-timer
+ eglot-autoreconnect nil
+ (lambda ()
+ (setf (eglot--inhibit-autoreconnect
server)
+ (null eglot-autoreconnect)))))))
+ (run-hook-with-args 'eglot-connect-hook server)
+ (run-hook-with-args 'eglot-server-initialized-hook
server)
+ (eglot--message
+ "Connected! Server `%s' now managing `%s' buffers \
+in project `%s'."
+ (jsonrpc-name server) managed-major-mode
+ (eglot--project-nickname server))
+ (when tag (throw tag t))))
+ :timeout eglot-connect-timeout
+ :error-fn (jsonrpc-lambda (&key code message _data)
+ (unless cancelled
+ (jsonrpc-shutdown server)
+ (let ((msg (format "%s: %s" code message)))
+ (if tag (throw tag `(error . ,msg))
+ (eglot--error msg)))))
+ :timeout-fn (lambda ()
+ (unless cancelled
+ (jsonrpc-shutdown server)
+ (let ((msg (format "Timed out")))
+ (if tag (throw tag `(error . ,msg))
+ (eglot--error msg))))))
+ (cond ((numberp eglot-sync-connect)
+ (accept-process-output nil eglot-sync-connect))
+ (eglot-sync-connect
+ (while t (accept-process-output nil 30)))))))
+ (pcase retval
+ (`(error . ,msg) (eglot--error msg))
+ (`nil (eglot--message "Waiting in background for server `%s'"
+ (jsonrpc-name server))
+ nil)
+ (_ server)))
+ (quit (jsonrpc-shutdown server) (setq cancelled 'quit)))
+ (setq tag nil))))
(defun eglot--inferior-bootstrap (name contact &optional connect-args)
"Use CONTACT to start a server, then connect to it.
- [elpa] 1.2 f4f93d3 014/101: Fix placement of diagnostics with same start and end positions, (continued)
- [elpa] 1.2 f4f93d3 014/101: Fix placement of diagnostics with same start and end positions, Christian Johansson, 2021/04/29
- [elpa] 1.2 01ec85f 011/101: Implement TextDocument/rangeFormatting, Christian Johansson, 2021/04/29
- [elpa] 1.2 d1cfc9e 008/101: Work around Emacs bugs 32237, 32278 (#53), Christian Johansson, 2021/04/29
- [elpa] 1.2 e33fadb 012/101: * eglot.el (eglot-client-capabilities): Fix a typo., Christian Johansson, 2021/04/29
- [elpa] 1.2 ee372b4 022/101: * eglot.el (advice-add jsonrpc-request): Add &allow-other-keys, Christian Johansson, 2021/04/29
- [elpa] 1.2 9bf88ee 031/101: Allow tests to be run with custom jsonrpc.el, Christian Johansson, 2021/04/29
- [elpa] 1.2 cf3376a 034/101: Close #64: handle edits to same position in the correct order, Christian Johansson, 2021/04/29
- [elpa] 1.2 5e30066 032/101: Update README.md, Christian Johansson, 2021/04/29
- [elpa] 1.2 afa5439 027/101: Kill server's output and events buffers from eglot-shutdown (#66), Christian Johansson, 2021/04/29
- [elpa] 1.2 d6b49d8 028/101: * README.md (Build Status): Show status for master, Christian Johansson, 2021/04/29
- [elpa] 1.2 c2e05a3 030/101: Close #68: Implement asynchronous server connection,
Christian Johansson <=
- [elpa] 1.2 3432d21 037/101: Per #74: Fix eglot-capabilities when querying for multiple features, Christian Johansson, 2021/04/29
- [elpa] 1.2 6eac51f 061/101: Sort references and definitions by line number, Christian Johansson, 2021/04/29
- [elpa] 1.2 88a4f8c 044/101: Actually add snippet example gif referenced in README.md, Christian Johansson, 2021/04/29
- [elpa] 1.2 29d641b 066/101: Handle case when project was not found in eclipse.jdt.ls contact, Christian Johansson, 2021/04/29
- [elpa] 1.2 2971683 075/101: Rework test macros for hopefully more stable testing, Christian Johansson, 2021/04/29
- [elpa] 1.2 3b3c1db 036/101: Close #73: Prompt for server in interactive eglot-shutdown, Christian Johansson, 2021/04/29
- [elpa] 1.2 a1cc0ad 043/101: * README.md (Obligatory animated gif section): Add snippet gif., Christian Johansson, 2021/04/29
- [elpa] 1.2 3532bd0 064/101: Per #63: Add support for eclipse.jdt.ls server, Christian Johansson, 2021/04/29
- [elpa] 1.2 0ef245f 051/101: Fix serious breakage introduced by #93, Christian Johansson, 2021/04/29
- [elpa] 1.2 71012bc 067/101: Add tests for eclipse.jdt.ls connection, Christian Johansson, 2021/04/29