Idea automatically format code-configuration

In order to keep the code tidy during the development process, I often use the shortcut keys Ctrl+Alt+L of Idea to format the code. This function is very useful, so I basically use this shortcut to format the code every time I type a piece of code. However, since it is a multi-person collaborative project in development, this shortcut key will always accidentally format other people's development. Unnecessary spaces and blank lines are always accidentally submitted when submitting. These formatting causes submitted spaces to cause code conflicts in many cases.

Therefore, the function of Idea formatting must meet my development needs:

format code

only format my modified code

Do not format unmodified code

I believe that Idea is so powerful that it should be able to meet my needs through settings, so after a period of unremitting efforts, I finally found a configuration method. You can use the shortcut key: Ctrl+Alt+Shift+L to call up the formatting configuration dialog frame:

By default, the Scope option is Whole file, which is to format all the code of the current window. If the code file contains code submitted by others, if the format does not match the format we configured, the entire file will be formatted. It will also lead to some useless spaces and blank lines in our submission. These spaces and blank lines for formatting are often the source of conflicts in our code, and it is inconvenient when doing code review .

Here we only need to set the Scope to Only changes uncommitted to VCS in the figure. Since this configuration is memorized, we only need to configure it once. At this time, when we use Ctrl+Alt+L to format, we will continue to use this configuration to achieve formatting freedom.

In the new version of Idea, we can also set the formatting operation to be triggered when it is automatically saved. we can in

Settings in Preferences->Settings->Tools->Actions on Save.

Note that after checking the Reformat code, the following Whole file should be changed to Changed lines.

Guess you like

Origin blog.csdn.net/xijinno1/article/details/132157886