From 8def9a619605708a1b8783093f031c054b3ad585 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Fri, 8 Jun 2018 18:37:54 +0100 Subject: [PATCH] Simplify jsonrpc connection shutdown * eglot.el (eglot--process): Delete. (eglot-shutdown): Use jsonrpc-shutdown. (eglot--on-shutdown): Simplify. (eglot-reconnect): Simplify. (eglot--connect): Simplify. * jsonrpc-tests.el (jsonrpc--with-emacsrpc-fixture): Simplify. * jsonrpc.el (jsonrpc-process-type, jsonrpc-running-p) (jsonrpc-shutdown): New methods. * eglot-tests.el (auto-reconnect): Use jsonrpc--process. (eglot--call-with-dirs-and-files): Use jsonrpc-running-p. --- lisp/progmodes/eglot.el | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index e1592ab3b16..cdb1c5b097c 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -203,11 +203,6 @@ lasted more than that many seconds." (defvar eglot--servers-by-project (make-hash-table :test #'equal) "Keys are projects. Values are lists of processes.") -;; HACK: Do something to fix this in the jsonrpc API or here, but in -;; the meantime concentrate the hack here. -(defalias 'eglot--process 'jsonrpc--process - "An abuse of `jsonrpc--process', a jsonrpc.el internal.") - (defun eglot-shutdown (server &optional _interactive) "Politely ask SERVER to quit. Forcefully quit it if it doesn't respond. Don't leave this @@ -218,16 +213,15 @@ function with the server still running." (progn (setf (eglot--shutdown-requested server) t) (jsonrpc-request server :shutdown nil :timeout 3) - ;; this one is supposed to always fail, hence ignore-errors + ;; this one is supposed to always fail, because it asks the + ;; server to exit itself. Hence ignore-errors. (ignore-errors (jsonrpc-request server :exit nil :timeout 1))) ;; Turn off `eglot--managed-mode' where appropriate. (dolist (buffer (eglot--managed-buffers server)) (with-current-buffer buffer (eglot--managed-mode-onoff server -1))) - (while (progn (accept-process-output nil 0.1) - (not (eq (eglot--shutdown-requested server) :sentinel-done))) - (eglot--warn "Sentinel for %s still hasn't run, brutally deleting it!" - (eglot--process server)) - (delete-process (eglot--process server))))) + ;; Now ask jsonrpc.el to shutdown server (which in normal + ;; conditions should return immediately). + (jsonrpc-shutdown server))) (defun eglot--on-shutdown (server) "Called by jsonrpc.el when SERVER is already dead." @@ -243,7 +237,7 @@ function with the server still running." (delq server (gethash (eglot--project server) eglot--servers-by-project))) (cond ((eglot--shutdown-requested server) - (setf (eglot--shutdown-requested server) :sentinel-done)) + t) ((not (eglot--inhibit-autoreconnect server)) (eglot--warn "Reconnecting after unexpected server exit.") (eglot-reconnect server)) @@ -337,8 +331,7 @@ INTERACTIVE is t if called interactively." (let* ((nickname (file-name-base (directory-file-name (car (project-roots project))))) (current-server (jsonrpc-current-connection)) - (live-p (and current-server - (process-live-p (eglot--process current-server))))) + (live-p (and current-server (jsonrpc-running-p current-server)))) (if (and live-p interactive (y-or-n-p "[eglot] Live process found, reconnect instead? ")) @@ -360,7 +353,7 @@ managing `%s' buffers in project `%s'." "Reconnect to SERVER. INTERACTIVE is t if called interactively." (interactive (list (jsonrpc-current-connection-or-lose) t)) - (when (process-live-p (eglot--process server)) + (when (jsonrpc-running-p server) (ignore-errors (eglot-shutdown server interactive))) (eglot--connect (eglot--project server) (eglot--major-mode server) @@ -421,9 +414,7 @@ appeases checkdoc, that's all." (jsonrpc-request server :initialize - (list :processId (unless (eq (process-type - (eglot--process server)) - 'network) + (list :processId (unless (eq (jsonrpc-process-type server) 'network) (emacs-pid)) :rootPath (expand-file-name (car (project-roots project))) @@ -446,7 +437,7 @@ appeases checkdoc, that's all." (setf (eglot--inhibit-autoreconnect server) (null eglot-autoreconnect))))))) (setq success server)) - (unless (or success (not (process-live-p (eglot--process server)))) + (when (and (not success) (jsonrpc-running-p server)) (eglot-shutdown server))))) -- 2.39.2