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?
9 Answers
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

- 1
- 1

- 13,858
- 11
- 76
- 167
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 + /
-
8This should be on the top. This works and does not require a plugin. I also believe OP meant to say **Folding -> Enable folding of control flow statements (if/else, do/while, for, switch)** – JC203 Nov 02 '16 at 17:24
-
9The OP asked about Java code, and this answer only applies to the C/C++ editor. – Ti Strga Feb 05 '20 at 20:23
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)

- 68,052
- 28
- 140
- 210
-
11I agree but I still want to fold "if else", the code was not written by me but someone else, or just consider it a 3rdparty code. – Shawn Mar 03 '10 at 06:13
-
Unfortunately, in my case I need code folding _exactly_ to be able to split 2k LoC method. – Nikita Bosik Oct 06 '16 at 12:18
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).

- 1,135
- 12
- 22
-
1Thanks but it doesn't work. the plugin home page is broken. I have searched but no answer from google :) – Shawn Mar 03 '10 at 06:03
-
Found it at: http://code.google.com/p/coffee-bytes/ But still some issues with 3.5...(String index out of bound..) anyway.. – Shawn Mar 03 '10 at 06:15
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.

- 15,171
- 8
- 38
- 76

- 183
- 5
-
2This was laready mentioned by [this answer](https://stackoverflow.com/a/37784143/7111561) over 3 years ago .. and still unfortunately it doesn't apply to JAVA only C/C++ – derHugo Aug 30 '19 at 11:08
-
@derHugo - The answer you linked is different from this one, albeit subtly. The other one only mentions "Enable folding of preprocessor branches (#if/#else)". I was actually disappointed at first, thinking it did not apply to my needs ("Enable folding of control flow statements (if/else, do/while, for, switch)", as added in a comment below the answer). I went to check in Eclipse, and there it was the other option as well. I was actually about to post an answer clarifying this, but here the answer was already available (even if it could be more explicit about the two check boxes). – sancho.s ReinstateMonicaCellio Feb 20 '20 at 03:28
-
1
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

- 1,182
- 12
- 26
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"
- Select folding: select "Coffee Bytes Java Folding"
- Switch to "User Defined Regions"
- "Start Identifier" = { ; End Identifier = }
- click "Apply and Close"
- Reopen java source editor you will see "if" or "for" block is collapsable

- 83
- 6
-
-
4click Eclipse menu "Help" -- "Install New Software..." click "Add" button https://github.com/stefaneidelloth/EclipseFolding/raw/master/com.cb.platsupp.site – Luke Apr 22 '21 at 01:33
-
Did you successfully install this plugin ? or Have you see option "Coffee Bytes Java Folding" on Eclipse Preferences windows, Step as "Java->Editor->Folding" – Luke Apr 23 '21 at 03:52
-
Doesn't work with Eclipse 2022-06 (no change in Folding Tab after installing plugin and restarting Eclipse). – XelaNimed Aug 24 '22 at 19:32
-
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.

- 895
- 11
- 21
-
6Pay attention using this, because you're changing the scope of your variables, so the code in the first block is NOT equivalent to the code in the second block (and the third, collapsed). – Andrea Colleoni Nov 23 '15 at 17:24
-
For Python, i.e. Eclipse/PyDev, go to Windows > Preferences > PyDev > Editor > Code Folding
and check all the boxes.

- 6,448
- 13
- 82
- 139