Skip to content

Commit c3cc4c9

Browse files
committed
Add php-pipe-op face for the PHP 8.5 pipe operator
The pipe operator came out half-fontified. No rule matched `|>', so the comparison-operator matcher -- whose `[<>]=?' alternative claims a bare `>' -- took the second character and left the first one plain: '|' -> nil '>' -> php-comparison-op Give it a face of its own. php-pipe-op inherits php-operator like the rest of the operator faces, and its rule sits ahead of the comparison operators so it wins the `>'. The rule is deliberately narrow (`|>' only), so nothing else moves: `||', `>=', `>', `===', `<=>', `=>' and a bare `|' all keep the faces they had. A test pins that down alongside the pipe itself. See https://www.php.net/releases/8.5/en.php
1 parent f0bb9eb commit c3cc4c9

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

lisp/php-face.el

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@
126126
"PHP Mode face used to object operators (->)."
127127
:tag "PHP Object Op")
128128

129+
(defface php-pipe-op '((t (:inherit php-operator)))
130+
"PHP Mode face used to the pipe operator (|>).
131+
The operator was added in PHP 8.5."
132+
:tag "PHP Pipe Op")
133+
129134
(defface php-paamayim-nekudotayim '((t ()))
130135
"PHP Mode face used to highlight scope resolution operators (::).
131136
The operator is also knows as \"Paamayim Nekudotayim\"."

lisp/php-mode.el

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,11 @@ for \\[find-tag] (which see)."
15181518
;; Assignment operators (=, +=, ...)
15191519
("\\([^=<!>]+?\\([\-+./%]?=\\)[^=<!]+?\\)" 2 'php-assignment-op)
15201520

1521+
;; Pipe operator (|>) --- PHP 8.5. Must precede the comparison
1522+
;; operators, whose `[<>]=?' alternative would otherwise claim the
1523+
;; `>' and leave the `|' unfontified.
1524+
("\\(|>\\)" 1 'php-pipe-op)
1525+
15211526
;; Comparison operators (==, ===, >=, ...)
15221527
("\\([!=]=\\{1,2\\}[>]?\\|[<>]=?\\)" 1 'php-comparison-op)
15231528

tests/php-mode-test.el

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,36 @@ path; sending those to an HTML mode would take most PHP files away from
803803
"Test highlighting language constructs added in PHP 8.4."
804804
(with-php-mode-test ("8.4/property-hooks.php" :faces t)))
805805

806+
(defun php-mode-test--faces-of (code token)
807+
"Return the list of faces on TOKEN's characters after fontifying CODE."
808+
(with-temp-buffer
809+
(insert code)
810+
(php-mode)
811+
(font-lock-ensure)
812+
(goto-char (point-min))
813+
(should (search-forward token nil t))
814+
(let ((start (- (point) (length token))))
815+
(mapcar (lambda (i) (get-text-property (+ start i) 'face))
816+
(number-sequence 0 (1- (length token)))))))
817+
818+
(ert-deftest php-mode-test-php85-pipe-op ()
819+
"The PHP 8.5 pipe operator is fontified as `php-pipe-op'.
820+
821+
Both characters must get the face. The comparison-operator matcher
822+
claims a bare `>', so without a rule of its own `|>' came out
823+
half-fontified: the `|' plain and the `>' as `php-comparison-op'."
824+
(should (equal '(php-pipe-op php-pipe-op)
825+
(php-mode-test--faces-of "<?php\n$slug = $title |> trim(...);\n" "|>")))
826+
;; Operators that the new rule must not steal from.
827+
(dolist (probe '(("<?php\n$a = $b || $c;\n" "||" (php-logical-op php-logical-op))
828+
("<?php\n$a = $b >= $c;\n" ">=" (php-comparison-op php-comparison-op))
829+
("<?php\n$a = $b > $c;\n" ">" (php-comparison-op))
830+
("<?php\n$a = $b <=> $c;\n" "<=>" (php-comparison-op php-comparison-op php-comparison-op))
831+
("<?php\n$a = $b | $c;\n" "|" (nil))))
832+
(cl-destructuring-bind (code token expected) probe
833+
(should (equal (cons token expected)
834+
(cons token (php-mode-test--faces-of code token)))))))
835+
806836
(ert-deftest php-mode-test-lang ()
807837
"Test highlighting for language constructs."
808838
(with-php-mode-test ("lang/class/anonymous-class.php" :indent t :magic t :faces t))

0 commit comments

Comments
 (0)