Skip to content

Commit

Permalink
fix(printer): properly break and indent lambda with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Sep 6, 2023
1 parent a7f9a5f commit b1d9303
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ArgumentListCstNode, IToken } from "java-parser";
import { builders } from "prettier/doc";
import { isSingleArgumentLambdaExpressionWithBlock } from "./expressions-utils";
import { printTokenWithComments } from "../printers/comments/format-comments";
import { concat, dedent, indent } from "../printers/prettier-builder";
import { concat, dedent } from "../printers/prettier-builder";
import { putIntoBraces } from "../printers/printer-utils";

const { softline, ifBreak } = builders;
Expand All @@ -18,14 +18,13 @@ export default function printSingleLambdaInvocation(
isInsideMethodInvocationSuffix: true
});

const formattedRBrace = isSingleArgumentLambdaExpressionWithBlock(
argumentListCtx
)
? ifBreak(
indent(concat([softline, rBrace])),
printTokenWithComments(rBrace),
{ groupId: lambdaParametersGroupId }
)
: indent(concat([softline, rBrace]));
return dedent(putIntoBraces(argumentList, "", lBrace, formattedRBrace));
if (isSingleArgumentLambdaExpressionWithBlock(argumentListCtx)) {
const formattedRBrace = ifBreak(
concat([softline, rBrace]),
printTokenWithComments(rBrace),
{ groupId: lambdaParametersGroupId }
);
return putIntoBraces(dedent(argumentList), "", lBrace, formattedRBrace);
}
return putIntoBraces(argumentList, softline, lBrace, rBrace);
}
20 changes: 20 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/lambda/_input.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ public void testConstructor() {
}
);
}

void lambdaWithLeadingComments() {
System.out.println(
List.of(1, 2, 3).stream().map(
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
v -> v * 2
).collect(Collectors.summingInt(v -> v))
);
}

void lambdaWithTrailingComments() {
System.out.println(
List.of(1, 2, 3).stream().map(
v -> v * 2
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
).collect(Collectors.summingInt(v -> v))
);
}
}

class T {
Expand Down
35 changes: 32 additions & 3 deletions packages/prettier-plugin-java/test/unit-test/lambda/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public void onlyOneMethodInBody() {
}

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

Expand Down Expand Up @@ -143,6 +144,34 @@ public void testConstructor() {
}
);
}

void lambdaWithLeadingComments() {
System.out.println(
List
.of(1, 2, 3)
.stream()
.map(
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
v -> v * 2
)
.collect(Collectors.summingInt(v -> v))
);
}

void lambdaWithTrailingComments() {
System.out.println(
List
.of(1, 2, 3)
.stream()
.map(
v -> v * 2
// a very long comment which explains the beatifullness of multiplication by 2
// yes this is very important
)
.collect(Collectors.summingInt(v -> v))
);
}
}

class T {
Expand Down

0 comments on commit b1d9303

Please sign in to comment.