PhpStorm formats multiline array with double tab

Answered

I've posted my question on stackoverflow but didn't get solution.

The problem is that PhpStorm(10) formats array with double indentation instead of single. So I expect this

public function behaviors ()
{
    return [
        [
            'class' => NestedSetsBehavior::className()
        ]
    ];
}

but get this

public function behaviors ()
{
    return [
            [
                    'class' => NestedSetsBehavior::className()
            ]
    ];
}

I've tried to change different settings for indentation including 'Continuation indent' but still get the same.

14 comments
Comment actions Permalink

Hi there,

Please share you Code Style ("File | Export Settings") so it can be downloaded and checked locally.

0
Comment actions Permalink

Thanks. So your code style schema is called "Default (1)", right?

Can you please also share some sample file (before reformatting) that has such issue.

I've tried it on a code you have provided (in a .php file -- I've only surrounded it with a class {} ) .. but it all works just fine. This is what I'm getting:

<?php

class abc
{
    public function behaviors ()
    {
        return [
            [
                'class' => NestedSetsBehavior::className()
            ]
        ];
    }
}

The only thing that I have changed is unchecked "Indent code in PHP tags" from "Code Style | PHP | Other" (otherwise "class" is indented too much ) but that does not have any affect on actual array indenting (I've checked reformatting results before and after).

P.S.

BTW -- do you use EditorConfig plugin .. and do you have .editorconfig files in this project? Settings from there will override whatever you will have in Code Style.

0
Comment actions Permalink

I've 3 installations of PhpStorm on different computers and the same thing happens for all. I look for EditorConfig plugin and have not found it.

So the file before reformatting (the way I want it to be)

https://www.dropbox.com/s/8m9zf8b3d8k55lm/TagBeforeFomat.php?dl=0

And after

https://www.dropbox.com/s/seb9vlz110f4dzy/TagAfterFormat.php?dl=0

0
Comment actions Permalink

There must be something else .. as it reformats arrays just fine here on my 2 computers (PhpStorm 10.0.3 on Windows 7 x64) -- I've tried again with your original non-modified code style.

Could you please disable ALL 3rd party plugins (not bundled by default); restart IDE and try again.

Another thing to check -- maybe that code style that you have provided is not what is actually used? Maybe you have and use project-specific code style ("Project") and not global one ("Default (1)")?

If disabling plugins does not do any different .. could you please: create brand new empty project; copy that file into it; try to reformat -- any difference?

0
Comment actions Permalink

I tried all that propose and nothing helped. Then I thought 'Wait!' and delete all my config directory. That helped. Though I need to restore all my configs from memory (to avoid this thing with double tab) now I am really happy with arrays.

0
Comment actions Permalink

Also, why does PHPStorm indent shorthand array brackets!?

I expect this:

$arr =
[
// values
];

But I get this:

$arr =
[
// values
];

It doesn't match with function formatting which I find jarring:

function test()
{
// code
}

Any reason? Or just an omission?

0
Comment actions Permalink

@Dave

1) Please create separate thread for your issue

2) Please provide sample file (that can be downloaded and opened as is and not just copy-pasted from browser) + your code style

0
Comment actions Permalink

I've been having the same problem recently. Here's my Code Style settings:

https://www.dropbox.com/s/3pxludzbbhh5lut/phpstorm_code-style.jar?dl=0

As far as I can see above, the only solution offered is deleting all settings and setting everything again from memory. Obviously not ideal - has anything else come to light in the meantime? It would be useful to know which setting controls this, to just go and change it, but I can't find anything that changes it.

0
Comment actions Permalink

@Steve

No ideas.

I've activated your Code Style (named "Steve") and used it on this code (spaces used for indent):

<?php

class classA
{
public function behaviors ()
{
    return [
            [
                    'class' => NestedSetsBehavior::className()
            ]
    ];
}}

 

After invoking "Code | Reformat Code" I've got the following (tabs in use):

<?php

class classA {
    public function behaviors() {
        return [
            [
                'class' => NestedSetsBehavior::className(),
            ],
        ];
    }
}

I personally have no ideas why you see different results. Maybe it depends on the file it is used on?.. Maybe some 3rd party plugin affects this?..

I hope devs or tech support guys will have something to tell here.

0
Comment actions Permalink

This has been driving me absolutely crazy, and I think I just figured it out. If you have mixed content HTML/PHP in the same file, the array shorthand for some reason uses the HTML setting for continuation indent as you type, rather than the PHP one. HTML's continuation indent defaults to 8. I changed it to 4 and now my bracket array declarations look correct.

1
Comment actions Permalink

@Josh my God, that could be it. It did always seem to be erratic, and I have some dim awareness that this may have related to whether I'm working in (WordPress) functions files or template files that include HTML. Will try this out and see, but I'm optimistic. Thanks!

1
Comment actions Permalink

@Josh C, that's what the fix was for me. HTML's `Continuation indent` was the issue for m.

0
Comment actions Permalink

That fixed it for me as well. How annoying that was!

0

Please sign in to leave a comment.