Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ, Java formatting: force empty blocks to be on one line

Is it possible to tell IntelliJ to put empty block on one line when formatting Java files.

I'd like this:

@Override
public void onClickPositive(int tag, Object payload) {
}

To automatically become this:

@Override
public void onClickPositive(int tag, Object payload) {}
like image 292
BoD Avatar asked Jan 31 '15 17:01

BoD


People also ask

How can I beautify code in Intellij?

Go to Settings/Preferences | Editor | Code Style, select your programming language, and open the Wrapping and Braces tab. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied. Reformat your code ( Ctrl+Alt+L ).

How do I enable auto format in Intellij?

You can use ⌘⌥L (macOS), or Ctrl+Alt+L (Windows/Linux) to reformat a selection of code according to your reformat settings. You can also display the Reformat File dialog with ⌥⇧⌘L (macOS), or Ctrl+Alt+Shift+L (Windows/Linux).

How do I turn off auto indent in Intellij?

In Preferences > Editor > Code style > [language] uncheck reformat on file save .


3 Answers

There is no way to do it for reformatting code.

This can only be done manually.

Check your Code Style settings, under "Alignment and Braces" to preserve your formatting. You should find a "Simple methods in one line" option there. Check it and clear the "Line breaks" check box.

like image 155
Svichkarev Anatoly Avatar answered Oct 26 '22 16:10

Svichkarev Anatoly


Enable following Simple blocks in one line code style in settings.

Settings > Editor > Code Style > Java > Wrapping and Braces > Keep when reformatting > Simple blocks in one line 
like image 32
ziishaned Avatar answered Oct 26 '22 17:10

ziishaned


You can remove the unnecessary line breaks more universally throughout a project than just remove the newline before curly braces.

First go to the following setting and set a larger value for hard wrap:

Settings > Editor > Code Style

Intellij line length setting

The default is 120. Change it to 250 or any other large value.

Next, navigate to the following setting:

Settings > Editor > Code Style > Java > Wrapping and Braces > Keep when reformatting > Line breaks

Intellij line breaks setting

Uncheck "line breaks"

Finally, go back to your file and try to reformat the code.

Reformat code

This removes all the unnecessary line breaks that have been put into the code.

like image 28
Mugen Avatar answered Oct 26 '22 16:10

Mugen