78

IntelliJ keeps formatting this:

public void addElement(Element elem) {
    this.elements.add(elem);
}

into this:

public void addElement(Element elem) { this.elements.add(elem); }

I prefer the multi-line formatting. How can I tell this to IntelliJ? I have unchecked the following boxes in Settings -> Editor -> Java -> Wrapping and Braces

  • Control statements in one line
  • Simple blocks in one line
  • Simple methods in one line

Didn't help.

Pang
  • 9,564
  • 146
  • 81
  • 122
Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89
  • 4
    Possible duplicate of [IntelliJ collapsing single line methods](http://stackoverflow.com/questions/35995083/intellij-collapsing-single-line-methods) – Matthew Simoneau Sep 09 '16 at 17:18

2 Answers2

107

below is the shortcut for Merge multiple lines into a line -

Ctrl + Shift + J

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ajay Kumar
  • 4,864
  • 1
  • 41
  • 44
  • 4
    This saved me a lot of trouble – klutt Sep 24 '20 at 10:15
  • 4
    This should be the top answer if this question was for a quick way to do it. – Macintosh Fan Nov 05 '20 at 03:07
  • @MacintoshFan the original question is how to stop intellij from collapsing multiple lines into one line, which I suspect is just happening in the display of the file for the author of the question. This answer is how to collapse multiple lines into one line in the actual file. Also, this answer doesn't work in my Intellij v2021 on a Mac. – Kirby Jan 25 '23 at 17:27
  • It works just fine for me: latest macOS and IntelliJ. – Macintosh Fan Jan 26 '23 at 05:34
106

I suspect that your IntelliJ is not actually changing the actual file, but instead, visually collapsing one line methods in the display.

To have IntelliJ not change the display of one line methods, look at:

Preferences (or Settings), Editor, General, Code Folding, Collapse by default, One-line methods

enter image description here

Pang
  • 9,564
  • 146
  • 81
  • 122
Kirby
  • 15,127
  • 10
  • 89
  • 104
  • 5
    You will have to reopen the file(s) to see the effect. – lxxxvi Jan 24 '17 at 08:36
  • It's settings now, not preferences. (May just be the difference between mac and windows, I am on windows) – J. Schei Jan 26 '17 at 05:26
  • 3
    It remains "Preferences" on the Mac ;-) – wh81752 May 16 '17 at 20:32
  • I didn't have to `Refresh` – Raf Feb 25 '19 at 20:33
  • 3
    Op was looking for formatting, which changes structure. Folding is for presentation in the editor gui and does not format the single line annotation/decorator as desired. – Draculater Jul 25 '21 at 18:30
  • for the opposite effect https://stackoverflow.com/questions/59081326/reformat-a-one-line-method-to-a-single-line-of-text-in-intellij – Alex78191 Dec 15 '21 at 14:15
  • 1
    @Draculater When I asked the question I thought it was doing formatting. I didn't realize at the time this was just for presentation. This answer solves the issue I had. – Atte Juvonen Jan 25 '23 at 17:02
  • @Draculater, Intellij won't rewrite the actual multi line method to a one line method in the file. I suspect Intellij is just reformatting the presentation of the file, so I stand behind my answer. – Kirby Jan 25 '23 at 17:23