Skip to content

Commit

Permalink
enh(php) named arguments and fix php constants (#3459)
Browse files Browse the repository at this point in the history
* enh(php) named arguments
* fix(php) PHP constants
  • Loading branch information
wkania authored Jan 12, 2022
1 parent 8a8835b commit 112135f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Version 11.4.1

Grammars:

- enh(php) named arguments [Wojciech Kania][]
- fix(php) PHP constants [Wojciech Kania][]

[Wojciech Kania]: https://github.com/wkania

## Version 11.4.0

New Language:
Expand Down
72 changes: 54 additions & 18 deletions src/languages/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,6 @@ export default function(hljs) {
]
};

const FUNCTION_INVOKE = {
relevance: 0,
match: [
/\b/,
// to prevent keywords from being confused as the function title
regex.concat("(?!fn\\b|function\\b|", normalizeKeywords(KWS).join("\\b|"), "|", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
IDENT_RE,
regex.concat(WHITESPACE, "*"),
regex.lookahead(/(?=\()/)
],
scope: {
3: "title.function.invoke",
}
};

const CONSTANT_REFERENCE = regex.concat(IDENT_RE, "\\b(?!\\()");

const LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON = {
Expand All @@ -368,6 +353,20 @@ export default function(hljs) {
2: "variable.language",
},
},
{
match: [
PASCAL_CASE_CLASS_NAME_RE,
regex.concat(
/::/,
regex.lookahead(/(?!class\b)/)
),
CONSTANT_REFERENCE,
],
scope: {
1: "title.class",
3: "variable.constant",
},
},
{
match: [
PASCAL_CASE_CLASS_NAME_RE,
Expand All @@ -394,6 +393,44 @@ export default function(hljs) {
]
};

const NAMED_ARGUMENT = {
scope: 'attr',
match: regex.concat(IDENT_RE, regex.lookahead(':'), regex.lookahead(/(?!::)/)),
};
const PARAMS_MODE = {
relevance: 0,
begin: /\(/,
end: /\)/,
keywords: KEYWORDS,
contains: [
NAMED_ARGUMENT,
VARIABLE,
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
hljs.C_BLOCK_COMMENT_MODE,
STRING,
NUMBER,
CONSTRUCTOR_CALL,
],
};
const FUNCTION_INVOKE = {
relevance: 0,
match: [
/\b/,
// to prevent keywords from being confused as the function title
regex.concat("(?!fn\\b|function\\b|", normalizeKeywords(KWS).join("\\b|"), "|", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
IDENT_RE,
regex.concat(WHITESPACE, "*"),
regex.lookahead(/(?=\()/)
],
scope: {
3: "title.function.invoke",
},
contains: [
PARAMS_MODE
]
};
PARAMS_MODE.contains.push(FUNCTION_INVOKE);

return {
case_insensitive: false,
keywords: KEYWORDS,
Expand Down Expand Up @@ -440,7 +477,6 @@ export default function(hljs) {
/const/,
/\s/,
IDENT_RE,
/\s*=/,
],
scope: {
1: "keyword",
Expand Down Expand Up @@ -476,7 +512,7 @@ export default function(hljs) {
STRING,
NUMBER
]
}
},
]
},
{
Expand Down Expand Up @@ -520,7 +556,7 @@ export default function(hljs) {
]
},
STRING,
NUMBER
NUMBER,
]
};
}
8 changes: 8 additions & 0 deletions test/markup/php/functions.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@
*/</span>
<span class="hljs-variable">$fun</span> = <span class="hljs-title function_ invoke__">mb_strlen</span>();
<span class="hljs-variable">$fun</span>();

<span class="hljs-comment">/**
* Named arguments
*/</span>
<span class="hljs-title function_ invoke__">setAlarm</span>(
<span class="hljs-attr">label</span>: <span class="hljs-string">&#x27;foo&#x27;</span>,
<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>,
);
8 changes: 8 additions & 0 deletions test/markup/php/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ function testMe(string|int $name): int
*/
$fun = mb_strlen();
$fun();

/**
* Named arguments
*/
setAlarm(
label: 'foo',
time:time() + array(5)[0] + Foo::HOUR,
);

0 comments on commit 112135f

Please sign in to comment.