Skip to content

Commit cbe8194

Browse files
committed
new line if broken conditionally
1 parent 4281eb1 commit cbe8194

File tree

4 files changed

+173
-3
lines changed

4 files changed

+173
-3
lines changed

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,12 @@ protected void visitFormals(Optional<VariableTree> receiver, List<? extends Vari
23852385
if (!receiver.isPresent() && parameters.isEmpty()) {
23862386
return;
23872387
}
2388+
boolean isRecordParams = false;
2389+
if (!parameters.isEmpty() && parameters.get(0) instanceof JCTree.JCVariableDecl) {
2390+
JCTree.JCVariableDecl param = (JCTree.JCVariableDecl) parameters.get(0);
2391+
isRecordParams = (param.mods.flags & RECORD) == RECORD;
2392+
}
2393+
23882394
builder.open(ZERO);
23892395
boolean first = true;
23902396
if (receiver.isPresent()) {
@@ -2408,13 +2414,25 @@ protected void visitFormals(Optional<VariableTree> receiver, List<? extends Vari
24082414
if (!first) {
24092415
builder.breakOp(" ");
24102416
}
2417+
BreakTag declarationAnnotationBreak = new BreakTag();
2418+
2419+
if (isRecordParams && !first) {
2420+
builder.blankLineWanted(BlankLineWanted.conditional(declarationAnnotationBreak));
2421+
}
2422+
24112423
visitToDeclare(
24122424
DeclarationKind.PARAMETER,
24132425
Direction.HORIZONTAL,
24142426
parameter,
24152427
/* initializer= */ Optional.empty(),
24162428
"=",
2417-
i < parameters.size() - 1 ? Optional.of(",") : /* a= */ Optional.empty());
2429+
i < parameters.size() - 1 ? Optional.of(",") : /* a= */ Optional.empty(),
2430+
Optional.of(declarationAnnotationBreak));
2431+
2432+
// If this is a record parameter and not the last one, add a conditional blank line after
2433+
if (isRecordParams && i < parameters.size() - 1) {
2434+
builder.blankLineWanted(BlankLineWanted.conditional(declarationAnnotationBreak));
2435+
}
24182436
first = false;
24192437
}
24202438
builder.close();
@@ -2584,6 +2602,17 @@ private void visitToDeclare(
25842602
Optional<ExpressionTree> initializer,
25852603
String equals,
25862604
Optional<String> trailing) {
2605+
visitToDeclare(kind, annotationsDirection, node, initializer, equals, trailing, Optional.empty());
2606+
}
2607+
2608+
private void visitToDeclare(
2609+
DeclarationKind kind,
2610+
Direction annotationsDirection,
2611+
VariableTree node,
2612+
Optional<ExpressionTree> initializer,
2613+
String equals,
2614+
Optional<String> trailing,
2615+
Optional<BreakTag> annotationBreakForRecords) {
25872616
sync(node);
25882617
declareOne(
25892618
kind,
@@ -2596,7 +2625,8 @@ private void visitToDeclare(
25962625
initializer,
25972626
trailing,
25982627
/* receiverExpression= */ Optional.empty(),
2599-
/* typeWithDims= */ Optional.empty());
2628+
/* typeWithDims= */ Optional.empty(),
2629+
annotationBreakForRecords);
26002630
}
26012631

26022632
/** Does not omit the leading '<', which should be associated with the type name. */
@@ -3400,9 +3430,39 @@ int declareOne(
34003430
Optional<String> trailing,
34013431
Optional<ExpressionTree> receiverExpression,
34023432
Optional<TypeWithDims> typeWithDims) {
3433+
return declareOne(
3434+
kind,
3435+
annotationsDirection,
3436+
modifiers,
3437+
type,
3438+
name,
3439+
op,
3440+
equals,
3441+
initializer,
3442+
trailing,
3443+
receiverExpression,
3444+
typeWithDims,
3445+
Optional.empty());
3446+
}
3447+
3448+
/** Declare one variable or variable-like thing. */
3449+
@SuppressWarnings("TooManyArguments")
3450+
int declareOne(
3451+
DeclarationKind kind,
3452+
Direction annotationsDirection,
3453+
Optional<ModifiersTree> modifiers,
3454+
Tree type,
3455+
Name name,
3456+
String op,
3457+
String equals,
3458+
Optional<ExpressionTree> initializer,
3459+
Optional<String> trailing,
3460+
Optional<ExpressionTree> receiverExpression,
3461+
Optional<TypeWithDims> typeWithDims,
3462+
Optional<BreakTag> verticalAnnotationBreakForRecords) {
34033463

34043464
BreakTag typeBreak = new BreakTag();
3405-
BreakTag verticalAnnotationBreak = new BreakTag();
3465+
BreakTag verticalAnnotationBreak = verticalAnnotationBreakForRecords.orElseGet(BreakTag::new);
34063466

34073467
// If the node is a field declaration, try to output any declaration
34083468
// annotations in-line. If the entire declaration doesn't fit on a single

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/AnnotationFields.input

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,104 @@ public record QuoteRequest(
77
@Schema(description = "Date the quoted changes become active", example = "2023-06-25")
88
LocalDate quoteEffectiveDate
99
) {}
10+
11+
@Schema(
12+
description = "Type of quote being requested",
13+
example = "NEW_BUSINESS",
14+
somethingElse = "new",
15+
other = "my other long string")
16+
public record QuoteRequest(
17+
18+
int value,
19+
20+
@SomeInput RegulatoryState regulatoryState,
21+
22+
@SomeInput
23+
@NotNull
24+
@Deprecated
25+
@JsonValue(name = "something")
26+
@Schema(description = "US state of the product being quoted", example = "TX")
27+
RegulatoryState regulatoryState,
28+
29+
@Schema(description = "Reason for a quote", example = "New Business") String amendmentReason,
30+
int x,
31+
int j) {}
32+
33+
public record QuoteRequest(
34+
int value,
35+
@SomeInput RegulatoryState regulatoryState,
36+
int value,
37+
38+
@SomeInput
39+
@NotNull
40+
@Deprecated
41+
@JsonValue(name = "something")
42+
@Schema(description = "US state of the product being quoted", example = "TX")
43+
RegulatoryState regulatoryState,
44+
45+
@Schema(description = "Reason for a quote", example = "New Business") String amendmentReason,
46+
47+
@Schema(
48+
description = "Type of quote being requested",
49+
example = "NEW_BUSINESS",
50+
somethingElse = "new",
51+
other = "my other long string")
52+
QuoteType quoteType,
53+
54+
@Schema(description = "Date the quoted changes become active", example = "2023-06-25")
55+
LocalDate quoteEffectiveDate,
56+
57+
int value3) {}
58+
59+
60+
public record QuoteRequest(
61+
int value,
62+
@SomeInput RegulatoryState regulatoryState,
63+
64+
65+
int value,
66+
67+
@SomeInput
68+
@NotNull
69+
@Deprecated
70+
@JsonValue(name = "something")
71+
@Schema(description = "US state of the product being quoted", example = "TX")
72+
RegulatoryState regulatoryState,
73+
74+
int value3) {}
75+
76+
public record Url(
77+
int bla,
78+
@JsonValue("dadsa") String value,
79+
@SomeInput @JsonValue String key,
80+
int number,
81+
int smth,
82+
int smth2,
83+
int other) {}
84+
85+
public record Url(
86+
@NotNull
87+
@Something
88+
@JsonValue
89+
@Deprecated
90+
@Other
91+
@New
92+
@JsonValue
93+
@JsonNode
94+
@SomethingElse
95+
@SomethingNew
96+
@SomethingElse
97+
@New
98+
@One
99+
@Two
100+
@Three
101+
@Four
102+
String value) {}
103+
104+
public record Url(@NotNull String value, int value) {}
105+
106+
public record Url(
107+
int bla,
108+
@JsonValue("dadsa") String value) {}
109+
110+
public record Url(@New @JsonValue("dadsa") String value) {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@Schema(description = "Type of quote being requested", example = "NEW_BUSINESS", somethingElse= "new", other="my other long string")
2+
public record QuoteRequest(
3+
@Schema(description = "US state of the product being quoted", example = "TX")
4+
RegulatoryState regulatoryState,
5+
@Schema(description = "Reason for a quote", example = "New Business") String amendmentReason,
6+
@Schema(description = "Type of quote being requested", example = "NEW_BUSINESS", somethingElse= "new", other="my other long string") QuoteType quoteType,
7+
@Schema(description = "Date the quoted changes become active", example = "2023-06-25")
8+
LocalDate quoteEffectiveDate
9+
) {}

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata2/cr.output

Whitespace-only changes.

0 commit comments

Comments
 (0)