Gillespie
Gillespie

Reputation: 6561

VSCode Python Language Extension

I'm trying to create a VSCode extension for Ribosome .py.dna files

Basically, .py.dna is identical to python, except that lines that start with . should have comment syntax highlighting.

So far, this is what I have for my tmLanguage.json:

{
    "name": "RibosomePython",
    "patterns": [
        {
            "include": "#dots"
        }
    ],
    "repository": {
        "dots": {
            "name": "comment.dna",
            "begin": "\\.",
            "end": "$"
        }
    },
    "scopeName": "source.python.dna"
}

This works, in that lines starting with . have python comment syntax highlighting. But now I'm not sure how to tell VSCode to have the real Python grammar highlight everything else. How can I do this?

My package.json looks like:

{
    "name": "ribosome-dna",
    "displayName": "Ribosome DNA",
    "description": "Ribosome DNA Syntax Highlighting",
    "version": "0.0.1",
    "publisher": "rpgillespie",
    "engines": {
        "vscode": "^1.17.0"
    },
    "categories": [
        "Languages"
    ],
    "contributes": {
        "languages": [{
            "id": "dna",
            "aliases": ["DNA"],
            "extensions": [".py.dna"],
            "configuration": "./language-configuration.json"
        }],
        "grammars": [
            {
                "language": "dna",
                "scopeName": "source.python.dna",
                "path": "./syntaxes/dna.tmLanguage.json"
            }
        ]
    }
}

Note I was able to get it to work the way I wanted by copying and modifying python's grammar but this seems like overkill.

Edit:

For the curious, finished extension published here.

Upvotes: 2

Views: 533

Answers (1)

Gama11
Gama11

Reputation: 34138

Just add "include": "source.python" to your patterns:

{
    "name": "RibosomePython",
    "patterns": [
        {
            "include": "#dots"
        },
        {
            "include": "source.python"
        }
    ],
    "repository": {
        "dots": {
            "name": "comment.dna",
            "begin": "\\.",
            "end": "$"
        }
    },
    "scopeName": "source.python.dna"
}

This feature is called injection grammar. VSCode added support for this in response to #2793.

Upvotes: 1

Related Questions