Skip to content

About

Small VS Code extension to enable highlighting of parameters in PHP docblocks

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

PHPDoc parameter highlighting in VS Code

Problem

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 parameter

editor.tokenColorCustomizations only recolors scopes the grammar already produces. It can't split one token into two.

Solution: a grammar-injection extension

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.

Project structure

phpdoc-param-highlight/
├── package.json
└── syntaxes/
    └── phpdoc-param.injection.json

package.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.

syntaxes/phpdoc-param.injection.json

{
  "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" }
      }
    }
  ]
}

Design notes

  • injectionSelector limits 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 @param rule 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-write and @var, which use the same type $name description shape.

Limitations

  • 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.

Install

The most reliable way is to package the folder as a VSIX:

  1. Inside the folder, run npx @vscode/vsce package. It may warn about a missing README, license or repository field; you can accept those warnings.
  2. In VS Code, run Extensions: Install from VSIX… and pick the generated .vsix file.

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.

Color the new scopes

In settings.json:

"editor.tokenColorCustomizations": {
  "textMateRules": [
    {
      "scope": "variable.parameter.phpdoc.php",
      "settings": { "foreground": "#E5C07B" }
    },
    {
      "scope": "comment.description.phpdoc.php",
      "settings": { "foreground": "#7F848E", "fontStyle": "italic" }
    }
  ]
}

Check that it works

  1. Run Developer: Reload Window.
  2. Put the cursor on the parameter name and run Developer: Inspect Editor Tokens and Scopes.
  3. Check that the scope list shows variable.parameter.phpdoc.php, and comment.description.phpdoc.php on 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.

Background

  • 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.

About

Small VS Code extension to enable highlighting of parameters in PHP docblocks

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors