Is there keyboard shortcut to convert horizontal list to vertical list and vice versa in IntelliJ IDEA and based on it IDE PhpStorm? For example I have array
$arr = [
'test',
'a' => 'b',
];
and I want to make it single-line, I can select text and use Ctrl + Shift + J, I get
$arr = ['test', 'a' => 'b', ];
it is almost good, I can remove last ,
manually. But how to make the opposite: convert horizontal list to vertical? It is not only about arrays, this question is also about function signatures for example
public function test($arg1, $arg2, $arg3, $arg4)
and function calls
test($arg1, $arg2, $arg3, $arg4);
sometimes string become too long and it is needed to split it for readability like this:
test(
$arg1,
$arg2,
$arg3,
$arg4
);
Note that this question is not about code folding, I want to really change formatting, not just show-hide just for me.
Split any list using the new intention in IDE: Show Context Actions with Alt + Enter then choose option Split comma-separated values into multiple lines. The reversed intention is also available for cases where you want to combine items into one line: Alt + Enter then choose option Join comma-separated values into a single line.
Vertical list to horizontal (join)
Select items which should be joined and use Join Lines command Ctrl + Shift + J.
Horizontal list to vertical (split)
Use Realigner plugin, you can install it in Settings > Plugins > Browse repositories… (or Marketplace) It adds Split command with shortcut Ctrl + Shift + Alt + P. After calling of this command you will see dialog, configure it like this:
delimiter is comma only, without spaces. After clicking on "OK" plugin will split your list, but also settings will be remembered, so in future you can split any list using command shortcut then Enter without spending of time. But after splitting code will not have indents, need to run Reformat Code. As a result we can achieve target in two steps:
It is not perfect, but better then manual editing. Potential problem: Realigner doesn't analyse code, it just split text by delimiter. It would be better to create a macro for these commands and add shortcut for it, I tried but without success, it is easy to create macro but result is buggy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With