What I would like to achieve with lambda indentation is the following:
Multi-line statement:
String[] ppl = new String[] { \"Karen (F)\", \"Kevin (M)
Out of the box IntelliJ 13 will probably work for you.
If I write it this way:
// Mulit-Line Statement
String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)" };
List<String> strings = Arrays.stream(ppl)
.filter(
(x) ->
{
return x.contains("(M)");
}
).collect(Collectors.toList());
strings.stream().forEach(System.out::println);
And then apply the auto formatter (no changes):
// Mulit-Line Statement
String[] ppl = new String[]{"Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)"};
List<String> strings = Arrays.stream(ppl)
.filter(
(x) ->
{
return x.contains("(M)");
}
).collect(Collectors.toList());
strings.stream().forEach(System.out::println);
The same is true for your single line statement. It has been my experience that IntelliJ is more flexible in how its auto formatting is applied. IntelliJ is less likely to remove or add line returns, if you put it there then it assumes you meant to put it there. IntelliJ will happily adjust your tab-space for you.
IntelliJ can also be configured to do some of this for you. Under "settings" -> "code style" -> "java", in the "Wrapping and Braces" tab you can set "chain method calls" to "wrap always".
Before Auto-Formatting
// Mulit-Line Statement
List<String> strings = Arrays.stream(ppl).filter((x) -> { return x.contains("(M)"); }).collect(Collectors.toList());
// Single-Line Statement
List<String> strings = Arrays.stream(ppl).map((x) -> x.toUpperCase()).filter((x) -> x.contains("(M)")).collect(Collectors.toList());
After Auto-Formatting
// Mulit-Line Statement
List<String> strings = Arrays.stream(ppl)
.filter((x) -> {
return x.contains("(M)");
})
.collect(Collectors.toList());
// Single-Line Statement
List<String> strings = Arrays.stream(ppl)
.map((x) -> x.toUpperCase())
.filter((x) -> x.contains("(M)"))
.collect(Collectors.toList());
Latest version of Eclipse has a built-in formatter java 8 formatting, goto
Preferences -> Java -> Code Style -> Formatter -> Active profile
and Select Eclipse 2.1[built-in] profile
Eclipse (Mars) has an option for lambda expressions formatter.
Go to Window > Preferences > Java > Code Style > Formatter
Click the Edit
button, go to the Braces
Tag and set the Lambda Body
to Next Line Indented
Another option is update these properties into your project settings. (yourWorkspace > yourProject > .settings > org.eclipse.jdt.core.prefs
)
org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow=insert
org.eclipse.jdt.core.formatter.brace_position_for_lambda_body=next_line_shifted