In Eclipse, you can use the Ctrl+Shift+f combination button to format the code. The formatted code is neat and has a unified style. In team development, a unified style of code is very necessary.
Automatic formatting will automatically format the code itself and the commented part, so that the following problems may be encountered in actual development:
- Comments: General comments have been deployed according to a certain format and typesetting, but after formatting, they are messed up and the readability is lost
- Code: Some parts of the code may have some logic or other considerations. We hope to maintain the set newline or indentation style, such as Spring Security's security configuration code, and we hope that related configurations will be put together.
This correspondence requires two functions:
1. The comment part does not need to be automatically formatted
2. Some codes are exceptions when formatting, and no automatic formatting is performed
Eclipse supports the above functions, you only need to make relevant settings
Configure code comments not to be automatically formatted
An example of comment formatting
The code comment template is set in Eclipse, and the automatically generated class comments are as follows:
But after using the Ctrl+Shift+f combination button to format the code, the typesetting of the comments of the class is messed up.
Unformat class comments
The settings are as follows:
Windows > preferences > Java > Code Style > Formatter > Edit > Comments.
Uncheck "Enable Javadoc comment formatting".
Note: Eclipse's built-in code style cannot be modified, you need to create one yourself for modification
additional code formatting
// @formatter:off
Formatting can be turned off for the following code using- Use to
// @formatter:on
turn on automatic formatting, so use and
in pairs , you can realize that the code between these two tags will not be automatically formatted.// @formatter:off
// @formatter:on
// @formatter:off
public void method() { System.out.println("hello"); }
// @formatter:on
In addition, the labels for turning off and turning on auto-formatting can also be customized, and the setting method is similar to the settings noted above, find Off/On Tags
the block to set