7

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

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172

1 Answers1

8

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.

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • Hello! I'm trying to do the same thing, but without results, can you please post your package json with multiple grammars definition? – marco burrometo Feb 09 '18 at 11:59
  • 1
    Sure, take a look at [**eppz! (C# theme for Unity)**](https://marketplace.visualstudio.com/items?itemName=eppz.eppz-code) sources. – Geri Borbás Feb 09 '18 at 12:45