Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I extend VS Code TextMate Language Grammars?

Is there a way to extend the default csharp.tmLanguage with some additional rule? With references to existing scopes?

like image 472
Geri Borbás Avatar asked Mar 30 '17 17:03

Geri Borbás


1 Answers

You can include a grammar just like:

{ "include": "source.cs" }

But if you replace the grammar with your own (defining your own source.cs scope), then you cannot include the original source.cs scope anymore into it (VS Code reports it failed tokenize the file).

Visual Studio Code supports grammar injection, like:

"grammars": [
            {
                "scopeName": "source.todo",
                "path": "./syntaxes/todo.json",
                "injectTo": [  "source.js", "source.ts" ]
            },

Which is surprisingly under documented. It works, but you can inject into existing scopes, except the top level source scope. Also, it seems to me you can inject additions only, instead override rules. So again, I cannot extend grammar this way.


For now I go with a workaround, where I contribute 2 grammars. One is the original C# grammar with a fake name, the 2nd is the overwriting C# grammar, including all the original rules via the fake scope. I wish there was a way without this.

This way I can add some rules "before" I include the original grammar. But still, these are additions, I cannot really extend existing rules.

like image 142
Geri Borbás Avatar answered Sep 28 '22 16:09

Geri Borbás