Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable indentation for Lambda Expressions/Anonymous Classes in Intellij IDEA

The following image pretty much illustrates the whole issue: When pressing ENTER IntelliJ IDEA indents the closing brace relative to the lambda expression, while it should go relative to the parent expression's braces.

I have tried using // formatter:off with the appropriate settings, but to no avail. I have also tried looking for indentation rules for lambda expressions/anonymous classes, but I couldn't find those.

Any suggestion is much appreciated!

like image 438
Nikita Avatar asked Feb 22 '15 19:02

Nikita


People also ask

How do I get rid of indentation error in IntelliJ?

Reformat line indents If you need to adjust indentation settings, in the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | Code Style. On the appropriate language page, on the Tabs and Indents tab, specify the appropriate indents options and click OK.

Can we replace anonymous class with lambda?

By the way, you cannot always use lambda expression in place of Anonymous class, because of its limitation of being SAM type. If you are using an anonymous class to implement an interface with two abstract methods then you cannot replace it with a lambda of Java 8.

How do I fix tabbing in IntelliJ?

Open indentation settings in code style scheme Click the widget and select Configure Indents for 'Language'. In the dialog that opens, you can change settings for tabs and indents and also configure other code style settings. Click OK. Reformat the necessary part of your project to apply new indentation settings.


2 Answers

You need to close the outmost parenthesis right next to the closing bracket for the anonymous class to get the desired indentation.

Instead of:

itemClose.addActionListener(new ActionListener() {
                              @Override
                              public void actionPerformed(ActionEvent e) {

                              }
                            }
);

Remove the line/space between the last } and ); and you get:

itemClose.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {

  }
}); 
like image 146
Isabella Almeida Avatar answered Sep 20 '22 05:09

Isabella Almeida


What helped for me was setting Editor > Code Style > Java > Continuation indent to a lower value.

like image 43
knub Avatar answered Sep 23 '22 05:09

knub