@@ -211,6 +211,16 @@ a completion list."
211
211
212
212
These are different from \" constants\" in strict terms.
213
213
see https://www.php.net/manual/language.constants.predefined.php" )
214
+
215
+ (defconst php-re-token-symbols
216
+ (eval-when-compile
217
+ (regexp-opt (list " &" " &=" " array(" " (array)" " &&" " ||" " (bool)" " (boolean)" " break;" " ?>" " %>"
218
+ " ??" " ??=" " .=" " --" " /=" " =>" " (real)" " (double)" " (float)" " ::" " ..."
219
+ " __halt_compiler()" " ++" " (int)" " (integer)" " ==" " >=" " ===" " !=" " <>" " !=="
220
+ " <=" " -=" " %=" " *=" " \\ " " (object)" " ->" " ?->" " <?php" " <?" " <?=" " |=" " +="
221
+ " **" " **=" " <<" " <<=" " <=>" " >>" " >>=" " <<<" " (string)" " ^=" " yield from"
222
+ " [" " ]" " (" " )" " {" " }" " ;" )
223
+ t )))
214
224
215
225
; ;; Utillity for locate language construction
216
226
(defsubst php-in-string-p ()
@@ -432,6 +442,17 @@ can be used to match against definitions for that classlike."
432
442
(eval-when-compile
433
443
(php-create-regexp-for-classlike (regexp-opt '(" class" " interface" " trait" )))))
434
444
445
+ (defvar php--analysis-syntax-table
446
+ (eval-when-compile
447
+ (let ((table (make-syntax-table )))
448
+ (c-populate-syntax-table table)
449
+ (modify-syntax-entry ?_ " w" table)
450
+ (modify-syntax-entry ?` " \" " table)
451
+ (modify-syntax-entry ?\" " \" " table)
452
+ (modify-syntax-entry ?# " < b" table)
453
+ (modify-syntax-entry ?\n " > b" table)
454
+ table)))
455
+
435
456
(defun php-get-current-element (re-pattern )
436
457
" Return backward matched element by RE-PATTERN."
437
458
(save-excursion
@@ -469,27 +490,36 @@ can be used to match against definitions for that classlike."
469
490
(goto-char (match-end 0 )))))
470
491
(> (point ) start)))))
471
492
472
- (defun php-get-pattern ()
473
- " Find the pattern we want to complete.
474
- `find-tag-default' from GNU Emacs etags.el"
493
+ (defun php-leading-tokens (length )
494
+ " Return a list of leading LENGTH tokens from cursor point.
495
+
496
+ The token list is lined up in the opposite side of the visual arrangement.
497
+ The order is reversed by calling as follows:
498
+ \( nreverse \( php-leading-tokens 3\)\) "
475
499
(save-excursion
476
500
(save-match-data
477
- (while (looking-at " \\ sw\\ |\\ s_" )
478
- (forward-char 1 ))
479
- (when (or (re-search-backward " \\ sw\\ |\\ s_"
480
- (save-excursion (beginning-of-line ) (point ))
481
- t )
482
- (re-search-forward " \\ (\\ sw\\ |\\ s_\\ )+"
483
- (save-excursion (end-of-line ) (point ))
484
- t ))
485
- (goto-char (match-end 0 ))
486
- (buffer-substring-no-properties
487
- (point )
488
- (progn
489
- (forward-sexp -1 )
490
- (while (looking-at " \\ s'" )
491
- (forward-char 1 ))
492
- (point )))))))
501
+ (with-syntax-table php--analysis-syntax-table
502
+ (cl-loop
503
+ repeat length
504
+ do (progn
505
+ (forward-comment (- (point )))
506
+ (c-backward-token-2 1 nil ))
507
+ collect
508
+ (cond
509
+ ((when-let (bounds (php--thing-at-point-bounds-of-string-at-point))
510
+ (prog1 (buffer-substring-no-properties (car bounds) (cdr bounds))
511
+ (goto-char (car bounds)))))
512
+ ((looking-at php-re-token-symbols)
513
+ (prog1 (match-string-no-properties 0 )
514
+ (goto-char (match-beginning 0 ))))
515
+ (t
516
+ (buffer-substring-no-properties (point )
517
+ (save-excursion (php--c-end-of-token) (point ))))))))))
518
+
519
+ (defun php-get-pattern ()
520
+ " Find the pattern we want to complete.
521
+ `find-tag-default' from GNU Emacs etags.el."
522
+ (car (php-leading-tokens 1 )))
493
523
494
524
; ;; Provide support for Flymake so that users can see warnings and
495
525
; ;; errors in real-time as they write code.
0 commit comments