Intellij format complete file when I use Ctl+Atl+L for from menu code-> Reformat Code. This generates whole diff in git commit. I want to format only the code I have changed not the whole file. How can I do it?
You can reformat line indents based on the specified settings. While in the editor, select the necessary code fragment and press Ctrl+Alt+I . If you need to adjust indentation settings, in the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | Code Style.
Sometimes code formatting can get out of sync, but there's an easy fix in IntelliJ IDEA. You can use ⌘⌥L (macOS), or Ctrl+Alt+L (Windows/Linux) to reformat a selection of code according to your reformat settings.
In Preferences > Editor > Code style > [language] uncheck reformat on file save .
In the editor, select a code fragment you want to reformat. From the main menu, select Code | Reformat Code or press Ctrl+Alt+L. If you don't select a code fragment, IntelliJ IDEA will reformat the whole file. Either open your file in the editor and press Ctrl+Alt+Shift+L or in the Project tool window, right-click the file and select Reformat Code.
For example, if we want for loops to also have curly braces, we can highlight an example of the code we want to change the formatting of, and press Alt+Enter. Then we select “Adjust code style settings”. IntelliJ IDEA will show just the settings that apply to this bit of code.
These formatting settings are stored in a settings file in the project’s .idea folder. IntelliJ IDEA also supports using an EditorConfig file to define the code style. Create a new EditorConfig file in a package or directory. This will be our root, add the standard EditorConfig properties and IntelliJ IDEA-specific settings for Java code.
IntelliJ IDEA lets you reformat your code according to the requirements you've specified in the Code Style settings. However, if you use EditorConfig in your project, options specified in the.editorconfig file override the ones specified in the code style settings when you reformat the code.
If your file is under version control, another option could be selecting Only VCS changed text
from the Reformat dialog modal.
As it stated in the documentation
If this checkbox is selected, then reformatting will apply only to the files that have been changed locally, but not yet checked in to the repository. This checkbox is only available for the files under version control.
You have to select the portion of the code you'd like to format (in your case, the edited code). And then press Ctrl+Alt+L
.
You can find more info here.
In case this is helpful to anyone, there is an option in PhpStorm under "Tools > Actions on Save" that allows you to reformat code on save. The option allows you to only reformat changed lines.
This effectively allows you to reformat changed lines only by pressing cmd + s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With