Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal list to vertical list and vice versa - IntelliJ IDEA based IDE

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.

like image 287
kdmitry Avatar asked Oct 31 '22 15:10

kdmitry


1 Answers

Since IntelliJ IDEA 2019.2

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.

gif image demonstrating new intention

Prior to IntelliJ IDEA 2019.2

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:

split by delimiter dialog

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:

  1. Split Ctrl + Shift + Alt + P then Enter.
  2. Reformat Code Ctrl + Alt + L.

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.

like image 159
kdmitry Avatar answered Nov 09 '22 09:11

kdmitry