Automatically add curly braces to single line if/for/while etc statements in IntelliJ for java
Asked Answered
T

3

6

I want to add curly braces in IntelliJ to single line statements. Lets say I have:

if(x)
  print(y)

I want IntelliJ to automatically format that to:

if(x){
  print(y)
}
Tannenbaum answered 10/1, 2020 at 16:46 Comment(0)
J
8

File -> Settings -> Editor -> Code style -> Java -> Wrapping and braces -> if/while/for() statement -> Force braces -> Always

Junette answered 10/1, 2020 at 16:54 Comment(0)
T
5

Under File > Settings > Editor > Code Style > Java on the tab "Wrapping and Braces" under "'if()' statement", set "Force braces" to "Always". And the same for "'for()' statement", "'while()' statement" and "'do ... while' statement".

When you reformat (Ctrl+Alt+L), or complete the statement using Ctrl+Shift+Enter, IntelliJ will add the braces for you.

Triarchy answered 10/1, 2020 at 16:55 Comment(0)
D
3

For thoses (like me) who struggle finding this option in Rider, it is located just right here : Editor > Code Style > C# > Syntax Style > Braces See image, then you have to select "Enforce Always".

Destiny answered 4/5, 2024 at 10:38 Comment(2)
I believe the question is about IntelliJ IDEA and not about Rider.Rummage
Thanks @Abra, I know, but when I was looking for a solution for Rider, I came across this question. Maybe my comment could help others like me. I would have been grateful to find this.Destiny

© 2022 - 2025 — McMap. All rights reserved.