Skip to content

Commit 7954afb

Browse files
authored
Merge pull request #666 from emacs-php/improve/imenu-regexp
Improve php-imenu-generic-expression
2 parents 209913f + 0817e36 commit 7954afb

File tree

6 files changed

+149
-30
lines changed

6 files changed

+149
-30
lines changed

Diff for: .github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- uses: actions/checkout@v2
3838
- name: Run tests
3939
if: matrix.allow_failure != true
40-
run: 'make test'
40+
run: 'make .cask test'
4141
- name: Run tests (allow failure)
4242
if: matrix.allow_failure == true
4343
run: 'make test || true'

Diff for: CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ 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+
* Add `php-imenu-generic-expression-legacy` for compatibility
13+
* Add `php-imenu-generic-expression-simple` for simple display
14+
15+
### Changed
16+
17+
* Re-organized `php-imenu-generic-expression`
18+
* Added `Import`, `Constants` and `Properties`
19+
* Removed `Anonymous Functions`
20+
* Renamed `Named Functions` to `Functions`
21+
* Renamed `All Methods` to `Methods`
22+
* Removed `Public Methods`, `Protected Methods` and `Provate Methods`
23+
* Unified `Classes`, `Traits`, `Interfaces` into `Classes`
1124

1225
## [1.24.0] - 2021-03-07
1326

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ dev:
5454
#
5555
# for an example of using a script like this with the 'git bisect run'
5656
# command.
57-
test: .cask clean all
57+
test: clean all
5858
touch tests/project/1/.git
5959
$(EMACS) -Q -batch -L lisp/ --eval \
6060
"(let ((default-directory (expand-file-name \".cask\" default-directory))) \

Diff for: lisp/php-mode.el

+3-1
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,9 @@ After setting the stylevars run hooks according to STYLENAME
11951195
(add-hook 'syntax-propertize-extend-region-functions
11961196
#'php-syntax-propertize-extend-region t t)
11971197

1198-
(setq imenu-generic-expression php-imenu-generic-expression)
1198+
(setq imenu-generic-expression (if (symbolp php-imenu-generic-expression)
1199+
(symbol-value php-imenu-generic-expression)
1200+
php-imenu-generic-expression))
11991201

12001202
;; PHP vars are case-sensitive
12011203
(setq case-fold-search t)

Diff for: lisp/php.el

+126-21
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,7 +278,101 @@ 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
282+
(eval-when-compile
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)
329+
("Classes"
330+
,(php-create-regexp-for-classlike "\\(?:class\\|interface\\|trait\\|enum\\)") 0)
331+
("Namespace"
332+
,(php-create-regexp-for-classlike "namespace") 1)))
333+
"Imenu generic expression for PHP Mode. See `imenu-generic-expression'.")
334+
335+
(defconst php-imenu-generic-expression-simple
336+
(eval-when-compile
337+
`(("Methods"
338+
,(php-create-regexp-for-method nil) 2)
339+
("Properties"
340+
,(rx line-start
341+
(* (syntax whitespace))
342+
(+ (or "public" "protected" "private" "static" "var")
343+
(+ (syntax whitespace)))
344+
(* (? (? (or "|" "?"))
345+
(or "\\" (syntax word) (syntax symbol))
346+
(+ (syntax whitespace))))
347+
(group
348+
"$" (+ (or (syntax word) (syntax symbol))))
349+
word-boundary)
350+
1)
351+
("Constants"
352+
,(rx line-start
353+
(* (syntax whitespace))
354+
(group
355+
(* (or "public" "protected" "private")
356+
(+ (syntax whitespace)))
357+
"const"
358+
(+ (syntax whitespace))
359+
(+ (or (syntax word) (syntax symbol)))))
360+
1)
361+
("Functions"
362+
,(rx line-start
363+
(* (syntax whitespace))
364+
"function"
365+
(+ (syntax whitespace))
366+
(group
367+
(+ (or (syntax word) (syntax symbol)))))
368+
1)
369+
("Classes"
370+
,(php-create-regexp-for-classlike "\\(?:class\\|interface\\|trait\\|enum\\)") 1)
371+
("Namespace"
372+
,(php-create-regexp-for-classlike "namespace") 1)))
373+
"Imenu generic expression for PHP Mode. See `imenu-generic-expression'.")
374+
375+
(defconst php-imenu-generic-expression-legacy
279376
(eval-when-compile
280377
`(("Namespaces"
281378
,(php-create-regexp-for-classlike "namespace") 1)
@@ -288,17 +385,25 @@ can be used to match against definitions for that classlike."
288385
("All Methods"
289386
,(php-create-regexp-for-method) 1)
290387
("Private Methods"
291-
,(php-create-regexp-for-method '("private")) 1)
388+
,(php-create-regexp-for-method '("private")) 2)
292389
("Protected Methods"
293-
,(php-create-regexp-for-method '("protected")) 1)
390+
,(php-create-regexp-for-method '("protected")) 2)
294391
("Public Methods"
295-
,(php-create-regexp-for-method '("public")) 1)
392+
,(php-create-regexp-for-method '("public")) 2)
296393
("Anonymous Functions"
297394
"\\<\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*=\\s-*f\\(unctio\\)?n\\s-*(" 1)
298395
("Named Functions"
299396
"^\\s-*function\\s-+\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(" 1)))
300397
"Imenu generic expression for PHP Mode. See `imenu-generic-expression'.")
301398

399+
(defcustom php-imenu-generic-expression 'php-imenu-generic-expression-default
400+
"Default Imenu generic expression for PHP Mode. See `imenu-generic-expression'."
401+
:type '(choice (alist :key-type string :value-type list)
402+
(const 'php-imenu-generic-expression-legacy)
403+
(const 'php-imenu-generic-expression-simple)
404+
variable)
405+
:group 'php)
406+
302407
(defconst php--re-namespace-pattern
303408
(eval-when-compile
304409
(php-create-regexp-for-classlike "namespace")))

Diff for: tests/php-mode-test.el

+5-6
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,11 @@ 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 (equal all-methods
324+
(list
325+
"static public function staticBeforeVisibility()"
326+
"public static function staticAfterVisibility()"))))))
328327

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

0 commit comments

Comments
 (0)