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 Feb 14, 2024
1 parent 820ebaf commit 9070129
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ export default function printArgumentListWithBraces(
rBrace: IToken,
lBrace: IToken
) {
const argumentListCtx = argumentListNodes?.[0].children;
if (argumentListCtx && isArgumentListHuggable(argumentListCtx)) {
if (
argumentListNodes &&
!argumentListNodes[0].leadingComments &&
!rBrace.leadingComments &&
isArgumentListHuggable(argumentListNodes[0].children)
) {
const [flat, expanded] = [false, true].map(shouldBreak => {
const argumentList = this.visit(argumentListNodes, { shouldBreak });
return putIntoBraces(argumentList, "", lBrace, rBrace);
Expand Down
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 @@ -208,6 +208,26 @@ void argumentAfterLambdaWithBlock() {
return f;
}, g);
}

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
26 changes: 26 additions & 0 deletions packages/prettier-plugin-java/test/unit-test/lambda/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,32 @@ void argumentAfterLambdaWithBlock() {
g
);
}

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 9070129

Please sign in to comment.