Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lisp/php.el
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,17 @@ indentation."

;;;###autoload
(defun php-mode-maybe ()
"Select PHP mode or other major mode."
"Select PHP mode or other major mode.

The selected mode is resolved through `major-mode-remap-alist' the way
`set-auto-mode' would have done had `auto-mode-alist' named it directly,
so that a remapping the user asked for still applies. Emacs's built-in
`php-ts-mode' registers (php-mode . php-ts-mode) for exactly this
purpose, and `treesit-enabled-modes' installs it when the user opts in."
(interactive)
(run-hooks php-mode-maybe-hook)
(funcall (php-derivation-major-mode)))
(let ((mode (php-derivation-major-mode)))
(funcall (if (fboundp 'major-mode-remap) (major-mode-remap mode) mode))))

;;;###autoload
(defun php-current-class ()
Expand Down
35 changes: 35 additions & 0 deletions tests/php-mode-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,41 @@ than depending on `poly-php', because that package pulls in a released
(set-buffer-modified-p nil)
(setq buffer-file-name nil)))))

(define-derived-mode php-mode-test--stub-ts-mode prog-mode "PHP/stub-ts"
"Stand-in for `php-ts-mode' in `php-mode-test-mode-remap'.
Using a stub keeps the test independent of whether the PHP tree-sitter
grammar is installed.")

(ert-deftest php-mode-test-mode-remap ()
"`php-mode-maybe' must honor `major-mode-remap-alist'.

Emacs's built-in `php-ts-mode' registers (php-mode . php-ts-mode) in
`treesit-major-mode-remap-alist' so that users can toggle between this
package and the core tree-sitter mode; `treesit-enabled-modes' copies
that entry into `major-mode-remap-alist' when they opt in.

`set-auto-mode' applies the remapping to whatever `auto-mode-alist'
names, so file patterns pointing straight at `php-mode' honored it while
those going through `php-mode-maybe' did not: the same buffer became
`php-mode' for \".php\" but the remapped mode for \".stub\"."
(skip-unless (fboundp 'major-mode-remap))
(dolist (probe '((((php-mode . php-mode-test--stub-ts-mode))
. php-mode-test--stub-ts-mode)
;; Without a remapping the derived mode is used as-is.
(nil . php-mode)))
(let ((major-mode-remap-alist (car probe)))
(with-temp-buffer
;; `php-derivation-major-mode' consults `buffer-file-name'.
(setq buffer-file-name (expand-file-name "remap.php" temporary-file-directory))
(unwind-protect
(progn
(insert "<?php\necho 'hi';\n")
(php-mode-maybe)
(should (equal (cons (car probe) (cdr probe))
(cons (car probe) major-mode))))
(set-buffer-modified-p nil)
(setq buffer-file-name nil))))))

(ert-deftest php-mode-test-php74 ()
"Test highlighting language constructs added in PHP 7.4."
(with-php-mode-test ("7.4/arrow-function.php" :faces t))
Expand Down
Loading