Skip to content

Commit cdd23d3

Browse files
committed
Impl PHP-UI
1 parent 4503672 commit cdd23d3

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

lisp/php-ui-phpactor.el

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
;;; php-ui-phpactor.el --- UI support for PHP developing -*- 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-UI implementation to integrate Phpactor (phpactor.el).
27+
28+
;;; Code:
29+
(require 'phpactor nil t)
30+
(require 'popup nil t)
31+
32+
(defvar-local php-ui-phpactor-buffer nil
33+
".")
34+
35+
(defvar-local php-ui-phpactor-last-hover-pos nil
36+
".")
37+
38+
(defvar php-ui-phpactor-timer nil
39+
".")
40+
41+
(defun php-ui-phpactor-hover ()
42+
""
43+
(interactive)
44+
(when (and php-ui-phpactor-buffer (not (eq (point) php-ui-phpactor-last-hover-pos)))
45+
(setq php-ui-phpactor-last-hover-pos (point))
46+
(popup-tip (phpactor-hover))))
47+
48+
;;;###autoload
49+
(defun php-ui-phpactor-activate ()
50+
""
51+
(interactive)
52+
(unless php-ui-phpactor-timer
53+
(setq php-ui-phpactor-timer (run-with-timer 1.0 5 #'php-ui-phpactor-hover)))
54+
(setq php-ui-phpactor-buffer t))
55+
56+
;;;###autoload
57+
(defun php-ui-phpactor-deactivate ()
58+
""
59+
(interactive)
60+
(setq php-ui-phpactor-buffer nil))
61+
62+
(provide 'php-ui-phpactor)
63+
;;; php-ui-phpactor.el ends here

lisp/php-ui.el

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
;;; php-ui.el --- 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-UI 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+
;; - lsp-mode
45+
;; https://emacs-lsp.github.io/lsp-mode/
46+
;; https://github.com/emacs-lsp/lsp-mode
47+
;; - phpactor
48+
;; https://phpactor.readthedocs.io/
49+
;; https://github.com/phpactor/phpactor
50+
;; https://github.com/emacs-php/phpactor.el
51+
;;
52+
;; ## Configuration
53+
;;
54+
;; Put follows code into your .emacs (~/.emacs.d/init.el) file:
55+
;;
56+
;; (defun my-php-mode-setup ()
57+
;; (add-hook 'hack-local-variables-hook #'php-ui-mode t t))
58+
;;
59+
;; (with-eval-after-load 'php-ui
60+
;; (custom-set-variables
61+
;; '(php-ui-feature 'eglot) ;; or 'none, 'phpactor, 'lsp-mode
62+
;; '(php-ui-eglot-executable '("psalm-language-server")) ;; or "intelephense", '("php" "vendor/bin/path/to/server")
63+
;; ;; If you want to hide php-ui-mode from the mode line, set an empty string
64+
;; '(php-ui-mode-lighter ""))
65+
;;
66+
;; ;; Only Eglot users
67+
;; (add-to-list 'php-ui-eglot-executable '(php-mode . php-ui-eglot-server-program))
68+
;;
69+
;; (add-hook 'php-mode #'my-php-mode-setup))
70+
;;
71+
;; If you don't enable IDE support by default, set '(php-ui-feature 'none)
72+
;;
73+
;; ### For per project configuration
74+
;;
75+
;; Put follows code into .dir-locals.el in project directory:
76+
;;
77+
;; ((nil (php-project-root . git)
78+
;; (php-ui-eglot-executable . ("psalm-language-server"))
79+
;; ;; or (php-ui-eglot-executable . ("php" "vendor/bin/path/to/server"))
80+
;; (php-ui-feature . lsp-mode)))
81+
;;
82+
;; If you can't put .dir-locals.el in your project directory, consider the sidecar-locals package.
83+
;; https://melpa.org/#/sidecar-locals
84+
;; https://gitlab.com/ideasman42/emacs-sidecar-locals
85+
;;
86+
87+
;;; Code:
88+
(eval-when-compile
89+
(require 'php-ui-phpactor))
90+
91+
(defvar php-ui-feature-alist
92+
'((none :test (lambda () t)
93+
:activate (lambda () t)
94+
:deactivate (lambda () t))
95+
(phpactor :test (lambda () (and (require 'phpactor nil t) (featurep 'phpactor)))
96+
:activate php-ui-phpactor-activate
97+
:deactivate php-ui-phpactor-activate)
98+
(lsp-mode :test (lambda () (and (require 'lsp nil t) (featurep 'lsp)))
99+
:activate lsp
100+
:deactivate lsp-workspace-shutdown)))
101+
102+
(defgroup php-ui nil
103+
"UI support for PHP developing."
104+
:tag "PHP-UI"
105+
:prefix "php-ui-"
106+
:group 'php)
107+
108+
(defcustom php-ui-feature nil
109+
"A symbol of PHP-UI feature"
110+
:tag "PHP-UI Feature"
111+
:group 'php-ui
112+
:type 'symbol
113+
:safe #'symbolp)
114+
115+
(defcustom php-ui-mode-lighter " PHP-UI"
116+
"A symbol of PHP-UI feature"
117+
:tag "PHP-UI Mode Lighter"
118+
:group 'php-ui
119+
:type 'string
120+
:safe #'stringp)
121+
122+
;;;###autoload
123+
(define-minor-mode php-ui-mode
124+
"Minor mode for integrate IDE-like tools."
125+
:lighter php-ui-mode-lighter
126+
(let ((ui-plist (cdr-safe (assq php-ui-feature php-ui-feature-alist))))
127+
(if (null ui-plist)
128+
(message "Please set `php-ui-feature' variable in .dir-locals.el or custom variable")
129+
(if php-ui-mode
130+
(php-ui--activate-buffer ui-plist)
131+
(php-ui--deactivate-buffer ui-plist)))))
132+
133+
;;;###autoload
134+
(defun php-ui (feature)
135+
"Select a PHP-UI feature and execute `php-ui-mode'."
136+
(interactive (list (php-ui--select-feature)))
137+
(unless feature
138+
(user-error "No PHP-UI feature is installed. Install the lsp-mode, eglot or phpactor package"))
139+
(unless (assq feature php-ui-feature-alist)
140+
(user-error "`%s' does not include in available PHP-UI features. (%s)"
141+
feature
142+
(mapconcat #'symbol-name (php-ui--avilable-features) ", ")))
143+
(when (and php-ui-mode feature php-ui-feature
144+
(not (eq php-ui-feature php-ui-feature)))
145+
(php-ui-mode -1))
146+
(let ((php-ui-feature feature))
147+
(php-ui-mode +1)))
148+
149+
(defun php-ui--activate-buffer (ui-plist)
150+
"Activate php-ui implementation by UI-PLIST."
151+
(funcall (plist-get ui-plist :activate)))
152+
153+
(defun php-ui--deactivate-buffer (ui-plist)
154+
"Deactivate php-ui implementation by UI-PLIST."
155+
(funcall (plist-get ui-plist :deactivate)))
156+
157+
(defun php-ui--avilable-features ()
158+
"Return list of available PHP-UI features."
159+
(cl-loop for (ui . plist) in php-ui-feature-alist
160+
if (funcall (plist-get plist :test))
161+
collect ui))
162+
163+
(defun php-ui--select-feature ()
164+
"Choose PHP-UI feature"
165+
(let* ((features (php-ui--avilable-features))
166+
(count (length features)))
167+
(cond
168+
((eq count 0) nil)
169+
((eq count 1) (car features))
170+
(t
171+
(intern (completing-read "Select PHP-UI feature: " features nil t))))))
172+
173+
(provide 'php-ui)
174+
;;; php-ui.el ends here

0 commit comments

Comments
 (0)