7.6 Code Folding

Code folding allows you to hide some sections of code so you can focus on others. Sections of C or assembly code may be collapsed (hidden) or expanded (displayed), depending on your selected options.

Code folding is enabled by default. For most C or assembly code, sections of code that may be folded are signified by “-” and “+” icons to the left of the code.

Enable and Set Up Code Folding

Use the following steps to enable and set up code folding:

  1. Select Tools > Options (MPLAB X IDE > Preferences for macOS) to open the Options dialog.
  2. Click on the Editor button.
  3. Click on the Folding tab and check Enable Code Folding.
  4. Check other options to specify sections to fold. If you do not see the type of code you want to fold listed there, you can do a custom fold.

Expand/Collapse Code Folding

Within the editor, click the “-” icon next to a method and it folds. Click the “+” icon next to a folded method and it expands. Folded code is denoted by an ellipsis box. Hold the cursor over the ellipsis box and the IDE displays the collapsed method.

Figure 7-3. Expanded Code
Figure 7-4. Collapsed Code

Custom Code Folding – C

To custom fold C code, do one of the following:

  • Type the following comments around your code:
    // <editor-fold defaultstate="collapsed" desc="user-description">
    C code block to fold
    // </editor-fold>

OR

  • Type fcom and press the Tab key to automatically enter the comment text above.

After the comment has been entered, customize it for your code:

defaultstate Enter either collapsed or expanded,
desc Enter a description of the code. Once collapsed, this description can still be seen for reference.

An example of custom code folding is shown in Expand/Collapse Code Folding above.

For additional information see the NetBeans help topic:

ui.netbeans.org/docs/ui/code_folding/cf_uispec.html

Custom Code Folding – Assembly

To custom fold assembly code, do the following:

  • For MPLAB XC16 and MPLAB XC-DSC assembler code, type the following comments around your code:
    ; <editor-fold defaultstate="collapsed" desc="user-description">
    ASM code block to fold
    ; </editor-fold>
  • For MPLAB XC32 assembler code, type the following comments around your code:
    // <editor-fold defaultstate="collapsed" desc="user-description">
    ASM code block to fold
    // </editor-fold>

OR

  • For MPLAB XC16 and MPLAB XC-DSC assembler code, type fcom; and press the Tab key to automatically enter the relevant comment text above.
  • For MPLAB XC32 assembler code, type fcom// and press the Tab key to automatically enter the relevant comment text above.

Assembly code will fold in the same way that C code folds. For an example of code folding, see the C code example above.

Expand/Collapse Custom Code Folding

To expand/collapse code blocks, go to the View menu, or right-click in the Editor window, and select one of the following:

  1. Code Folds > Collapse Fold to hide the block of code.
  2. Code Folds > Expand Fold to display the block of code.
  3. Code Folds > Collapse All to hide all folding blocks of code.
  4. Code Folds > Expand All to display all folding blocks of code.

An example of custom code folding is shown in Expand/Collapse Code Folding above.