Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: lambda formatting #497

Merged
merged 3 commits into from
Aug 28, 2021

Conversation

clementdessoude
Copy link
Contributor

What changed with this PR:

Still needs to clean the Typescript migration PR before but hopefully should close #418

Example

// Input
public class Lambda {

  public void singleArgumentWithParens() {
    call((x) -> {
      System.out.println(x);
      System.out.println(x);
    });
  }

  public void singleArgumentWithoutParens() {
    call(x -> {
      System.out.println(x);
      System.out.println(x);
    });
  }

  public void multiArguments() {
    call((x, y) -> {
      System.out.println(x);
      System.out.println(y);
    });
  }

  public void multiParameters() {
    call((Object x, final String y) -> {
      System.out.println(x);
      System.out.println(y);
    });
  }

  public void emptyArguments() {
    call(() -> {
      System.out.println();
      System.out.println();
    });
  }

  public void onlyOneMethodInBodyWithCurlyBraces() {
    call(x -> {
      System.out.println(x);
    });
  }

  public void onlyOneMethodInBody() {
    call(x -> System.out.println(x));
  }

  public void lambdaWithoutBracesWhichBreak() {
    call(x -> foo.isVeryVeryVeryLongConditionTrue() &&
    foo.isAnotherVeryVeryLongConditionTrue());
  }

  public void chainCallWithLambda() {
      Stream
          .of(1, 2)
          .map(n -> {
              // testing method
              return n * 2;
          })
          .collect(Collectors.toList());
  }

  public void lambdaWithLongListOfParameters() {
      final List<Integer> values = Stream
          .of(1, 2)
          .map((
                   aVeryLongListOfParameter,
                   aVeryLongListOfParameter,
                   aVeryLongListOfParameter,
                   aVeryLongListOfParameter,
                   aVeryLongListOfParameter,
                   aVeryLongListOfParameter
               ) -> {
              // testing method
              return n * 2;
          })
          .collect(Collectors.toList());
  }

  public void shortLambdaAssignation() {
      V t = t -> toto();
  }

    public void longLambdaAssignation() {
        V t = (
            aVeryLongListOfParameter,
            aVeryLongListOfParameter,
            aVeryLongListOfParameter,
            aVeryLongListOfParameter,
            aVeryLongListOfParaa
        ) -> {
            // testing method
            return n * 2;
        };
    }
}

// Output
public class Lambda {

  public void singleArgumentWithParens() {
    call(x -> {
      System.out.println(x);
      System.out.println(x);
    });
  }

  public void singleArgumentWithoutParens() {
    call(x -> {
      System.out.println(x);
      System.out.println(x);
    });
  }

  public void multiArguments() {
    call((x, y) -> {
      System.out.println(x);
      System.out.println(y);
    });
  }

  public void multiParameters() {
    call((Object x, final String y) -> {
      System.out.println(x);
      System.out.println(y);
    });
  }

  public void emptyArguments() {
    call(() -> {
      System.out.println();
      System.out.println();
    });
  }

  public void onlyOneMethodInBodyWithCurlyBraces() {
    call(x -> {
      System.out.println(x);
    });
  }

  public void onlyOneMethodInBody() {
    call(x -> System.out.println(x));
  }

  public void lambdaWithoutBracesWhichBreak() {
    call(x ->
      foo.isVeryVeryVeryLongConditionTrue() &&
      foo.isAnotherVeryVeryLongConditionTrue()
    );
  }

  public void chainCallWithLambda() {
    Stream
      .of(1, 2)
      .map(n -> {
        // testing method
        return n * 2;
      })
      .collect(Collectors.toList());
  }

  public void lambdaWithLongListOfParameters() {
    final List<Integer> values = Stream
      .of(1, 2)
      .map(
        (
          aVeryLongListOfParameter,
          aVeryLongListOfParameter,
          aVeryLongListOfParameter,
          aVeryLongListOfParameter,
          aVeryLongListOfParameter,
          aVeryLongListOfParameter
        ) -> {
          // testing method
          return n * 2;
        }
      )
      .collect(Collectors.toList());
  }

  public void shortLambdaAssignation() {
    V t = t -> toto();
  }

  public void longLambdaAssignation() {
    V t = (
      aVeryLongListOfParameter,
      aVeryLongListOfParameter,
      aVeryLongListOfParameter,
      aVeryLongListOfParameter,
      aVeryLongListOfParaa
    ) -> {
      // testing method
      return n * 2;
    };
  }
}

Relative issues or prs:

Fix #418

@clementdessoude clementdessoude force-pushed the fix/lambda-formatting branch 3 times, most recently from f28759c to a8c27ee Compare August 22, 2021 16:22
@clementdessoude clementdessoude changed the title Wip: fix: lambda formatting Fix: lambda formatting Aug 23, 2021
@clementdessoude clementdessoude force-pushed the fix/lambda-formatting branch 4 times, most recently from 506aec2 to de2a03b Compare August 28, 2021 10:22

Verified

This commit was signed with the committer’s verified signature.
ondrejmirtes Ondřej Mirtes
@clementdessoude clementdessoude merged commit 3d673f9 into jhipster:main Aug 28, 2021
@clementdessoude clementdessoude deleted the fix/lambda-formatting branch August 28, 2021 12:09
@clementdessoude
Copy link
Contributor Author

clementdessoude added a commit to clementdessoude/prettier-java that referenced this pull request Mar 4, 2023
* feat: improve lambda formatting in method invocations and assignations

* feat: improve lambda formatting in constructors and enums

* refactor: extract printSingleLambdaInvocation method to regroup common logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function calls that take a single lambda should not be on a new line
1 participant