lambda formatting with new lines and indents

What I would like to achieve with the lambda indent is the following:

Multiline declaration:

String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)" };
List strings = Arrays.stream(ppl)
                         .filter(
                             (x) -> 
                             {
                                 return x.contains("(M)");
                             }
                         ).collect(Collectors.toList());
strings.stream().forEach(System.out::println);

Single line declaration:

List strings = Arrays.stream(ppl)
                         .map((x) -> x.toUpperCase())
                         .filter((x) -> x.contains("(M)"))
                         .collect(Collectors.toList());

Currently, Eclipse is automatically formatting the following:

Multiline declaration:

String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)" };
List strings = Arrays.stream(ppl).filter((x) ->
{
    return x.contains("(M)");
}).collect(Collectors.toList());
strings.stream().forEach(System.out::println);

Single line declaration:

String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des(M)", "Rick (M)" };
List strings = Arrays.stream(ppl).map((x) -> x.toUpperCase())
        .filter((x) -> x.contains("(M)")).collect(Collectors.toList());
strings.stream().forEach(System.out::println);

And I find it really messy, because of the way they call it collect is directly under the return and there is no space between them. I would prefer if I could start the lambda on a new indented line, and so that the .filter( call would be right above the .collect( who loves. However, the only thing that can be customized with standard Java-8 Eclipse Formatter is the brace at the beginning of the lambda body, but nothing for the () parenthesis in advance, nor the return.

And in the case of single line calls, it just uses baseline wrapping and makes it a chained mess. I don’t think I need to explain why it is difficult to decipher later.

Is there any way to customize the formatting more and get the first kind of formatting in Eclipse? (Or optionally in another IDE such as IntelliJ IDEA.)

EDIT: The closest I could get was with IntelliJ IDEA 13 Community Edition (read: free version: P) which was the following (defined by continuous indent which in this case is 8):

public static void main(String[] args)
{
    int[] x = new int[] {1, 2, 3, 4, 5, 6, 7};
    int sum = Arrays.stream(x)
            .map((n) -> n * 5)
            .filter((n) -> {
                System.out.println("Filtering: " + n);
                return n % 3 != 0;
            })
            .reduce(0, Integer::sum);

    List list = Arrays.stream(x)
            .filter((n) -> n % 2 == 0)
            .map((n) -> n * 4)
            .boxed()
            .collect(Collectors.toList());
    list.forEach(System.out::println);
    System.out.println(sum);    

It also allows you to “align” the invocation of the chained method like this:

    int sum = Arrays.stream(x)
                    .map((n) -> n * 5)
                    .filter((n) -> {
                        System.out.println("Filtering: " + n);
                        return n % 3 != 0;
                    })
                    .reduce(0, Integer::sum);


    List list = Arrays.stream(x)
                               .filter((n) -> n % 2 == 0)
                               .map((n) -> n * 4)
                               .boxed()
                               .collect(Collectors.toList());
    list.forEach(System.out::println);
    System.out.println(sum);
}

I personally find that while it makes more sense, the second version pushes it too far, so I prefer the first.

The setting responsible for the first setting is as follows:



  

I tried to make sure everything was reasonable, but I could have screwed something up, so it may need a little tweaking.

If you are Hungarian like me and are using a Hungarian layout, this keymap may be useful to you, so that you will be unable to not use AltGR + F, AltGR + G, AltGR + B, AltGR + N and AltGR + M (which correspond a Ctrl + Alt).



  
    
  
  
    
  
  
    
  
  
    
  
  
    
  
  
    
  
  
  
    
  

While IntelliJ doesn’t seem to provide a way to insert the lambda opening brace into a new line, otherwise it’s a pretty reasonable way of formatting, so I’ll mark it as accepted.

Leave a comment