My blog has moved!

You should automatically be redirected in 6 seconds. If not, visit
http://blogs.i2m.dk/allan
and update your bookmarks.

Wednesday, 6 February 2008

Custom code folding in NetBeans

When you got large source files it can sometimes be helpful to create foldings for certain sections of your code. NetBeans got a nifty functionality for this using non-intrusive XML code (similar to Visual Studio).

To create a custom code folding section simply insert the following before the content to fold:


//<editor-fold defaultstate="collapsed" desc="My custom code folding">


and the following after the content to fold:


//</editor-fold>
.

So for example, if you had a JSF backing bean where you want to create a custom code folding for your properties, action listeners, and action handlers you could do as follows:


package backingbeans;

import javax.faces.model.*;
import javax.faces.event.*;

public class MyBackingBean {

private String prop1 = "";

public MyBackingBean() {
}

//<editor-fold defaultstate="collapsed" desc="Properties">
public String getProp1() {
return this.prop1;
}

public void setProp1(String prop1) {
this.prop1 = prop1;
}
//</editor-fold>


//<editor-fold defaultstate="collapsed" desc="Action listeners">
public void myFirstListener(ActionEvent event) {
... do something ...
}

public void mySecondListener(ActionEvent event) {
... do something else ...
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="Action handlers">
public String myFirstActionHandler() {
... do something ...
return "OUTCOME1";
}

public String mySecondActionHandler() {
... do something else ...
return "OUTCOME2";
}
//</editor-fold>
}


This will create three custom folders that are collapsed by default when the file is opened.

If you like this, I'd suggest setting up a coding template for inserting the XML code as it can otherwise be tricky to remember. This is done by following these steps:


  1. Click Tools -> Options

  2. Select "Editor" from the top options

  3. Select the "Code Templates" tab

  4. Select "Java" in the Language dropdown

  5. Click the "New" button

  6. Enter the shorthand for inserting the template, for example I use 'efold'

  7. Click the "OK" button

  8. The template has been created, now enter the following code into the Expanded Text textbox:

    //<editor-fold defaultstate="collapsed" desc="${cursor}">

    //</editor-fold>

  9. Click the "OK" button.



You can try out the template by opening a Java source file, place the cursor where you want to insert the folding and type efold followed by the tab key. It will now have inserted the complete editor-fold and place the cursor ready for you to enter a description for the folding.

6 comments:

michael said...

Thanks for the example. Will this 'trick' work in, say, Visual Studio if my code is opened and run there?

MC

Allan Lykke Christensen said...

I'm almost certain that the code folding tags shown are exclusive to NetBeans. Many editors/IDE support user-defined code folding but unfortunately they all use different syntax.

taveller said...

hello. Do you have any idea if we can have code folding for the "{"
eg.

if () {
if () {
}
}
i can only see code folding for modules, functions, etc

Allan Lykke Christensen said...

Besides custom code folding, the only code fragments that is supported by code foldings are methods, inner classes, import statements, javadocs, and file comments. Hence, code folding is not supported for conditional statements like if, while, and do while.

If this is a desired feature I'd suggest that you submit a feature request on http://issues.netbeans.org

Nicola De Franceschi - User Experience said...

Thank you Allan, this was a very needed function for me.
You can even improve it so that you can fold a block of code you've already written.

Simply place:

${selection}

between the two lines you suggested.
When you select the code click the light bulb that appears on the line number bar and choose "surround with Custom fold".

Allan Lykke Christensen said...

That is a very cool trick indeed. Thanks for sharing Nicola!