New and Changed Formatting Options in PhpStorm 8
We continue to make improvements and add features to the PHP formatting engine based on your requests. Below is an overview of the new options and changes already included in PhpStorm 8 EAP.
Class fields and constants: Alignment and blank lines
There are two new options “Align fields in columns” and “Align constants” under “Class field/constant groups” section (“Wrapping and Braces“):
If the option “Align fields in columns” is selected, the following code:
class Data { private $name = ""; private $id = -1; public static $defaultPath = '/'; }
will be formatted as follows (assuming that “Blank lines around fields” is 0):
class Data { private $name = ""; private $id = -1; public static $defaultPath = '/'; }
but if you put a blank line between $id and $defaultPath fields, the final result will be:
class Data { private $name = ""; private $id = -1; public static $defaultPath = '/'; }
That’s because $name and $id now form their own group separated from $defaultPath with a blank line. For example, if you set “Blank lines around fields” to 1, you will need to use at least 2 blank lines to separate groups. Otherwise the fields in the example above will be formatted as one group:
class Data { private $name = ""; private $id = -1; public static $defaultPath = '/'; }
Class constants are processed separately from fields but the option “Align constants” works similar to “Align fields in columns“. Class constants also have their own setting to specify blank lines around them (0 by default):
Controlling indentation of code being commented out
When you comment out a chunk of code with line commenting action (Ctrl+/ or CMD-/), you can specify if you want line comment characters // to start at first column, for example:
function foo($flag) { // if ($flag) { // return 0; // } }
or be indented with the code:
function foo($flag) { //if ($flag) { // return 0; //} }
For the latter you need to deselect the option “Code commenting“/”Line comment at fist column” (“Other” tab). The option is on by default (line comments are created starting at first column):
Arrangement section rules
Sections are logical blocks of code enclosed in start/end comments. Optionally they may contain custom folding descriptors like “<edtior-fold>“…”</editor-fold>” or “region“…”endregion“.
You may add a special section rule like shown here:
This will bring up a dialog where you can specify a comment type (“section start” or “section end“) and a comment text which can be any text including manually defined “<editor-fold>” or “region“/”endregion” markers:
If the comment with the given text already exists in the code, it will not be generated again but possibly only moved according to the rules, if needed.
Consider the following example:
Rearranging the following code with the above rules:
will produce:
Note that class fields are not only grouped together but also enclosed in “region“…”endregion” comments which make the group collapsible.
Spaces within brackets: WordPress style
WordPress has a specific requirement for spacing inside brackets where simple values do not have spaces around them while variables and expressions must be surrounded with spaces as described here. There is a special option “Spaces around variable/expression in brackets” to meet this requirement:
Spacing around unary NOT (!)
Previously you could define spaces both around additive operators (+,-,++,–) and Not (!) unary operators. Now there are two separate options for spaces before and after the unary Not (!):
Note that the old option for spaces around unary operators no longer affects the unary Not.
Other changes
“Align consecutive assignments” has been moved from “Other” to “Wrapping and Braces” panel but the option is still there:
Afterword
All the new options can already be tried with the recent PhpStorm EAP build. Needless to say, we would be glad to hear back from you. If you have new ideas or find out that something doesn’t work as expected, please submit an issue in YouTrack. Thanks!
Develop with pleasure!
-JetBrains PhpStorm Team