JavaScript-inside-Ruby syntax highlighting with TextMate
If you had a look into the Netzke code, you’d see that for constructing JavaScript classes, a lot of the JavaScript code gets dynamically generated by Ruby. JavaScript code is mixed right into the Ruby functions. I adore the power of generating one dynamic language code with another dynamic language, however I didn’t feel comfortable typing JavaScript inside Ruby due the following reason: TextMate would neither highlight the JavaScript fragments, nor allow you to use the JavaScript-bundle goodies (like snippets) in it, as it “sees” the JavaScript code as simple Ruby strings. If you’d like to know how to reclaim that comfort - read on.
We want to modify the language grammar of the TextMate’s Ruby-on-Rails bundle, so that it could detect our JavaScript code and apply all the necessary rules and patterns. To make the detection of the JavaScript code easy, I chose for a convention to always wrap it up into the following Ruby construct, which defines a string:
<<-JS
// JavaScript code comes here
JS
Now we only need to tell the Ruby-on-Rails bundle to find the code fragments limited by these 2 lines. To do this, open the TextMate bundle editor (by pressing ctrl+alt+command+B). Go to the Ruby-on-Rails bundle and select the Ruby-on-Rails language grammar (the item in the selector marked with the capital ‘L’). Find the section defining the parsing patterns, and insert the following piece:
{ name = "source.js";
begin = "<<-JS";
end = "JS";
patterns = ( { include = "source.js"; } );
},
so that it looks like this:

And that’s all there’s to it.
