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:

2 comments:

  1. Anonymous12:42 PM

    Someone is already working to write the IntelliJ plug-in for Ruby support, but I can't let the cat out of the bag yet. Despite the fact that they've already dropped a hit.

    As far as the size of IntelliJ, honestly, I have no idea. I've never looked at the code-base. Really, I don't even have much experience in the IDE. My only Java development was pre-IntelliJ days.

    But, I do believe that Ruby and IDEA is the best option. IntelliJ could be better, but you can't deny the fact that it's currently the best.

    ReplyDelete
  2. Anonymous4:09 PM

    the file I use to get syntax highlighting:
    other.xml
    just copy over the existing other.xml in config\options.

    ReplyDelete

Note: Only a member of this blog may post a comment.