Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to fold/collapse regional code in Eclipse?

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?

like image 670
PhatHV Avatar asked Nov 22 '12 03:11

PhatHV


People also ask

Is there a way to collapse all code blocks in Eclipse?

There is a hotkey, mapped by default to Ctrl + Shift + NUM_KEYPAD_DIVIDE . You can change it to something else via Window -> Preferences, search for "Keys", then for "Collapse All". To open all code blocks the shortcut is Ctrl + Shift + NUM_KEYPAD_MULTIPLY .

What do the preferences in Java editor folding control?

Code Folding enables you to 'collapse', or hide, certain sections of code while you are not working on them. This enables you to manage larger amounts of code within one window.

How do I revert to previous code in Eclipse?

Press Alt + Left Arrow and Alt + Right Arrow like you would in a web browser.

How do I collapse code in IntelliJ?

Expand or collapse code elements To collapse or expand all code fragments, press Ctrl+Shift+NumPad - / Ctrl+Shift+NumPad + . IntelliJ IDEA collapses or expands all fragments within the selection, or, if nothing is selected, all fragments in the current file, for example, all methods in a file.


5 Answers

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.

like image 128
Isaac Avatar answered Oct 19 '22 10:10

Isaac


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

like image 26
DanChianucci Avatar answered Oct 19 '22 09:10

DanChianucci


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.

like image 38
ViniciusCR Avatar answered Oct 19 '22 09:10

ViniciusCR


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

like image 2
Stefan Avatar answered Oct 19 '22 08:10

Stefan


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!

like image 1
SimonC Avatar answered Oct 19 '22 09:10

SimonC