Skip to content

Commit e46ab97

Browse files
committed
fix: improve throws formatting
1 parent 1f7f1d7 commit e46ab97

File tree

3 files changed

+97
-45
lines changed

3 files changed

+97
-45
lines changed

packages/prettier-plugin-java/src/printers/classes.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ import {
1212
sortClassTypeChildren,
1313
sortModifiers
1414
} from "./printer-utils";
15-
import { concat, group, indent, join, indentIfBreak } from "./prettier-builder";
15+
import {
16+
concat,
17+
group,
18+
ifBreak,
19+
indent,
20+
join,
21+
indentIfBreak
22+
} from "./prettier-builder";
1623
import { printTokenWithComments } from "./comments/format-comments";
1724
import {
1825
hasLeadingComments,
@@ -509,18 +516,23 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
509516

510517
methodDeclaration(ctx: MethodDeclarationCtx) {
511518
const modifiers = sortModifiers(ctx.methodModifier);
519+
const throwsGroupId = Symbol("throws");
512520
const firstAnnotations = this.mapVisit(modifiers[0]);
513521
const otherModifiers = this.mapVisit(modifiers[1]);
514522

515-
const header = this.visit(ctx.methodHeader);
523+
const header = this.visit(ctx.methodHeader, { throwsGroupId });
516524
const body = this.visit(ctx.methodBody);
517525

518-
const headerBodySeparator = isStatementEmptyStatement(body) ? "" : " ";
526+
const headerBodySeparator = isStatementEmptyStatement(body)
527+
? ""
528+
: ctx.methodHeader[0].children.throws
529+
? ifBreak(hardline, " ", { groupId: throwsGroupId })
530+
: " ";
519531

520532
return rejectAndJoin(hardline, [
521-
rejectAndJoin(hardline, firstAnnotations),
533+
...firstAnnotations,
522534
rejectAndJoin(" ", [
523-
rejectAndJoin(" ", otherModifiers),
535+
...otherModifiers,
524536
rejectAndJoin(headerBodySeparator, [header, body])
525537
])
526538
]);
@@ -534,12 +546,12 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
534546
return printTokenWithComments(this.getSingle(ctx) as IToken);
535547
}
536548

537-
methodHeader(ctx: MethodHeaderCtx) {
549+
methodHeader(ctx: MethodHeaderCtx, opts?: { throwsGroupId?: symbol }) {
538550
const typeParameters = this.visit(ctx.typeParameters);
539551
const annotations = this.mapVisit(ctx.annotation);
540552
const result = this.visit(ctx.result);
541553
const declarator = this.visit(ctx.methodDeclarator);
542-
const throws = this.visit(ctx.throws);
554+
const throws = this.visit(ctx.throws, opts);
543555

544556
return group(
545557
concat([
@@ -646,15 +658,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
646658
return printTokenWithComments(this.getSingle(ctx) as IToken);
647659
}
648660

649-
throws(ctx: ThrowsCtx) {
661+
throws(ctx: ThrowsCtx, opts?: { throwsGroupId?: symbol }) {
650662
const exceptionTypeList = this.visit(ctx.exceptionTypeList);
651-
const throwsDeclaration = join(" ", [ctx.Throws[0], exceptionTypeList]);
652-
return group(indent(rejectAndConcat([softline, throwsDeclaration])));
663+
return group(indent(join(line, [ctx.Throws[0], exceptionTypeList])), {
664+
id: opts?.throwsGroupId
665+
});
653666
}
654667

655668
exceptionTypeList(ctx: ExceptionTypeListCtx) {
656669
const exceptionTypes = this.mapVisit(ctx.exceptionType);
657-
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
670+
const commas = ctx.Comma?.map(comma => concat([comma, line]));
658671
return rejectAndJoinSeps(commas, exceptionTypes);
659672
}
660673

@@ -681,25 +694,26 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
681694
}
682695

683696
constructorDeclaration(ctx: ConstructorDeclarationCtx) {
697+
const throwsGroupId = Symbol("throws");
684698
const modifiers = sortModifiers(ctx.constructorModifier);
685699
const firstAnnotations = this.mapVisit(modifiers[0]);
686700
const otherModifiers = this.mapVisit(modifiers[1]);
687-
688701
const constructorDeclarator = this.visit(ctx.constructorDeclarator);
689-
const throws = this.visit(ctx.throws);
702+
const throws = this.visit(ctx.throws, { throwsGroupId });
690703
const constructorBody = this.visit(ctx.constructorBody);
691704

692-
return rejectAndJoin(" ", [
705+
return concat([
693706
group(
694707
rejectAndJoin(hardline, [
695708
rejectAndJoin(hardline, firstAnnotations),
696709
rejectAndJoin(" ", [
697-
join(" ", otherModifiers),
710+
rejectAndJoin(" ", otherModifiers),
698711
constructorDeclarator,
699712
throws
700713
])
701714
])
702715
),
716+
ctx.throws ? ifBreak(hardline, " ", { groupId: throwsGroupId }) : " ",
703717
constructorBody
704718
]);
705719
}

packages/prettier-plugin-java/src/printers/prettier-builder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ export function dedent(doc: Doc | IToken) {
5353

5454
export function ifBreak(
5555
breakContents: Doc | IToken,
56-
flatContents: Doc | IToken
56+
flatContents: Doc | IToken,
57+
options?: { groupId?: symbol | undefined }
5758
) {
5859
return builders.ifBreak(
5960
processComments(breakContents),
60-
processComments(flatContents)
61+
processComments(flatContents),
62+
options
6163
);
6264
}
6365

packages/prettier-plugin-java/test/unit-test/throws/_output.java

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,33 @@ void throwException2(String string) throws RuntimeException {
88
throw new RuntimeException();
99
}
1010

11-
void throwException3(String string1, String string2, String string3)
12-
throws RuntimeException {
11+
void throwException3(String string1, String string2, String string3) throws
12+
RuntimeException
13+
{
1314
throw new RuntimeException();
1415
}
1516

16-
void throwException4()
17-
throws RuntimeException, RuntimeException, RuntimeException {
17+
void throwException4() throws
18+
RuntimeException,
19+
RuntimeException,
20+
RuntimeException
21+
{
1822
throw new RuntimeException();
1923
}
2024

21-
void throwException5(String string)
22-
throws RuntimeException, RuntimeException, RuntimeException {
25+
void throwException5(String string) throws
26+
RuntimeException,
27+
RuntimeException,
28+
RuntimeException
29+
{
2330
throw new RuntimeException();
2431
}
2532

26-
void throwException6(String string1, String string2, String string3)
27-
throws RuntimeException, RuntimeException, RuntimeException {
33+
void throwException6(String string1, String string2, String string3) throws
34+
RuntimeException,
35+
RuntimeException,
36+
RuntimeException
37+
{
2838
throw new RuntimeException();
2939
}
3040

@@ -51,19 +61,33 @@ void throwException9(
5161
String string2,
5262
String string3,
5363
String string4
54-
)
55-
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
64+
) throws
65+
RuntimeException,
66+
RuntimeException,
67+
RuntimeException,
68+
RuntimeException
69+
{
5670
throw new RuntimeException();
5771
}
5872

59-
void aVeryLongNameForAMethodWichShouldBreakTheExpression()
60-
throws aVeryLongException {}
61-
62-
void aVeryLongNameForAMethodWichShouldBreakTheExpression()
63-
throws aVeryLongException, aVeryLongException {}
64-
65-
void aVeryLongNameForAMethodWichShouldBreakTheExpression()
66-
throws Exception, Exception, Exception, Exception, Exception, Exception, Exception {}
73+
void aVeryLongNameForAMethodWichShouldBreakTheExpression() throws
74+
aVeryLongException
75+
{}
76+
77+
void aVeryLongNameForAMethodWichShouldBreakTheExpression() throws
78+
aVeryLongException,
79+
aVeryLongException
80+
{}
81+
82+
void aVeryLongNameForAMethodWichShouldBreakTheExpression() throws
83+
Exception,
84+
Exception,
85+
Exception,
86+
Exception,
87+
Exception,
88+
Exception,
89+
Exception
90+
{}
6791

6892
abstract void absThrowException1() throws RuntimeException;
6993

@@ -75,11 +99,15 @@ abstract void absThrowException3(
7599
String string3
76100
) throws RuntimeException;
77101

78-
abstract void absThrowException4()
79-
throws RuntimeException, RuntimeException, RuntimeException;
102+
abstract void absThrowException4() throws
103+
RuntimeException,
104+
RuntimeException,
105+
RuntimeException;
80106

81-
abstract void absThrowException5(String string)
82-
throws RuntimeException, RuntimeException, RuntimeException;
107+
abstract void absThrowException5(String string) throws
108+
RuntimeException,
109+
RuntimeException,
110+
RuntimeException;
83111

84112
abstract void absThrowException6(
85113
String string1,
@@ -99,15 +127,19 @@ abstract void absThrowException8(
99127
String string2,
100128
String string3,
101129
String string4
102-
)
103-
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException;
130+
) throws
131+
RuntimeException,
132+
RuntimeException,
133+
RuntimeException,
134+
RuntimeException;
104135

105136
public Throws(String string1) throws RuntimeException {
106137
System.out.println("Constructor with throws that should not wrap");
107138
}
108139

109-
public Throws(String string1, String string2, String string3)
110-
throws RuntimeException {
140+
public Throws(String string1, String string2, String string3) throws
141+
RuntimeException
142+
{
111143
System.out.println("Constructor with throws that should wrap");
112144
}
113145

@@ -127,8 +159,12 @@ public Throws(
127159
String string3,
128160
String string4,
129161
String string5
130-
)
131-
throws RuntimeException, RuntimeException, RuntimeException, RuntimeException {
162+
) throws
163+
RuntimeException,
164+
RuntimeException,
165+
RuntimeException,
166+
RuntimeException
167+
{
132168
System.out.println("Constructor with throws that should wrap");
133169
}
134170
}

0 commit comments

Comments
 (0)