Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 433177

Summary: [1.8][Code Formatter] lambdas with comment formatted badly
Product: [Eclipse Project] JDT Reporter: Doug Simon <doug.simon>
Component: CoreAssignee: Manoj N Palat <manoj.palat>
Status: VERIFIED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: flirtomania.vk, josef.eisl, scolebourne, srikanth_sankaran
Version: 4.4   
Target Milestone: 4.5   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Configuration used by Code Formatter none

Description Doug Simon CLA 2014-04-22 04:55:39 EDT
Placing a comment in a lambda causes following lines to be indented incorrectly:

import java.util.stream.*;
public class FormatBug {

    public void good(int[] output) {
        IntStream.range(0, output.length).parallel().forEach(p -> {
            System.out.println(output[p]);
        });
    }

    public void bad(int[] output) {
        IntStream.range(0, output.length).parallel().forEach(p -> {
            // A comment
                        System.out.println(output[p]);
                    });
    }
}
Comment 1 Doug Simon CLA 2014-04-22 04:57:33 EDT
Created attachment 242172 [details]
Configuration used by Code Formatter
Comment 2 Manoj N Palat CLA 2014-05-08 09:03:50 EDT
moving to 4.5
Comment 3 Martin Mathew CLA 2014-07-29 05:28:45 EDT
*** Bug 440620 has been marked as a duplicate of this bug. ***
Comment 4 Josef Eisl CLA 2014-09-29 08:22:48 EDT
Since this is not yet fixed, one could use block comments to work around the issue, e.g.:

import java.util.stream.*;
public class FormatBug {

    public void workaround(int[] output) {
        IntStream.range(0, output.length).parallel().forEach(p -> {
            /* A comment */
            System.out.println(output[p]);
        });
    }
}
Comment 5 Stephen Colebourne CLA 2015-01-23 12:53:17 EST
This issue has hit us too. This issue occurs as follows (formatted code):

public class Bug433177 {

  public Function<String, String> testOK() {
    return foo((s) -> {
      // nothing
      System.out.println("");
      return "";
    });
  }

  public Function<String, String> testBad() {
    return this.foo((s) -> {
      // nothing
        System.out.println("");
        return "";
      });
  }

  public Function<String, String> foo(Function<String, String> f) {
    return null;
  }

}

Note the difference between invoking foo() vs this.foo(). The incorrect formatting indents all lines in the lambda block after the first comment. Subsequent comments are not further indented.

In other words, the bug occurs when the lambda is passed directly to a method expression that includes a dot method invocation. Both // and /* */ comments are affected.

We'd love to see a fix for this ;-)
Comment 6 Manoj N Palat CLA 2015-04-07 02:43:35 EDT
From M6 (bug 303519 for formatter redesign) this issue is fixed. The Test case committed via http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=43acf92f388300d5f01b9f356b08fd3725e374b7
Comment 7 Manoj N Palat CLA 2015-04-08 00:09:31 EDT
(In reply to Manoj Palat from comment #6)
> From M6 (bug 303519 for formatter redesign) this issue is fixed. The Test
> case committed via
> http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/
> ?id=43acf92f388300d5f01b9f356b08fd3725e374b7

*** This bug has been marked as a duplicate of bug 303519 ***
Comment 8 Sasikanth Bharadwaj CLA 2015-05-19 02:33:06 EDT
Verified for 4.5 RC1 using 20150514-1000 build