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:


  1. I'm not sure if I've missed something in your post..

    But why aren't you defining a new filetype with a list of keywords? Ruby isn't that different to Groovy in this aspect, is it? And Groovy syntax is highlighted using this approach.. I only changed the line comment entry in the "Groovy Script Files.xml" and added a bunch of Ruby keywords.. Works fine for me.. I used a bit of JRuby in some projects. Works nicely in IDEA. (Especially the combination of JUnit and JRuby and Java.)

    But you're probably more interested in support for block comments, the names of the classes, and % quotes, right? Which afaik can't be defined with these filetype XML files.. On the other hand there shouldn't be any trouble with stuff like ,true etc.. This is handled properly by IDEA.


  2. Alright, I got your point. I just checked out the syntax hilighting I'm using. It never really occurred to me, but using this 'XML filetype definition' merely gives you coloured strings and comments, and only a few hilighted keywords.. Weird that it never bothered me.. :)

    I'm looking at the Codehaus Groovy stuff right now.. Maybe it's easy enough to extract some parts from there and turn them into simple Ruby support.

    Slightly off-topic: Doesn't it bother you how big IDEA got by now? I wonder if Ruby and IDEA are such a good team..

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

  4. Quick follow-up: I spent quite a few evenings working on a generic highlighter (with basic Ruby highlighting as the example config). You may want to have a look here.


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


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