You can rearrange your code according to the arrangement rules set in the Code Style. PhpStorm will reformat your code in accordance with the current style settings, keeping existing formatting for the rules which you've selected. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied. For example, you can adjust the IDE to keep simple methods and functions in one line, whereas normally they are expanded into multiple lines after code reformatting. You can select formatting rules which will be ignored when you reformat the code. If you need, click the link in the popup to open the Code Style settings page and change the exclusion scope. If you try reformatting the excluded file, PhpStorm displays a popup notifying you that formatting for this file is disabled. In the Scope area, click to add a scope where you can specify files that you want to exclude from reformatting. On the Formatter Control tab, select the Enable formatter markers in comments checkbox. You can exclude a group of files or part of code from reformatting. On the appropriate language page, on the Tabs and Indents tab, specify the appropriate indents options and click OK. If you want to see the exact changes made to your code during the reformatting, use the Local History feature. Cleanup code : select this option to run the code cleanup inspections.Ĭlick OK.
Rearrange entries : select this option if you need to rearrange your code based on the arrangement rules specified in the code style settings. For more information, refer to the Optimize imports section. Optimize imports : select this option if you want to remove unused imports, add missing ones, or organize import statements. In the dialog that opens, if you need, select the following reformatting options. You can also exclude part of code or some files from the reformatting. You can reformat a part of code, the whole file, group of files, a directory, and a module. However, if you use EditorConfig in your project, options specified in the.
Find out more details in the new features for PHPStorm 2021.2 post.PhpStorm lets you reformat your code according to the requirements you've specified in the Code Style settings. The team at Jetbrains reached out and brought to my attention that PHPStorm 2021.2 actually has a new feature too - "Actions on Save" - which is able to do the same thing, without using a Macro. So in that regard, 2021.2 actually fixed the issue.īut regardless, if you’re a Format and Save fan like me, tweak your Macro to keep the formatting love on every save. While this feels like a bug given it has only cropped up with PHPStorm 2021.2, it actually is more a sign of an earlier bug with 2021.1 and earlier: moving the caret up and down when the entire text is selected does actually make sense to move to the top and down one line. So now rather than touching the caret at all, a simple Escape deselects all of the content too, and leaves the caret in place. In PHPStorm 2021.2, I have re-recorded a Macro, with 4 steps – and to be honest, I feel should actually work in earlier versions: This actually meant that on every save, I was actually moved back to Line 2 of every file. Steps 1-3 are all fine, but…Ĥ moved the caret to the top of the document Those last two steps allowed the caret to stay in the same position, and to have all of the content de-selected (after step 1’s Select All).īut in PHPStorm 2021.2, the caret behaviour of selected text has changed, and steps 4 and 5 here led to some undesirable behaviour. In PHPStorm 2021.1, this Macro had 5 steps: While reformatting on every save may be overkill, it does mean that my code does remain clean, neat and consistent. I’ve been using a Macro for years in PHPStorm that, when I hit Cmd+S, reformatted my code based on the project’s settings.