Skip to content

Commit

Permalink
feat: split record parameters on several lines
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Oct 2, 2021
1 parent ccc76f6 commit c2653b9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/prettier-plugin-java/src/printers/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
}
recordComponentList(ctx: RecordComponentListCtx) {
const recordComponents = this.mapVisit(ctx.recordComponent);
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];

return rejectAndJoinSeps(commas, recordComponents);
}
Expand Down
39 changes: 34 additions & 5 deletions packages/prettier-plugin-java/test/unit-test/records/_input.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
public record Pet(
@NotNull String name
) {}

public record Pet(
@NotNull String name, int age
) {}

public record Pet(
@NotNull String name, int age, String... others, Object @Nullable... errorMessageArgs
) {
Expand Down Expand Up @@ -46,7 +54,7 @@ record MyRecord(String name, int age
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}

if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
Expand All @@ -71,18 +79,18 @@ public MyRecord(String name,

public class MyRecordWithAnnotationAndModifiers {
public record MyRecord(
String name,
String name,
int age) {
@Annotation
@Annotation2
public MyRecord {
if (
age < 0)
age < 0)
{
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null ||

if (name == null ||
name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
Expand All @@ -91,9 +99,30 @@ public record MyRecord(
}
}

class MySplitRecordConstructor {
record MyRecord(String name,
int age, String name,
int age,String name,
int age) {
public MyRecord(String name,
int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}

public interface MyInterface {
record MyRecord(
String param) implements MyInterface {

}
}

public interface MyInterface {
record MySplitRecord(String param, String param, String param, String param, String param, String param) implements MyInterface {}
}
47 changes: 45 additions & 2 deletions packages/prettier-plugin-java/test/unit-test/records/_output.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
public record Pet(@NotNull String name) {}

public record Pet(@NotNull String name, int age) {}

public record Pet(
@NotNull String name, int age, String... others, Object @Nullable... errorMessageArgs
@NotNull String name,
int age,
String... others,
Object @Nullable... errorMessageArgs
) {
public Pet {
if (age < 0) {
Expand All @@ -15,7 +22,10 @@ public void test() {}
}

public record Pet(
@NotNull String name, int age, String... others, Object @Nullable... errorMessageArgs
@NotNull String name,
int age,
String... others,
Object @Nullable... errorMessageArgs
) {}

public record Pet() {}
Expand Down Expand Up @@ -79,6 +89,39 @@ public record MyRecord(String name, int age) {
}
}

class MySplitRecordConstructor {

record MyRecord(
String name,
int age,
String name,
int age,
String name,
int age
) {
public MyRecord(String name, int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
}
}

public interface MyInterface {
record MyRecord(String param) implements MyInterface {}
}

public interface MyInterface {
record MySplitRecord(
String param,
String param,
String param,
String param,
String param,
String param
)
implements MyInterface {}
}

0 comments on commit c2653b9

Please sign in to comment.