Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format only changed line instead of the whole file in Intelijj IDEA

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?

like image 867
Masi Boo Avatar asked Jul 24 '20 09:07

Masi Boo


People also ask

How do I format multiple lines in IntelliJ?

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.

How do I fix format in IntelliJ?

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.

How do I turn off auto format in IntelliJ?

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

How to reformat code in IntelliJ IDEA?

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.

How do I change the format of a for loop in IntelliJ?

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.

Where are IntelliJ code formatting settings stored?

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.

What is EditorConfig in IntelliJ IDEA?

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.


3 Answers

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.

reformat dialog

like image 138
Ersoy Avatar answered Nov 07 '22 11:11

Ersoy


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.

enter image description here

like image 41
Epic Martijn Avatar answered Nov 07 '22 10:11

Epic Martijn


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

Reformat Code on Save option

like image 38
Ramy Nasr Avatar answered Nov 07 '22 11:11

Ramy Nasr