Skip to content

Commit 11d1c65

Browse files
committed
Improve php-imenu-generic-expression
The old list had duplicate items and lacked an index of modern syntax elements. Modern PHP code focuses on building one class per file. (PSR-4)
1 parent 209913f commit 11d1c65

File tree

3 files changed

+90
-43
lines changed

3 files changed

+90
-43
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ All notable changes of the PHP Mode 1.19.1 release series are documented in this
88

99
* Support new PHP 8.0 and 8.1 syntax hilighting and indentation
1010
* [8.0] `#[Attributes]`
11+
* Add `php-imenu-generic-expression-default` for default value or `php-imenu-generic-expression`
12+
13+
### Changed
14+
15+
* Re-organized `php-imenu-generic-expression`
16+
* Added `Import`, `Constants` and `Properties`
17+
* Removed `Anonymous Functions`
18+
* Renamed `Named Functions` to `Functions`
19+
* Renamed `All Methods` to `Methods`
20+
* Removed `Public Methods`, `Protected Methods` and `Provate Methods`
21+
* Unified `Classes`, `Traits`, `Interfaces` into `Classes`
1122

1223
## [1.24.0] - 2021-03-07
1324

lisp/php.el

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ it is the character that will terminate the string, or t if the string should be
227227
"Regular expression for a PHP function.")
228228

229229
(eval-when-compile
230-
(defun php-create-regexp-for-method (&optional visibility)
230+
(cl-defun php-create-regexp-for-method (&optional visibility &key include-args)
231231
"Make a regular expression for methods with the given VISIBILITY.
232232
233233
VISIBILITY must be a string that names the visibility for a PHP
@@ -242,22 +242,25 @@ which will be the name of the method."
242242
(setq visibility (list visibility)))
243243
(rx-to-string `(: line-start
244244
(* (syntax whitespace))
245-
,@(if visibility
246-
`((* (or "abstract" "final" "static")
247-
(+ (syntax whitespace)))
248-
(or ,@visibility)
249-
(+ (syntax whitespace))
250-
(* (or "abstract" "final" "static")
251-
(+ (syntax whitespace))))
252-
'((* (* (or "abstract" "final" "static"
253-
"private" "protected" "public")
254-
(+ (syntax whitespace))))))
255-
"function"
256-
(+ (syntax whitespace))
257-
(? "&" (* (syntax whitespace)))
258-
(group (+ (or (syntax word) (syntax symbol))))
259-
(* (syntax whitespace))
260-
"(")))
245+
(group
246+
,@(if visibility
247+
`((* (or "abstract" "final" "static")
248+
(+ (syntax whitespace)))
249+
(or ,@visibility)
250+
(+ (syntax whitespace))
251+
(* (or "abstract" "final" "static")
252+
(+ (syntax whitespace))))
253+
'((* (* (or "abstract" "final" "static"
254+
"private" "protected" "public")
255+
(+ (syntax whitespace))))))
256+
"function"
257+
(+ (syntax whitespace))
258+
(? "&" (* (syntax whitespace)))
259+
(group (+ (or (syntax word) (syntax symbol))))
260+
(* (syntax whitespace))
261+
"("
262+
,@(when include-args
263+
'((* any) line-end))))))
261264

262265
(defun php-create-regexp-for-classlike (type)
263266
"Accepts a `TYPE' of a 'classlike' object as a string, such as
@@ -275,30 +278,66 @@ can be used to match against definitions for that classlike."
275278
;; this is not necessarily correct for all values of `type'.
276279
"\\s-+\\(\\(?:\\sw\\|\\\\\\|\\s_\\)+\\)")))
277280

278-
(defconst php-imenu-generic-expression
281+
(defconst php-imenu-generic-expression-default
279282
(eval-when-compile
280-
`(("Namespaces"
281-
,(php-create-regexp-for-classlike "namespace") 1)
283+
`(("Methods"
284+
,(php-create-regexp-for-method nil :include-args t) 1)
285+
("Properties"
286+
,(rx line-start
287+
(* (syntax whitespace))
288+
(group
289+
(+ (or "public" "protected" "private" "static" "var")
290+
(+ (syntax whitespace)))
291+
(* (? (? (or "|" "?"))
292+
(or "\\" (syntax word) (syntax symbol))
293+
(+ (syntax whitespace))))
294+
"$" (+ (or (syntax word) (syntax symbol)))
295+
word-boundary))
296+
1)
297+
("Constants"
298+
,(rx line-start
299+
(* (syntax whitespace))
300+
(group
301+
(* (or "public" "protected" "private")
302+
(+ (syntax whitespace)))
303+
"const"
304+
(+ (syntax whitespace))
305+
(+ (or (syntax word) (syntax symbol)))
306+
(* (syntax whitespace))
307+
(? "=" (* (syntax whitespace))
308+
(repeat 0 40 any))))
309+
1)
310+
("Functions"
311+
,(rx line-start
312+
(* (syntax whitespace))
313+
(group
314+
"function"
315+
(+ (syntax whitespace))
316+
(+ (or (syntax word) (syntax symbol)))
317+
(* (syntax whitespace))
318+
"("
319+
(repeat 0 100 any)))
320+
1)
321+
("Import"
322+
,(rx line-start
323+
;; (* (syntax whitespace))
324+
(group
325+
"use"
326+
(+ (syntax whitespace))
327+
(repeat 0 100 any)))
328+
1)
282329
("Classes"
283-
,(php-create-regexp-for-classlike "class") 1)
284-
("Interfaces"
285-
,(php-create-regexp-for-classlike "interface") 1)
286-
("Traits"
287-
,(php-create-regexp-for-classlike "trait") 1)
288-
("All Methods"
289-
,(php-create-regexp-for-method) 1)
290-
("Private Methods"
291-
,(php-create-regexp-for-method '("private")) 1)
292-
("Protected Methods"
293-
,(php-create-regexp-for-method '("protected")) 1)
294-
("Public Methods"
295-
,(php-create-regexp-for-method '("public")) 1)
296-
("Anonymous Functions"
297-
"\\<\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*=\\s-*f\\(unctio\\)?n\\s-*(" 1)
298-
("Named Functions"
299-
"^\\s-*function\\s-+\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(" 1)))
330+
,(php-create-regexp-for-classlike "\\(?:class\\|interface\\|trait\\|enum\\)") 0)
331+
("Namespace"
332+
,(php-create-regexp-for-classlike "namespace") 1)))
300333
"Imenu generic expression for PHP Mode. See `imenu-generic-expression'.")
301334

335+
(defcustom php-imenu-generic-expression php-imenu-generic-expression-default
336+
"Default Imenu generic expression for PHP Mode. See `imenu-generic-expression'."
337+
:type '(alist :key-type string
338+
:value-type list)
339+
:group 'php)
340+
302341
(defconst php--re-namespace-pattern
303342
(eval-when-compile
304343
(php-create-regexp-for-classlike "namespace")))

tests/php-mode-test.el

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,9 @@ style from Drupal."
319319
"All static method should appear on imenu whether 'static' keyword is placed before or after visibility"
320320
(with-php-mode-test ("issue-83.php")
321321
(let* ((index-alist (imenu--make-index-alist))
322-
(public-methods (mapcar 'car (cdr (assoc "Public Methods" index-alist))))
323-
(all-methods (mapcar 'car (cdr (assoc "All Methods" index-alist)))))
324-
(should (member "staticBeforeVisibility" public-methods))
325-
(should (member "staticBeforeVisibility" all-methods))
326-
(should (member "staticAfterVisibility" public-methods))
327-
(should (member "staticAfterVisibility" all-methods)))))
322+
(all-methods (mapcar 'car (cdr (assoc "Methods" index-alist)))))
323+
(should (member "static public function staticBeforeVisibility()" all-methods))
324+
(should (member "public static function staticAfterVisibility()" all-methods)))))
328325

329326
(ert-deftest php-mode-test-issue-99 ()
330327
"Proper indentation for 'foreach' statements without braces."

0 commit comments

Comments
 (0)