The most easy way to reformat the code is with the shortcut: Ctrl + Alt + L (or also Ctrl + Windows Key + Alt + L, useful for Linux users that have the Ctrl + Alt + L shortcut for locking the screen.
Format options
The options for formatting the code are defined in “File/Settings/Editor/Code Style”. Under this last option, there’s a code style scheme for different file types, but we will just look for the Java ones.
The code style options are categorized in the following categories:
- Tabs and indents: the configuration for the indentation, i.e., the indentation of the code blocks. You can also choose to use the tab character, or several spaces.
- Spaces: this section allows to configure the white spaces before and after the control structures and operators, for almost every possible option. For example, to set a white space before the opening curly brace , after commas in method definitions/calls, the assignment operation , and a large etc.
- Wrapping and braces: similar to the previous one, but in this case wrapping the lines, for control structures curly braces, method calls, etc.
- Blank lines: here we can configure the number of blank lines between different “sections” of the code, for both the minimum blank lines to apply, and for the maximum lines to keep. We can, for example, the configure the number of lines for before and after the imports, the lines between each method declaration, after the class header, etc.
- JavaDoc: for the code documentation. For setting the options for the alignment of the parameter descriptions and blank lines, among others.
- Arrangement: here there are two different sections:
- Grouping rules: how to make groups. We can configure to set together the getters and setters, the overridden methods, and the dependent methods.
- Matching rules: for ordering the fields, methods, etc. depending on their modifiers. An example is shown in the 2nd image.
This means that, if we reformat the code with this options, inside the class, in the first “position” would go the fields matching
, and modifiers; in the second, the fields matching , and ; and so on.For rearranging the code, the code formatting shortcut won’t work; we have to select “Code/Rearrange code” option. So, for example, if we would have the following code:
No comments:
Post a Comment