Friday March 30, 2007
Code Folding for Groovy via Tokens in Schliemann
My first explorations of how to provide code folding in NetBeans IDE for Groovy, via Schliemann. Two pictures before the journey begins. In this blog entry, I'm going to look at code folding for comment blocks in Groovy (although, I suspect that there may be some default recognition and handling of comment blocks in Schliemann). In the first picture, you see the expanded code fold:
And here it is closed, within the context of other code folds, showing that the content of the fold is shown as a popup when you hover over its collapsed state:
Note: I'm not happy with the syntax colors above, by the way, and am willing to take any suggestions for improvements at all.
I had assumed that Schliemannesque (woo hoo, another new word) code folding is only possible for grammar rules, not tokens, but I was wrong. Here, I declare that everything from "/*" to "*/" should render the token "block_comment":
TOKEN:block_comment: ("/*" - "*/")
Next, I declare that the parsing of the document should skip everything that is defined as a "block_comment":
SKIP:block_comment
Next, lets color everything that is defined by "block_comment" (although, possibly, the SKIP token above may provide this gray color too):
COLOR:block_comment: {
foreground_color: "lightGray";
}
Finally, everything that is defined by the token "block_comment" should be in a code fold:
FOLD:block_comment: {
fold_display_name:"Comment Fold";
expand_type_action_name:"Expand Comment Fold";
collapse_type_action_name:"Collapse Comment Fold";
}
And what do the attributes above signify? Well, fold_display_name defines the text that you see when the fold is collapsed. And expand_type_action_name and collapse_type_action_name define the labels of the menu items for expanding/collapsing all folds defined for the token (which could also be defined in a localizing bundle file):
My approach here is probably not perfect, but this is ALL I had to do, which, especially when compared to the Editor Code Folding API, is not very much at all. (The only reason why there is no NetBeans API tutorial on that API is that I didn't understand it sufficiently, even after receiving a complete sample on the simplest imaginable scenario, which is also the main reason why this API is not discussed in the forthcoming "Rich Client Programming: Plugging into the NetBeans Platform".) The generic languages framework (i.e., Schliemann) takes care of EVERYTHING else—my only responsibility was to specify WHERE the code fold should appear, WHAT the color should be, and WHAT the labels on menu items and so on should contain. The HOW is not my concern, thanks to Schliemann.
I did a similar thing for import statements. The rest will require some more understanding of 'grammar rules' in Schliemann. But it's cool to see that code folding can work for tokens too.
In other news. One of the many reasons that it is cool to be Dutch (or to, at least, understand the language) is that you can go to http://www.uitzendinggemist.nl/ and watch just about everything that's been on (Dutch) TV over the past years. I highly recommend Keyzer & De Boer Advocaten, it is really really good. Also, without being/knowing Dutch, you're missing out on experiencing some fascinatingly interesting personalities, such as Johan Cruijff and Hugo Borst, to name just two. (The latter is a less-camp version of the UK's Russell Brand. Also a football fanatic, with a similarly philosophical bent. Also very 'eigenzinnig', which means 'idiosyncratic'.) I could go on and on recommending programs and praising authentic Dutch people, but will leave it here.
Mar 30 2007, 06:43:32 AM PDT Permalink


