I found that when I save a .java file in Eclipse, it helps me to reformat any indentation and line breaks. This is very annoying. How can I avoid it?
5 Answers
This is part of "Code Clean up". You can configure what it does in Source -> "Clean Up ..."
Apart from that, you can also configure the formatter if you want the code formatted in general but don't like the defaults. To change these settings, use Window -> Preferences -> Java -> Code Style -> Formatter.
On the last tab of the formatter, you can define on/off tags which allow to you to prevent reformatting of some code.
-
on /off tags just allow me prevent formatting part of code... I don't want to reformat any part of codes using eclipse. Any way without setting a new formatter? Jul 13, 2011 at 9:15
-
As I said: Configure the code cleanup. Just remove "Format Source Code" from the options. Jul 13, 2011 at 9:18
-
-
-
Java Editor --> save action, should also disable ! in order to make it work Aug 4, 2011 at 15:02
You should be able to turn off formatting by placing a special comment, like
// @formatting:off
...
// @formatting:on
This can be changed in the preferences
-
1You have to check "Enable On/Off tags" in the code formatter preferences for these to work.– NoumenonFeb 25, 2020 at 23:00
-
Working in vscode I found
@formatter:off
(s/ing/er/) to work. [heads up for those looking] Aug 23, 2021 at 23:40
You could disable you Save actions
if you have any.
Go to Window > Preferences
and follow Java > Editor > Save actions
. Then uncheck the tick for Perform the selected actions on save
.
I think I found the answer for my own question.
Project-> Preferences->Java Editor-> Save Action- >uncheck Format source code
This will disable all the source code format.
There are some answers saying that
Project ->Preferences->Java Code style-> Formatter (disable)
or
Project ->Preferences->Java Code style-> Clean up (disable)
Both both are only the setting that tell how the code be formatted It will still format the source code even if you uncheck the Formatter and Clean UP but check the Editor Save action
Java Editor will override all the source code settings.
See https://stackoverflow.com/a/5466000/125617
There is another solution that you can use to suppress the formatting of specific block comments. Use /*- (note the hyphen) at the beginning of the block comment, and the formatting won't be affected if you format the rest of the file.
/*- * Here is a block comment with some very special * formatting that I want indent(1) to ignore. * * one * two * three */
Source: http://java.sun.com/docs/codeconv/html/CodeConventions.doc4.html