ChatGPT解决这个技术问题 Extra ChatGPT

How to correct indentation in IntelliJ

How can indentation be automatically (not manually) corrected in IntelliJ?

In Eclipse, it's possible to just highlight the code that needs indenting, right-click, and select Source > Correct indentation.

Is there any method for doing the same thing in IntelliJ?


N
Neuron

CodeReformat Code... (default Ctrl + Alt + L) for the whole file or CodeAuto-Indent Lines (default Ctrl + Alt + I) for the current line or selection.

You can customise the settings for how code is auto-formatted under FileSettingsEditorCode Style.

To ensure comments are also indented to the same level as the code, you can simply do as follows:

https://i.stack.imgur.com/m52kZ.jpg


it doesn't work for comments, is there any way for auto-indent for comments. I want to format code and comments of the same way that eclipse does.
is there a way to do it for all classes in my project?
@ZiadHalabi reformat code from the project root context menu.
It is working, but only for lines beginning with *(line comments). If you have a paragraph block comments, with only first line beginning with /* and after last line ending with */, with all intermediate line beginning with text, it does not work, when it also should. So... the logic is a little bit.. naive?
C
Community

Just select the code and

on Windows do Ctrl + Alt + L

on Linux do Ctrl + Windows Key + Alt + L

on Mac do CMD + Option + L


consider accepting existing answer instead of answering yourself with exactly the same (Reformat Code... is Ctrl+Alt+L in the default Windows keymap).
In Linux you will lock the screen. WTH this shortcurt is in the linux version? hahah
It is the same... Ctrl-Alt-L (I'm using Android Studio)... which is a small shame among other shames. Hopefully, the adoption of Android Studio will provide more traction to the Linux version.
In Linux, you can use it with the 'Super' (or Windows) key. So, Ctrl + Alt + Super + L, does the required indentation.
There is no ALt key in mac. What are you guys talking about?
N
Neuron

https://i.stack.imgur.com/BkRtD.png

Then Ctrl + Alt + L to format your file (or your selection).


L
L. Spiekermann

In Android Studio this works: Go to File->Settings->Editor->CodeStyle->Java. Under Wrapping and Braces uncheck "Comment at first Column" Then formatting shortcut will indent the comment lines as well.


N
Neuron

Ctrl + Alt + L works with Android Studio under xfce4 on Linux. I see that Gnome used to use this shortcut for lock screen, but in Gnome 3 it was changed to Super+L (AKA Windows+L): https://wiki.gnome.org/Design/OS/KeyboardShortcuts


P
Pang

You can also try out Ctrl + Alt + I even though you can also use L as well.


W
WesternGun

Solution of unchecking comment at first column is partially working, because it works for line comments, but not block comments.

So, with lines like:

/* first line
 * second line
 * ...
 */

or

// line 1
// line 2
// line 3
...

they are indented with "Auto reformat", but lines like:

/* first line
   second line
   ...
 */

the identation will not be fixed.

So you should:

add * or // before each line of comments

then uncheck Keep when reformatting -> comment at first column

and Auto reformat.


Line selection mode will help you(mouse middle key). To add line comments, search for line comment in key maps.