1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, it works! So I guess I had to use this "injections" with "pattern" you mentioned alongside the regular "patterns" including "source.php".
There is one issue though, it does override support.other.namespace.php (class name it extends from) but it does not override entity.name.type.class.php (the class name itself). It does work when I do not include source.php. Do you have any idea why maybe?
you can only inject into a patterns array within the begin/end/while rules. prob need to inject into a higher scope and override the whole rule

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.