Skip to content

Commit

Permalink
fix: handle comments with static invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Jan 17, 2025
1 parent 3fdaa64 commit b79ef3b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b79ef3b

Please sign in to comment.