From 957e4b3b41a45c98e6d2177ff91c861bd695a1bf Mon Sep 17 00:00:00 2001 From: Wojciech Kania Date: Sun, 12 Dec 2021 19:11:43 +0100 Subject: [PATCH] enh(php) support class constructor call --- CHANGES.md | 3 +++ src/languages/php.js | 25 ++++++++++++++++++++----- test/markup/php/functions.expect.txt | 4 ++-- test/markup/php/functions.txt | 2 +- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6bafdafe09..395b87e7ee 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,9 @@ These changes should be for the better and should not be super noticeable but if Grammars: +- enh(php) support class constructor call (#3427) [Wojciech Kania][] +- enh(php) support function invoke (#3427) [Wojciech Kania][] +- enh(php) Switch highlighter to partially case-insensitive (#3427) [Wojciech Kania][] - enh(php) improve `namespace` and `use` highlighting (#3427) [Josh Goebel][] - enh(php) `$this` is a `variable.language` now (#3427) [Josh Goebel][] - enh(php) add `__COMPILER_HALT_OFFSET__` (#3427) [Josh Goebel][] diff --git a/src/languages/php.js b/src/languages/php.js index baf64f5d9f..eb33285d81 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -288,6 +288,23 @@ export default function(hljs) { built_in: BUILT_INS.concat([ "Error|0" ]), }; + const CONSTRUCTOR_CALL = { + variants: [ + { + match: [ + /new/, + /\s+/, + /\\?\w+/, + /\s*\(/, + ], + scope: { + 1: "keyword", + 3: "title.class", + }, + } + ] + }; + const FUNCTION_INVOKE = { relevance: 0, match: [ @@ -301,6 +318,7 @@ export default function(hljs) { 3: "function.title.invoke", } }; + return { case_insensitive: false, keywords: KEYWORDS, @@ -343,12 +361,9 @@ export default function(hljs) { FUNCTION_INVOKE, { // swallow composed identifiers to avoid parsing them as keywords - begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?!\()/ - }, - { - // swallow create object - begin: /new\s\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\s?\(/ + begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ }, + CONSTRUCTOR_CALL, { className: 'function', relevance: 0, diff --git a/test/markup/php/functions.expect.txt b/test/markup/php/functions.expect.txt index ba14faad0b..63dc8aefd3 100644 --- a/test/markup/php/functions.expect.txt +++ b/test/markup/php/functions.expect.txt @@ -10,9 +10,9 @@ /** * Function invoke */ -$date = new DateTimeImmutable (); +$date = new DateTimeImmutable (); $date->format('Y-m-d'); -DateTimeImmutable::createFromMutable(new \DateTime()); +DateTimeImmutable::createFromMutable(new \DateTime('now')); str_contains (\strtoupper(substr('abcdef', -2), 'f')); diff --git a/test/markup/php/functions.txt b/test/markup/php/functions.txt index 525442d0bf..015c63a917 100644 --- a/test/markup/php/functions.txt +++ b/test/markup/php/functions.txt @@ -13,6 +13,6 @@ $fn2 = function ($x) use ($y) { $date = new DateTimeImmutable (); $date->format('Y-m-d'); -DateTimeImmutable::createFromMutable(new \DateTime()); +DateTimeImmutable::createFromMutable(new \DateTime('now')); str_contains (\strtoupper(substr('abcdef', -2), 'f'));