Skip to content

Commit e379f32

Browse files
committed
fix: improve throws formatting
1 parent 983a09d commit e379f32

File tree

3 files changed

+90
-44
lines changed

3 files changed

+90
-44
lines changed

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import {
1616
concat,
1717
group,
18+
ifBreak,
1819
indent,
1920
join,
2021
indentIfBreak
@@ -515,18 +516,23 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
515516

516517
methodDeclaration(ctx: MethodDeclarationCtx) {
517518
const modifiers = sortModifiers(ctx.methodModifier);
519+
const throwsGroupId = Symbol("throws");
518520
const firstAnnotations = this.mapVisit(modifiers[0]);
519521
const otherModifiers = this.mapVisit(modifiers[1]);
520522

521-
const header = this.visit(ctx.methodHeader);
523+
const header = this.visit(ctx.methodHeader, { throwsGroupId });
522524
const body = this.visit(ctx.methodBody);
523525

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

526532
return rejectAndJoin(hardline, [
527-
rejectAndJoin(hardline, firstAnnotations),
533+
...firstAnnotations,
528534
rejectAndJoin(" ", [
529-
rejectAndJoin(" ", otherModifiers),
535+
...otherModifiers,
530536
rejectAndJoin(headerBodySeparator, [header, body])
531537
])
532538
]);
@@ -540,12 +546,12 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
540546
return printTokenWithComments(this.getSingle(ctx) as IToken);
541547
}
542548

543-
methodHeader(ctx: MethodHeaderCtx) {
549+
methodHeader(ctx: MethodHeaderCtx, opts?: { throwsGroupId?: symbol }) {
544550
const typeParameters = this.visit(ctx.typeParameters);
545551
const annotations = this.mapVisit(ctx.annotation);
546552
const result = this.visit(ctx.result);
547553
const declarator = this.visit(ctx.methodDeclarator);
548-
const throws = this.visit(ctx.throws);
554+
const throws = this.visit(ctx.throws, opts);
549555

550556
return group(
551557
concat([
@@ -652,15 +658,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
652658
return printTokenWithComments(this.getSingle(ctx) as IToken);
653659
}
654660

655-
throws(ctx: ThrowsCtx) {
661+
throws(ctx: ThrowsCtx, opts?: { throwsGroupId?: symbol }) {
656662
const exceptionTypeList = this.visit(ctx.exceptionTypeList);
657-
const throwsDeclaration = join(" ", [ctx.Throws[0], exceptionTypeList]);
658-
return group(indent(rejectAndConcat([softline, throwsDeclaration])));
663+
return group(indent(join(line, [ctx.Throws[0], exceptionTypeList])), {
664+
id: opts?.throwsGroupId
665+
});
659666
}
660667

661668
exceptionTypeList(ctx: ExceptionTypeListCtx) {
662669
const exceptionTypes = this.mapVisit(ctx.exceptionType);
663-
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, " "])) : [];
670+
const commas = ctx.Comma?.map(comma => concat([comma, line]));
664671
return rejectAndJoinSeps(commas, exceptionTypes);
665672
}
666673

@@ -687,25 +694,26 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
687694
}
688695

689696
constructorDeclaration(ctx: ConstructorDeclarationCtx) {
697+
const throwsGroupId = Symbol("throws");
690698
const modifiers = sortModifiers(ctx.constructorModifier);
691699
const firstAnnotations = this.mapVisit(modifiers[0]);
692700
const otherModifiers = this.mapVisit(modifiers[1]);
693-
694701
const constructorDeclarator = this.visit(ctx.constructorDeclarator);
695-
const throws = this.visit(ctx.throws);
702+
const throws = this.visit(ctx.throws, { throwsGroupId });
696703
const constructorBody = this.visit(ctx.constructorBody);
697704

698-
return rejectAndJoin(" ", [
705+
return concat([
699706
group(
700707
rejectAndJoin(hardline, [
701708
rejectAndJoin(hardline, firstAnnotations),
702709
rejectAndJoin(" ", [
703-
join(" ", otherModifiers),
710+
rejectAndJoin(" ", otherModifiers),
704711
constructorDeclarator,
705712
throws
706713
])
707714
])
708715
),
716+
ctx.throws ? ifBreak(hardline, " ", { groupId: throwsGroupId }) : " ",
709717
constructorBody
710718
]);
711719
}

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)