|
| 1 | +;;; php-ide.el --- IDE-like UI support 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 LSP Mode (lsp-mode), Phpactor (phpactor.el) and IDE-like tools. |
| 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 | +;; ## Motivations |
| 33 | +;; |
| 34 | +;; There are some IDE-like features / packages for PHP development. |
| 35 | +;; PHP-IDE bridges projects and their IDE-like features. |
| 36 | +;; |
| 37 | +;; ## IDE Features |
| 38 | +;; |
| 39 | +;; We don't recommend features, but bundle some feature bridges. |
| 40 | +;; They are sorted alphabetically except "none." |
| 41 | +;; |
| 42 | +;; - none |
| 43 | +;; Does not launch any IDE features. |
| 44 | +;; - eglot |
| 45 | +;; https://github.com/joaotavora/eglot |
| 46 | +;; - lsp-mode |
| 47 | +;; https://emacs-lsp.github.io/lsp-mode/ |
| 48 | +;; https://github.com/emacs-lsp/lsp-mode |
| 49 | +;; - phpactor |
| 50 | +;; https://phpactor.readthedocs.io/ |
| 51 | +;; https://github.com/phpactor/phpactor |
| 52 | +;; https://github.com/emacs-php/phpactor.el |
| 53 | +;; |
| 54 | +;; ## Configuration |
| 55 | +;; |
| 56 | +;; Put follows code into your .emacs (~/.emacs.d/init.el) file: |
| 57 | +;; |
| 58 | +;; (defun my-php-mode-setup () |
| 59 | +;; (add-hook 'hack-local-variables-hook #'php-ide-mode t t)) |
| 60 | +;; |
| 61 | +;; (with-eval-after-load 'php-ide |
| 62 | +;; (custom-set-variables |
| 63 | +;; '(php-ide-features . 'eglot) ;; and/or 'none, 'phpactor, 'lsp-mode |
| 64 | +;; '(php-ide-eglot-executable "psalm-language-server") ;; or "intelephense", '("php" "vendor/bin/path/to/server") |
| 65 | +;; ;; If you want to hide php-ide-mode from the mode line, set an empty string |
| 66 | +;; '(php-ide-mode-lighter "")) |
| 67 | +;; |
| 68 | +;; ;; Only Eglot users |
| 69 | +;; (add-to-list 'php-ide-eglot-executable '(php-mode . php-ide-eglot-server-program)) |
| 70 | +;; |
| 71 | +;; (add-hook 'php-mode #'my-php-mode-setup)) |
| 72 | +;; |
| 73 | +;; If you don't enable IDE support by default, set '(php-ide-feature 'none) |
| 74 | +;; |
| 75 | +;; ### For per project configuration |
| 76 | +;; |
| 77 | +;; Put follows code into .dir-locals.el in project directory: |
| 78 | +;; |
| 79 | +;; ((nil (php-project-root . git) |
| 80 | +;; (php-ide-eglot-executable . ("psalm-language-server")) |
| 81 | +;; ;; or (php-ide-eglot-executable . ("php" "vendor/bin/path/to/server")) |
| 82 | +;; (php-ide-feature . lsp-mode))) |
| 83 | +;; |
| 84 | +;; If you can't put .dir-locals.el in your project directory, consider the sidecar-locals package. |
| 85 | +;; https://melpa.org/#/sidecar-locals |
| 86 | +;; https://codeberg.org/ideasman42/emacs-sidecar-locals |
| 87 | +;; |
| 88 | + |
| 89 | +;;; Code: |
| 90 | +(require 'php-project) |
| 91 | + |
| 92 | +(eval-when-compile |
| 93 | + (require 'cl-lib) |
| 94 | + (require 'php-ide-phpactor) |
| 95 | + (defvar eglot-server-programs) |
| 96 | + (declare-function eglot-ensure "ext:eglot" ()) |
| 97 | + (declare-function eglot--managed-mode-off "ext:eglot" ()) |
| 98 | + (declare-function phpactor--find-executable "ext:phpactor" ())) |
| 99 | + |
| 100 | +(defvar php-ide-feature-alist |
| 101 | + '((none :test (lambda () t) |
| 102 | + :activate (lambda () t) |
| 103 | + :deactivate (lambda () t)) |
| 104 | + (phpactor :test (lambda () (and (require 'phpactor nil t) (featurep 'phpactor))) |
| 105 | + :activate php-ide-phpactor-activate |
| 106 | + :deactivate php-ide-phpactor-activate) |
| 107 | + (eglot :test (lambda () (and (require 'eglot nil t) (featurep 'eglot))) |
| 108 | + :activate eglot-ensure |
| 109 | + :deactivate eglot--managed-mode-off) |
| 110 | + (lsp-mode :test (lambda () (and (require 'lsp nil t) (featurep 'lsp))) |
| 111 | + :activate lsp |
| 112 | + :deactivate lsp-workspace-shutdown))) |
| 113 | + |
| 114 | +(defvar php-ide-lsp-command-alist |
| 115 | + '((intelephense "intelephense" "--stdio") |
| 116 | + (phpactor . (lambda () (list (if (fboundp 'phpactor--find-executable) |
| 117 | + (phpactor--find-executable) |
| 118 | + "phpactor")))))) |
| 119 | + |
| 120 | +(defgroup php-ide nil |
| 121 | + "IDE-like support for PHP developing." |
| 122 | + :tag "PHP-IDE" |
| 123 | + :prefix "php-ide-" |
| 124 | + :group 'php) |
| 125 | + |
| 126 | +;;;###autoload |
| 127 | +(defcustom php-ide-features nil |
| 128 | + "A set of PHP-IDE features symbol." |
| 129 | + :tag "PHP-IDE Feature" |
| 130 | + :group 'php-ide |
| 131 | + :type `(set ,@(mapcar (lambda (feature) (list 'const (car feature))) |
| 132 | + php-ide-feature-alist) |
| 133 | + symbol) |
| 134 | + :safe (lambda (v) (if (listp v) |
| 135 | + (cl-loop for feature in v |
| 136 | + always (assq feature php-ide-feature-alist)) |
| 137 | + (assq v php-ide-feature-alist)))) |
| 138 | + |
| 139 | +(defcustom php-ide-mode-lighter " PHP-IDE" |
| 140 | + "A symbol of PHP-IDE feature." |
| 141 | + :tag "PHP-IDE Mode Lighter" |
| 142 | + :group 'php-ide |
| 143 | + :type 'string |
| 144 | + :safe #'stringp) |
| 145 | + |
| 146 | +;;;###autoload |
| 147 | +(define-minor-mode php-ide-mode |
| 148 | + "Minor mode for integrate IDE-like tools." |
| 149 | + :lighter php-ide-mode-lighter |
| 150 | + (let ((ide-features php-ide-features)) |
| 151 | + (when-let (unavailable-features (cl-loop for feature in ide-features |
| 152 | + unless (assq feature php-ide-feature-alist) |
| 153 | + collect feature)) |
| 154 | + (user-error "%s includes unavailable PHP-IDE features. (available features are: %s)" |
| 155 | + ide-features |
| 156 | + (mapconcat (lambda (feature) (concat "'" (symbol-name feature))) |
| 157 | + (php-ide--avilable-features) ", "))) |
| 158 | + (cl-loop for feature in ide-features |
| 159 | + for ide-plist = (cdr-safe (assq feature php-ide-feature-alist)) |
| 160 | + do (if (null ide-plist) |
| 161 | + (message "Please set `php-ide-feature' variable in .dir-locals.el or custom variable") |
| 162 | + (if php-ide-mode |
| 163 | + (php-ide--activate-buffer feature ide-plist) |
| 164 | + (php-ide--deactivate-buffer ide-plist)))))) |
| 165 | + |
| 166 | +;;;###autoload |
| 167 | +(defun php-ide-turn-on () |
| 168 | + "Turn on PHP IDE-FEATURES and execute `php-ide-mode'." |
| 169 | + (unless php-ide-features |
| 170 | + (user-error "No PHP-IDE feature is installed. Install the lsp-mode, eglot or phpactor package")) |
| 171 | + (php-ide-mode +1)) |
| 172 | + |
| 173 | +(defun php-ide--activate-buffer (name ide-plist) |
| 174 | + "Activate php-ide implementation by NAME and IDE-PLIST." |
| 175 | + (unless (funcall (plist-get ide-plist :test)) |
| 176 | + (user-error "PHP-IDE feature `%s' is not available" name)) |
| 177 | + (funcall (plist-get ide-plist :activate))) |
| 178 | + |
| 179 | +(defun php-ide--deactivate-buffer (ide-plist) |
| 180 | + "Deactivate php-ide implementation by IDE-PLIST." |
| 181 | + (funcall (plist-get ide-plist :deactivate))) |
| 182 | + |
| 183 | +(defun php-ide--avilable-features () |
| 184 | + "Return list of available PHP-IDE features." |
| 185 | + (cl-loop for (ide . plist) in php-ide-feature-alist |
| 186 | + if (funcall (plist-get plist :test)) |
| 187 | + collect ide)) |
| 188 | + |
| 189 | +(provide 'php-ide) |
| 190 | +;;; php-ide.el ends here |
0 commit comments