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 5ceccde
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
5 changes: 2 additions & 3 deletions packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import forEach from "lodash/forEach.js";
import { builders } from "prettier/doc";
import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
import { isAnnotationCstNode } from "../types/utils.js";
import { isArgumentListHuggable } from "../utils/expressions-utils.js";
import { printArgumentListWithBraces } from "../utils/index.js";
import { printTokenWithComments } from "./comments/format-comments.js";
import { handleCommentsBinaryExpression } from "./comments/handle-comments.js";
Expand Down Expand Up @@ -625,7 +624,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
);
}

argumentList(ctx: ArgumentListCtx, params?: { shouldBreak?: boolean }) {
argumentList(ctx: ArgumentListCtx, params?: { isHuggable?: boolean, shouldBreak?: boolean }) {
const shouldBreak = params?.shouldBreak;
const expressions = this.mapVisit(ctx.expression, params);

Expand All @@ -635,7 +634,7 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
const commas = ctx.Comma?.map(comma => concat([comma, commaSuffix]));
const otherArguments = rejectAndJoinSeps(commas, expressions);

if (lastArgument && isArgumentListHuggable(ctx)) {
if (lastArgument && params?.isHuggable) {
const argumentListGroupId = Symbol("argumentList");
const separator =
shouldBreak === true ? hardline : shouldBreak === false ? "" : softline;
Expand Down
12 changes: 0 additions & 12 deletions packages/prettier-plugin-java/src/utils/expressions-utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ArgumentListCstNode, IToken } from "java-parser";
import type { ArgumentListCstNode, ArgumentListCtx, IToken } from "java-parser";
import { builders, utils } from "prettier/doc";
import { isArgumentListHuggable } from "./expressions-utils.js";
import { putIntoBraces } from "../printers/printer-utils.js";

const { breakParent, conditionalGroup, softline } = builders;
Expand All @@ -11,10 +10,17 @@ 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 });
const argumentList = this.visit(argumentListNodes, {
isHuggable: true,
shouldBreak
});
return putIntoBraces(argumentList, "", lBrace, rBrace);
});
return [
Expand All @@ -26,3 +32,13 @@ export default function printArgumentListWithBraces(
const argumentList = this.visit(argumentListNodes);
return putIntoBraces(argumentList, softline, lBrace, rBrace);
}

function isArgumentListHuggable(argumentList: ArgumentListCtx) {
const expressions = argumentList.expression;
return (
(expressions.length === 1 ||
expressions[expressions.length - 1].children.lambdaExpression?.[0]
.children.lambdaBody[0].children.block !== undefined) &&
expressions.filter(({ children }) => children.lambdaExpression).length === 1
);
}
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 5ceccde

Please sign in to comment.