X

Geertjan's Blog

  • March 30, 2007

Code Folding for Groovy via Tokens in Schliemann

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 8 )
  • Guillaume Laforge Friday, March 30, 2007
    Being able to fold parts of the UI defined in the SwingBuilder makes the tree structure of UIs even more visible. That's really nice. And Schliemann rocks!
  • Java Fans Friday, March 30, 2007
    It's Cool! Thanks for your effort!
  • Rohan Ranade Friday, March 30, 2007
    The code folding api link mentioned should be http://www.netbeans.org/download/dev/javadoc/org-netbeans-modules-editor-fold/index.html?overview-summary.html and not http://www.netbeans.org/download/dev/javadoc/org-netbeans-modules-editor-completion/overview-summary.html



    Thanks, Rohan
  • Kevin Mitchell Tuesday, April 3, 2007
    I thought I'd put in a plea for a section on code folding in the Rich Client Programming book. I agree the folding API looks pretty cryptic, but that's why it would be good to have a clear explanation and example in the book. I got the rough-cuts version of the book, hoping it would explain this API, amongst other things, and was disappointed that folding wasn't covered at all. I suppose if the intention is that Schliemann will replace the current API then that's a different matter. But if not, at least in the mid-term, then people could really benefit from a section in the book.
  • Geertjan Tuesday, April 3, 2007
    Hi Kevin, point taken. However, we couldn't cover everything in the book. Simultaneously, we intend the book's site (http://www.netbeans.org/books/rcp.html) to contain supplemental chapters, which will later be integrated into future editions of the book. Code folding should certainly be one of these. People should have the option of choosing either the Schliemann route or the traditional NetBeans API route. Out of interest, which language are you looking at supporting?
  • Kevin Mitchell Tuesday, April 3, 2007
    It's good to hear there might be a supplemental chapter covering folding at some point. I'm writing an IDE for a domain-specific language I'm developing, and wanted to create and manipulate folds programmatically. Initially I was trying to do this in a standalone editor, using Antonio Vieiro's tutorial as a starting point. But I got completely stumped when I got to folding. Maybe I'll have more success if I try to use the NetBeans platform instead.
  • Geertjan Tuesday, April 3, 2007
    Yeah, I'll try and look into the whole code folding story. I was very relieved when we decided to exclude it from the book, because the code seemed pretty opaque to me (i.e., not transparent). But on a second look it might be a lot better than that. Watch my blog in the coming days, hopefully I'll figure out the way to do it from the engineers behind it. However, Schliemann in 6.0 dev builds provides code folding already, so maybe you want to look into that, using the instructions in this blog entry as a way in.
  • 礼仪公司 Monday, April 30, 2007
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.