@@ -211,6 +211,16 @@ a completion list."
211211
212212These are different from \" constants\" in strict terms.
213213see 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 )))
214224
215225; ;; Utillity for locate language construction
216226(defsubst php-in-string-p ()
@@ -432,6 +442,17 @@ can be used to match against definitions for that classlike."
432442 (eval-when-compile
433443 (php-create-regexp-for-classlike (regexp-opt '(" class" " interface" " trait" )))))
434444
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+
435456(defun php-get-current-element (re-pattern )
436457 " Return backward matched element by RE-PATTERN."
437458 (save-excursion
@@ -469,27 +490,36 @@ can be used to match against definitions for that classlike."
469490 (goto-char (match-end 0 )))))
470491 (> (point ) start)))))
471492
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\)\) "
475499 (save-excursion
476500 (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 )))
493523
494524; ;; Provide support for Flymake so that users can see warnings and
495525; ;; errors in real-time as they write code.
0 commit comments