IntelliJ IDEA: is there a way to format java code in a canonical way? - intellij-idea

I have two java source files. Both represent the same class (semantically), but they were formatted differently.
For example, one of them contains the following line:
return Boolean.valueOf(Boolean.getBoolean("abc"));
While in the second file it looks like 2 lines:
return Boolean.valueOf(
Boolean.getBoolean("abc"));
In both cases, when I apply formatting (Ctrl+Alt+L), these lines do not change.
Is it possible to format them in some canonical way: that is, to get the same code if the only difference is formatting?
Equivalently: is there any way to remove all ignorable whitespace? Such a 'dried-out' program would then be easily restored using 'Reformat code'.

You should be able to do this if you turn off "Wrapping and Braces | Keep when reformatting | Line Breaks" in the Java code style settings.

Go to File > Settings > Ediotor > Code Style > java
in tab Wrapping and Braces uncheck line breaks
Apply and make (Ctrl+Alt+L) again.

Related

How to shorten the first line in the Intellij console?

I want my first line at the top of the console to look like this:
, but mine looks like this, and it's long:
How can I shorten it to like the first picture?
I'm watching Java tutorials on youtube and their first lines ("C:\programs Files\Java\jdk\whatever"...) are always so short and pretty with 3 cute dots in the end, but mine is long and annoying.
You can define the lines which should be folded:
Right click on that line in the console
Choose "Fold Lines Like This"
Click OK
You can also define special keywords/phrases in this menu, where lines, which contains the phrase will be folded with ellipsis (...) at the end.
See the ConsoleViewImpl.java file in the source code of IntelliJ IDEA
This line (execution command) will be automatically hidden in case the string length is more than 1000 symbols.
So, once you add something to the execution command (for example by adding libraries), the execution command is folded automatically.

Formatting code in PhpStorm does not work as I need it to work

How to use automatic line wrap and code formatting together in PhpStorm? My PhpStorm version is 2019.3.1
My SQL statements and my Strings look like the pic below.
What do I have to do, that the automatic wrapped lines do NOT jump to column 1 in the editor?
What do I have to do, that the automatic wrapped lines do NOT jump to column 1 in the editor?
Configure it accordingly (affects soft wrapped lines only, obviously): Settings/Preferences | Editor | General | Soft Wraps | Use original line's indent for wrapped parts

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) {}
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.
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
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
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
Uncheck "line breaks"
Finally, go back to your file and try to reformat the code.
This removes all the unnecessary line breaks that have been put into the code.

Intellij - Reformat Code - Insert whitespace between // and the comment-text?

I am working with another human being on project from that the professor expects to have uniform code-style. We have written large separate junks of code on our own, in which one has written single line comments without a white-space between the single-line-comment-token and the other one has inserted a white-space. We are working with IntelliJ and have failed to find an option to enable the Reformat Code function, to insert a white-space.
TLDR:
Can you tell us how to convert comments from that to this in IntelliJ?
// This is a load bearing comment - don't dare to remove it
//This is a load bearing comment - don't dare to remove it!
You can do a global search and replace (ctrl-shift-r on windows with default keyboard layout, or Replace in Path under the Edit/Find menu).
Check the regular expression option and enter //(\S.*) as the text to find and // $1 as the replacement. Check the whole project option, and clear any file masks. You can single step through the replacements, or simply hit the All Files option.

IntelliJ force code formatter to join lines based on wrap settings

How can i reformat file(s) in IntelliJ and join all lines that are split.
I know that I can do that individually by selecting lines and "join lines" with CTRL + SHIFT + J
Since we changed our code formatting wrap policy recently I want to be able to join lines in all files based on the updated wrap setting. (Settings > Code Style > General > Right margin)
The only thing is that IntelliJ seems happy to split lines based on wrap setting, but will silently deny to join lines based on that setting.
Unlike the question Force code formatter in IntelliJ to join lines, I am not satisfied by splitting lines or joining manually (as the accepted answer suggests). I want IntelliJ to join lines automatically.
Bonus question: Which other editors can do this?
Disable the following code style option - Project Settings - Code Style - Wrapping and Braces - Keep when reformatting - Line breaks
IntelliJ IDEA 15
File > Settings... > Editor > Code Style > Java > Wrapping and Braces > Keep when reformatting > uncheck Line breaks
(if you want to have the same setting for another type of file, choose it from Code Style)
Go to Code > Reformat Code (Ctrl + Alt + L).