|
| 1 | +;;; eglot-php.el --- Eglot enhancement for PHP development -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2022 Friends of Emacs-PHP development |
| 4 | + |
| 5 | +;; Author: USAMI Kenta <[email protected]> |
| 6 | +;; Keywords: tools, files |
| 7 | +;; URL: https://github.com/emacs-php/php-mode |
| 8 | +;; Version: 1.24.0 |
| 9 | +;; License: GPL-3.0-or-later |
| 10 | + |
| 11 | +;; This program is free software; you can redistribute it and/or modify |
| 12 | +;; it under the terms of the GNU General Public License as published by |
| 13 | +;; the Free Software Foundation, either version 3 of the License, or |
| 14 | +;; (at your option) any later version. |
| 15 | + |
| 16 | +;; This program is distributed in the hope that it will be useful, |
| 17 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +;; GNU General Public License for more details. |
| 20 | + |
| 21 | +;; You should have received a copy of the GNU General Public License |
| 22 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 23 | + |
| 24 | +;;; Commentary: |
| 25 | + |
| 26 | +;; PHP Mode integrates Eglot <https://github.com/joaotavora/eglot>. |
| 27 | +;; |
| 28 | +;; **Note**: |
| 29 | +;; This feature is under development and experimental. |
| 30 | +;; All of these functions, modes and terms are subject to change without notice. |
| 31 | +;; |
| 32 | +;; ## Configuration |
| 33 | +;; |
| 34 | +;; Put follows code into your .emacs (~/.emacs.d/init.el) file: |
| 35 | +;; |
| 36 | +;; (add-to-list 'eglot-server-programs ((php-mode phps-mode) . eglot-php-server-program)) |
| 37 | +;; |
| 38 | +;; ### For per project configuration |
| 39 | +;; |
| 40 | +;; Put follows code into .dir-locals.el in project directory: |
| 41 | +;; |
| 42 | +;; ((nil (php-project-root . git) |
| 43 | +;; (eglot-php-executable . ("psalm-language-server")) |
| 44 | +;; ;; or (eglot-php-executable . ("php" "vendor/bin/path/to/server")) |
| 45 | +;; )) |
| 46 | +;; |
| 47 | +;; If you can't put .dir-locals.el in your project directory, consider the sidecar-locals package. |
| 48 | +;; https://melpa.org/#/sidecar-locals |
| 49 | +;; https://codeberg.org/ideasman42/emacs-sidecar-locals |
| 50 | +;; |
| 51 | + |
| 52 | +;;; Code: |
| 53 | +(require 'cl-lib) |
| 54 | +(require 'php-project) |
| 55 | +(require 'eglot nil t) |
| 56 | +(require 'phpactor nil t) |
| 57 | + |
| 58 | +(eval-when-compile |
| 59 | + (defvar eglot-server-programs) |
| 60 | + (defvar eglot--managed-mode) |
| 61 | + (declare-function eglot-ensure "ext:eglot" ()) |
| 62 | + (declare-function phpactor--find-executable "ext:phpactor" ())) |
| 63 | + |
| 64 | +(defvar eglot-php-lsp-command-alist |
| 65 | + '((intelephense "intelephense" "--stdio") |
| 66 | + (phpactor . (lambda () (list (if (require 'phpactor nil t) |
| 67 | + (phpactor--find-executable) |
| 68 | + "phpactor")))))) |
| 69 | + |
| 70 | +(defgroup eglot-php nil |
| 71 | + "Eglot PHP integration." |
| 72 | + :tag "Eglot-PHP" |
| 73 | + :prefix "eglot-php-" |
| 74 | + :group 'eglot |
| 75 | + :group 'php) |
| 76 | + |
| 77 | +;;;###autoload |
| 78 | +(defcustom eglot-php-executable nil |
| 79 | + "Command name or path to the command of Eglot LSP executable." |
| 80 | + :tag "Eglot-PHP Executable" |
| 81 | + :group 'eglot-php |
| 82 | + :type '(choice |
| 83 | + (const intelephense) |
| 84 | + (const phpactor) |
| 85 | + string (repeat string)) |
| 86 | + :safe (lambda (v) (cond |
| 87 | + ((stringp v) (file-exists-p v)) |
| 88 | + ((listp v) (cl-every #'stringp v)) |
| 89 | + ((assq v eglot-php-lsp-command-alist))))) |
| 90 | + |
| 91 | +;;;###autoload |
| 92 | +(defun eglot-php-server-program () |
| 93 | + "Return a list of command to execute LSP Server." |
| 94 | + (cond |
| 95 | + ((stringp eglot-php-executable) (list eglot-php-executable)) |
| 96 | + ((listp eglot-php-executable) eglot-php-executable) |
| 97 | + ((when-let (command (assq eglot-php-executable eglot-php-lsp-command-alist)) |
| 98 | + (cond |
| 99 | + ((functionp command) (funcall command)) |
| 100 | + ((listp command) command)))))) |
| 101 | + |
| 102 | +(defun eglot-php-ensure () |
| 103 | + "Start PHP-specific Eglot session for current buffer if there isn't one." |
| 104 | + (when-let (server-program (eglot-php-server-program)) |
| 105 | + (setq-local eglot-server-programs (list (cons major-mode server-program)))) |
| 106 | + (setq-local project-find-functions (list #'php-project-project-find-function)) |
| 107 | + (add-function :after (local 'eglot--managed-mode) #'eglot-php--eglot--managed-mode-after) |
| 108 | + (eglot-ensure)) |
| 109 | + |
| 110 | +(defun eglot-php--eglot--managed-mode-after (&optional _arg) |
| 111 | + "Rollback variables when turning off `eglot--managed-mode'. |
| 112 | +
|
| 113 | +ARG is ignored." |
| 114 | + (unless eglot--managed-mode |
| 115 | + (setq-local eglot-server-programs (default-value 'eglot-server-programs)))) |
| 116 | + |
| 117 | +(provide 'eglot-php) |
| 118 | +;;; eglot-php.el ends here |
0 commit comments