diff options
| author | João Távora | 2023-12-14 23:53:07 +0000 |
|---|---|---|
| committer | João Távora | 2023-12-14 23:53:13 +0000 |
| commit | a4feb79ad4c23fbe2a0741b2a6d931c9cd1dc263 (patch) | |
| tree | 688725776b418b0e9b772f66e330fc662ce6b125 | |
| parent | 9e24cde227a1bf2e1f0c005ca16b2a70e704ff5c (diff) | |
| download | emacs-a4feb79ad4c23fbe2a0741b2a6d931c9cd1dc263.tar.gz emacs-a4feb79ad4c23fbe2a0741b2a6d931c9cd1dc263.zip | |
Eglot: use new jsonrpc-autoport-bootstrap
* lisp/progmodes/eglot.el (eglot-lsp-server): Delete slot
inferior-process.
(eglot--on-shutdown): Simplify.
(eglot--inferior-bootstrap): Delete.
(eglot--connect): Call jsonrpc-autoport-bootstrap.
| -rw-r--r-- | lisp/progmodes/eglot.el | 73 |
1 files changed, 8 insertions, 65 deletions
diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 84c5e6639df..51d2dd74e2b 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el | |||
| @@ -1011,10 +1011,7 @@ ACTION is an LSP object of either `CodeAction' or `Command' type." | |||
| 1011 | :accessor eglot--managed-buffers) | 1011 | :accessor eglot--managed-buffers) |
| 1012 | (saved-initargs | 1012 | (saved-initargs |
| 1013 | :documentation "Saved initargs for reconnection purposes." | 1013 | :documentation "Saved initargs for reconnection purposes." |
| 1014 | :accessor eglot--saved-initargs) | 1014 | :accessor eglot--saved-initargs)) |
| 1015 | (inferior-process | ||
| 1016 | :documentation "Server subprocess started automatically." | ||
| 1017 | :accessor eglot--inferior-process)) | ||
| 1018 | :documentation | 1015 | :documentation |
| 1019 | "Represents a server. Wraps a process for LSP communication.") | 1016 | "Represents a server. Wraps a process for LSP communication.") |
| 1020 | 1017 | ||
| @@ -1151,9 +1148,6 @@ PRESERVE-BUFFERS as in `eglot-shutdown', which see." | |||
| 1151 | (maphash (lambda (_dir watch-and-ids) | 1148 | (maphash (lambda (_dir watch-and-ids) |
| 1152 | (file-notify-rm-watch (car watch-and-ids))) | 1149 | (file-notify-rm-watch (car watch-and-ids))) |
| 1153 | (eglot--file-watches server)) | 1150 | (eglot--file-watches server)) |
| 1154 | ;; Kill any autostarted inferior processes | ||
| 1155 | (when-let (proc (eglot--inferior-process server)) | ||
| 1156 | (delete-process proc)) | ||
| 1157 | ;; Sever the project/server relationship for `server' | 1151 | ;; Sever the project/server relationship for `server' |
| 1158 | (setf (gethash (eglot--project server) eglot--servers-by-project) | 1152 | (setf (gethash (eglot--project server) eglot--servers-by-project) |
| 1159 | (delq server | 1153 | (delq server |
| @@ -1464,7 +1458,6 @@ This docstring appeases checkdoc, that's all." | |||
| 1464 | (let* ((default-directory (project-root project)) | 1458 | (let* ((default-directory (project-root project)) |
| 1465 | (nickname (project-name project)) | 1459 | (nickname (project-name project)) |
| 1466 | (readable-name (format "EGLOT (%s/%s)" nickname managed-modes)) | 1460 | (readable-name (format "EGLOT (%s/%s)" nickname managed-modes)) |
| 1467 | autostart-inferior-process | ||
| 1468 | server-info | 1461 | server-info |
| 1469 | (contact (if (functionp contact) (funcall contact) contact)) | 1462 | (contact (if (functionp contact) (funcall contact) contact)) |
| 1470 | (initargs | 1463 | (initargs |
| @@ -1477,16 +1470,16 @@ This docstring appeases checkdoc, that's all." | |||
| 1477 | readable-name nil | 1470 | readable-name nil |
| 1478 | (car contact) (cadr contact) | 1471 | (car contact) (cadr contact) |
| 1479 | (cddr contact))))) | 1472 | (cddr contact))))) |
| 1480 | ((and (stringp (car contact)) (memq :autoport contact)) | 1473 | ((and (stringp (car contact)) |
| 1474 | (cl-find-if (lambda (x) | ||
| 1475 | (or (eq x :autoport) | ||
| 1476 | (eq (car-safe x) :autoport))) | ||
| 1477 | contact)) | ||
| 1481 | (setq server-info (list "<inferior process>")) | 1478 | (setq server-info (list "<inferior process>")) |
| 1482 | `(:process ,(lambda () | 1479 | `(:process ,(jsonrpc-autoport-bootstrap |
| 1483 | (pcase-let ((`(,connection . ,inferior) | ||
| 1484 | (eglot--inferior-bootstrap | ||
| 1485 | readable-name | 1480 | readable-name |
| 1486 | contact | 1481 | contact |
| 1487 | '(:noquery t)))) | 1482 | :connect-args '(:noquery t)))) |
| 1488 | (setq autostart-inferior-process inferior) | ||
| 1489 | connection)))) | ||
| 1490 | ((stringp (car contact)) | 1483 | ((stringp (car contact)) |
| 1491 | (let* ((probe (cl-position-if #'keywordp contact)) | 1484 | (let* ((probe (cl-position-if #'keywordp contact)) |
| 1492 | (more-initargs (and probe (cl-subseq contact probe))) | 1485 | (more-initargs (and probe (cl-subseq contact probe))) |
| @@ -1535,7 +1528,6 @@ This docstring appeases checkdoc, that's all." | |||
| 1535 | (setf (eglot--languages server) | 1528 | (setf (eglot--languages server) |
| 1536 | (cl-loop for m in managed-modes for l in language-ids | 1529 | (cl-loop for m in managed-modes for l in language-ids |
| 1537 | collect (cons m l))) | 1530 | collect (cons m l))) |
| 1538 | (setf (eglot--inferior-process server) autostart-inferior-process) | ||
| 1539 | (run-hook-with-args 'eglot-server-initialized-hook server) | 1531 | (run-hook-with-args 'eglot-server-initialized-hook server) |
| 1540 | ;; Now start the handshake. To honor `eglot-sync-connect' | 1532 | ;; Now start the handshake. To honor `eglot-sync-connect' |
| 1541 | ;; maybe-sync-maybe-async semantics we use `jsonrpc-async-request' | 1533 | ;; maybe-sync-maybe-async semantics we use `jsonrpc-async-request' |
| @@ -1628,55 +1620,6 @@ in project `%s'." | |||
| 1628 | (quit (jsonrpc-shutdown server) (setq canceled 'quit))) | 1620 | (quit (jsonrpc-shutdown server) (setq canceled 'quit))) |
| 1629 | (setq tag nil)))) | 1621 | (setq tag nil)))) |
| 1630 | 1622 | ||
| 1631 | (defun eglot--inferior-bootstrap (name contact &optional connect-args) | ||
| 1632 | "Use CONTACT to start a server, then connect to it. | ||
| 1633 | Return a cons of two process objects (CONNECTION . INFERIOR). | ||
| 1634 | Name both based on NAME. | ||
| 1635 | CONNECT-ARGS are passed as additional arguments to | ||
| 1636 | `open-network-stream'." | ||
| 1637 | (let* ((port-probe (make-network-process :name "eglot-port-probe-dummy" | ||
| 1638 | :server t | ||
| 1639 | :host "localhost" | ||
| 1640 | :service 0)) | ||
| 1641 | (port-number (unwind-protect | ||
| 1642 | (process-contact port-probe :service) | ||
| 1643 | (delete-process port-probe))) | ||
| 1644 | inferior connection) | ||
| 1645 | (unwind-protect | ||
| 1646 | (progn | ||
| 1647 | (setq inferior | ||
| 1648 | (make-process | ||
| 1649 | :name (format "autostart-inferior-%s" name) | ||
| 1650 | :stderr (format "*%s stderr*" name) | ||
| 1651 | :noquery t | ||
| 1652 | :command (cl-subst | ||
| 1653 | (format "%s" port-number) :autoport contact))) | ||
| 1654 | (setq connection | ||
| 1655 | (cl-loop | ||
| 1656 | repeat 10 for i from 1 | ||
| 1657 | do (accept-process-output nil 0.5) | ||
| 1658 | while (process-live-p inferior) | ||
| 1659 | do (eglot--message | ||
| 1660 | "Trying to connect to localhost and port %s (attempt %s)" | ||
| 1661 | port-number i) | ||
| 1662 | thereis (ignore-errors | ||
| 1663 | (apply #'open-network-stream | ||
| 1664 | (format "autoconnect-%s" name) | ||
| 1665 | nil | ||
| 1666 | "localhost" port-number connect-args)))) | ||
| 1667 | (cons connection inferior)) | ||
| 1668 | (cond ((and (process-live-p connection) | ||
| 1669 | (process-live-p inferior)) | ||
| 1670 | (eglot--message "Done, connected to %s!" port-number)) | ||
| 1671 | (t | ||
| 1672 | (when inferior (delete-process inferior)) | ||
| 1673 | (when connection (delete-process connection)) | ||
| 1674 | (eglot--error "Could not start and connect to server%s" | ||
| 1675 | (if inferior | ||
| 1676 | (format " started with %s" | ||
| 1677 | (process-command inferior)) | ||
| 1678 | "!"))))))) | ||
| 1679 | |||
| 1680 | 1623 | ||
| 1681 | ;;; Helpers (move these to API?) | 1624 | ;;; Helpers (move these to API?) |
| 1682 | ;;; | 1625 | ;;; |