Skip to content

Commit

Permalink
enh(php) support class constructor call
Browse files Browse the repository at this point in the history
  • Loading branch information
wkania committed Dec 12, 2021
1 parent 8614a10 commit 957e4b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -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][]
Expand Down
25 changes: 20 additions & 5 deletions src/languages/php.js
Expand Up @@ -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: [
Expand All @@ -301,6 +318,7 @@ export default function(hljs) {
3: "function.title.invoke",
}
};

return {
case_insensitive: false,
keywords: KEYWORDS,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions test/markup/php/functions.expect.txt
Expand Up @@ -10,9 +10,9 @@
<span class="hljs-comment">/**
* Function invoke
*/</span>
<span class="hljs-variable">$date</span> = new DateTimeImmutable ();
<span class="hljs-variable">$date</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">DateTimeImmutable</span> ();
<span class="hljs-variable">$date</span>-&gt;<span class="hljs-function title_ invoke__">format</span>(<span class="hljs-string">&#x27;Y-m-d&#x27;</span>);

DateTimeImmutable::<span class="hljs-function title_ invoke__">createFromMutable</span>(new \DateTime());
DateTimeImmutable::<span class="hljs-function title_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>(<span class="hljs-string">&#x27;now&#x27;</span>));

<span class="hljs-function title_ invoke__">str_contains</span> (\<span class="hljs-function title_ invoke__">strtoupper</span>(<span class="hljs-function title_ invoke__">substr</span>(<span class="hljs-string">&#x27;abcdef&#x27;</span>, -<span class="hljs-number">2</span>), <span class="hljs-string">&#x27;f&#x27;</span>));
2 changes: 1 addition & 1 deletion test/markup/php/functions.txt
Expand Up @@ -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'));

0 comments on commit 957e4b3

Please sign in to comment.