Is there a way to fold eclipse sub-blocks like an "if" statement?

EclipseIde

Eclipse Problem Overview


Currently Eclipse only fold the java doc and at function level, but when reading long methods, there could be quite a lot of if/else etc, is there a way to fold them?

Eclipse Solutions


Solution 1 - Eclipse

I found the Coffee-Bytes plugin. I downloaded it from this link and found this guide by the author, for using it.

You can find more details in these references:

What code folding plugins work on Eclipse 3.6?
How to use Coffee-Bytes code folding

Solution 2 - Eclipse

in updated versions of Eclipse

Change folding preferences at:

Window -> Preferences -> C/C++ -> Editor -> Folding -> Enable folding of preprocessor branches (#if/#else)

Enable folding using ctrl + shift + /

Solution 3 - Eclipse

No, in the Preferences Dialog (Menu Window/Prefernces): Java/Editor/Folding you may choose,

  • Comments
  • Head Comments
  • Inner Types
  • Members and Imports

if Enable Folding is checked.

If you wan't to do this because the blocks are so long that can't reconize the structure you should consider to split if/else blocks into methods using Alt-Shift-M (Extract Method)

Solution 4 - Eclipse

It appears Eclipse does not have built-in support for folding if/else statements but allows folding for other more complex cases like Anonymous inner classes. Try looking for plugins like this one (last modified 2007, try it if it supports your Eclipse version).

Solution 5 - Eclipse

For now, there is built-in function.
Click "Window->Preferences->C/C++->Editor->Folding" and then enable appropriate option you want.
Apply, close and either refresh project or reopen eclipse.

Solution 6 - Eclipse

Ok, this is a little bit older, but maybe someone could find this useful: In most cases you can surround the piece of code by an additional pair of scope brackets, and to remember what you folded you can add a line comment.

For example, if you want to collapse the following:

int SectionA_var1;
int SectionA_var2;
int SectionA_var3;
int SectionA_var4;
int SectionA_var5;
int SectionB_var1;

just add the brackets an the comment:

{ // SectionA
    int SectionA_var1;
    int SectionA_var2;
    int SectionA_var3;
    int SectionA_var4;
    int SectionA_var5;
}
int SectionB_var1;

Then you get the (-) sign and you can collapse the whole section to this:

{ // SectionA[...]
int SectionB_var1;

No plugin necessary, and until now I had no situation where this gave me any downsides, except that you cannot use it on a top level declaration to collapse methods.

Solution 7 - Eclipse

As weird as it looks like, sounds like developers never thought about that. if you have a big if statement or any switch/loop ... just use notepad++ to be able to fold/unfold

Solution 8 - Eclipse

For Python, i.e. Eclipse/PyDev, go to Windows > Preferences > PyDev > Editor > Code Folding and check all the boxes.

Solution 9 - Eclipse

Fold java source code like "if else for" statement

install pluins com.cb.eclipse.folding

restart your Eclipse make sure the pluins enabled

Click "Window->Preferences->Java->Editor->Folding"

  1. Select folding: select "Coffee Bytes Java Folding"
  2. Switch to "User Defined Regions"
  3. "Start Identifier" = { ; End Identifier = }
  4. click "Apply and Close"
  5. Reopen java source editor you will see "if" or "for" block is collapsable

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShawnView Question on Stackoverflow
Solution 1 - EclipsehasanghaforianView Answer on Stackoverflow
Solution 2 - EclipselahmaniaView Answer on Stackoverflow
Solution 3 - EclipsestackerView Answer on Stackoverflow
Solution 4 - Eclipsepm_labsView Answer on Stackoverflow
Solution 5 - EclipseIvan KryvosheiaView Answer on Stackoverflow
Solution 6 - Eclipsez00lView Answer on Stackoverflow
Solution 7 - EclipseKishanView Answer on Stackoverflow
Solution 8 - EclipseAl LelopathView Answer on Stackoverflow
Solution 9 - EclipseLukeView Answer on Stackoverflow