Is there a way to reformat a multi-line list into a single line, and vice-versa?

Given this code:

const x = [
2,
3,
8,
];

Is there a way to quickly reformat it (with a keystroke) to this?

const x = [2, 3, 8];

And also take a single line like the one above and break it out into multi-line, too.

12 comments

1. In Settings | Editor | Code Style |JavaScript, Wrapping and Braces, turn Keep when reformatting/Line Breaks OFF, then format your code (Code/Reformat Code...)

2. In Settings | Editor | Code Style |JavaScript, Wrapping and Braces, Array Initializer, choose 'Wrap always' and enable 'Place ']' on new line', then reformat code

0

@Elena

I believe that actual question was "how to switch between such formatting (both ways) at any time" -- something similar to how "Toggle Case" sort of actions work.

1

Andriy is right. I'm looking for a quick key command that will toggle.

1

I see. Then the answer is simple - no, where is no way to do this

1

Would be a handy feature.  Any plugins available?

1

@Stephen Wille

No idea TBH -- I personally know none.

1

Yep agreed... Came here looking for this feature.  Would be very handy as it's something I often do manually, both ways.

1

The closest solution I know of is to bring array elements onto as few lines as possible. Highlight the lines you want to bring in, then Ctrl-Shift-J . To avoid having the formatter distribute array elements one per line again, put

//@formatter:off

before the code you do not want to have auto-formatted and

//@formatter:on

after
4

move a list to 1 line, you can use join line.  put the cursor on the first item, and press ctrl-shift-j.

To split the list, you can use find and replace with regex option. Higlight the list, then find the delimiter, and then replace the delimiter with delimiter\n .

BTW, the delimiter can be a space.

6

For singlelining code, I often do Search & Replace with regex: '\n\s*' => ''

For reverse action, I use code formatting, it does most of the job well.

0

Please sign in to leave a comment.