Is there any way to fold/collapse regional code in Eclipse? - 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!

Related

How can I convince Eclipse CDT that a macro is defined for source code editing and code completion?

I have in my source code:
// foo.cpp
struct foo
{
foo() {}
#ifdef I_WANT_THIS_FEATURE
void bar() {}
#endif
};
In my Makefile I have
foo.o: foo.cpp
g++ -c -DI_WANT_THIS_FEATURE foo.cpp -o foo.o
This compiles fine from the command line as well as with the external builder that I have created in Eclipse (which basically defines a few environment variables and calls make) and I can call foo::bar().
However, in the Eclipse CDT source code editor, the part where I define foo::bar() has a grey background (suggesting that foo::bar() would not be included in the build) and code completion on objects of type foo does not suggest bar() as a method that can be called.
How can I define the I_WANT_THIS_FEATURE macro in an Eclipse CDT makefile project with custom makefile so that it is known to the source code editor and the code completion?
In addition to Oswald's answer:
If you have several build configurations, the default behavior of the Eclipse Indexer seem to be that it always uses the first build configuration.
In my case the define was only defined in the 3rd build configuration, so the solution provided by Oswald did not help.
To change this globally, select Window -> Preferences -> C/C++ -> Indexer.
Choose Use active build configuration
You could also choose to override the global settings in the project settings under Project -> Properties -> C/C++ General -> Indexer and select Enable project specific settings followed by selecting Use active build configuration.
After this, the solution provided by Oswald should work:
Project -> Properties -> C/C++ General -> Paths and Symbols
Choose the Symbols tab and Add... a new Symbol with Name I_WANT_THIS_FEATURE and a Value of 1.
Found it: Project -> Properties -> C/C++ General -> Paths and Symbols
Choose the Symbols tab and Add... a new Symbol with Name I_WANT_THIS_FEATURE and a Value of 1.
Using -D with almost every compiler and just supplying a name like -DI_WANT_THIS_FEATURE defines the symbol I_WANT_THIS_FEATURE with a value of 1.
The eclipse indexer/editor apparently does not know that, so:
#if I_WANT_THIS_FEATURE
this code is marked inactive in editor,
but will be seen by compiler and cause error
#endif
where:
#ifdef I_WANT_THIS_FEATURE
this code is marked active in editor
#endif
So, this is really a problem with eclipse not knowing that default value for a symbol defined through -D is 1.
Eclipse makes this rather confusing, since there are multiple places to set this, and the settings are coupled, but here's how it works:
To set custom macros for a given project (affects both building and indexing in Eclipse)
Here's my preferred way to do it.
In this example we will set the following defines at the Eclipse project level (for its indexer and builder) rather than in your source code.
#define ARDUINO 1000
#define AVR
#define __AVR_ATmega328__
If you were defining them at the command-line when manually building a gcc or g++ project, the above #defines would look like this (search for -Dmacro in the man gcc pages for details):
-DARDUINO=1000 -DAVR -D__AVR_ATmega328__
So, do the following in your Eclipse project. My project here is called "Arduino 1.8.13" (see full screenshot of this a couple images below):
Right-click on your project in the "Project Explorer" pane on the left --> Properties --> C/C++ General --> Paths and Symbols --> Symbols tab --> select either GNU C or GNU C++ --> click the Add button at the top-right --> type ARDUINO for name and 1000 for value --> BE SURE TO CHECK THE 2 BOXES FOR Add to all configurations and Add to all languages (unless you don't want those behaviors) --> click OK.
Repeat this step for all defines, as follows. Be sure to check the boxes for Add to all configurations and Add to all languages (unless you don't want those behaviors) for each one:
Name: ARDUINO, Value: 1000
Name: AVR, Value: (leave empty)
Name: __AVR_ATmega328__, Value: (leave empty)
Here's a screenshot showing the first one. Notice all the highlighted sections to click or pay attention to:
Once you've done this for all macros you wish to define (ex: the 3 above), you will see the following:
If you checked the box for Add to all languages for each one, then these macros will have been applied to BOTH the GNU C and GNU C++ languages. So, click on one and then the other and you should see the macros in both places, like this:
If you checked the box for Add to all configurations for each one, then these macros will have also been applied to all build "Configurations", such as Debug and Release, as shown here:
Keep in mind all of the various combinations of "Languages" and "Configuration" are unique. You can set macros for one or both languages for each configuration individually if you don't check the 2 boxes for Add to all configurations and Add to all languages when adding the macros.
If you navigate to Project Properties --> C/C++ Build --> Settings --> Tool Settings tab --> Cross GCC Compiler --> Preprocessor you will also see these macros now defined for all "GCC" C files, when building or indexing!:
Note that you can also edit, add, or delete macros specific to a given "Configuration" or "Language" (this particular place is for the GNU C language) right here, rather than as previously done above, if you like. BUT, the only way to apply a given macro to ALL languages and ALL build configurations at once is doing it how I showed you above.
You can also see these macros are applied to the GNU C++ build and index settings if you navigate to Project Properties --> C/C++ Build --> Settings --> Tool Settings tab --> Cross G++ Compiler --> Preprocessor:
Again, to customize macros just for C++ and just for this selected "Configuration" you could edit them right here.
When done adding all macros, click Apply or Apply and Close. When asked, choose YES to re-index the entire project:
If you didn't click YES, you may manually trigger the project to be reindexed by right-clicking on it in the Project Explorer and going to --> Index --> Rebuild.
Troubleshooting
If your settings/macros don't seem to be getting applied, and your code still shows sections blacked-out, indicating the macros for those sections are false or undefined, you may need to do or check the following:
Try reindexing your project by right-clicking on it in the Project Explorer and going to --> Index --> Rebuild.
You may not have set the macros for the right build configuration or language. You will need to check all of the various build configurations and languages as I showed in the various screenshots above.
Follow the instructions above and re-add the macros, this time BEING SURE TO CHECK THE 2 BOXES FOR Add to all configurations and Add to all languages.
OR, manually navigate to the Project Properties --> C/C++ Build --> Settings --> Tool Settings tab -->
--> Cross GCC Compiler --> Preprocessor OR
--> Cross G++ Compiler --> Preprocessor...
...sections to manually configure the macros just for one language and/or configuration, or another. ALL of these settings must be either in-sync or set individually.
The easiest place to set these settings, as already stated above, is here: Right-click on your project in the "Project Explorer" pane on the left --> Properties --> C/C++ General --> Paths and Symbols --> Symbols tab. BUT, if you forgot to check the boxes for Add to all configurations and Add to all languages, I recommend just deleting the macros and then adding them again, this time checking those boxes.
If you don't want to worry about which build Configuration you have selected, and you didn't check the Add to all configurations box when you added the macros, you can also change this global workspace setting, but I don't really recommend it:
Window --> Preferences --> C/C++ --> Indexer --> select Use active build configuration. Again, however, I do NOT use this option myself and do not necessarily recommend you use it either. It's just something to be aware of is all.
See also
This answer is also posted on my website here: https://gabrielstaples.com/eclipse-defining-custom-macros-for-indexer/
My full Eclipse setup document here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/eclipse

Combine syntax highlighting of different Eclipse editors

I use Eclipse, StatET and the Sweave plugin to write my R and Latex code. The cool thing is that R and Latex code can be put together into one file, however you end up with a syntax highlighting problem.
I have loads of R code and I very much like the Eclipse R syntax highlighting. But now combining R and Latex means that I have to work with .Rnw files where there is no particular syntax highlighting for R.
When I go to Eclipse -> Preferences -> Content Types I can add *.Rnw to "R script file" which makes Eclipse to open the .Rnw files with the standard R Editor. However, this means that I do not have syntax highlighting for Sweave any longer. In addition, the Sweave code is shown as an error in the R editor.
My question is whether it is possible to combine different syntax highlighting styles in an easy way?
I don't think any Eclipse plugins/editors really support mixing up several syntaxes inside one editor. At any rate it is not currently supported in Eclipse Platform.
However you can try Eclipse Colorer plugin. It allows to switch coloring style for the current editor. It may mot support both R and Latex syntax, but you can create your own highlighting by adding your own HRC file.

Word-wrapping comments in Netbeans

/*
*
* This is a long comment. I broke it into lines, but that made it impossible to edit without screwing up the formatting. Is there a way to make Netbeans add line breaks automatically?
*
*/
Reads: This is a long comment. I broke it into lines, but that made it impossible to edit without screwing up the formatting. Is there a way to make Netbeans add line breaks automatically?
As for today, the current NetBeans (7.3) word-wraps comments with some nice available extra options when formatting. NB: Unfortunately, this is currently only available for the Java language.
You can launch the formatter with the context menu→Format, or if you did not change the standard shortcuts, Alt+Shift+F should do.
To access the corresponding configuration: Tools → Options → Editor → Formatting; Language: Java; Category: Comments
Your IDE won't format the comment for you. You will have to do it by hand. Yes, that means you have to re-format it once you add new words to it.
If you press Enter to insert a new line, NetBeans will simply extend the docblock formatting - it won't break anything. There's also a useful option in the Preferences to display a line in the right margin, so you know when to break a line. It's in:
Tools > Options > Editor > Formatting > Right Margin
Is this what you're looking for?
Go to tools->options->editor->formatting
find line wrap and choose one the wrapping style you desire,there are two options : afterword and anywhere.
and then click OK! :D
nb : I suggest you to choose afterword

How to place $NON-NLS-1$ kind of comments in eclipse

I have realized that through eclipse source code there are a lot of comments like this one, I know they are for instructing eclipse that the strings in these lines aren´t supposed to be internationalizable, and I would like if I can place that kind of single line comments using eclipse code completion assistant.
private String toolTip = ""; //$NON-NLS-1$
The Externalize Strings process can take care of that. Select Source → Externalize Strings. Now, select which Strings you wish to externalize. If you wish to Externalize some of the strings, go ahead. Those which will be marked as "Ignore" will automatically get the //$NON-NLS-1$ comment.
You can quickly add these comments by typing nls and clicking Ctrl+Space (code completion, could be different on your platform). There's a template for NLS with a number you need to enter. You can create a template without a number under the Preferences → Java / Editor / Templates.
You can set Eclipse to automatically remove unnecessary $NON-NLS-1$ comments. See under Preferences → Java / Editor / Save Actions → Enable additional actions → Configure → under Unnecessary code.

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

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