Skip to content

Commit e45bfae

Browse files
committed
Add php-ide.el
1 parent a284802 commit e45bfae

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

Diff for: Cask

+2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
"lisp/php-project.el"
1313
"lisp/php-local-manual.el"
1414
"lisp/php-ide-phpactor.el"
15+
"lisp/php-ide.el"
1516
"lisp/php-mode-debug.el")
1617

1718
(development
19+
;;(depends-on "lsp-mode")
1820
(depends-on "phpactor")
1921
(depends-on "pkg-info")
2022
(depends-on "projectile")

Diff for: Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ELS += lisp/php-defs.el
88
ELS += lisp/php-face.el
99
ELS += lisp/php-flymake.el
1010
ELS += lisp/php-ide-phpactor.el
11+
ELS += lisp/php-ide.el
1112
ELS += lisp/php-local-manual.el
1213
ELS += lisp/php-mode-debug.el
1314
ELS += lisp/php-mode.el

Diff for: lisp/php-ide.el

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
"language-server")))))
120+
121+
(defgroup php-ide nil
122+
"IDE-like support for PHP developing."
123+
:tag "PHP-IDE"
124+
:prefix "php-ide-"
125+
:group 'php)
126+
127+
;;;###autoload
128+
(defcustom php-ide-features nil
129+
"A set of PHP-IDE features symbol."
130+
:tag "PHP-IDE Feature"
131+
:group 'php-ide
132+
:type `(set ,@(mapcar (lambda (feature) (list 'const (car feature)))
133+
php-ide-feature-alist)
134+
symbol)
135+
:safe (lambda (v) (cl-loop for feature in (if (listp v) v (list v))
136+
always (symbolp feature))))
137+
138+
(defcustom php-ide-mode-lighter " PHP-IDE"
139+
"A symbol of PHP-IDE feature."
140+
:tag "PHP-IDE Mode Lighter"
141+
:group 'php-ide
142+
:type 'string
143+
:safe #'stringp)
144+
145+
;;;###autoload
146+
(define-minor-mode php-ide-mode
147+
"Minor mode for integrate IDE-like tools."
148+
:lighter php-ide-mode-lighter
149+
(let ((ide-features php-ide-features))
150+
(when-let (unavailable-features (cl-loop for feature in ide-features
151+
unless (assq feature php-ide-feature-alist)
152+
collect feature))
153+
(user-error "%s includes unavailable PHP-IDE features. (available features are: %s)"
154+
ide-features
155+
(mapconcat (lambda (feature) (concat "'" (symbol-name feature)))
156+
(php-ide--avilable-features) ", ")))
157+
(cl-loop for feature in ide-features
158+
for ide-plist = (cdr-safe (assq feature php-ide-feature-alist))
159+
do (if (null ide-plist)
160+
(message "Please set `php-ide-feature' variable in .dir-locals.el or custom variable")
161+
(if php-ide-mode
162+
(php-ide--activate-buffer feature ide-plist)
163+
(php-ide--deactivate-buffer ide-plist))))))
164+
165+
;;;###autoload
166+
(defun php-ide-turn-on ()
167+
"Turn on PHP IDE-FEATURES and execute `php-ide-mode'."
168+
(unless php-ide-features
169+
(user-error "No PHP-IDE feature is installed. Install the lsp-mode, eglot or phpactor package"))
170+
(php-ide-mode +1))
171+
172+
(defun php-ide--activate-buffer (name ide-plist)
173+
"Activate php-ide implementation by NAME and IDE-PLIST."
174+
(unless (funcall (plist-get ide-plist :test))
175+
(user-error "PHP-IDE feature `%s' is not available" name))
176+
(funcall (plist-get ide-plist :activate)))
177+
178+
(defun php-ide--deactivate-buffer (ide-plist)
179+
"Deactivate php-ide implementation by IDE-PLIST."
180+
(funcall (plist-get ide-plist :deactivate)))
181+
182+
(defun php-ide--avilable-features ()
183+
"Return list of available PHP-IDE features."
184+
(cl-loop for (ide . plist) in php-ide-feature-alist
185+
if (funcall (plist-get plist :test))
186+
collect ide))
187+
188+
(provide 'php-ide)
189+
;;; php-ide.el ends here

0 commit comments

Comments
 (0)