[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/eglot 146108a 15/54: Fix #342: Provide stable eglot-cur

From: João Távora
Subject: [elpa] externals/eglot 146108a 15/54: Fix #342: Provide stable eglot-current-server helper
Date: Thu, 16 Apr 2020 05:31:47 -0400 (EDT)

branch: externals/eglot
commit 146108af7a56bef835556bc32a4dc85e9d83f6c0
Author: João Távora <address@hidden>
Commit: João Távora <address@hidden>

    Fix #342: Provide stable eglot-current-server helper
    It's better if eglot--current-server is removed, since it was being
    abused by other packages, and has side effects.  The only place where
    it was really needed was eglot--maybe-activate-editing-mode, so the
    find-and-cache logic has been moved there.  All other places that can
    handle a nil server now use eglot-current-server, the external
    * eglot.el (eglot-shutdown, eglot, eglot--read-server)
    (eglot--mode-line-format): Use eglot-current-server.
    (eglot--connect): Update comment.
    (eglot--current-server): Remove.
    (eglot-current-server): New helper.
    (eglot--maybe-activate-editing-mode): find and cache the server here.
    * eglot-tests.el (auto-detect-running-server)
    (auto-shutdown, auto-reconnect, eglot-ensure)
    (slow-async-connection): Use eglot-current-server.
 eglot-tests.el | 22 +++++++++++-----------
 eglot.el       | 38 ++++++++++++++++++++------------------
 2 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/eglot-tests.el b/eglot-tests.el
index 3d0d432..29770c6 100644
--- a/eglot-tests.el
+++ b/eglot-tests.el
@@ -284,11 +284,11 @@ Pass TIMEOUT to `eglot--with-timeout'."
           (eglot--find-file-noselect "project/coiso.py")
         (should (setq server (eglot--tests-connect)))
-        (should (eglot--current-server)))
+        (should (eglot-current-server)))
           (eglot--find-file-noselect "project/merdix.py")
-        (should (eglot--current-server))
-        (should (eq (eglot--current-server) server)))
+        (should (eglot-current-server))
+        (should (eq (eglot-current-server) server)))
           (eglot--find-file-noselect "anotherproject/cena.py")
         (should-error (eglot--current-server-or-lose))))))
@@ -303,7 +303,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
           (setq buffer (eglot--find-file-noselect "project/coiso.py"))
         (should (setq server (eglot--tests-connect)))
-        (should (eglot--current-server))
+        (should (eglot-current-server))
         (let ((eglot-autoshutdown nil)) (kill-buffer buffer))
         (should (jsonrpc-running-p server))
         ;; re-find file...
@@ -327,13 +327,13 @@ Pass TIMEOUT to `eglot--with-timeout'."
         (run-with-timer 1.2 nil (lambda () (delete-process
                                             (jsonrpc--process server))))
         (while (jsonrpc-running-p server) (accept-process-output nil 0.5))
-        (should (eglot--current-server))
+        (should (eglot-current-server))
         ;; Now try again too quickly
-        (setq server (eglot--current-server))
+        (setq server (eglot-current-server))
         (let ((proc (jsonrpc--process server)))
           (run-with-timer 0.5 nil (lambda () (delete-process proc)))
           (while (process-live-p proc) (accept-process-output nil 0.5)))
-        (should (not (eglot--current-server)))))))
+        (should (not (eglot-current-server)))))))
 (ert-deftest rls-watches-files ()
   "Start RLS server.  Notify it when a critical file changes."
@@ -647,11 +647,11 @@ pyls prefers autopep over yafp, despite its README 
stating the contrary."
            '(find-file "project/foo.py"))
-        (should (setq server (eglot--current-server))))
+        (should (setq server (eglot-current-server))))
            '(find-file "project/bar.py"))
-        (should (eq server (eglot--current-server)))))))
+        (should (eq server (eglot-current-server)))))))
 (ert-deftest slow-sync-connection-wait ()
   "Connect with `eglot-sync-connect' set to t."
@@ -689,9 +689,9 @@ pyls prefers autopep over yafp, despite its README stating 
the contrary."
              `((python-mode . ("sh" "-c" "sleep 2 && pyls")))))
         (should-not (apply #'eglot--connect (eglot--guess-contact)))
         (eglot--with-timeout 3
-          (while (not (eglot--current-server))
+          (while (not (eglot-current-server))
             (accept-process-output nil 0.2))
-          (should (eglot--current-server)))))))
+          (should (eglot-current-server)))))))
 (ert-deftest slow-sync-timeout ()
   "Failed attempt at connection synchronously."
diff --git a/eglot.el b/eglot.el
index 7133426..281a293 100644
--- a/eglot.el
+++ b/eglot.el
@@ -576,7 +576,7 @@ If PRESERVE-BUFFERS is non-nil (interactively, when called 
with a
 prefix argument), do not kill events and output buffers of
 SERVER.  ."
   (interactive (list (eglot--read-server "Shutdown which server"
-                                         (eglot--current-server))
+                                         (eglot-current-server))
                      t nil current-prefix-arg))
   (eglot--message "Asking %s politely to terminate" (jsonrpc-name server))
@@ -726,7 +726,7 @@ described in `eglot-server-programs', which see.
 INTERACTIVE is t if called interactively."
   (interactive (append (eglot--guess-contact t) '(t)))
-  (let* ((current-server (eglot--current-server))
+  (let* ((current-server (eglot-current-server))
          (live-p (and current-server (jsonrpc-running-p current-server))))
     (if (and live-p
@@ -866,8 +866,9 @@ This docstring appeases checkdoc, that's all."
                             (with-current-buffer buffer
                               ;; No need to pass SERVER as an argument: it has
                               ;; been registered in 
-                              ;; so that it can be obtained from the function
-                              ;; `eglot--current-server' in any managed buffer.
+                              ;; so that it can be found (and cached) from
+                              ;; `eglot--maybe-activate-editing-mode' in any
+                              ;; managed buffer.
                           (setf (eglot--inhibit-autoreconnect server)
@@ -1159,7 +1160,7 @@ and just return it.  PROMPT shouldn't end with a question 
     (cond ((null servers)
            (eglot--error "No servers!"))
           ((or (cdr servers) (not dont-if-just-the-one))
-           (let* ((default (when-let ((current (eglot--current-server)))
+           (let* ((default (when-let ((current (eglot-current-server)))
                              (funcall name current)))
                   (read (completing-read
                          (if default
@@ -1276,20 +1277,13 @@ For example, to keep your Company customization use
 (defvar-local eglot--cached-current-server nil
   "A cached reference to the current EGLOT server.")
-(defun eglot--current-server ()
-  "Find and cache logical EGLOT server for current buffer."
-  (or
-   eglot--cached-current-server
-   (setq eglot--cached-current-server
-         (cl-find major-mode
-                  (gethash (or (project-current)
-                               `(transient . ,default-directory))
-                           eglot--servers-by-project)
-                  :key #'eglot--major-mode))))
+(defun eglot-current-server ()
+  "Return logical EGLOT server for current buffer, nil if none."
+  eglot--cached-current-server)
 (defun eglot--current-server-or-lose ()
   "Return current logical EGLOT server connection or error."
-  (or (eglot--current-server)
+  (or eglot--cached-current-server
       (jsonrpc-error "No current JSON-RPC connection")))
 (defvar-local eglot--unreported-diagnostics nil
@@ -1307,7 +1301,15 @@ If it is activated, also signal textDocument/didOpen."
   (unless eglot--managed-mode
     ;; Called when `revert-buffer-in-progress-p' is t but
     ;; `revert-buffer-preserve-modes' is nil.
-    (when (and buffer-file-name (eglot--current-server))
+    (when (and buffer-file-name
+               (or
+                eglot--cached-current-server
+                (setq eglot--cached-current-server
+                      (cl-find major-mode
+                               (gethash (or (project-current)
+                                            `(transient . ,default-directory))
+                                        eglot--servers-by-project)
+                               :key #'eglot--major-mode))))
       (setq eglot--unreported-diagnostics `(:just-opened . nil))
@@ -1354,7 +1356,7 @@ Uses THING, FACE, DEFS and PREPEND."
 (defun eglot--mode-line-format ()
   "Compose the EGLOT's mode-line."
-  (pcase-let* ((server (eglot--current-server))
+  (pcase-let* ((server (eglot-current-server))
                (nick (and server (eglot--project-nickname server)))
                (pending (and server (hash-table-count
                                      (jsonrpc--request-continuations server))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]