Align key-value pairs in different style of array declaration

There is option "Align key-value pairs" in Settings > Editor >Code Style > PHP > Other > Array Declaration Style

$x = array(
0   => "zero",
   123 => "one two three",
   25  => "two five"
);

It works great i need similar option but for following style of array
(note this is array with sub arrays as values)

$array =  [["one", $snippetId, \PDO::PARAM_INT, 11],
["twooooo", $accessLevel, \PDO::PARAM_INT, 1],
["three", $editLevel, \PDO::PARAM_INT, 1],
["hello world", $status, \PDO::PARAM_INT, 1]];

to format in similar way like this

$array =  [["one",         $snippetId,   \PDO::PARAM_INT, 11],
["twooooo", $accessLevel, \PDO::PARAM_INT, 1],
["three", $editLevel, \PDO::PARAM_INT, 1],
["hello world", $status, \PDO::PARAM_INT, 1]];

is there such option or how can i implement it?

2
Avatar
Permanently deleted user

Hello,

Unfortunately I cannot reproduce this. Could you, please, download and install the latest Early Access Program build from https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Early+Access+Program and check if the described issues are still present there?

0

@Vladimir

The guy does not have an issue -- he just wants some special formatting option (or, possibly, some workaround) to have his multi-level array (2nd code sample) formatted aligned into columns (last code sample).

Right now (e.g. with Default Code Style) it reformats 3rd into 2nd .. but the guy wants other way around.

0

@Ivan

There is no such option. And right now I do not have any real ideas on what can be used to quickly implement such formatting manually (multiple carets perhaps ... but still not sure -- never bothered to do it myself).

But if you format it like that manually .. and want to prevent IDE from breaking it on code reformat -- just surround such region with no-format comments:

https://www.jetbrains.com/help/phpstorm/2016.3/reformatting-source-code.html?search=format#d955779e110

0

Thank you comrade, formatting tags are useful, would be great if you could implement feature i suggest in future releases.

Perhaps you could create a feature request for me, i am not sure how its done after you changed your website software.

0
Avatar
Permanently deleted user

I have often though the same. But I would allow this with one-dimensional arrays to get something like this:

 

        $a = [
            'top'    => $top,   'left'   => $left,
            'width'  => $width, 'height' => $height,
        ];
0

@robsch

There are no such options .. and I doubt that such rules will be created (they are very specific/limited usage (2 elements per row) -- how it should behave if values will be much longer?).

The best I could offer right now: format manually and then surround with do-not-format comments so it will survive any reformatting.

1
Avatar
Permanently deleted user

In fact I also turn off formatting when I need such an layout.

If values are longer, then nothing gets change. The line are then just longer. Would that be a problem? The user could enter a new-line if the layout isn't acceptable anymore. This is up to the user.

Example, how I would expect it to be:

$a = [
    'a' => 'a',
    'b' => 'b',
    'c' => 'c',   'd' => 'dddddddddddddddddddddddddddddddd',
    'e' => 'e',   'f' => '',                                 'g' => 'g',
    'h',          'i' => '',                                 'j' => 'j',
    'l' => 'lll',
];

Obviously, it is not always a good option. The user has to decide if he or she wants that. This should be a setting in the preferences.

Maybe a small formatter instruction could activate it explicitly (for the current statement):

$a = [ //@formatter:array-column-layout

 

But if there's no proper way... I can live with it. It's not my main programming task to deal with such array tables ;-)

1
//@formatter:array-column-layout

There are no such comments support unfortunately. I wish to have such stuff myself, at very least for arrays.

0
Avatar
Permanently deleted user

That would be awesome for me right now, I have a huge array and it would help much for readability + adding, removing keys using the multi selectors. Not that it's impossible to do now, but would be sweet. 

0
Avatar
Permanently deleted user

Feel free to submit a request for that via http://youtrack.jetbrains.com/issues/WI#newissue=yes .

0

请先登录再写评论。