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

1

6

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.

Joanjoana answered 10/11, 2015 at 16:5 Comment(2)
Would search-and-replace with comma , to "comma - newline - tab" within the selected code fragment work?Unaccomplished
This is partial solution. Search-and-replace in selected text in IntelliJ IDEA requires interaction with mouse, I would like to avoid it. Also replacing of "comma" on "comma - newline - tab" is only one case, spaces also may be used for indent and there may be several levels of indentation. But thanks for idea, I looked in this direction and found acceptable solution.Joanjoana
J
2

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.

Joanjoana answered 12/11, 2015 at 3:31 Comment(2)
It took years but finally they did it. Old way with usage of Realigner still works in 2019.2.Joanjoana
Is there a pattern/template for that. I don't like how idea splits/joins itByron

© 2022 - 2024 — McMap. All rights reserved.