From b79ef3b62b8156a5d155a01b855dd29a3282b107 Mon Sep 17 00:00:00 2001 From: Clement Dessoude Date: Fri, 17 Jan 2025 08:12:51 +0100 Subject: [PATCH] fix: handle comments with static invocations --- .../src/printers/expressions.ts | 21 +++++++++- .../test/unit-test/member_chain/_input.java | 36 ++++++++++++++++ .../test/unit-test/member_chain/_output.java | 42 +++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/packages/prettier-plugin-java/src/printers/expressions.ts b/packages/prettier-plugin-java/src/printers/expressions.ts index 6207ff62..0be26a81 100644 --- a/packages/prettier-plugin-java/src/printers/expressions.ts +++ b/packages/prettier-plugin-java/src/printers/expressions.ts @@ -63,6 +63,7 @@ import { builders, utils } from "prettier/doc"; import { BaseCstPrettierPrinter } from "../base-cst-printer.js"; import { isAnnotationCstNode } from "../types/utils.js"; import { printArgumentListWithBraces } from "../utils/index.js"; +import { hasLeadingComments } from "./comments/comments-utils.js"; import { printTokenWithComments } from "./comments/format-comments.js"; import { handleCommentsBinaryExpression, @@ -366,11 +367,18 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { .find(methodInvocationSuffix => methodInvocationSuffix); const hasFqnRefPart = fqnOrRefType?.fqnOrRefTypePartRest !== undefined; - const isCapitalizedIdentifier = !!this.isCapitalizedIdentifier(fqnOrRefType); + const lastFqnRefPartDot = this.lastDot(fqnOrRefType); + const isCapitalizedIdentifier = + !!this.isCapitalizedIdentifier(fqnOrRefType); + const isCapitalizedIdentifierWithoutTrailingComment = + isCapitalizedIdentifier && + (lastFqnRefPartDot === undefined || + !hasLeadingComments(lastFqnRefPartDot)); + const shouldBreakBeforeFirstMethodInvocation = countMethodInvocation > 1 && hasFqnRefPart && - !isCapitalizedIdentifier && + !isCapitalizedIdentifierWithoutTrailingComment && firstMethodInvocation !== undefined; const shouldBreakBeforeMethodInvocations = @@ -918,4 +926,13 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter { /^\p{Uppercase_Letter}/u.test(nextToLastIdentifier) ); } + + private lastDot(fqnOrRefType: FqnOrRefTypeCtx | undefined) { + if (fqnOrRefType === undefined || fqnOrRefType.Dot === undefined) { + return undefined; + } + + const lastDot = fqnOrRefType.Dot[fqnOrRefType.Dot.length - 1]; + return lastDot; + } } diff --git a/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java b/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java index bf5219c3..d0720d55 100644 --- a/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java +++ b/packages/prettier-plugin-java/test/unit-test/member_chain/_input.java @@ -4,6 +4,42 @@ public void doSomething() { return new Object().something().more(); } + public void doSomethingNewWithComment() { + return new Object() + // comment + .something().more(); + } + + public void doSomethingWithComment() { + return Object + // comment + .something().more(); + } + + public void doSomethingWithComment() { + return object + // comment + .something().more(); + } + + public void doSomethingNewWithComment() { + return new Object() + /* comment */ + .something().more(); + } + + public void doSomethingWithComment() { + return Object + /* comment */ + .something().more(); + } + + public void doSomethingWithComment() { + return object + /* comment */ + .something().more(); + } + public void doSomethingLongNew() { return something().more().and().that().as().well().but().not().something().something(); } diff --git a/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java b/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java index 9ebcdc64..0ab139a7 100644 --- a/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java +++ b/packages/prettier-plugin-java/test/unit-test/member_chain/_output.java @@ -4,6 +4,48 @@ public void doSomething() { return new Object().something().more(); } + public void doSomethingNewWithComment() { + return new Object() + // comment + .something() + .more(); + } + + public void doSomethingWithComment() { + return Object + // comment + .something() + .more(); + } + + public void doSomethingWithComment() { + return object + // comment + .something() + .more(); + } + + public void doSomethingNewWithComment() { + return new Object() + /* comment */ + .something() + .more(); + } + + public void doSomethingWithComment() { + return Object + /* comment */ + .something() + .more(); + } + + public void doSomethingWithComment() { + return object + /* comment */ + .something() + .more(); + } + public void doSomethingLongNew() { return something() .more()