Skip to content

Commit

Permalink
fix: consistent break after equals
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel authored and clementdessoude committed Feb 15, 2024
1 parent 6457cc5 commit 0111610
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
20 changes: 17 additions & 3 deletions packages/prettier-plugin-java/src/printers/printer-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ import {
getTokenLeadingComments,
printTokenWithComments
} from "./comments/format-comments.js";
import { concat, group, ifBreak, join } from "./prettier-builder.js";
import {
concat,
group,
ifBreak,
indentIfBreak,
join
} from "./prettier-builder.js";

const { indent, hardline, line } = builders;

Expand Down Expand Up @@ -654,8 +660,16 @@ export function binary(nodes: Doc[], tokens: IToken[], isRoot = false): Doc {
}
}
level.push(nodes.shift()!);
const content = group(join(line, level));
return levelOperator === "=" ? indent(content) : content;
const lineGroupId = Symbol("line");
return group(
levelOperator === "="
? [
level[0],
indent(group(line, { id: lineGroupId })),
indentIfBreak(level[1], { groupId: lineGroupId })
]
: join(line, level)
);
}

function getOperator(tokens: IToken[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,9 @@ void assignment() {
bbbbbbbbbbbbbbbbb ? ccccccccccccccccc : ddddddddddddddddd;

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ccccccccccccccccccccccccccccccccccccccc + ddddddddddddddddddddddddddddddddd;

foo = new Foo("aaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccc", "dddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeee", "ffffffffffffffffffff");

final Foo bar = new Foo("aaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccc", "dddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeee", "ffffffffffffffffffff");
}
}
40 changes: 29 additions & 11 deletions packages/prettier-plugin-java/test/unit-test/variables/_output.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,39 @@ public methodWithVariableInitializationWithComments() {
}

void assignment() {
fileSystemDetails =
FileHandlerDetails.builder()
.fileSystemType(
EntityUtils.update(
entity.getFileSystemDetails().getFileSystemType(),
update.getFileSystemDetails().getFileSystemType()
)
);

aaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbb ? ccccccccccccccccc : ddddddddddddddddd;
fileSystemDetails = FileHandlerDetails.builder()
.fileSystemType(
EntityUtils.update(
entity.getFileSystemDetails().getFileSystemType(),
update.getFileSystemDetails().getFileSystemType()
)
);

aaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbb
? ccccccccccccccccc
: ddddddddddddddddd;

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb =
ccccccccccccccccccccccccccccccccccccccc +
ddddddddddddddddddddddddddddddddd;

foo = new Foo(
"aaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbbbbbbbbbbbb",
"cccccccccccccccccccc",
"dddddddddddddddddddd",
"eeeeeeeeeeeeeeeeeeee",
"ffffffffffffffffffff"
);

final Foo bar = new Foo(
"aaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbbbbbbbbbbbb",
"cccccccccccccccccccc",
"dddddddddddddddddddd",
"eeeeeeeeeeeeeeeeeeee",
"ffffffffffffffffffff"
);
}
}

0 comments on commit 0111610

Please sign in to comment.