VS Code's built-in PHP grammar gives @param and the type their own scopes. Everything after the type stays one plain comment.block.documentation.phpdoc.php token, so the parameter name and the description can't get different colors:
* @param int $someParameter Description of this parametereditor.tokenColorCustomizations only recolors scopes the grammar already produces. It can't split one token into two.
A TextMate grammar injection is a small grammar layered on top of the PHP one. It re-tokenizes the inside of docblocks and adds new scopes that themes and settings can target.
phpdoc-param-highlight/
├── package.json
└── syntaxes/
└── phpdoc-param.injection.json
{
"name": "phpdoc-param-highlight",
"publisher": "local",
"version": "0.0.1",
"engines": { "vscode": "^1.60.0" },
"contributes": {
"grammars": [
{
"scopeName": "phpdoc.param.injection",
"path": "./syntaxes/phpdoc-param.injection.json",
"injectTo": ["source.php", "text.html.php"]
}
]
}
}injectTo lists the grammars to inject into: plain PHP and PHP mixed with HTML.
{
"scopeName": "phpdoc.param.injection",
"injectionSelector": "L:comment.block.documentation.phpdoc.php",
"patterns": [
{
"match": "(@(?:param|property(?:-read|-write)?|var))\\s+(?:([^\\s$]\\S*)\\s+)?(\\$\\w+)(?:\\s+(.*?))?(?=\\s*\\*/|$)",
"captures": {
"1": { "name": "keyword.other.phpdoc.php" },
"2": { "name": "keyword.other.type.php" },
"3": { "name": "variable.parameter.phpdoc.php" },
"4": { "name": "comment.description.phpdoc.php" }
}
}
]
}injectionSelectorlimits the injection to text inside PHPDoc blocks.- The
L:prefix gives the injected patterns priority over the built-in docblock patterns at the same position. Without it, the built-in@paramrule matches first and this one never fires. - Captures 1 and 2 match the tag and the type again, so they need scopes too. They reuse the built-in scope names to keep their current colors.
- Capture 3 uses
variable.parameter…, a standard scope that many themes already color a little. - Capture 4 keeps the description in a
comment.*scope, so it looks like a comment until you give it a color. - The regex also covers
@property,@property-read,@property-writeand@var, which use the sametype $name descriptionshape.
- The type is matched as
\S+, so a type that contains spaces breaks the match, e.g.array<int, string>. Fixing that needs a more elaborate begin/end rule. - TextMate patterns match within one line, so a description that continues on the next lines is not colored past the first line.
The most reliable way is to package the folder as a VSIX:
- Inside the folder, run
npx @vscode/vsce package. It may warn about a missing README, license or repository field; you can accept those warnings. - In VS Code, run Extensions: Install from VSIX… and pick the generated
.vsixfile.
Copying the folder into ~/.vscode/extensions/ sometimes works too, but recent VS Code versions don't always pick up folders added by hand.
After a change to the grammar, bump version, then package and install again.
In settings.json:
"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "variable.parameter.phpdoc.php",
"settings": { "foreground": "#E5C07B" }
},
{
"scope": "comment.description.phpdoc.php",
"settings": { "foreground": "#7F848E", "fontStyle": "italic" }
}
]
}- Run Developer: Reload Window.
- Put the cursor on the parameter name and run Developer: Inspect Editor Tokens and Scopes.
- Check that the scope list shows
variable.parameter.phpdoc.php, andcomment.description.phpdoc.phpon the description.
If the new scopes appear but the colors don't change, a semantic token from a PHP language server (e.g. Intelephense or PHP Tools) may be overriding them. The inspector shows this in its semantic token section.
- TextMate grammars: regex-based syntax rules, originally from the macOS editor TextMate, reused by VS Code. Each match gets a dotted scope name, and themes color text by scope.
- Semantic highlighting: a second layer in which a language server that parses the code assigns token types. It can override TextMate colors and is customized through
editor.semanticTokenColorCustomizations.