36

I used this way to get code folding in Netbeans:

// <editor-fold defaultstate="collapsed" desc=" description">
....
// </editor-fold>

and Visual Studio:

#region description
...
#endregion

but I can't find the same usage in eclipse. How can I use code folding in Eclipse?

Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
PhatHV
  • 8,010
  • 6
  • 31
  • 40
  • @Ajeet: This question was 2 years ago past. But, pls read my question and question in link you had provided carefully. They are different questions. – PhatHV Mar 28 '14 at 05:23
  • There used to be a plug-in called Coffee-Bytes for this -- but it doesn't seem to exist anymore, if it does I doubt it's compatible with Eclipse Luna. – BrainSlugs83 Oct 05 '14 at 00:38
  • 1
    the Coffee-Bytes plugin is mentioned in this [answer](http://stackoverflow.com/a/8158721/3380951) – XoXo Feb 15 '16 at 15:56

5 Answers5

28

Eclipse supports code folding.

Go to workbench preferences -> General -> Editors -> Structured Text Editors, and check the "Enable folding" box.

Then go to workbench preferences -> Java -> Editor -> Folding, and adjust your folding preferences.

Isaac
  • 16,458
  • 5
  • 57
  • 81
  • 8
    But, I still can't use code folding for a block of code (codes not in comment out block and parentheses block). – PhatHV Nov 23 '12 at 01:25
  • 17
    As far as I know, such fine-level folding isn't available with Eclipse, at least not with Eclipse Juno (which is the version that I am using). A quick search in Eclipse's BugZilla database shows many feature requests related to folding with the Java editor, some of them date years back. – Isaac Nov 23 '12 at 01:34
  • Thanks Isaac. Your answer helped me so much. – PhatHV Nov 23 '12 at 02:42
  • 4
    Technically speaking this answer is misleading, as Eclipse doesn't seem to support the code folding sought by OP. – G. Stoynev Mar 07 '18 at 00:25
  • Also, you could check the keys (Ctrl + Shift + NUM_PAD_DIVIDE) in Preferences -> General -> Keys. – Socrates Jul 10 '18 at 14:20
  • useful answer ty – Firas RG Jan 23 '20 at 09:33
6

Windows->Preferences->(C/C++)->Editors->Folding

(C/C++) will change based on the language you are using. Generally each language plugin will have its own folding options

Chanoch
  • 563
  • 7
  • 16
DanChianucci
  • 1,175
  • 2
  • 11
  • 21
5

The <editor-fold and #region can be used as a block folding wrapping anything you want, including not just a function or a comment but both or even multiples functions, comments, variables, etc.

Eclipse does not has such functionality. I am using Eclipse Neon and still missing this.

ViniciusCR
  • 83
  • 1
  • 7
  • 2023, still true, I'm using STM32CubeIDE and C++, I have `#pragma region` / `#pragma endregion` that is weirdly supported in `Outline` view, but not supported by the editor. – Harry Jan 07 '23 at 12:53
2

I created a fork and an update site for the old coffee bytes code folding plugin that works with Eclipse Neon: https://github.com/stefaneidelloth/EclipseFolding/raw/master/com.cb.platsupp.site

Stefan
  • 10,010
  • 7
  • 61
  • 117
1

Although this is an old question, I'd like to add some input.

Eclipse doesn't natively support custom code folding blocks, as does Visual Studio with its #region and #endregion directives, or Netbeans with its //<editor-fold defaulstate="collapsed" desc="My custom code folding block" and //</editor-fold>.

(IntelliJ supports this as well, with both aforementioned methods working, depending on how the IDE is configured.)


If you happen to be working in Eclipse with the CDT (as in C/C++), there is a way around. I've tried installing the plugins mentioned, but they either do not exist anymore, or the installation makes the IDE unstable.

Create a header file in a central location, which contains macros, etc (optional). In that header, simply define a FOLD macro, as below:

#define FOLD //

Each file that #includes your central header file will also have a reference to the macro above.

An example use of this would be:

#ifdef FOLD Struct MyFileStruct
#pragma pack(1)
typedef struct MyFileStruct {
        WCHAR fileName[FILENAMELEN];  // File name
        WCHAR fileInfos[32];          // File info
        WCHAR fileDate[32];           // File date
        DWORD sizeInBytes;            // File size
} File;
#pragma pack()
#endif

If the way this works is unclear, I suggest looking in to the C Preprocessor

I hope this is of some use!

SimonC
  • 1,547
  • 1
  • 19
  • 43
  • 1
    There's even simpler way: `#if 1 // My region name` and `#endif` at the end. It's even shorter than any original "region" tags ;) Works nicely with CDT / STM32CubeIDE. – Harry Jan 07 '23 at 12:59