Search code examples
phpstormjetbrains-idecode-formatting

How to reverse the effect of code style "Chop down if long"?


I have a big multi-dimensions array like this (the ouput of a PhpUnit provider) :

return [
    'my first test case' => [
        $variable1,
        $variable2,
        $variable3,
        $variable4
    ],
    'my second test case' => [
        $variable5,
        $variable6
        $variable7
        $variable8
    ],
    // And this goes on for a long time...
];

The inner dimension of this array has been chopped down by PhpStorm code formatting, because the lines exceeded the characters count I set as a maximum.

Since I did that formatting, I've refactored my test a little and reduced the number of variables in each test case. Now I could collapse it again and still be OK with my code style rules

return [
    'my first test case' => [$variable1, $variable2, $variable3, $variable4],
    'my second test case' => [$variable5, $variable6, $variable7, $variable8],
];

Is there a way I could automate this tedious process ? I tried using code formatting again but it won't "unchop" when that's not required anymore. I took a look at intentions to see if there was something to chop down, wrap or collapse a specific array without doing the whole formatting, but no luck.

Note : my PhpStorm is up to date (2023.1.4 at the time of writing)

I also tried using an external formatter (phpcbf) with the same style rules, but it leaves the array chopped down as well.


Solution

  • Option one:

    Disable Settings | Editor | Code Style | PHP | Wrapping and Braces | Keep when reformatting | Line breaks.

    Option two, a more granular one:

    Place the caret into the beginning of what you want to merge into one line and hold Join Lines (Ctrl+Shift+J by default).