Saturday, April 15, 2006

Hacking IntelliJ for syntax highlighting

Currently, IntelliJ does not provide support for Ruby. This is quite unfortunate in my opinion since I'm primarily developing in Ruby these days. However, Obie and I found a trick/hack for providing custom syntax highlighting via regular expressions.

IntelliJ provides the ability to set up custom TODO markers by specifying a regular expression. When a custom TODO match is found the style attributes of the text matched are altered by the definition of the TODO. The available style attributes you can change include color, bold, italics, and many more. To create a custom TODO, click CTRL + ALT + S, then click R.

Creating patterns for Ruby range from simple to impossible. I have about 10 defined, such as coloring symbols red :[a-z][A-Za-z]*\w+ and making classes bold ([A-Z]+[a-z]+)+. Problems occur because any part of the match will be affected. Therefore, checking for ',true' can be problematic. It's impossible to highlight some things without highlighting a comma or parenthesis. Remember, this is just a hack after all.

The last part of the hack requires setting the block comment. IntelliJ only performs TODO matching on comments. Therefore, you have to set the begin block comment in your File Types (CTRL + ALT + S, K, Ruby) to something that you are willing to put at the top of every file you wish to be highlighted. We considered using 'require' but in the end decided on '#rh'. For block commenting to correctly work you will need to specify an end block comment also. For this we chose '#/rh'; however, this never appears in our code. Because we want the entire file to appear as a comment, you never need to include the end block comment text.

Hopefully the IntelliJ team is working to include Ruby support. Until then, I hope this hack can provide you some value. Here's what developing Ruby in IntelliJ looks like to me:

Post a Comment