Search code examples
phpstormsettings

PhpStorm Space and Align settings in 'Code style'


I'm trying to set the spaces and the alignment for code formatting in PhpStorm but it doesn't work.

In the screenshot you can see that the box is checked but in the code the setting is not applied. I have also some problems with the spaces, I ticked all the boxes in "Space Within" section but when I write an if() statement, for example, the editor doesn't add the space between the parenthesis. Same problem for the semicolon at the end, is never added automatically.

I've tried to change the settings in "File->Settings and File->Default Settings", in Default scheme and Project Scheme with no success.

Thank you in advance for your help!

PhpStorm settings


Solution

  • Array initializers

    There is no way (AFAIK) around using Code | Reformat Code. The default shortcut for this is Ctrl+Alt+L (aka Alt Gr+L), works either for the whole file or for the currently selected code.

    If your code style rules are set up well, you can see this as your "save" action and do it as frequently as you need.

    If statements

    As @LazyOne suggested, your can also make use of Live Templates to achieve what you want in this case.

    In Settings | Editor | Live Templates, add an entry to the PHP section, call it if and use the following template text:

    if ( $CONDITION$ ) {
        $THEN$
    }
    

    Note: I've added a tab character before $THEN$, feel free to change it to spaces if needed.

    Now if you use if and hit Ctrl+Enter, PHPStorm should suggest your live template before the default if, so you can select it and then navigate through the template variables using Tab.