phpvscode-extensionssyntax-highlightingtextmate

How to override PHP syntax coloring in VSCode Language


I tried both including source.php after my match rule and injection into the PHP language syntax.

Including the source shows PHP syntax coloring without my override, whereas injection shows my override without PHP coloring.

Attempt PHP Syntax My Syntax Override
With Injection
Include PHP Source (source.php)

I would like to see both work at the same time where my syntax overrides the PHP syntax.

Any ideas what is happening or how to achieve this please?

My code setup:

# Language
"languages": [
    {
        "id": "phpf",
        "aliases": ["PHPF", "phpf"],
        "extensions": [".phpf"],
        "configuration": "./language-configuration.json",
        "icon": {
        "light": "file-icons/phpf.png",
        "dark": "file-icons/phpf.png"
        }
    }
]

# Grammar
"grammars": [
    {
        "language": "phpf",
        "scopeName": "source.phpf",
        "path": "./syntaxes/phpf.json",
        "injectTo": ["source.php"]
    }
]

# Syntax
{
    "scopeName": "source.phpf",
    "fileTypes": ["phpf"],
    "name": "PHPF",
    "injectionSelector": "L:source.php",
    "patterns": [
    {
        "name": "phpf.a",
        "match": "___[A-Z]+___"
    },
    {
        "include": "source.php"
    }
    ]
}

# Coloring
"configurationDefaults": {
    "editor.tokenColorCustomizations": {
        "textMateRules": [
        {
            "scope": "phpf.a",
            "settings": {
            "fontStyle": "bold",
            "foreground": "#e24d33"
            }
        }
        ]
    }
}

Solution

  • I think you're wanting to use "injections".
    It will inject rules into your own grammar.
    Heres the schema for it "$schema": "https://raw.githubusercontent.com/RedCMD/TmLanguage-Syntax-Highlighter/main/vscode.tmLanguage.schema.json"
    Injections documentation

    "injections": {
        "L:source.phpf -comment -string": {
            "patterns": [
                {
                    "name": "phpf.a",
                    "match": "___[A-Z]+___"
                }
            ]
        }
    }
    

    "injectionSelector" & "injectTo" is used when you want to inject your language into an existing language.
    currently "injectTo": ["source.php"] is injecting your phpf into the existing php language.
    making "include": "source.php" redundant.