18

When working on C#, I often do this:

#region Properties
  public int Property1{get;set;}
  ...(more properties)
#endregion

This makes Visual Studio add the option to fold the code inside the region. Offcourse, you can use regions for anything, it's not limited to Properties or anything at all.

I'm wondering 2 things:

  1. Is this just something to make Visual Studio add code folding to an area of the editor or does it affect the compiler?
  2. Can I do something similar in Netbeans?
KdgDev
  • 14,299
  • 46
  • 120
  • 156

4 Answers4

34
  1. (In Netbeans at least) It is purely a programmer aid and has no effect on compilation
  2. Yes you can:

Can I Create Custom Code Folds?

Apart from usual folds, you can define custom folds. To add your custom fold, type in two special comments as shown in this example:

// <editor-fold>
   Your code goes here...
// </editor-fold>

You may define the default description of a collapsed fold by adding a "desc" tag:

// <editor-fold desc="This is my super secret genius code.">
   Your code goes here...
// </editor-fold>

You may set a fold to be collapsed by default by adding a "defaultstate" tag:

// <editor-fold defaultstate="collapsed">
   Your code goes here...
// </editor-fold>

Please note that the above examples are for java language. In other languages custom folds may not be supported or you may have to use different characters for marking line comments. Also <editor-fold/> element syntax is not a real XML, we use a simple regex to find your custom folds in a document, which means that there are limitations in the syntax. The most important one is that attributes may only be combined in this specific order: defaultstate, desc.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
11
  1. It does not affect compilation, it's just to organise code in the editor. You can pick up on arguments on when best to use them in this question.
  2. In NetBeans you can use Custom Folds, depending on the language:

    // <editor-fold defaultstate="collapsed" desc="My Fold">
    Your amazing code
    // </editor-fold>

The attributes are optional. Without defaultstate specified the region will default to expand (except for when you first wrap code in it!)

Community
  • 1
  • 1
noelicus
  • 14,468
  • 3
  • 92
  • 111
1

The latest Netbeans 8.1 has already this feature build in, You need just select you rows and then click on the "light bulb" icon, then select:

enter code hereSurround with // <editor-fold defaultstatus="collapsed" desc="comment">...

This is the source: http://wiki.netbeans.org/SurroundWithCodeFolding

Pini Cheyni
  • 5,073
  • 2
  • 40
  • 58
0

I wrote a Macro for folding by mouse draging.

My shortcute: CTRL+SHIFT+Q

Folding Macro:

cut-to-clipboard 
"// <editor-fold desc=\"This is my custom folding\" defaultstate=\"collapsed\">"
 paste-from-clipboard 
"// </editor-fold>"

UNFolding Macro Shortcut: ctrl+SUBTRACK

enter image description here

Ali Hesari
  • 1,821
  • 5
  • 25
  • 51