Eclipse, Checkstyle and spaces in a java doc comment - eclipse

I have installed the Checkstyle plugin for Eclipse. When i habe writen now a doc comment for a method and there is an empty line line between my acutal comment and the parameter list, like this:
/**
* A comment.
*
* #param param A param.
*/
void aMethod(String param) {
// some code
}
Checkstyle now complaints that there is an trailing space. And, although i didn't put a space at that postition there is an extra space...
..../**
.....*.A.comment\.
.....*.
.....*.#param.param.A.param\.
.....*/
It looks to me that Eclipse is adding this extra space to the docblock. How can i turn that off or configure the checkstyle plugin to ignore this extra space in a docblock?

I am supposing that you have already tried going to Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments and ensuring that there is no trailing space for Constructors and so on.
I have had a lot of success with the Eclipse Plugin AnyEdit Tools. There is an option to removing trailing whitespace and another to Convert tabs to space, both of which will make Checkstyle much happier.

Related

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 code formatter: spacing in empty blocks

i configured the eclipse code formatter to display an empty block's brackets in one line. Now my projects checkstyle configuration requires spacing between these brackets. Is it possible to tell Eclipse to do this.
e.g.: emptyMethod() { }
Thanks for any help.
As far as I know there is no way to put a space between the braces with the Eclipse formatter.
As a workaround I removed the "WhitespaceAround" rule from the checkStyle plugin configuration.

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.

How to auto-remove trailing whitespace in Eclipse?

The question has two parts, one of which I already have the answer for.
How to auto-remove trailing whitespace from the entire file being edited? -> Answer: use the AnyEdit plugin, which can be set to do that on any save to the file.
How to auto-remove trailing whitespace only from the lines I changed? -> This I don't know and would appreciate any help.
I assume your questions is with regards to Java code. If that's the case, you don't actually need any extra plugins to accomplish 1). You can just go to Preferences -> Java -> Editor -> Save Actions and configure it to remove trailing whitespace.
By the sounds of it you also want to make this a team-wide setting, right? To make life easier and avoid having to remember setting it up every time you have a new workspace you can set the save action as a project specific preference that gets stored into your SCM along with the code.
In order to do that right-click on your project and go to Properties -> Java Editor -> Save Actions. From there you can enable project specific settings and configure it to remove trailing whitespace (among other useful things).
NB: This option has been removed in Eclipse Kepler (4.3) and following releases.
NB #2: The option seems to be back in Eclipse Luna - Luna Service Release 1a (4.4.1)
Removing whitespace from the entire file being edited:
Preferences -> Java -> Editors -> Save Actions -> check Perform the selected actions on save -> check Additional actions -> click Configure.. -> go to Code organizing tab -> check Remove trailing whitespace -> select All lines.
Removing whitespace only from the lines I changed:
Preferences -> Java -> Editor -> Save Actions -> check "Perform the selected actions on save -> check Format source code -> select Format edited lines.
Note it is not necessary to click Configure the formatter settings on the Formatter page - all the code styles automatically include removing trailing whitespace. At least I couldn't find a setting for this in Formatter configuration, and it worked out of the box for built-in Java Conventions, Eclipse, Eclipse 2.1 styles, as well as GoogleStyle.
When using this set-up, you obviously need to also turn off the solution to part 1 of the question.
Eclipse version checked: 4.5.2, 4.11
You don't need any plugin to do so. For instance, if you code JAVA, you can erase trailing whitespaces configuring save actions:
Eclipse 3.6
Preferences -> Java -> Editors -> Save Actions -> Check Perform the selected actions on save -> Check Additional actions -> Click the Configure.. button.
In the Code organizing tab, check Remove trailing whitespace
PyDev can do it by either Ctrl+Shift+F if you have code formatter option set to do it, or by during saving:
Eclipse -> Window -> Preferences -> PyDev -> Editor -> Code Style -> Code Formatter:
I use at least these:
Auto format before saving
Right trim lines?
Add new line at end of file
Do following:
Preferences -> Java -> Editor -> Save Actions
You can map a key in Eclipse to manually remove trailing whitespaces in the whole file, but only on request instead of automatically at save. (Preference/Keys and then map a set of keys to File/Remove Trailing Whitespace) This can be useful if you want to sanitize all new files, but keep legacy code untouched.
Another strategy is to activate visual display of whitespace, so at least you'll know when you're adding some trailing whitespace. As far as I know, there's no way to display only trailing whitespace though, but I'll be glad to be proved wrong.
In a pinch, for those editors that don't support removal of trailing whitespace at all (e.g. the XML editor), you can remove it from all lines by doing a find and replace, enabling regular expressions, then finding "[\t ]+$" and replacing it with "" (blank). There's probably a better regex to do that but it works for me without needing to install AnyEdit.
I would say AnyEdit too. It does not provide this specific functionalities. However, if you and your team use the AnyEdit features at each save actions, then when you open a file, it must not have any trailing whitespace.
So, if you modify this file, and if you add new trailing spaces, then during the save operation, AnyEdit will remove only these new spaces, as they are the only trailing spaces in this file.
If, for some reasons, you need to keep the trailing spaces on the lines that were not modified by you, then I have no answer for you, and I am not sure this kind of feature exists in any Eclipse plugin...
It is impossible to do it in Eclipse in generic way right now, but it can be changed given with basic Java knowledge and some free time to add basic support for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=180349
The dependent issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=311173
For php there is also an option:
There is a really easy way to do this with sed, the Unix command line tool. You could probably create a macro in Eclipse to run this:
sed -i 's/[[:space:]]*$//' <filename>
As #Malvineous said, It's not professional but a work-around to use the Find/Replace method to remove trailing space (below including tab U+0009 and whitespace U+0020).
Just press Ctrl + F (or command + F)
Find [\t ][\t ]*$
Replace with blank string
Use Regular expressions
Replace All
extra:
For removing leading space, find ^[\t ][\t ]* instead of [\t ][\t ]*$
For removing blank lines, find ^\s*$\r?\n
I am not aware of any solution for the second part of your question. The reason is that it is not clear how to define I changed. Changed when? Just between 2 saves or between commits... Basically - forget it.
I assume you would like to stick to some guideline, but do not touch the rest of the code. But the guideline should be used overall, and not for bites and pieces. So my suggestion is - change all the code to the guideline: it is once-off operation, but make sure that all your developers have the same plugin (AnyEdit) with the same settings for the project.
I used this command for git: git config --global core.whitespace cr-at-eol
It removes ^M characters that are trailing.