Extending a grammar


Is there a way to extend, say, the language-c grammar? Essentially, I want to add a single pattern which is custom to a small set of c projects I work on. I started a package and added the rule there, but it completely overrides language-c now. Is there a way to have multiple grammars apply to the same file type?

How to extend/modify existing syntax?
Languageception & grammar packages
How to extend a syntax?

There are a couple suggestions in this post:

But I haven’t tried them, so I don’t know how well they’ll work for your purposes …


Awesome! The include one works.

"patterns": [
    { "include": "source.c" }

Did the trick for me!


I have done the same to extend a python language grammar. The { “include”: “source.python” } in the cson file within the grammar folder of my package does the job of making all python syntax being highlighted. However when I use the toggle comment keyboard shortcut, it adds /* and */ to the begin and end of the line instead of #.
Furthermore the autocompletion does not give me python options. How can that be fixed?


The toggle comments and autocomplete features are looking at the scope of wherever the cursor is, so if it’s not inside an area that reads as Python, they aren’t going to trigger correctly. Mind sharing your language package?


Thank you for your answer!
I even copied your language-rollcsv package :smiley:
This is the current version of the package: https://github.com/StefanLeupold/dot-atom/tree/master/packages-myown/language-my-python

Apparently I was wrong on the autocompletion problem. It behaves the same if I activate python grammar.
The only remaining thing is the wrong comments on toggle command but I found a solution here:

Furthermore I had a problem with the package hydrogen as it could not determine which kernel to open in my custom language. But they provided a fix for that.

So my question is:
Will I have more trouble using an extended language? Or is there maybe a simpler solution for adding only a few custom syntax matches?

Edit: I have had another problem with using hydrogen and my extended grammar: hydrogen does not detect #%% as cell delimiters anymore. Thus I feel like extending a grammar is not a good way for simply adding a few patterns.

Instead I forked the language-todo which injects into every grammar. I had only to add one new pattern that I can now customize in my .atom/styles.less file.
For anyone interested here is my example commit: https://github.com/StefanLeupold/language-todo/commit/f32809e41cbd326e546a00e12a296098b34e6f46

If there is another easier way than forking a language, please let me know as I am very open to that.


Using injections looks like it works for you, so that’s a good solution as far as I know.

It depends on the use case. Hydrogen is a particularly complex and context-sensitive package, so it makes sense that it requires things to be “just so”. Most of the questions I’ve answered along these lines have been cases where the user only cared about having additional things highlighted.


Great, I will keep the injection method then. At a later time I may create a minimalistic grammar injection package so that I don’t need to keep up the fork.

Yes, you are right of course. In general it seems quite suitable to extend a grammar.
Thanks for your answers, I appreciate it!