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
4 changes: 4 additions & 0 deletions php-face.el
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
"PHP Mode face used to highlight constants."
:group 'php-faces)

(defface php-magical-constant '((t (:inherit font-lock-builtin-face)))
"PHP Mode face used to highlight magical constants."
:group 'php-faces)

(defface php-$this '((t (:inherit php-constant)))
"PHP Mode face used to highlight $this variables."
:group 'php-faces)
Expand Down
10 changes: 5 additions & 5 deletions php-mode-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -701,13 +701,12 @@ style from Drupal."
"__IS_CONSTANT__"
"IS_CONSTANT99"
"extraconstant"
"ClassName"
"class;")))
"ClassName")))
(dolist (variable variables)
(search-forward variable)
(goto-char (match-beginning 0))
(should (eq 'php-constant
(get-text-property (point) 'face))))))
(should (equal (cons variable 'php-constant)
(cons variable (get-text-property (point) 'face)))))))
Copy link
Member Author

@zonuexe zonuexe May 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To improve the visibility of test results:

Before

Test php-mode-test-constants condition:
    (ert-test-failed
     ((should
       (eq 'php-constant
	    (get-text-property ... ...)))
      :form
      (eq php-constant php-magical-constant)
      :value nil))

After

Test php-mode-test-constants condition:
    (ert-test-failed
     ((should
       (equal
	(cons variable ...)
	(cons variable ...)))
      :form
      (equal
       ("class;" . php-constant)
       ("class;" . php-magical-constant))
      :value nil :explanation
      (cdr
       (different-atoms php-constant php-magical-constant))))


;; Set default
(custom-set-variables '(php-extra-constants (quote ())))
Expand Down Expand Up @@ -989,6 +988,7 @@ Meant for `php-mode-test-issue-503'."
(with-php-mode-test ("lang/types/cast.php" :faces t))
(with-php-mode-test ("lang/types/function.php" :faces t))
(with-php-mode-test ("lang/types/keywords.php" :faces t))
(with-php-mode-test ("lang/errorcontrol.php" :faces t)))
(with-php-mode-test ("lang/errorcontrol.php" :faces t))
(with-php-mode-test ("lang/magical-constants/echo.php" :faces t)))

;;; php-mode-test.el ends here
3 changes: 2 additions & 1 deletion php-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1409,12 +1409,13 @@ a completion list."
;; - when used as cast, so that (int) and (array) look the same
("(\\(array\\))" 1 font-lock-type-face)

(,(regexp-opt php-magical-constants 'symbols) (1 'php-magical-constant))
;; namespaces
("\\(\\([a-zA-Z0-9_]+\\\\\\)+[a-zA-Z0-9_]+\\|\\(\\\\[a-zA-Z0-9_]+\\)+\\)[^:a-zA-Z0-9_\\\\]" 1 'font-lock-type-face)
("\\(\\([a-zA-Z0-9_]+\\\\\\)+[a-zA-Z0-9_]+\\|\\(\\\\[a-zA-Z0-9_]+\\)+\\)::" 1 'php-constant)

;; Support the ::class constant in PHP5.6
("\\sw+\\(::\\)\\(class\\)\\b" (1 'php-paamayim-nekudotayim) (2 'php-constant))
("\\sw+\\(::\\)\\(class\\)\\b" (1 'php-paamayim-nekudotayim) (2 'php-magical-constant))

;; Highlight static method calls as such. This is necessary for method
;; names which are identical to keywords to be highlighted correctly.
Expand Down
8 changes: 8 additions & 0 deletions php.el
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ You can replace \"en\" with your ISO language code."
:group 'php
:type 'string)

;;; PHP Keywords
(defconst php-magical-constants
(list "__LINE__" "__FILE__" "__FUNCTION__" "__CLASS__" "__TRAIT__" "__METHOD__" "__NAMESPACE__")
"Magical keyword that is expanded at compile time.

These are different from \"constants\" in strict terms.
see https://www.php.net/manual/language.constants.predefined.php")

;;; Utillity for locate language construction
(defsubst php-in-string-p ()
"Return non-nil if inside a string.
Expand Down
2 changes: 1 addition & 1 deletion tests/constants.php.faces
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
("Class name resolution is a special case\n" . font-lock-comment-face)
("stdClass" . php-constant)
("::" . php-paamayim-nekudotayim)
("class" . php-constant)
("class" . php-magical-constant)
(";\n")
("SomeClass" . php-constant)
("::" . php-paamayim-nekudotayim)
Expand Down
11 changes: 11 additions & 0 deletions tests/lang/magical-constants/echo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

echo __LINE__, PHP_EOL;
echo __FILE__, PHP_EOL;
echo __FILE__, PHP_EOL;
echo __FUNCTION__, PHP_EOL;
echo __CLASS__, PHP_EOL;
echo __TRAIT__, PHP_EOL;
echo __METHOD__, PHP_EOL;
echo __NAMESPACE__, PHP_EOL;
echo \Directory::class, PHP_EOL;
59 changes: 59 additions & 0 deletions tests/lang/magical-constants/echo.php.faces
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
;; -*- mode: emacs-lisp -*-
(("<?php" . php-php-tag)
("\n\n")
("echo" . php-keyword)
(" ")
("__LINE__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("__FILE__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("__FILE__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("__FUNCTION__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("__CLASS__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("__TRAIT__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("__METHOD__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("__NAMESPACE__" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n")
("echo" . php-keyword)
(" ")
("\\Directory" . php-constant)
("::" . php-paamayim-nekudotayim)
("class" . php-magical-constant)
(", ")
("PHP_EOL" . php-constant)
(";\n"))