Rainbow syntax highlighters were created for LISP and related languages, not C++ and imperative languages, but they can be quite useful. Sadly, nothing is easy, and trying to find an IDE that worked on my PC and allowed custom rainbow syntax highlighting proved to be more difficult to find than I imagined. However, I was determined to not write an entire IDE just for syntax highlighting. In fact, I was determined to keep it simple and avoid even having to learn Eclipse (so that I could write an extension). My persistence paid off.
My default editor is GEdit, which uses the GTK Source View. Writing a custom highlighter for GTK Source View would allow me to use it in a number of other applications. The bummer is that the custom highlighter colors must be defined within the styles (known as the “theme” from inside some applications) folder, not the language specifications folder. At the same time, I still need to write a custom language highlighter to define exactly what tokens are highlighted, how, and why. What does all this gibberish mean? It means I must write two files instead of one, and that the user has the slightly tedious task of changing both the code theme (style of the GTK Source View) and the syntax highlighting in order to get rainbow syntax highlighting.
What are the benefits of rainbow syntax highlighting anyways? How is my highlighter better than existing ones? First, rainbow highlighting allows programmers to recognize the beginnings and ends of sections of code enclosed by special characters such as parentheses and square brackets. It can show when you have forgotten to add that closing parenthesis, which is quite important in a language (such as Lisp) that revolves around using parentheses.
Enough talk, the code is on Github inside the CopperLang project.
The files belong in their respective GTK Source View folders with the same name, located in:
You will need root access, mind you, but I trust you can figure that out on your own.
Note that, for GEdit, you will need to add the color scheme via Preferences > Font and Colors, within the actual program GEdit. Otherwise, GEdit won’t recognize it… unless you want to find the mysterious config file and edit that by hand.
Now for bashing it: Unfortunately, the gtk syntax highlighter can only handle so many layers of nesting. The coloring is messed up if you nest too much. Fortunately, closing all of the parentheses prevents the color invalidation from spreading beyond a single group. I tried fixing this to no avail. While there is a matching parenthesis highlighter, it would be nicer if the color always stayed consistent.
Those of you who have browsed the repository already may have noticed that it has been there quite some time. Indeed, I wrote this post quite some time ago and hadn’t published it. Fortunately, this is not one of those things that has changed much. A few changes here and there still need to be uploaded to Github (oops), but those are mostly aesthetic updates specifically for the language of Copper (such as keyword highlighting). You should find it quite useful for working with C++ (as I have… working on the Copper engine. XD).