emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/eglot 1506172 10/11: Close #17: New eglot-ensure to put


From: João Távora
Subject: [elpa] externals/eglot 1506172 10/11: Close #17: New eglot-ensure to put in a major-mode's hook
Date: Sun, 10 Jun 2018 09:57:29 -0400 (EDT)

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

    Close #17: New  eglot-ensure to put in a major-mode's hook
    
    * eglot.el (Commentary): Mention eglo-ensure.
    (eglot--connect): Rearrange args.
    (eglot--guess-contact): Rename from eglot--interactive.
    (eglot): Use eglot--guess-contact.
    (eglot, eglot-reconnect): Rearrange call to eglot--connect.
    (eglot-ensure): New command to put in mode hook.
    
    * eglot-tests.el (eglot--tests-connect): New helper.
    (auto-detect-running-server, auto-reconnect, rls-watches-files)
    (rls-basic-diagnostics, rls-hover-after-edit, rls-rename)
    (basic-completions, hover-after-completions): Use it.
---
 eglot-tests.el | 19 ++++++++------
 eglot.el       | 82 +++++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 69 insertions(+), 32 deletions(-)

diff --git a/eglot-tests.el b/eglot-tests.el
index 7e28a04..8238ecc 100644
--- a/eglot-tests.el
+++ b/eglot-tests.el
@@ -181,6 +181,9 @@ Pass TIMEOUT to `eglot--with-timeout'."
   (define-derived-mode rust-mode prog-mode "Rust"))
 (add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
 
+(defun eglot--tests-connect ()
+  (apply #'eglot--connect (eglot--guess-contact)))
+
 (ert-deftest auto-detect-running-server ()
   "Visit a file and M-x eglot, then visit a neighbour. "
   (skip-unless (executable-find "rls"))
@@ -192,7 +195,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
       (eglot--with-timeout 2
         (with-current-buffer
             (eglot--find-file-noselect "project/coiso.rs")
-          (should (setq server (apply #'eglot (eglot--interactive))))
+          (should (setq server (eglot--tests-connect)))
           (should (eglot--current-server)))
         (with-current-buffer
             (eglot--find-file-noselect "project/merdix.rs")
@@ -212,7 +215,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
       (eglot--with-timeout 3
         (with-current-buffer
             (eglot--find-file-noselect "project/coiso.rs")
-          (should (setq server (apply #'eglot (eglot--interactive))))
+          (should (setq server (eglot--tests-connect)))
           ;; In 1.2 seconds > `eglot-autoreconnect' kill servers. We
           ;; should have a automatic reconnection.
           (run-with-timer 1.2 nil (lambda () (delete-process
@@ -246,7 +249,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
                             :client-notifications c-notifs
                             :client-replies c-replies
                             )
-            (should (apply #'eglot (eglot--interactive)))
+            (should (eglot--tests-connect))
             (let (register-id)
               (eglot--wait-for (s-requests 1)
                   (&key id method &allow-other-keys)
@@ -276,7 +279,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
           (eglot--find-file-noselect "diag-project/main.rs")
         (should (zerop (shell-command "cargo init")))
         (eglot--sniffing (:server-notifications s-notifs)
-          (apply #'eglot (eglot--interactive))
+          (eglot--tests-connect)
           (eglot--wait-for (s-notifs 1)
               (&key _id method &allow-other-keys)
             (string= method "textDocument/publishDiagnostics"))
@@ -306,7 +309,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
                           :client-replies c-replies
                           :client-requests c-reqs
                           )
-          (apply #'eglot (eglot--interactive))
+          (eglot--tests-connect)
           (goto-char (point-min))
           (search-forward "return te")
           (insert "st")
@@ -344,7 +347,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
                           :client-replies c-replies
                           :client-requests c-reqs
                           )
-          (apply #'eglot (eglot--interactive))
+          (eglot--tests-connect)
           (goto-char (point-min)) (search-forward "return te")
           (eglot-rename "bla")
           (should (equal (buffer-string) "fn test() -> i32 { let bla=3; return 
bla; }")))))))
@@ -357,7 +360,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
     (eglot--with-timeout 4
       (with-current-buffer
           (eglot--find-file-noselect "project/something.py")
-        (should (apply #'eglot (eglot--interactive)))
+        (should (eglot--tests-connect))
         (goto-char (point-max))
         (completion-at-point)
         (should (looking-back "sys.exit"))))))
@@ -370,7 +373,7 @@ Pass TIMEOUT to `eglot--with-timeout'."
     (eglot--with-timeout 4
       (with-current-buffer
           (eglot--find-file-noselect "project/something.py")
-        (should (apply #'eglot (eglot--interactive)))
+        (should (eglot--tests-connect))
         (goto-char (point-max))
         (setq eldoc-last-message nil)
         (completion-at-point)
diff --git a/eglot.el b/eglot.el
index 4d3a469..47352f7 100644
--- a/eglot.el
+++ b/eglot.el
@@ -46,6 +46,13 @@
 ;; To "unmanage" these buffers, shutdown the server with M-x
 ;; eglot-shutdown.
 ;;
+;; You can also do:
+;;
+;;   (add-hook 'foo-mode-hook 'eglot-ensure)
+;;
+;; To attempt to start an eglot session automatically everytime a
+;; foo-mode buffer is visited.
+;;
 ;;; Code:
 
 (require 'json)
@@ -279,7 +286,7 @@ CONTACT is in `eglot'.  Returns a process object."
 
 (defvar eglot-connect-hook nil "Hook run after connecting in 
`eglot--connect'.")
 
-(defun eglot--connect (project managed-major-mode contact server-class)
+(defun eglot--connect (managed-major-mode project server-class contact)
   "Connect for PROJECT, MANAGED-MAJOR-MODE and CONTACT.
 INTERACTIVE is t if inside interactive call.  Return an object of
 class SERVER-CLASS."
@@ -331,8 +338,10 @@ class SERVER-CLASS."
 (defvar eglot--command-history nil
   "History of COMMAND arguments to `eglot'.")
 
-(defun eglot--interactive ()
-  "Helper for `eglot'."
+(defun eglot--guess-contact (&optional interactive)
+  "Helper for `eglot'.
+Return (MANAGED-MODE PROJECT CONTACT CLASS).
+If INTERACTIVE, maybe prompt user."
   (let* ((guessed-mode (if buffer-file-name major-mode))
          (managed-mode
           (cond
@@ -349,17 +358,20 @@ class SERVER-CLASS."
          (class (and (consp guess) (symbolp (car guess))
                      (prog1 (car guess) (setq guess (cdr guess)))))
          (program (and (listp guess) (stringp (car guess)) (car guess)))
-         (base-prompt "[eglot] Enter program to execute (or <host>:<port>): ")
+         (base-prompt
+          (and interactive
+               "[eglot] Enter program to execute (or <host>:<port>): "))
          (prompt
-          (cond (current-prefix-arg base-prompt)
-                ((null guess)
-                 (format "[eglot] Sorry, couldn't guess for `%s'\n%s!"
-                         managed-mode base-prompt))
-                ((and program (not (executable-find program)))
-                 (concat (format "[eglot] I guess you want to run `%s'"
-                                 (combine-and-quote-strings guess))
-                         (format ", but I can't find `%s' in PATH!" program)
-                         "\n" base-prompt))))
+          (and base-prompt
+               (cond (current-prefix-arg base-prompt)
+                     ((null guess)
+                      (format "[eglot] Sorry, couldn't guess for `%s'!\n%s"
+                              managed-mode base-prompt))
+                     ((and program (not (executable-find program)))
+                      (concat (format "[eglot] I guess you want to run `%s'"
+                                      (combine-and-quote-strings guess))
+                              (format ", but I can't find `%s' in PATH!" 
program)
+                              "\n" base-prompt)))))
          (contact
           (if prompt
               (let ((s (read-shell-command
@@ -371,10 +383,10 @@ class SERVER-CLASS."
                     (list (match-string 1 s) (string-to-number (match-string 2 
s)))
                   (split-string-and-unquote s)))
             guess)))
-    (list managed-mode project contact class t)))
+    (list managed-mode project class contact)))
 
 ;;;###autoload
-(defun eglot (managed-major-mode project command server-class
+(defun eglot (managed-major-mode project server-class command
                                  &optional interactive)
   "Manage a project with a Language Server Protocol (LSP) server.
 
@@ -405,7 +417,7 @@ SERVER-CLASS is a symbol naming a class that must inherit 
from
 `eglot-server', or nil to use the default server class.
 
 INTERACTIVE is t if called interactively."
-  (interactive (eglot--interactive))
+  (interactive (append (eglot--guess-contact t) '(t)))
   (let ((current-server (eglot--current-server)))
     (if (and current-server
              (process-live-p (eglot--process current-server))
@@ -415,10 +427,10 @@ INTERACTIVE is t if called interactively."
       (when (and current-server
                  (process-live-p (eglot--process current-server)))
         (ignore-errors (eglot-shutdown current-server)))
-      (let ((server (eglot--connect project
-                                    managed-major-mode
-                                    command
-                                    server-class)))
+      (let ((server (eglot--connect managed-major-mode
+                                    project
+                                    server-class
+                                    command)))
         (eglot--message "Connected! Server `%s' now \
 managing `%s' buffers in project `%s'."
                         (eglot--name server) managed-major-mode
@@ -431,12 +443,34 @@ INTERACTIVE is t if called interactively."
   (interactive (list (eglot--current-server-or-lose) t))
   (when (process-live-p (eglot--process server))
     (ignore-errors (eglot-shutdown server interactive)))
-  (eglot--connect (eglot--project server)
-                  (eglot--major-mode server)
-                  (eglot--contact server)
-                  (eieio-object-class server))
+  (eglot--connect (eglot--major-mode server)
+                  (eglot--project server)
+                  (eieio-object-class server)
+                  (eglot--contact server))
   (eglot--message "Reconnected!"))
 
+(defvar eglot--managed-mode) ;forward decl
+
+(defun eglot-ensure ()
+  "Start Eglot session for current buffer if there isn't one."
+  (let ((buffer (current-buffer)))
+    (cl-labels
+        ((maybe-connect
+          ()
+          (remove-hook 'post-command-hook #'maybe-connect nil)
+          (eglot--with-live-buffer buffer
+            (if eglot--managed-mode
+                (eglot--message "%s is already managed by existing `%s'"
+                                buffer
+                                (eglot--name (eglot--current-server)))
+              (let ((server (apply #'eglot--connect (eglot--guess-contact))))
+                (eglot--message
+                 "Automatically started `%s' to manage `%s' buffers in project 
`%s'"
+                 (eglot--name server)
+                 major-mode
+                 (eglot--project-nickname server)))))))
+      (add-hook 'post-command-hook #'maybe-connect 'append nil))))
+
 (defun eglot--process-sentinel (proc change)
   "Called when PROC undergoes CHANGE."
   (let ((server (process-get proc 'eglot-server)))



reply via email to

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