Skip to content

Commit 112135f

Browse files
authored
enh(php) named arguments and fix php constants (#3459)
* enh(php) named arguments * fix(php) PHP constants
1 parent 8a8835b commit 112135f

File tree

4 files changed

+79
-18
lines changed

4 files changed

+79
-18
lines changed

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## Version 11.4.1
2+
3+
Grammars:
4+
5+
- enh(php) named arguments [Wojciech Kania][]
6+
- fix(php) PHP constants [Wojciech Kania][]
7+
8+
[Wojciech Kania]: https://github.com/wkania
9+
110
## Version 11.4.0
211

312
New Language:

src/languages/php.js

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -328,21 +328,6 @@ export default function(hljs) {
328328
]
329329
};
330330

331-
const FUNCTION_INVOKE = {
332-
relevance: 0,
333-
match: [
334-
/\b/,
335-
// to prevent keywords from being confused as the function title
336-
regex.concat("(?!fn\\b|function\\b|", normalizeKeywords(KWS).join("\\b|"), "|", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
337-
IDENT_RE,
338-
regex.concat(WHITESPACE, "*"),
339-
regex.lookahead(/(?=\()/)
340-
],
341-
scope: {
342-
3: "title.function.invoke",
343-
}
344-
};
345-
346331
const CONSTANT_REFERENCE = regex.concat(IDENT_RE, "\\b(?!\\()");
347332

348333
const LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON = {
@@ -368,6 +353,20 @@ export default function(hljs) {
368353
2: "variable.language",
369354
},
370355
},
356+
{
357+
match: [
358+
PASCAL_CASE_CLASS_NAME_RE,
359+
regex.concat(
360+
/::/,
361+
regex.lookahead(/(?!class\b)/)
362+
),
363+
CONSTANT_REFERENCE,
364+
],
365+
scope: {
366+
1: "title.class",
367+
3: "variable.constant",
368+
},
369+
},
371370
{
372371
match: [
373372
PASCAL_CASE_CLASS_NAME_RE,
@@ -394,6 +393,44 @@ export default function(hljs) {
394393
]
395394
};
396395

396+
const NAMED_ARGUMENT = {
397+
scope: 'attr',
398+
match: regex.concat(IDENT_RE, regex.lookahead(':'), regex.lookahead(/(?!::)/)),
399+
};
400+
const PARAMS_MODE = {
401+
relevance: 0,
402+
begin: /\(/,
403+
end: /\)/,
404+
keywords: KEYWORDS,
405+
contains: [
406+
NAMED_ARGUMENT,
407+
VARIABLE,
408+
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
409+
hljs.C_BLOCK_COMMENT_MODE,
410+
STRING,
411+
NUMBER,
412+
CONSTRUCTOR_CALL,
413+
],
414+
};
415+
const FUNCTION_INVOKE = {
416+
relevance: 0,
417+
match: [
418+
/\b/,
419+
// to prevent keywords from being confused as the function title
420+
regex.concat("(?!fn\\b|function\\b|", normalizeKeywords(KWS).join("\\b|"), "|", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
421+
IDENT_RE,
422+
regex.concat(WHITESPACE, "*"),
423+
regex.lookahead(/(?=\()/)
424+
],
425+
scope: {
426+
3: "title.function.invoke",
427+
},
428+
contains: [
429+
PARAMS_MODE
430+
]
431+
};
432+
PARAMS_MODE.contains.push(FUNCTION_INVOKE);
433+
397434
return {
398435
case_insensitive: false,
399436
keywords: KEYWORDS,
@@ -440,7 +477,6 @@ export default function(hljs) {
440477
/const/,
441478
/\s/,
442479
IDENT_RE,
443-
/\s*=/,
444480
],
445481
scope: {
446482
1: "keyword",
@@ -476,7 +512,7 @@ export default function(hljs) {
476512
STRING,
477513
NUMBER
478514
]
479-
}
515+
},
480516
]
481517
},
482518
{
@@ -520,7 +556,7 @@ export default function(hljs) {
520556
]
521557
},
522558
STRING,
523-
NUMBER
559+
NUMBER,
524560
]
525561
};
526562
}

test/markup/php/functions.expect.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@
4141
*/</span>
4242
<span class="hljs-variable">$fun</span> = <span class="hljs-title function_ invoke__">mb_strlen</span>();
4343
<span class="hljs-variable">$fun</span>();
44+
45+
<span class="hljs-comment">/**
46+
* Named arguments
47+
*/</span>
48+
<span class="hljs-title function_ invoke__">setAlarm</span>(
49+
<span class="hljs-attr">label</span>: <span class="hljs-string">&#x27;foo&#x27;</span>,
50+
<span class="hljs-attr">time</span>:<span class="hljs-title function_ invoke__">time</span>() + <span class="hljs-keyword">array</span>(<span class="hljs-number">5</span>)[<span class="hljs-number">0</span>] + <span class="hljs-title class_">Foo</span>::<span class="hljs-variable constant_">HOUR</span>,
51+
);

test/markup/php/functions.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ function testMe(string|int $name): int
4141
*/
4242
$fun = mb_strlen();
4343
$fun();
44+
45+
/**
46+
* Named arguments
47+
*/
48+
setAlarm(
49+
label: 'foo',
50+
time:time() + array(5)[0] + Foo::HOUR,
51+
);

0 commit comments

Comments
 (0)