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.
My syntax highlighter goes a step further than just parentheses. It also checks for curly brackets. If you can’t tell, it means I’ve tailored this highlighter to Copper, but it should also be very useful for JavaScript. Another thing, my highlighter makes the parentheses and brackets themselves a new color – the color of the next level of nesting – rather than the color of the first layer (which seems to be what other rainbow syntax highlighters do, or so I’ve seen). This is very useful to me because it saves a few keystrokes in writing. Think about it: You’ve just typed an opening parenthesis. Without a color change to stimulate your mind, you forget about the fact that you have opened a new scope and you type another character. Realizing your mistake, you have to hit the backspace (maybe more than once, to get rid of that tab and newline character) and add that silly closing parenthesis. Time wasted. What a pity. You will waste countless hours on hitting the wrong keystroke. May you waste fewer because I have lit the way… properly! Ok, so that’s a minor point. On top of that, by having parentheses be the color of the next level, you can tell if a parentheses does not close anything. This can happen if the parenthesis is an extra (because you somehow didn’t notice you already added it).

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).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s