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

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?

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

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 + /

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)

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).

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.

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.

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

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

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

Related

How to make Eclipse format according to template when typing

I'm using eclipse Version: 2019-09 R (4.13.0) on mac High Sierra.
I didn't like the default formatting. For example when I write a method, it puts a space before the brace like this myMethod() { instead of myMethod(){ which I prefer.
So I changed the formatting template in Preferences -> Java -> Code Style -> Formatter
Now when I do CMD+shift+F it formats it how I want without the whitespace.
However, it STILL adds the white space as I type in the method! I can't figure out how to stop it doing that. When I type CMD+shift+F it corrects it, so it knows what I want, but retains the old behaviour when I type. Please save my sanity!
(Yes I've tried restarting eclipse!)
Thanks!
In Preferences: Java > Editor > Typing in section Automatically insert at correct position uncheck the checkbox Braces.

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?
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.
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
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.
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
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!

Eclipse mark occurrences doesn't mark all occurrences

Just installed Eclipse Juno Release, Build id: 20120614-1722.
I have 'Mark Occurrences' turned on, and I can see that it does indeed mark some of the occurrences in the JS file I'm editing. However, there's a function call that it doesn't mark.
var trackView = function() {
saveData();
},
saveData = function() {
// do something
};
When I click into the saveData occurrence where it is defined (line 4), it highlights, but it doesn't highlight the occurrence on line 2 where the function is being called. When I click into the saveData occurrence on line 2, neither highlights.
Any ideas?
The bug report in Eclipse also mentions another workaround: select something in the Outline view, and then select the field in your source again.
This works mostly always for me.
If you toggle from eclipse window to another window using ATL+TAB and come back to Eclipse window it is higlighted.
This is a known bug in Eclipse Juno and is actively (as of this writing) being investigated by Eclipse developers.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=400661
https://bugs.eclipse.org/bugs/show_bug.cgi?id=398509
UPDATE 6/2: This bug has been resolved in Release 4.2.2
https://bugs.eclipse.org/bugs/show_bug.cgi?id=398509
If you uncheck the below option it will work fine
Preference > Java > Editor > Mark Ocurrences > Uncheck: Keep marks when the selection changes.
This is a bug in Eclipse - the occurrence-highlight seems to "stick" to the first thing you point to.
Two "solutions" that at least get the highlight stuck on a new item (!!):
As #Subas Raj mentioned, defocus and refocus the window - when you come back, your current position is occurrence-highlighted. Either hit AltTab twice, or use your mouse to click/focus some other window, then back.
As #Leo and #Gorky mentions, hit AltShiftO twice. This is the shortcut for toggling highlight occurrences, and when you turn it back on, your current position is occurrence-highlighted.
And finally, a third ACTUAL solution: Replace one specific jar with a patched one: https://bugs.eclipse.org/bugs/show_bug.cgi?id=398509#c60
.. or upgrade to M7 (4.3), which now are released: https://bugs.eclipse.org/bugs/show_bug.cgi?id=398509#c62
All configuration and settings for "Mark Occurrences" are proper but it seems this is Eclipse JUNO problem.
I have been observing this issue and tried to resolve many way but in Juno, highlighting occurrences is not consistent.
Sometimes it works pretty fast, sometimes it takes time to highlight, sometimes it doesn't at all.
Also observed that once we restart Eclipse, things works proper but gradually, highlighting occurrences become slow and stops also.
~Chirag
Go to Window -> Preferences -> General -> Editors -> Text Editors -> Annotations
Under both Occurrences & Write Occurrences, make sure you have the Text as Highlighted option selected.
Also, under General -> JavaScript -> Editor -> Mark Occurrences make sure all of the check boxes are checked.
I unchecked General -- Appearance Use mixed fonts and colors for labels and now it works better...
Same as I encountered, you can try to click "Toggle Mark Occurrences" twice (turn it off and turn on) or you can also try to use Alt+Shift+O twice.
And can also try the solution of #Subas Raj, is good.
For now there is only workaround (push "Alt + Shift + O" twice):
Cannot highlight all occurrences of a selected word in Eclipse

How do you turn off method autocompletion on open parenthesis in NetBeans 7.0?

I guess I'm a quick typer because if I type the characters
ArrayList myArray;
myArray.size(
NetBeans auto complete puts the following in my editor
ArrayList myArray;
myArray.add(someVar);
Why? Because the auto completion doesn't have time to find and filter all the method names by the time I type '('. So it takes the the first one from the list it has created and filtered so far: "add". Grrrr!!!
I want to keep method name completion, but I don't want it to happen on '('. I haven't been able to find out how to fine tune this awesome feature to stop it from mangling my code.
I cannot reproduce you problem in NetBeans 7.1.2. Maybe I'm not typing fast enough...
Take a look at the Options to switch off some of the code completion options: Tools->Options, select the Editor section, then the Code Completion tab.
A.S. This is a comment but I don't have the rep.
I definitely experience this issue, and similar ones in other IDEs like Eclipse or Qt Creator - though I can't always be bothered figuring out how they happened. For example in Qt Creator I type:
size_t len = array.size();
and it becomes
size_t len = array.size(;)
because it didn't recognize quickly enough that I wanted to type over the closing ')'.
It is even more annoying when the editor doesn't allow overwriting or tab-jumping those braces as you have to press Right arrow.

An autoformat tool to automatically insert braces around single-line clauses?

So assuming that in our work environment we've decided that the One True Faith forbids this:
if (something)
doSomething();
And instead we want:
if (something) {
doSomething();
}
Is there a tool that will do that automagically? Awesomest would be to have eclipse just do it, but since we're really just recovering from one ex-employee that thought he was too pretty for coding conventions, a less-friendly tool that would do it right just once would suffice.
I think you can set this in Eclipse's formatting rules and run the built-in formatter.. e.g. the "Use blocks" option:
(source: ibm.com)
Of course, you need the rest of the formatting options to be set how you want them, since I don't think you can selectively apply only a single rule/option..
Ah, it's actually not a formatter, but a "Clean Up":
Preferences -> Java -> Code Style -> Clean Up
And then to execute. Source -> Clean Up
As opposed to Source -> Format
I believe that GreatCode can do this for you (along with a ton of other options)